use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.
the class LaoDetailViewModel method sendConsensusElect.
/**
* Sends a ConsensusElect message.
*
* <p>Publish a GeneralMessage containing ConsensusElect data.
*
* @param creation the creation time of the consensus
* @param objId the id of the object the consensus refers to (e.g. election_id)
* @param type the type of object the consensus refers to (e.g. election)
* @param property the property the value refers to (e.g. "state")
* @param value the proposed new value for the property (e.g. "started")
*/
public void sendConsensusElect(long creation, String objId, String type, String property, Object value) {
Log.d(TAG, String.format("creating a new consensus for type: %s, property: %s, value: %s", type, property, value));
Lao lao = getCurrentLaoValue();
if (lao == null) {
Log.d(TAG, LAO_FAILURE_MESSAGE);
return;
}
Channel channel = lao.getChannel().subChannel("consensus");
ConsensusElect consensusElect = new ConsensusElect(creation, objId, type, property, value);
Log.d(TAG, PUBLISH_MESSAGE);
MessageGeneral msg = new MessageGeneral(keyManager.getMainKeyPair(), consensusElect, gson);
Disposable disposable = networkManager.getMessageSender().publish(channel, msg).subscribe(() -> Log.d(TAG, "created a consensus with message id : " + msg.getMessageId()), error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_start_election));
disposables.add(disposable);
}
use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.
the class JsonMessageGeneralSerializer method deserialize.
@Override
public MessageGeneral deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonMessageData jsonObject = context.deserialize(json, JsonMessageData.class);
JsonElement dataElement = JsonParser.parseString(new String(jsonObject.data.getData(), StandardCharsets.UTF_8));
Data data = context.deserialize(dataElement, Data.class);
return new MessageGeneral(jsonObject.sender, jsonObject.data, data, jsonObject.signature, jsonObject.messageID, jsonObject.witnessSignatures);
}
use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.
the class JsonResultSerializer method deserialize.
@Override
public Result deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject root = json.getAsJsonObject();
int id = root.get("id").getAsInt();
JsonElement resultElement = root.get(RESULT);
if (resultElement.isJsonPrimitive()) {
return new Result(id);
} else {
Type listType = new TypeToken<ArrayList<MessageGeneral>>() {
}.getType();
List<MessageGeneral> messages = context.deserialize(resultElement.getAsJsonArray(), listType);
return new ResultMessages(id, messages);
}
}
use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.
the class ElectionStartFragmentTest method startButtonSendElectMessageTest.
@Test
public void startButtonSendElectMessageTest() {
setupViewModel(PAST_TIME);
long minCreation = Instant.now().getEpochSecond();
electionStartButton().perform(ViewActions.click());
ArgumentCaptor<MessageGeneral> captor = ArgumentCaptor.forClass(MessageGeneral.class);
Mockito.verify(messageSender).publish(eq(consensusChannel), captor.capture());
MessageGeneral msgGeneral = captor.getValue();
long maxCreation = Instant.now().getEpochSecond();
assertEquals(mainKeyPair.getPublicKey(), msgGeneral.getSender());
ConsensusElect elect = (ConsensusElect) msgGeneral.getData();
assertEquals(KEY, elect.getKey());
assertEquals(INSTANCE_ID, elect.getInstanceId());
assertEquals("started", elect.getValue());
assertTrue(minCreation <= elect.getCreation() && elect.getCreation() <= maxCreation);
}
use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.
the class ElectionStartFragmentTest method displayWithUpdatesIsCorrect.
@Test
public void displayWithUpdatesIsCorrect() throws DataHandlingException {
setupViewModel(PAST_TIME);
// Election start time has passed, should display that it's ready and start button enabled
displayAssertions(STATUS_READY, START_START, true);
DataInteraction grid = nodesGrid();
nodeAssertions(grid, ownPos, "Waiting\n" + publicKey, false);
nodeAssertions(grid, node2Pos, "Waiting\n" + node2, false);
nodeAssertions(grid, node3Pos, "Waiting\n" + node3, false);
// Nodes 3 try to start
MessageGeneral elect3Msg = createMsg(node3KeyPair, elect);
messageHandler.handleMessage(laoRepository, messageSender, consensusChannel, elect3Msg);
nodeAssertions(grid, node3Pos, "Approve Start by\n" + node3, true);
// We try to start (it should disable the start button)
MessageGeneral elect1Msg = createMsg(mainKeyPair, elect);
messageHandler.handleMessage(laoRepository, messageSender, consensusChannel, elect1Msg);
displayAssertions(STATUS_READY, START_START, false);
nodeAssertions(grid, ownPos, "Approve Start by\n" + publicKey, true);
// We accepted node 3 (it should disable button for node3)
ConsensusElectAccept electAccept3 = new ConsensusElectAccept(INSTANCE_ID, elect3Msg.getMessageId(), true);
MessageGeneral accept3Msg = createMsg(mainKeyPair, electAccept3);
messageHandler.handleMessage(laoRepository, messageSender, consensusChannel, accept3Msg);
nodeAssertions(grid, node3Pos, "Approve Start by\n" + node3, false);
// Receive a learn message => node3 was accepted and has started the election
ConsensusLearn learn3 = new ConsensusLearn(INSTANCE_ID, elect3Msg.getMessageId(), PAST_TIME, true, Collections.emptyList());
MessageGeneral learn3Msg = createMsg(node3KeyPair, learn3);
messageHandler.handleMessage(laoRepository, messageSender, consensusChannel, learn3Msg);
displayAssertions(STATUS_STARTED, START_STARTED, false);
nodeAssertions(grid, node3Pos, "Started by\n" + node3, false);
}
Aggregations