Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Feat: Making mini protocol components server/client aware #64

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public abstract class Agent<T extends AgentListener> {
private final List<T> agentListeners = new ArrayList<>();
private AcceptVersion acceptVersion;

private final boolean isClient;

public Agent(boolean isClient) {
this.isClient = isClient;
}

public void setChannel(Channel channel) {
if (this.channel != null && this.channel.isActive())
log.warn("An active channel is already attached to this agent");
Expand All @@ -25,7 +31,7 @@ public void setChannel(Channel channel) {
}

public void sendRequest(Message message) {
if (currenState.hasAgency()) {
if (currenState.hasAgency(isClient)) {
currenState = currenState.nextState(message);
} else {
//TODO
Expand Down Expand Up @@ -69,7 +75,7 @@ public final void sendNextMessage() {
}

public final boolean hasAgency() {
return currenState.hasAgency();
return currenState.hasAgency(isClient);
}

public final synchronized void addListener(T agentListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public interface State {
State nextState(Message message);

boolean hasAgency();
boolean hasAgency(boolean isClient);

default Message handleInbound(byte[] bytes) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public class BlockfetchAgent extends Agent<BlockfetchAgentListener> {
private long errorBlks;

public BlockfetchAgent() {
this(true);
}
public BlockfetchAgent(boolean isClient) {
super(isClient);
this.currenState = Idle;

this.startTime = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ else if (message instanceof ClientDone)
}

@Override
public boolean hasAgency() {
return true;
public boolean hasAgency(boolean isClient) {
return isClient;
}
},
Busy {
Expand All @@ -33,8 +33,8 @@ else if (message instanceof StartBatch)
}

@Override
public boolean hasAgency() {
return false;
public boolean hasAgency(boolean isClient) {
return !isClient;
}
},
Streaming {
Expand All @@ -49,8 +49,8 @@ public State nextState(Message message) {
}

@Override
public boolean hasAgency() {
return false;
public boolean hasAgency(boolean isClient) {
return !isClient;
}
},
Done {
Expand All @@ -60,7 +60,7 @@ public State nextState(Message message) {
}

@Override
public boolean hasAgency() {
public boolean hasAgency(boolean isClient) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class LocalChainSyncAgent extends Agent<LocalChainSyncAgentListener> {
private int counter = 0;

public LocalChainSyncAgent(Point[] knownPoints) {
this(knownPoints, true);
}
public LocalChainSyncAgent(Point[] knownPoints, boolean isClient) {
super(isClient);
this.currenState = Idle;
this.knownPoints = knownPoints;

Expand All @@ -29,6 +33,10 @@ public LocalChainSyncAgent(Point[] knownPoints) {
}

public LocalChainSyncAgent(Point[] knownPoints, long stopSlotNo, int agentNo) {
this(knownPoints,stopSlotNo, agentNo, true);
}
public LocalChainSyncAgent(Point[] knownPoints, long stopSlotNo, int agentNo, boolean isClient) {
super(isClient);
this.currenState = Idle;
this.knownPoints = knownPoints;
this.stopAt = stopSlotNo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ else if (message instanceof ChainSyncMsgDone)
}

@Override
public boolean hasAgency() {
return true;
public boolean hasAgency(boolean isClient) {
return isClient;
}
},
CanAwait {
Expand All @@ -39,8 +39,8 @@ else if (message instanceof Rollbackward)
}

@Override
public boolean hasAgency() {
return false;
public boolean hasAgency(boolean isClient) {
return !isClient;
}
},
MustReply {
Expand All @@ -55,8 +55,8 @@ else if (message instanceof Rollbackward)
}

@Override
public boolean hasAgency() {
return false;
public boolean hasAgency(boolean isClient) {
return !isClient;
}
},
Intersect {
Expand All @@ -71,8 +71,8 @@ else if (message instanceof IntersectNotFound)
}

@Override
public boolean hasAgency() {
return false;
public boolean hasAgency(boolean isClient) {
return !isClient;
}
},
Done {
Expand All @@ -82,7 +82,7 @@ public State nextState(Message message) {
}

@Override
public boolean hasAgency() {
public boolean hasAgency(boolean isClient) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ else if (message instanceof ChainSyncMsgDone)
}

@Override
public boolean hasAgency() {
return true;
public boolean hasAgency(boolean isClient) {
return isClient;
}
},
CanAwait {
Expand All @@ -39,8 +39,8 @@ else if (message instanceof Rollbackward)
}

@Override
public boolean hasAgency() {
return false;
public boolean hasAgency(boolean isClient) {
return !isClient;
}
},
MustReply {
Expand All @@ -55,8 +55,8 @@ else if (message instanceof Rollbackward)
}

@Override
public boolean hasAgency() {
return false;
public boolean hasAgency(boolean isClient) {
return !isClient;
}
},
Intersect {
Expand All @@ -71,8 +71,8 @@ else if (message instanceof IntersectNotFound)
}

@Override
public boolean hasAgency() {
return false;
public boolean hasAgency(boolean isClient) {
return !isClient;
}
},
Done {
Expand All @@ -82,7 +82,7 @@ public State nextState(Message message) {
}

@Override
public boolean hasAgency() {
public boolean hasAgency(boolean isClient) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public class ChainsyncAgent extends Agent<ChainSyncAgentListener> {
private long startTime;

public ChainsyncAgent(Point[] knownPoints) {
this(knownPoints, true);
}
public ChainsyncAgent(Point[] knownPoints, boolean isClient) {
super(isClient);
this.currenState = Idle;
this.knownPoints = knownPoints;

Expand All @@ -31,6 +35,10 @@ public ChainsyncAgent(Point[] knownPoints) {
}

public ChainsyncAgent(Point[] knownPoints, long stopSlotNo, int agentNo) {
this(knownPoints, stopSlotNo, agentNo, true);
}
public ChainsyncAgent(Point[] knownPoints, long stopSlotNo, int agentNo, boolean isClient) {
super(isClient);
this.currenState = Idle;
this.knownPoints = knownPoints;
this.stopAt = stopSlotNo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public class HandshakeAgent extends Agent<HandshakeAgentListener> {
private final VersionTable versionTable;

public HandshakeAgent(VersionTable versionTable) {
this(versionTable,true);
}
public HandshakeAgent(VersionTable versionTable, boolean isClient) {
super(isClient);
this.versionTable = versionTable;
this.currenState = Propose;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public HandshkeState nextState(Message message) {
}

@Override
public boolean hasAgency() {
return true;
public boolean hasAgency(boolean isClient) {
return isClient;
}
},
Confirm {
Expand All @@ -21,8 +21,8 @@ public HandshkeState nextState(Message message) {
}

@Override
public boolean hasAgency() {
return false;
public boolean hasAgency(boolean isClient) {
return !isClient;
}
},
Done {
Expand All @@ -32,7 +32,7 @@ public HandshkeState nextState(Message message) {
}

@Override
public boolean hasAgency() {
public boolean hasAgency(boolean isClient) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class KeepAliveAgent extends Agent<KeepAliveListener> {
private Queue<MsgKeepAlive> reqQueue;

public KeepAliveAgent() {
this(true);
}
public KeepAliveAgent(boolean isClient) {
super(isClient);
this.currenState = Client;
this.reqQueue = new ConcurrentLinkedQueue<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ else if (message instanceof MsgDone)
}

@Override
public boolean hasAgency() {
return true;
public boolean hasAgency(boolean isClient) {
return isClient;
}
},
Server {
Expand All @@ -28,8 +28,8 @@ public KeepAliveState nextState(Message message) {
}

@Override
public boolean hasAgency() {
return false;
public boolean hasAgency(boolean isClient) {
return !isClient;
}
},
Done {
Expand All @@ -39,8 +39,8 @@ public KeepAliveState nextState(Message message) {
}

@Override
public boolean hasAgency() {
return false;
public boolean hasAgency(boolean isClient) {
return !isClient;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public class LocalStateQueryAgent extends Agent<LocalStateQueryListener> {
private Queue<Query> pendingQueryCommands;

public LocalStateQueryAgent() {
this(true);
}
public LocalStateQueryAgent(boolean isClient) {
super(isClient);
this.currenState = Idle;

acquiredCommands = new ConcurrentLinkedQueue<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ else if (message instanceof MsgDone)
}

@Override
public boolean hasAgency() {
return true;
public boolean hasAgency(boolean isClient) {
return isClient;
}

List<Class> allowedMsgTypes = List.of(MsgAcquire.class, MsgDone.class);
Expand All @@ -44,8 +44,8 @@ else if (message instanceof MsgFailure)
}

@Override
public boolean hasAgency() {
return false;
public boolean hasAgency(boolean isClient) {
return !isClient;
}

List<Class> allowedMsgTypes = List.of(MsgAcquired.class, MsgFailure.class);
Expand All @@ -70,8 +70,8 @@ else if (message instanceof MsgRelease)
}

@Override
public boolean hasAgency() {
return true;
public boolean hasAgency(boolean isClient) {
return isClient;
}

List<Class> allowedMsgTypes = List.of(MsgQuery.class, MsgReAcquire.class, MsgRelease.class);
Expand All @@ -92,8 +92,8 @@ public State nextState(Message message) {
}

@Override
public boolean hasAgency() {
return false;
public boolean hasAgency(boolean isClient) {
return !isClient;
}

List<Class> allowedMsgTypes = List.of(MsgResult.class);
Expand All @@ -111,7 +111,7 @@ public State nextState(Message message) {
}

@Override
public boolean hasAgency() {
public boolean hasAgency(boolean isClient) {
return false;
}
}
Expand Down
Loading
Loading