Skip to content

Commit

Permalink
#302 Update lock logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dvoraka committed Jan 8, 2018
1 parent 5988837 commit a00dc90
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,18 @@ default ReplicationMessage createDiagnosticsMessage(String fromNode) {
.toId(fromNode)
.build();
}

default boolean isUnicast(ReplicationMessage message) {
return message.getRouting() == MessageRouting.UNICAST;
}

default boolean isCommand(ReplicationMessage message, Command... commands) {
for (Command command : commands) {
if (message.getCommand() == command) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import dvoraka.avservice.client.service.response.ReplicationMessageList;
import dvoraka.avservice.client.service.response.ReplicationResponseClient;
import dvoraka.avservice.common.data.Command;
import dvoraka.avservice.common.data.replication.MessageRouting;
import dvoraka.avservice.common.data.replication.ReplicationMessage;
import dvoraka.avservice.common.data.replication.ReplicationStatus;
import dvoraka.avservice.common.helper.WaitingHelper;
Expand Down Expand Up @@ -293,20 +292,21 @@ public void setMaxResponseTime(int maxResponseTime) {

@Override
public void onMessage(ReplicationMessage message) {

if (!isRunning()) {
return;
}

// input filtering
if (message.getRouting() == MessageRouting.UNICAST
|| message.getCommand() == Command.DISCOVER
|| message.getCommand() == Command.EXISTS) {
if (isUnicast(message) || isCommand(message, Command.DISCOVER, Command.EXISTS)) {
return;
}

log.debug("On message {}: {}", idString, message);

switch (message.getCommand()) {
case SEQUENCE:
if (getSequence() != NOT_INITIALIZED) {
serviceClient.sendMessage(createSequenceReply(message, nodeId, getSequence()));
}
sequence(message);
break;

case LOCK:
Expand All @@ -327,6 +327,10 @@ public void onMessage(ReplicationMessage message) {
}
}

private void sequence(ReplicationMessage message) {
serviceClient.sendMessage(createSequenceReply(message, nodeId, getSequence()));
}

private void lock(ReplicationMessage message) {
if (getSequence() == message.getSequence() && lockingLock.tryLock()) {

Expand Down

0 comments on commit a00dc90

Please sign in to comment.