Skip to content

Commit bdbf920

Browse files
committed
start working on named multihop scenario
1 parent 1a36305 commit bdbf920

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

src/net/sharksystem/cmdline/CmdLineUI.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class CmdLineUI {
2323
public static final String CREATE_ASAP_MESSAGE = "newmessage";
2424
public static final String RESET_ASAP_STORAGES = "resetstorage";
2525
public static final String SET_SEND_RECEIVED_MESSAGES = "sendReceived";
26+
public static final String ADD_RECIPIENT = "sentRecipient";
2627

2728
private final PrintStream consoleOutput;
2829
private final BufferedReader userInput;
@@ -91,6 +92,9 @@ public void printUsage() {
9192
b.append(SET_SEND_RECEIVED_MESSAGES);
9293
b.append(".. set whether received message are to be sent");
9394
b.append("\n");
95+
b.append(ADD_RECIPIENT);
96+
b.append(".. add recipient to a storage");
97+
b.append("\n");
9498
b.append(EXIT);
9599
b.append(".. exit");
96100

@@ -157,6 +161,11 @@ public void printUsage(String cmdString, String comment) {
157161
out.println("set whether send received messages");
158162
out.println("example: " + SET_SEND_RECEIVED_MESSAGES + " Alice:chat on");
159163
break;
164+
case ADD_RECIPIENT:
165+
out.println(ADD_RECIPIENT + " storageName recipientName");
166+
out.println("add recipient to storage");
167+
out.println("example: " + ADD_RECIPIENT + " Alice:chat Bob");
168+
break;
160169
}
161170

162171
out.println("unknown command: " + cmdString);
@@ -210,6 +219,8 @@ public void runCommandLoop() {
210219
this.doResetASAPStorages(); break;
211220
case SET_SEND_RECEIVED_MESSAGES:
212221
this.doSetSendReceivedMessage(parameterString); break;
222+
case ADD_RECIPIENT:
223+
this.doAddRecipient(parameterString); break;
213224
case "q": // convenience
214225
case EXIT:
215226
this.doKill("all");
@@ -445,6 +456,32 @@ public void doSetSendReceivedMessage(String parameterString) {
445456
}
446457
}
447458

459+
public void doAddRecipient(String parameterString) {
460+
StringTokenizer st = new StringTokenizer(parameterString);
461+
462+
try {
463+
String storageName = st.nextToken();
464+
String recipient = st.nextToken();
465+
466+
ASAPStorage storage = this.getStorage(storageName);
467+
storage.addRecipient(this.getUriFromStorageName(storageName), recipient);
468+
}
469+
catch(RuntimeException | IOException | ASAPException e) {
470+
this.printUsage(SET_SEND_RECEIVED_MESSAGES, e.getLocalizedMessage());
471+
}
472+
}
473+
474+
////////////////////////////////////////////////////////////////////////////////////////////////////////////
475+
// helper methods //
476+
////////////////////////////////////////////////////////////////////////////////////////////////////////////
477+
478+
private String getUriFromStorageName(String storageName) throws ASAPException {
479+
int i = storageName.indexOf(":");
480+
if(i < 0) throw new ASAPException("malformed storage name (missing \":\") " + storageName);
481+
482+
return storageName.substring(i);
483+
}
484+
448485
private ASAPStorage getStorage(String storageName) throws ASAPException {
449486
ASAPStorage asapStorage = this.storages.get(storageName);
450487
if(asapStorage == null) throw new ASAPException("no storage with name: " + storageName);

test/net/sharksystem/asap/MultihopTests.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,88 @@ public void twoHops() throws IOException, ASAPException, InterruptedException {
9191
boolean same = messageAlice2Clara.equalsIgnoreCase(message.toString());
9292
Assert.assertTrue(same);
9393
}
94+
95+
@Test
96+
public void twoHopsNonOpenStorage() throws IOException, ASAPException, InterruptedException {
97+
CmdLineUI ui = new CmdLineUI(System.out);
98+
99+
ui.doResetASAPStorages();
100+
101+
// create storages
102+
ui.doCreateASAPStorage("Alice twoHops");
103+
ui.doCreateASAPStorage("Bob twoHops");
104+
ui.doCreateASAPStorage("Clara twoHops");
105+
106+
ui.doAddRecipient("Alice:twoHops Bob");
107+
ui.doAddRecipient("Alice:twoHops Clara");
108+
109+
Assert.fail("test case implementation not yet finished");
110+
111+
ui.doSetSendReceivedMessage("Alice:twoHops on");
112+
ui.doSetSendReceivedMessage("Bob:twoHops on");
113+
ui.doSetSendReceivedMessage("Clara:twoHops on");
114+
115+
// add message to alice storage
116+
String messageAlice2Clara = "HiClara";
117+
String parameters = "Alice twoHops abcChat " + messageAlice2Clara;
118+
ui.doCreateASAPMessage(parameters);
119+
120+
System.out.println("**************************************************************************");
121+
System.out.println("** connect Alice with Bob **");
122+
System.out.println("**************************************************************************");
123+
// connect alice with bob
124+
ui.doCreateASAPMultiEngine("Alice");
125+
ui.doOpen("7070 Alice");
126+
// wait a moment to give server socket time to be created
127+
Thread.sleep(10);
128+
ui.doCreateASAPMultiEngine("Bob");
129+
130+
ui.doConnect("7070 Bob");
131+
132+
// alice should be in era 1 (content has changed before connection) and bob era is 0 - no changes
133+
134+
// wait a moment
135+
Thread.sleep(1000);
136+
137+
// kill connections
138+
ui.doKill("all");
139+
140+
// alice should stay in era 1 (no content change), bob should be in era 1 received something
141+
142+
// wait a moment
143+
Thread.sleep(1000);
144+
145+
System.out.println("**************************************************************************");
146+
System.out.println("** connect Bob with Clara **");
147+
System.out.println("**************************************************************************");
148+
ui.doCreateASAPMultiEngine("Clara");
149+
ui.doOpen("8080 Clara");
150+
// wait a moment to give server socket time to be created
151+
Thread.sleep(10);
152+
ui.doConnect("8080 Bob");
153+
154+
// bob should remain in era 1 o changes, clara is era 0
155+
156+
// wait a moment
157+
Thread.sleep(1000);
158+
// kill connections
159+
ui.doKill("all");
160+
161+
// get Clara storage
162+
String rootFolder = ui.getEngineRootFolderByStorageName("Clara:twoHops");
163+
ASAPStorage clara = ASAPEngineFS.getExistingASAPEngineFS(rootFolder);
164+
165+
/* message was actually from Bob but originated from Alice. It is put
166+
into a incoming folder as it would have been directly received from Alice.
167+
Signatures would allow ensuring if origin was really who mediator claims to be.
168+
*/
169+
ASAPChunkStorage claraBob = clara.getIncomingChunkStorage("Alice");
170+
171+
// clara era was increased after connection terminated - message from bob is in era before current one
172+
int eraToLook = ASAPEngine.previousEra(clara.getEra());
173+
ASAPChunk claraABCChat = claraBob.getChunk("abcChat", eraToLook);
174+
CharSequence message = claraABCChat.getMessages().next();
175+
boolean same = messageAlice2Clara.equalsIgnoreCase(message.toString());
176+
Assert.assertTrue(same);
177+
}
94178
}

0 commit comments

Comments
 (0)