Skip to content

Commit 435d66d

Browse files
committed
about implementing closed channels
1 parent 38ea93f commit 435d66d

File tree

4 files changed

+88
-24
lines changed

4 files changed

+88
-24
lines changed

src/net/sharksystem/asap/ASAPEngine.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.sharksystem.asap;
22

33
import net.sharksystem.asap.protocol.*;
4-
import net.sharksystem.asap.protocol.ASAPManagementProtocolEngine;
54
import net.sharksystem.asap.util.Log;
65

76
import java.io.IOException;
@@ -101,6 +100,8 @@ public void createChannel(CharSequence urlTarget, CharSequence recipient) throws
101100
ArrayList<CharSequence> recipients = new ArrayList<>();
102101
recipients.add(recipient);
103102
this.setRecipients(urlTarget, recipients);
103+
104+
// inform recipients about that event
104105
}
105106

106107
public void addRecipient(CharSequence urlTarget, CharSequence recipient) throws IOException {

src/net/sharksystem/cmdline/CmdLineUI.java

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import net.sharksystem.asap.*;
44

55
import java.io.*;
6-
import java.util.HashMap;
7-
import java.util.Map;
8-
import java.util.StringTokenizer;
6+
import java.util.*;
97

108
/**
119
* @author thsc
@@ -20,9 +18,11 @@ public class CmdLineUI {
2018
public static final String SETWAITING = "setwaiting";
2119
public static final String CREATE_ASAP_ENGINE = "newengine";
2220
public static final String CREATE_ASAP_STORAGE = "newstorage";
21+
public static final String CREATE_ASAP_CHANNEL = "newchannel";
2322
public static final String CREATE_ASAP_MESSAGE = "newmessage";
2423
public static final String RESET_ASAP_STORAGES = "resetstorage";
2524
public static final String SET_SEND_RECEIVED_MESSAGES = "sendReceived";
25+
public static final String PRINT_CHANNEL_INFORMATION = "printChannelInfo";
2626

2727
private final PrintStream consoleOutput;
2828
private final BufferedReader userInput;
@@ -82,6 +82,9 @@ public void printUsage() {
8282
b.append(CREATE_ASAP_STORAGE);
8383
b.append(".. create new asap storage");
8484
b.append("\n");
85+
b.append(CREATE_ASAP_CHANNEL);
86+
b.append(".. create new closed asap channel");
87+
b.append("\n");
8588
b.append(CREATE_ASAP_MESSAGE);
8689
b.append(".. add message to storage");
8790
b.append("\n");
@@ -91,6 +94,9 @@ public void printUsage() {
9194
b.append(SET_SEND_RECEIVED_MESSAGES);
9295
b.append(".. set whether received message are to be sent");
9396
b.append("\n");
97+
b.append(PRINT_CHANNEL_INFORMATION);
98+
b.append(".. print general information about a channel");
99+
b.append("\n");
94100
b.append(EXIT);
95101
b.append(".. exit");
96102

@@ -110,7 +116,6 @@ public void printUsage(String cmdString, String comment) {
110116
out.println("example: " + CONNECT + " localhost 7070 Bob");
111117
out.println("example: " + CONNECT + " 7070 Bob");
112118
out.println("in both cases try to connect to localhost:7070 and let engine Bob handle connection when established");
113-
114119
break;
115120
case OPEN:
116121
out.println(OPEN + " localPort engineName");
@@ -141,6 +146,10 @@ public void printUsage(String cmdString, String comment) {
141146
out.println(CREATE_ASAP_STORAGE + " owner appName");
142147
out.println("example: " + CREATE_ASAP_STORAGE + " Alice chat");
143148
break;
149+
case CREATE_ASAP_CHANNEL:
150+
out.println(CREATE_ASAP_CHANNEL + " owner appName uri (recipient)+");
151+
out.println("example: " + CREATE_ASAP_CHANNEL + " Alice chat sn2://abChat Bob Clara");
152+
break;
144153
case CREATE_ASAP_MESSAGE:
145154
out.println(CREATE_ASAP_MESSAGE + " owner appName uri message");
146155
out.println("example: " + CREATE_ASAP_MESSAGE + " Alice chat sn2://abChat HiBob");
@@ -157,6 +166,10 @@ public void printUsage(String cmdString, String comment) {
157166
out.println("set whether send received messages");
158167
out.println("example: " + SET_SEND_RECEIVED_MESSAGES + " Alice:chat on");
159168
break;
169+
case PRINT_CHANNEL_INFORMATION:
170+
out.println(PRINT_CHANNEL_INFORMATION + " user appName uri");
171+
out.println("example: " + PRINT_CHANNEL_INFORMATION + " Alice chat sn2://abChat");
172+
break;
160173
default:
161174
out.println("unknown command: " + cmdString);
162175
}
@@ -204,12 +217,16 @@ public void runCommandLoop() {
204217
this.doCreateASAPMultiEngine(parameterString); break;
205218
case CREATE_ASAP_STORAGE:
206219
this.doCreateASAPStorage(parameterString); break;
220+
case CREATE_ASAP_CHANNEL:
221+
this.doCreateASAPChannel(parameterString); break;
207222
case CREATE_ASAP_MESSAGE:
208223
this.doCreateASAPMessage(parameterString); break;
209224
case RESET_ASAP_STORAGES:
210225
this.doResetASAPStorages(); break;
211226
case SET_SEND_RECEIVED_MESSAGES:
212227
this.doSetSendReceivedMessage(parameterString); break;
228+
case PRINT_CHANNEL_INFORMATION:
229+
this.doPrintChannelInformation(parameterString); break;
213230
case "q": // convenience
214231
case EXIT:
215232
this.doKill("all");
@@ -400,6 +417,35 @@ public void doCreateASAPStorage(String parameterString) {
400417
}
401418
}
402419

420+
public void doCreateASAPChannel(String parameterString) {
421+
StringTokenizer st = new StringTokenizer(parameterString);
422+
423+
try {
424+
String owner = st.nextToken();
425+
String appName = st.nextToken();
426+
String uri = st.nextToken();
427+
428+
String appFolderName = TESTS_ROOT_FOLDER + "/" + owner + "/" + appName;
429+
String format = "sn2://" + appName;
430+
431+
List<CharSequence> recipients = new ArrayList<>();
432+
while(st.hasMoreTokens()) {
433+
recipients.add(st.nextToken());
434+
}
435+
436+
ASAPStorage storage = ASAPEngineFS.getASAPStorage(owner, appFolderName, format);
437+
438+
storage.createChannel(uri, recipients);
439+
440+
this.storages.put(this.getStorageKey(owner, appName), storage);
441+
}
442+
catch(RuntimeException e) {
443+
this.printUsage(CREATE_ASAP_STORAGE, e.getLocalizedMessage());
444+
} catch (IOException | ASAPException e) {
445+
this.printUsage(CREATE_ASAP_STORAGE, e.getLocalizedMessage());
446+
}
447+
}
448+
403449
public void doCreateASAPMessage(String parameterString) {
404450
StringTokenizer st = new StringTokenizer(parameterString);
405451

@@ -445,6 +491,37 @@ public void doSetSendReceivedMessage(String parameterString) {
445491
}
446492
}
447493

494+
public void doPrintChannelInformation(String parameterString) {
495+
// out.println("example: " + PRINT_CHANNEL_INFORMATION + " Alice chat sn2://abChat");
496+
StringTokenizer st = new StringTokenizer(parameterString);
497+
498+
try {
499+
String owner = st.nextToken();
500+
String appName = st.nextToken();
501+
String uri = st.nextToken();
502+
503+
// first - get storage
504+
ASAPStorage asapStorage = this.storages.get(this.getStorageKey(owner, appName));
505+
if(asapStorage == null) {
506+
System.err.println("storage does not exist: " + this.getStorageKey(owner, appName));
507+
return;
508+
}
509+
510+
List<CharSequence> recipients = asapStorage.getRecipients(uri);
511+
512+
System.out.println("Owner:App:Channel == " + owner + ":" + appName + ":" + uri);
513+
System.out.println("#Recipients == " + recipients.size());
514+
for(CharSequence recipient : recipients) {
515+
System.out.println(recipient);
516+
}
517+
}
518+
catch(RuntimeException e) {
519+
this.printUsage(CREATE_ASAP_MESSAGE, e.getLocalizedMessage());
520+
} catch (IOException e) {
521+
this.printUsage(CREATE_ASAP_MESSAGE, e.getLocalizedMessage());
522+
}
523+
}
524+
448525
////////////////////////////////////////////////////////////////////////////////////////////////////////////
449526
// helper methods //
450527
////////////////////////////////////////////////////////////////////////////////////////////////////////////

test/net/sharksystem/asap/MultihopTests.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,13 @@ public void twoHops() throws IOException, ASAPException, InterruptedException {
9393
Assert.assertTrue(same);
9494
}
9595

96-
//@Test
96+
@Test
9797
public void createNonOpenStorage() throws IOException, ASAPException, InterruptedException {
9898
CmdLineUI ui = new CmdLineUI(System.out);
99-
10099
ui.doResetASAPStorages();
101100

102-
// Alice creates storage with three recipients
103-
ui.doCreateASAPStorage("Alice nonOpen");
104-
ui.doCreateASAPStorage("Bob nonOpen");
105-
ui.doCreateASAPStorage("Clara nonOpen");
106-
107-
// add message to alice storage
108-
String messageAlice2Clara = "HiClara";
109-
String parameters = "Alice nonOpen abcChat " + messageAlice2Clara;
110-
ui.doCreateASAPMessage(parameters);
111-
112-
// message is no in Alice storage
101+
ui.doCreateASAPChannel(" Alice chat sn2://closedChannel Bob Clara");
102+
ui.doPrintChannelInformation("Alice chat sn2://closedChannel");
113103

114104
System.out.println("**************************************************************************");
115105
System.out.println("** connect Alice with Bob **");
@@ -137,26 +127,23 @@ public void createNonOpenStorage() throws IOException, ASAPException, Interrupte
137127
Thread.sleep(1000);
138128

139129
// Bob should now have created an closed asap storage with three recipients
140-
ASAPStorage bobStorage = this.getFreshStorageByName(ui, "Bob:nonOpen");
130+
ASAPStorage bobStorage = this.getFreshStorageByName(ui, "Bob:closedChannel");
141131

142-
CharSequence uri = bobStorage.getChannelURIs().get(0);
143132
List<CharSequence> recipientsList = bobStorage.getRecipients("nonOpen");
144133
boolean aliceFound = false;
145134
boolean bobFound = false;
146135
boolean claraFound = false;
147-
boolean davidFound = false;
148136
for(CharSequence recipient : recipientsList) {
149137
String recipientString = recipient.toString();
150138
switch(recipient.toString()) {
151139
case "Alice": aliceFound = true; break;
152140
case "Bob": bobFound = true; break;
153141
case "Clara": claraFound = true; break;
154-
case "David": davidFound = true; break;
155142
default: Assert.fail("found unexpected recipient: " + recipient);
156143
}
157144
}
158145

159-
Assert.assertTrue(aliceFound && bobFound && claraFound && davidFound);
146+
Assert.assertTrue(aliceFound && bobFound && claraFound);
160147
}
161148

162149
private ASAPStorage getFreshStorageByName(CmdLineUI ui, String storageName) throws ASAPException, IOException {

test/net/sharksystem/asap/Point2PointTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import net.sharksystem.asap.util.ASAPChunkReceivedTester;
88
import net.sharksystem.asap.util.ASAPEngineThread;
9-
import net.sharksystem.cmdline.CmdLineUI;
109
import net.sharksystem.cmdline.TCPChannel;
1110
import org.junit.Test;
1211
import org.junit.Assert;

0 commit comments

Comments
 (0)