Skip to content

Commit 38ea93f

Browse files
committed
about implementing closed channels
1 parent 52aff93 commit 38ea93f

File tree

6 files changed

+35
-176
lines changed

6 files changed

+35
-176
lines changed

src/net/sharksystem/asap/ASAPEngine.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,21 @@ public CharSequence getExtra(CharSequence uri, String key) throws IOException {
9292
}
9393

9494
@Override
95+
public void createChannel(CharSequence urlTarget, List<CharSequence> recipients) throws IOException {
96+
this.setRecipients(urlTarget, recipients);
97+
}
98+
99+
@Override
100+
public void createChannel(CharSequence urlTarget, CharSequence recipient) throws IOException {
101+
ArrayList<CharSequence> recipients = new ArrayList<>();
102+
recipients.add(recipient);
103+
this.setRecipients(urlTarget, recipients);
104+
}
105+
95106
public void addRecipient(CharSequence urlTarget, CharSequence recipient) throws IOException {
96107
this.chunkStorage.getChunk(urlTarget, this.era).addRecipient(recipient);
97108
}
98109

99-
@Override
100110
public void setRecipients(CharSequence urlTarget, List<CharSequence> recipients) throws IOException {
101111
this.chunkStorage.getChunk(urlTarget, this.era).setRecipients(recipients);
102112
}
@@ -105,7 +115,6 @@ public List<CharSequence> getRecipients(CharSequence urlTarget) throws IOExcepti
105115
return this.chunkStorage.getChunk(urlTarget, this.era).getRecipients();
106116
}
107117

108-
@Override
109118
public void removeRecipient(CharSequence urlTarget, CharSequence recipients) throws IOException {
110119
this.chunkStorage.getChunk(urlTarget, this.era).removeRecipient(recipients);
111120
}

