Skip to content

Commit 0175511

Browse files
committed
create a channel when assimilating pdu for a channel that does not yet exist.
1 parent 8bc4d39 commit 0175511

File tree

8 files changed

+165
-13
lines changed

8 files changed

+165
-13
lines changed

.idea/artifacts/ASAPJava_jar.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/net/sharksystem/asap/ASAPEngine.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* @author thsc
1919
*/
2020
public abstract class ASAPEngine extends ASAPStorageImpl implements ASAPStorage, ASAPProtocolEngine, ASAPManagementStorage {
21+
private DefaultPeerSecurityAdministrator securityAdministrator = new DefaultPeerSecurityAdministrator();
22+
2123
public static final String ANONYMOUS_OWNER = "anon";
2224
static String DEFAULT_OWNER = ANONYMOUS_OWNER;
2325
static int DEFAULT_INIT_ERA = 0;
@@ -382,7 +384,7 @@ public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimiliationPDU,
382384
//<<<<<<<<<<<<<<<<<<debug
383385
StringBuilder b = new StringBuilder();
384386
b.append(this.getLogStart());
385-
b.append("going to assimilate pdu sender: ");
387+
b.append("handle assimilate pdu received from ");
386388
b.append(sender);
387389
b.append(" | era: ");
388390
b.append(eraSender);
@@ -400,6 +402,7 @@ public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimiliationPDU,
400402
//>>>>>>>>>>>>>>>>>>>debug
401403

402404
boolean changed = false;
405+
boolean allowedAssimilation = true;
403406

404407
try {
405408
// read URI
@@ -409,17 +412,23 @@ public void handleASAPAssimilate(ASAP_AssimilationPDU_1_0 asapAssimiliationPDU,
409412
ASAPChunk localChunk = null;
410413

411414
if(!incomingSenderStorage.existsChunk(uri, eraSender)) {
412-
//<<<<<<<<<<<<<<<<<<debug
413-
b = new StringBuilder();
414-
b.append(this.getLogStart());
415-
b.append("no incoming chunk yet | ");
416-
b.append(asapAssimiliationPDU.toString());
417-
System.out.println(b.toString());
418-
//>>>>>>>>>>>>>>>>>>>debug
419-
420415
// is there a local chunk - to clone recipients from?
421416
if(this.channelExists(uri)) {
422417
localChunk = this.getStorage().getChunk(uri, this.getEra());
418+
} else {
419+
System.out.println(this.getLogStart()
420+
+ "asked to set up new channel: (uri/sender): " + uri + " | " + sender);
421+
// this channel is new to local peer - am I allowed to create it?
422+
if(!this.securityAdministrator.allowedToCreateChannel(asapAssimiliationPDU)) {
423+
System.out.println(this.getLogStart()
424+
+ ".. not allowed .. TODO not yet implemented .. always set up");
425+
426+
allowedAssimilation = false; // TODO
427+
} else {
428+
System.out.println(this.getLogStart()
429+
+ "allowed. Set it up.");
430+
this.createChannel(uri);
431+
}
423432
}
424433
}
425434

src/net/sharksystem/asap/ASAPEngineFS.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static ASAPEngineFS getASAPEngineFS(String owner, String rootDirectory, CharSequ
108108
// root directory must exist when setting up an engine
109109
File root = new File(rootDirectory);
110110
if(!root.exists() || !root.isDirectory()) {
111-
throw new ASAPException("chunk root directory must exist when creating an ASAPEngine");
111+
throw new ASAPException("chunk root directory must exist when creating an ASAPEngine: " + rootDirectory);
112112
}
113113

114114
String formatString = format != null ? format.toString() : ASAP_1_0.ANY_FORMAT;
@@ -263,10 +263,11 @@ public static void removeFolder(String eraPathName) {
263263
}
264264

265265
dir.delete();
266+
dir.deleteOnExit();
266267
try {
267268
Thread.sleep(1); // give file system a moment
268269
} catch (InterruptedException e) {
269-
e.printStackTrace();
270+
// nobody wants to know
270271
}
271272
}
272273
}

src/net/sharksystem/asap/DefaultPeerSecurityAdministrator.java

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

3+
import net.sharksystem.asap.protocol.ASAP_AssimilationPDU_1_0;
4+
35
import java.io.IOException;
46

57
public class DefaultPeerSecurityAdministrator implements PeerSecurityAdministrator, PeerSecuritySettings {
@@ -32,4 +34,9 @@ public boolean setRevealEngineFormat(String peerName) {
3234
public boolean setSendOpenMessages(String peerName) {
3335
return false;
3436
}
37+
38+
@Override
39+
public boolean allowedToCreateChannel(ASAP_AssimilationPDU_1_0 asapAssimiliationPDU) {
40+
return true; // it is a dummy
41+
}
3542
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
package net.sharksystem.asap;
22

3+
import net.sharksystem.asap.protocol.ASAP_AssimilationPDU_1_0;
4+
35
interface PeerSecurityAdministrator {
6+
boolean allowedToCreateChannel(ASAP_AssimilationPDU_1_0 asapAssimiliationPDU);
47
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package net.sharksystem.asap;
2+
3+
import net.sharksystem.asap.*;
4+
import net.sharksystem.asap.util.ASAPChunkReceivedTester;
5+
import net.sharksystem.asap.util.ASAPPeerHandleConnectionThread;
6+
import net.sharksystem.cmdline.TCPStream;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
import java.io.IOException;
11+
import java.util.Iterator;
12+
import java.util.List;
13+
14+
public class CreateNewChannelFromOutsideTest {
15+
public static final String ALICE_BOB_CHAT_URL = "content://aliceAndBob.talk";
16+
public static final String CHAT_FORMAT = "application/x-sn2-makan";
17+
public static final String ALICE_ROOT_FOLDER = "tests/Alice";
18+
public static final String ALICE_APP_FOLDER = ALICE_ROOT_FOLDER + "/appFolder";
19+
public static final String BOB_ROOT_FOLDER = "tests/Bob";
20+
public static final String BOB_APP_FOLDER = BOB_ROOT_FOLDER + "/appFolder";
21+
public static final String ALICE = "Alice";
22+
public static final String BOB = "Bob";
23+
public static final String ALICE2BOB_MESSAGE = "Hi Bob";
24+
public static final String ALICE2BOB_MESSAGE2 = "Hi Bob again";
25+
public static final String BOB2ALICE_MESSAGE = "Hi Alice";
26+
public static final String BOB2ALICE_MESSAGE2 = "Hi Alice again";
27+
28+
private static int portnumber = 7777;
29+
30+
private int getPortNumber() {
31+
portnumber++;
32+
return portnumber;
33+
}
34+
35+
@Test
36+
public void openMessageExchange() throws IOException, ASAPException, InterruptedException {
37+
///////////////////////////////////////////////////////////////////////////////////////////////////
38+
// prepare storages //
39+
///////////////////////////////////////////////////////////////////////////////////////////////////
40+
ASAPEngineFS.removeFolder(ALICE_ROOT_FOLDER); // clean previous version before
41+
ASAPEngineFS.removeFolder(BOB_ROOT_FOLDER); // clean previous version before
42+
43+
// alice writes a message into chunkStorage
44+
ASAPStorage aliceStorage =
45+
ASAPEngineFS.getASAPStorage(ALICE, ALICE_APP_FOLDER, CHAT_FORMAT);
46+
47+
aliceStorage.add(ALICE_BOB_CHAT_URL, ALICE2BOB_MESSAGE);
48+
aliceStorage.add(ALICE_BOB_CHAT_URL, ALICE2BOB_MESSAGE2);
49+
//aliceStorage.addRecipient(ALICE_BOB_CHAT_URL, BOB);
50+
51+
// bob does the same
52+
ASAPStorage bobStorage =
53+
ASAPEngineFS.getASAPStorage(BOB, BOB_APP_FOLDER, CHAT_FORMAT);
54+
55+
// there is only Bobs storage - nothing else. That's important.
56+
57+
///////////////////////////////////////////////////////////////////////////////////////////////////
58+
// prepare peers //
59+
///////////////////////////////////////////////////////////////////////////////////////////////////
60+
61+
ASAPPeer aliceEngine = ASAPPeerFS.createASAPPeer(ALICE_ROOT_FOLDER, null);
62+
ASAPPeer bobEngine = ASAPPeerFS.createASAPPeer(BOB_ROOT_FOLDER, null);
63+
64+
///////////////////////////////////////////////////////////////////////////////////////////////////
65+
// setup connection //
66+
///////////////////////////////////////////////////////////////////////////////////////////////////
67+
68+
int portNumber = this.getPortNumber();
69+
// create connections for both sides
70+
TCPStream aliceChannel = new TCPStream(portNumber, true, "a2b");
71+
TCPStream bobChannel = new TCPStream(portNumber, false, "b2a");
72+
73+
aliceChannel.start();
74+
bobChannel.start();
75+
76+
// wait to connect
77+
aliceChannel.waitForConnection();
78+
bobChannel.waitForConnection();
79+
80+
///////////////////////////////////////////////////////////////////////////////////////////////////
81+
// run asap connection //
82+
///////////////////////////////////////////////////////////////////////////////////////////////////
83+
84+
// run engine as thread
85+
ASAPPeerHandleConnectionThread aliceEngineThread = new ASAPPeerHandleConnectionThread(aliceEngine,
86+
aliceChannel.getInputStream(), aliceChannel.getOutputStream());
87+
88+
aliceEngineThread.start();
89+
90+
// and better debugging - no new thread
91+
bobEngine.handleConnection(bobChannel.getInputStream(), bobChannel.getOutputStream());
92+
93+
// wait until communication probably ends
94+
System.out.flush();
95+
System.err.flush();
96+
Thread.sleep(2000);
97+
System.out.flush();
98+
System.err.flush();
99+
100+
// close connections: note ASAPEngine does NOT close any connection!!
101+
aliceChannel.close();
102+
bobChannel.close();
103+
System.out.flush();
104+
System.err.flush();
105+
Thread.sleep(1000);
106+
System.out.flush();
107+
System.err.flush();
108+
109+
///////////////////////////////////////////////////////////////////////////////////////////////////
110+
// tests //
111+
///////////////////////////////////////////////////////////////////////////////////////////////////
112+
113+
// there is a new channel
114+
List<CharSequence> channelURIs = bobStorage.getChannelURIs();
115+
Assert.assertEquals(1, channelURIs.size());
116+
117+
// and the one and only is...
118+
Assert.assertEquals(ALICE_BOB_CHAT_URL, channelURIs.get(0));
119+
}
120+
}

test/net/sharksystem/asap/Point2PointTests2.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public void openMessageExchange() throws IOException, ASAPException, Interrupted
118118

119119

120120
///////////////////////////////////////////////////////////////////////////////////////////////////
121-
// open incomming storages //
121+
// open incoming storages //
122122
///////////////////////////////////////////////////////////////////////////////////////////////////
123123

124124
// get messages alice received
@@ -162,6 +162,8 @@ public void openMessageExchange() throws IOException, ASAPException, Interrupted
162162
bobStorage = ASAPEngineFS.getASAPStorage(BOB, BOB_APP_FOLDER, CHAT_FORMAT);
163163
Assert.assertEquals(2, bobStorage.getEra());
164164

165+
Assert.assertEquals(1, bobStorage.getChannelURIs().size());
166+
165167
Thread.sleep(1000);
166168
}
167169

test/net/sharksystem/asap/V1TestSuite.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
BasisMethodsTests.class,
1010
Point2PointTests.class,
1111
Point2PointTests2.class,
12-
UsageExamples.class})
12+
UsageExamples.class,
13+
CreateNewChannelFromOutsideTest.class
14+
})
1315
public class V1TestSuite {
1416

1517
}

0 commit comments

Comments
 (0)