src/net/sharksystem/asap/ASAPStorage.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* current era is declared to be finished and an new era is opened.
3939
* Any new message is now tagged as message from that new era. The ASAPEngine
4040
* transmits all message to the peer which are stored after the final
41-
* encounter. If no encounter ever happened - allavailablee messages are
41+
* encounter. If no encounter ever happened - all availablee messages are
4242
* transmitted.
4343
*
4444
* @see ASAPEngine
@@ -53,22 +53,24 @@ public interface ASAPStorage {
5353
CharSequence getOwner();
5454

5555
/**
56-
* Adds a recipient to chunk recipient list.
57-
* @param urlTarget chunk address
58-
* @param recipient recipient to add
59-
* @throws IOException
56+
* Creates a channel with named recipients - we call it a closed channel in opposite
57+
* to an open channel.
58+
*
59+
* Peers must not forward messages from a closed to other peers than those in recipient list.
60+
*
61+
* @param urlTarget
62+
* @param recipients
63+
* @throws IOException
6064
*/
61-
public void addRecipient(CharSequence urlTarget, CharSequence recipient) throws IOException;
65+
void createChannel(CharSequence urlTarget, List<CharSequence> recipients) throws IOException;
6266

6367
/**
64-
/**
65-
* Set a list of recipients for chunk. A former list is dropped.
66-
*
67-
* @param urlTarget chunk address
68-
* @param recipients new list of recipients
68+
* Create a channel with only two members - creator and recipient
69+
* @param urlTarget
70+
* @param recipient
6971
* @throws IOException
7072
*/
71-
public void setRecipients(CharSequence urlTarget, List<CharSequence> recipients) throws IOException;
73+
void createChannel(CharSequence urlTarget, CharSequence recipient) throws IOException;
7274

7375
/**
7476
* Chunks are delivered when seeing other peers. This flag allows to decide whether delivered chunks
@@ -123,14 +125,6 @@ public interface ASAPStorage {
123125
*/
124126
public CharSequence getExtra(CharSequence uri, String key) throws IOException;
125127

126-
/**
127-
* Removes recipients
128-
* @param urlTarget chunk address
129-
* @param recipients list of recipients to be removed
130-
* @throws IOException
131-
*/
132-
public void removeRecipient(CharSequence urlTarget, CharSequence recipients) throws IOException;
133-
134128
/**
135129
*
136130
* @param uri channel uri

src/net/sharksystem/asap/MultiASAPEngineFS_Impl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ public void handleASAPManagementPDU(ASAP_PDU_1_0 asapPDU, ASAP_1_0 protocol,
417417
}
418418

419419
// else - channel does not exist - create by setting recipients
420-
asapStorage.setRecipients(channelUri, recipients);
420+
asapStorage.createChannel(channelUri, recipients);
421421
}
422422

423423
private String getLogStart() {

src/net/sharksystem/cmdline/CmdLineUI.java

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ 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";
2726

2827
private final PrintStream consoleOutput;
2928
private final BufferedReader userInput;
@@ -92,9 +91,6 @@ public void printUsage() {
9291
b.append(SET_SEND_RECEIVED_MESSAGES);
9392
b.append(".. set whether received message are to be sent");
9493
b.append("\n");
95-
b.append(ADD_RECIPIENT);
96-
b.append(".. add recipient to a storage");
97-
b.append("\n");
9894
b.append(EXIT);
9995
b.append(".. exit");
10096

@@ -161,14 +157,9 @@ public void printUsage(String cmdString, String comment) {
161157
out.println("set whether send received messages");
162158
out.println("example: " + SET_SEND_RECEIVED_MESSAGES + " Alice:chat on");
163159
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;
160+
default:
161+
out.println("unknown command: " + cmdString);
169162
}
170-
171-
out.println("unknown command: " + cmdString);
172163
}
173164

174165
public void runCommandLoop() {
@@ -219,8 +210,6 @@ public void runCommandLoop() {
219210
this.doResetASAPStorages(); break;
220211
case SET_SEND_RECEIVED_MESSAGES:
221212
this.doSetSendReceivedMessage(parameterString); break;
222-
case ADD_RECIPIENT:
223-
this.doAddRecipient(parameterString); break;
224213
case "q": // convenience
225214
case EXIT:
226215
this.doKill("all");
@@ -456,21 +445,6 @@ public void doSetSendReceivedMessage(String parameterString) {
456445
}
457446
}
458447

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-
474448
////////////////////////////////////////////////////////////////////////////////////////////////////////////
475449
// helper methods //
476450
////////////////////////////////////////////////////////////////////////////////////////////////////////////

test/net/sharksystem/asap/MultihopTests.java

Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,6 @@ public void createNonOpenStorage() throws IOException, ASAPException, Interrupte
104104
ui.doCreateASAPStorage("Bob nonOpen");
105105
ui.doCreateASAPStorage("Clara nonOpen");
106106

107-
ui.doAddRecipient("Alice:nonOpen Bob");
108-
ui.doAddRecipient("Alice:nonOpen Clara");
109-
ui.doAddRecipient("Alice:nonOpen David");
110-
111-
// message shall reach Bob and Clara but not David
112-
ui.doAddRecipient("Bob:nonOpen Clara");
113-
114-
ui.doSetSendReceivedMessage("Alice:nonOpen on");
115-
ui.doSetSendReceivedMessage("Bob:nonOpen on");
116-
117107
// add message to alice storage
118108
String messageAlice2Clara = "HiClara";
119109
String parameters = "Alice nonOpen abcChat " + messageAlice2Clara;
@@ -169,116 +159,6 @@ public void createNonOpenStorage() throws IOException, ASAPException, Interrupte
169159
Assert.assertTrue(aliceFound && bobFound && claraFound && davidFound);
170160
}
171161

172-
//@Test
173-
public void twoHopsNonOpenStorage() throws IOException, ASAPException, InterruptedException {
174-
CmdLineUI ui = new CmdLineUI(System.out);
175-
176-
ui.doResetASAPStorages();
177-
178-
// create storages
179-
ui.doCreateASAPStorage("Alice twoHops");
180-
ui.doCreateASAPStorage("Bob twoHops");
181-
ui.doCreateASAPStorage("Clara twoHops");
182-
ui.doCreateASAPStorage("David twoHops");
183-
184-
ui.doAddRecipient("Alice:twoHops Bob");
185-
ui.doAddRecipient("Alice:twoHops Clara");
186-
ui.doAddRecipient("Alice:twoHops David");
187-
188-
// message shall reach Bob and Clara but not David
189-
ui.doAddRecipient("Bob:twoHops Clara");
190-
191-
ui.doSetSendReceivedMessage("Alice:twoHops on");
192-
ui.doSetSendReceivedMessage("Bob:twoHops on");
193-
194-
// add message to alice storage
195-
String messageAlice2Clara = "HiClara";
196-
String parameters = "Alice twoHops abcChat " + messageAlice2Clara;
197-
ui.doCreateASAPMessage(parameters);
198-
199-
System.out.println("**************************************************************************");
200-
System.out.println("** connect Alice with Bob **");
201-
System.out.println("**************************************************************************");
202-
// connect alice with bob
203-
ui.doCreateASAPMultiEngine("Alice");
204-
ui.doOpen("7070 Alice");
205-
// wait a moment to give server socket time to be created
206-
Thread.sleep(10);
207-
ui.doCreateASAPMultiEngine("Bob");
208-
209-
ui.doConnect("7070 Bob");
210-
211-
// alice should be in era 1 (content has changed before connection) and bob era is 0 - no changes
212-
213-
// wait a moment
214-
Thread.sleep(1000);
215-
216-
// kill connections
217-
ui.doKill("all");
218-
219-
// alice should stay in era 1 (no content change), bob should be in era 1 received something
220-
221-
// wait a moment
222-
Thread.sleep(1000);
223-
224-
System.out.println("**************************************************************************");
225-
System.out.println("** connect Bob with David **");
226-
System.out.println("**************************************************************************");
227-
// connect alice with bob
228-
ui.doCreateASAPMultiEngine("Bob");
229-
ui.doOpen("7070 Bob");
230-
// wait a moment to give server socket time to be created
231-
Thread.sleep(10);
232-
ui.doCreateASAPMultiEngine("David");
233-
234-
ui.doConnect("7070 David");
235-
236-
// alice should be in era 1 (content has changed before connection) and bob era is 0 - no changes
237-
238-
// wait a moment
239-
Thread.sleep(1000);
240-
241-
// kill connections
242-
ui.doKill("all");
243-
244-
// alice should stay in era 1 (no content change), bob should be in era 1 received something
245-
246-
// wait a moment
247-
Thread.sleep(1000);
248-
249-
System.out.println("**************************************************************************");
250-
System.out.println("** connect Bob with Clara **");
251-
System.out.println("**************************************************************************");
252-
ui.doCreateASAPMultiEngine("Clara");
253-
ui.doOpen("8080 Clara");
254-
// wait a moment to give server socket time to be created
255-
Thread.sleep(10);
256-
ui.doConnect("8080 Bob");
257-
258-
// bob should remain in era 1 o changes, clara is era 0
259-
260-
// wait a moment
261-
Thread.sleep(1000);
262-
// kill connections
263-
ui.doKill("all");
264-
265-
// get Clara storage
266-
ASAPStorage clara = this.getFreshStorageByName(ui, "Clara:twoHops");
267-
268-
/* message was actually from Bob but originated from Alice. It is put
269-
into a incoming folder as it would have been directly received from Alice.
270-
Signatures would allow ensuring if origin was really who mediator claims to be.
271-
*/
272-
ASAPChunkStorage claraBob = clara.getIncomingChunkStorage("Alice");
273-
274-
// clara era was increased after connection terminated - message from bob is in era before current one
275-
int eraToLook = ASAPEngine.previousEra(clara.getEra());
276-
ASAPChunk claraABCChat = claraBob.getChunk("abcChat", eraToLook);
277-
CharSequence message = claraABCChat.getMessages().next();
278-
boolean same = messageAlice2Clara.equalsIgnoreCase(message.toString());
279-
Assert.assertTrue(same);
280-
}
281-
282162
private ASAPStorage getFreshStorageByName(CmdLineUI ui, String storageName) throws ASAPException, IOException {
283163
String rootFolder = ui.getEngineRootFolderByStorageName(storageName);
284164
return ASAPEngineFS.getExistingASAPEngineFS(rootFolder);

test/net/sharksystem/asap/Point2PointTests.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,17 @@ public void notOpenMessageChunkExchange() throws IOException, ASAPException, Int
186186
ASAPStorage aliceStorage =
187187
ASAPEngineFS.getASAPStorage(ALICE, ALICE_APP_FOLDER, CHAT_FORMAT);
188188

189-
aliceStorage.addRecipient(ALICE_BOB_CHAT_URL, BOB); // Add recipient: make it a non-open channel
189+
aliceStorage.createChannel(ALICE_BOB_CHAT_URL, BOB); // Add recipient: make it a non-open channel
190190
aliceStorage.add(ALICE_BOB_CHAT_URL, ALICE2BOB_MESSAGE);
191191
aliceStorage.add(ALICE_BOB_CHAT_URL, ALICE2BOB_MESSAGE2);
192192

193193
// bob does the same
194194
ASAPStorage bobStorage =
195195
ASAPEngineFS.getASAPStorage(BOB, BOB_APP_FOLDER, CHAT_FORMAT);
196196

197-
bobStorage.addRecipient(ALICE_BOB_CHAT_URL, ALICE); // Add recipient: make it a non-open channel
197+
int bobInitialEra = bobStorage.getEra();
198+
199+
bobStorage.createChannel(ALICE_BOB_CHAT_URL, ALICE); // Add recipient: make it a non-open channel
198200
bobStorage.add(ALICE_BOB_CHAT_URL, BOB2ALICE_MESSAGE);
199201
bobStorage.add(ALICE_BOB_CHAT_URL, BOB2ALICE_MESSAGE2);
200202

@@ -305,7 +307,7 @@ public void notOpenMessageChunkExchange() throws IOException, ASAPException, Int
305307

306308
// simulate a sync
307309
bobStorage = ASAPEngineFS.getASAPStorage(BOB, BOB_APP_FOLDER, CHAT_FORMAT);
308-
Assert.assertEquals(2, bobStorage.getEra());
310+
Assert.assertEquals(bobInitialEra+2, bobStorage.getEra());
309311

310312
Thread.sleep(1000);
311313
}
@@ -324,7 +326,7 @@ public void usageWithImmediateSync() throws IOException, ASAPException, Interrup
324326

325327
int aliceInitialEra = aliceStorage.getEra();
326328

327-
aliceStorage.addRecipient(ALICE_BOB_CHAT_URL, BOB);
329+
aliceStorage.createChannel(ALICE_BOB_CHAT_URL, BOB);
328330

329331
// content changed - next change in topology should increase alice era.
330332
aliceStorage.add(ALICE_BOB_CHAT_URL, ALICE2BOB_MESSAGE);
@@ -334,7 +336,7 @@ public void usageWithImmediateSync() throws IOException, ASAPException, Interrup
334336

335337
int bobInitialEra = bobStorage.getEra();
336338

337-
bobStorage.addRecipient(ALICE_BOB_CHAT_URL, ALICE);
339+
bobStorage.createChannel(ALICE_BOB_CHAT_URL, ALICE);
338340

339341
// content changed - next change in topology should increase bob era.
340342
bobStorage.add(ALICE_BOB_CHAT_URL, BOB2ALICE_MESSAGE);

0 commit comments

Comments
 (0)