Skip to content

Commit 8bc4d39

Browse files
committed
going to make version 1 ready to release
1 parent bbbb1ca commit 8bc4d39

File tree

6 files changed

+202
-143
lines changed

6 files changed

+202
-143
lines changed

src/net/sharksystem/asap/ASAPDefaultProtocolEngine.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@
99
import java.io.InputStream;
1010
import java.io.OutputStream;
1111

12+
/**
13+
* TODO: extract protocol engine algorithms here..
14+
*/
1215
public class ASAPDefaultProtocolEngine implements ASAPProtocolEngine {
16+
private final ASAPStorage asapStorage;
17+
18+
ASAPDefaultProtocolEngine(ASAPStorage asapStorage) {
19+
this.asapStorage = asapStorage;
20+
}
21+
1322
@Override
1423
public void handleASAPInterest(ASAP_Interest_PDU_1_0 asapInterest, ASAP_1_0 protocol, OutputStream os) throws ASAPException, IOException {
1524

src/net/sharksystem/asap/ASAPEngine.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,6 @@ public ASAPMessages getChunkChain(int uriPosition, int toEra)
327327
return this.chunkStorage.getASAPMessages(uri, toEra);
328328
}
329329

330-
//////////////////////////////////////////////////////////////////////
331-
// ProtocolEngine //
332-
//////////////////////////////////////////////////////////////////////
333-
334-
List<String> activePeers = new ArrayList<>();
335-
336330
private String getLogStart() {
337331
StringBuilder b = new StringBuilder();
338332
b.append("ASAPEngine (");
@@ -346,6 +340,14 @@ private String getLogStart() {
346340
return b.toString();
347341
}
348342

343+
//////////////////////////////////////////////////////////////////////
344+
// ProtocolEngine //
345+
//////////////////////////////////////////////////////////////////////
346+
347+
// TODO: extract those algorithms to another class (ASAPDefaultProtocolEngine).
348+
349+
private ASAPProtocolEngine protocolEngine = null;
350+
349351
public void handleASAPOffer(ASAP_OfferPDU_1_0 asapOffer, ASAP_1_0 protocol, OutputStream os)
350352
throws ASAPException, IOException {
351353

test/net/sharksystem/asap/BatchprocessorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
public class BatchprocessorTest {
1313
@Test
14-
public void secondMessageNotSendTest() throws IOException, ASAPException, InterruptedException {
14+
public void secondMessageSentAfterReconnectTest() throws IOException, ASAPException, InterruptedException {
1515
Batchprocessor aliceCommands = new Batchprocessor();
1616
Batchprocessor bobCommands = new Batchprocessor();
1717

test/net/sharksystem/asap/Point2PointTests.java

Lines changed: 1 addition & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -37,141 +37,6 @@ private int getPortNumber() {
3737
return portnumber;
3838
}
3939

40-
@Test
41-
public void openMessageExchange() throws IOException, ASAPException, InterruptedException {
42-
///////////////////////////////////////////////////////////////////////////////////////////////////
43-
// prepare storages //
44-
///////////////////////////////////////////////////////////////////////////////////////////////////
45-
46-
ASAPEngineFS.removeFolder(ALICE_ROOT_FOLDER); // clean previous version before
47-
ASAPEngineFS.removeFolder(BOB_ROOT_FOLDER); // clean previous version before
48-
49-
// alice writes a message into chunkStorage
50-
ASAPStorage aliceStorage =
51-
ASAPEngineFS.getASAPStorage(ALICE, ALICE_APP_FOLDER, CHAT_FORMAT);
52-
53-
aliceStorage.add(ALICE_BOB_CHAT_URL, ALICE2BOB_MESSAGE);
54-
aliceStorage.add(ALICE_BOB_CHAT_URL, ALICE2BOB_MESSAGE2);
55-
//aliceStorage.addRecipient(ALICE_BOB_CHAT_URL, BOB);
56-
57-
// bob does the same
58-
ASAPStorage bobStorage =
59-
ASAPEngineFS.getASAPStorage(BOB, BOB_APP_FOLDER, CHAT_FORMAT);
60-
61-
bobStorage.add(ALICE_BOB_CHAT_URL, BOB2ALICE_MESSAGE);
62-
bobStorage.add(ALICE_BOB_CHAT_URL, BOB2ALICE_MESSAGE2);
63-
//bobStorage.addRecipient(ALICE_BOB_CHAT_URL, ALICE);
64-
65-
///////////////////////////////////////////////////////////////////////////////////////////////////
66-
// prepare multi engines //
67-
///////////////////////////////////////////////////////////////////////////////////////////////////
68-
69-
ASAPChunkReceivedTester aliceListener = new ASAPChunkReceivedTester();
70-
ASAPPeer aliceEngine = ASAPPeerFS.createASAPPeer(ALICE_ROOT_FOLDER, aliceListener);
71-
72-
ASAPChunkReceivedTester bobListener = new ASAPChunkReceivedTester();
73-
ASAPPeer bobEngine = ASAPPeerFS.createASAPPeer(BOB_ROOT_FOLDER, bobListener);
74-
75-
///////////////////////////////////////////////////////////////////////////////////////////////////
76-
// setup connection //
77-
///////////////////////////////////////////////////////////////////////////////////////////////////
78-
79-
int portNumber = this.getPortNumber();
80-
// create connections for both sides
81-
TCPStream aliceChannel = new TCPStream(portNumber, true, "a2b");
82-
TCPStream bobChannel = new TCPStream(portNumber, false, "b2a");
83-
84-
aliceChannel.start();
85-
bobChannel.start();
86-
87-
// wait to connect
88-
aliceChannel.waitForConnection();
89-
bobChannel.waitForConnection();
90-
91-
///////////////////////////////////////////////////////////////////////////////////////////////////
92-
// run asap connection //
93-
///////////////////////////////////////////////////////////////////////////////////////////////////
94-
95-
// run engine as thread
96-
ASAPPeerHandleConnectionThread aliceEngineThread = new ASAPPeerHandleConnectionThread(aliceEngine,
97-
aliceChannel.getInputStream(), aliceChannel.getOutputStream());
98-
99-
aliceEngineThread.start();
100-
101-
// and better debugging - no new thread
102-
bobEngine.handleConnection(bobChannel.getInputStream(), bobChannel.getOutputStream());
103-
104-
// wait until communication probably ends
105-
System.out.flush();
106-
System.err.flush();
107-
Thread.sleep(2000);
108-
System.out.flush();
109-
System.err.flush();
110-
111-
// close connections: note ASAPEngine does NOT close any connection!!
112-
aliceChannel.close();
113-
bobChannel.close();
114-
System.out.flush();
115-
System.err.flush();
116-
Thread.sleep(1000);
117-
System.out.flush();
118-
System.err.flush();
119-
120-
// check results
121-
122-
// listener must have been informed about new messages
123-
Assert.assertTrue(aliceListener.chunkReceived());
124-
Assert.assertTrue(bobListener.chunkReceived());
125-
126-
127-
///////////////////////////////////////////////////////////////////////////////////////////////////
128-
// open incomming storages //
129-
///////////////////////////////////////////////////////////////////////////////////////////////////
130-
131-
// get messages alice received
132-
ASAPChunkStorage aliceSenderStored =
133-
aliceStorage.getReceivedChunksStorage(aliceListener.getSender());
134-
135-
ASAPChunk aliceReceivedChunk =
136-
aliceSenderStored.getChunk(aliceListener.getUri(),
137-
aliceListener.getEra());
138-
139-
// #1
140-
Iterator<CharSequence> aliceReceivedMessages = aliceReceivedChunk.getMessagesAsCharSequence();
141-
CharSequence aliceReceivedMessage = aliceReceivedMessages.next();
142-
Assert.assertEquals(BOB2ALICE_MESSAGE, aliceReceivedMessage);
143-
// #2
144-
aliceReceivedMessage = aliceReceivedMessages.next();
145-
Assert.assertEquals(BOB2ALICE_MESSAGE2, aliceReceivedMessage);
146-
147-
// get message bob received
148-
ASAPChunkStorage bobSenderStored =
149-
bobStorage.getReceivedChunksStorage(bobListener.getSender());
150-
151-
ASAPChunk bobReceivedChunk =
152-
bobSenderStored.getChunk(bobListener.getUri(),
153-
bobListener.getEra());
154-
155-
// #1
156-
Iterator<CharSequence> bobReceivedMessages = bobReceivedChunk.getMessagesAsCharSequence();
157-
CharSequence bobReceivedMessage = bobReceivedMessages.next();
158-
Assert.assertEquals(ALICE2BOB_MESSAGE, bobReceivedMessage);
159-
// #2
160-
bobReceivedMessage = bobReceivedMessages.next();
161-
Assert.assertEquals(ALICE2BOB_MESSAGE2, bobReceivedMessage);
162-
163-
List<CharSequence> senderList = aliceStorage.getSender();
164-
// expect bob
165-
Assert.assertEquals(1, senderList.size());
166-
Assert.assertTrue(BOB.equalsIgnoreCase(senderList.get(0).toString()));
167-
168-
// simulate a sync
169-
bobStorage = ASAPEngineFS.getASAPStorage(BOB, BOB_APP_FOLDER, CHAT_FORMAT);
170-
Assert.assertEquals(2, bobStorage.getEra());
171-
172-
Thread.sleep(1000);
173-
}
174-
17540
@Test
17641
public void notOpenMessageChunkExchange() throws IOException, ASAPException, InterruptedException {
17742
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -264,7 +129,7 @@ public void notOpenMessageChunkExchange() throws IOException, ASAPException, Int
264129

265130

266131
///////////////////////////////////////////////////////////////////////////////////////////////////
267-
// open incomming storages //
132+
// open incoming storages //
268133
///////////////////////////////////////////////////////////////////////////////////////////////////
269134

270135
// get messages alice received
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package net.sharksystem.asap;
2+
3+
import net.sharksystem.asap.util.ASAPChunkReceivedTester;
4+
import net.sharksystem.asap.util.ASAPPeerHandleConnectionThread;
5+
import net.sharksystem.cmdline.TCPStream;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import java.io.IOException;
10+
import java.util.Iterator;
11+
import java.util.List;
12+
13+
public class Point2PointTests2 {
14+
public static final String ALICE_BOB_CHAT_URL = "content://aliceAndBob.talk";
15+
public static final String CHAT_FORMAT = "application/x-sn2-makan";
16+
public static final String ALICE_ROOT_FOLDER = "tests/Alice";
17+
public static final String ALICE_APP_FOLDER = ALICE_ROOT_FOLDER + "/appFolder";
18+
public static final String BOB_ROOT_FOLDER = "tests/Bob";
19+
public static final String BOB_APP_FOLDER = BOB_ROOT_FOLDER + "/appFolder";
20+
public static final String ALICE = "Alice";
21+
public static final String BOB = "Bob";
22+
public static final String ALICE2BOB_MESSAGE = "Hi Bob";
23+
public static final String ALICE2BOB_MESSAGE2 = "Hi Bob again";
24+
public static final String BOB2ALICE_MESSAGE = "Hi Alice";
25+
public static final String BOB2ALICE_MESSAGE2 = "Hi Alice again";
26+
27+
private static int portnumber = 7777;
28+
29+
private int getPortNumber() {
30+
portnumber++;
31+
return portnumber;
32+
}
33+
34+
@Test
35+
public void openMessageExchange() throws IOException, ASAPException, InterruptedException {
36+
///////////////////////////////////////////////////////////////////////////////////////////////////
37+
// prepare storages //
38+
///////////////////////////////////////////////////////////////////////////////////////////////////
39+
ASAPEngineFS.removeFolder(ALICE_ROOT_FOLDER); // clean previous version before
40+
ASAPEngineFS.removeFolder(BOB_ROOT_FOLDER); // clean previous version before
41+
42+
// alice writes a message into chunkStorage
43+
ASAPStorage aliceStorage =
44+
ASAPEngineFS.getASAPStorage(ALICE, ALICE_APP_FOLDER, CHAT_FORMAT);
45+
46+
aliceStorage.add(ALICE_BOB_CHAT_URL, ALICE2BOB_MESSAGE);
47+
aliceStorage.add(ALICE_BOB_CHAT_URL, ALICE2BOB_MESSAGE2);
48+
//aliceStorage.addRecipient(ALICE_BOB_CHAT_URL, BOB);
49+
50+
// bob does the same
51+
ASAPStorage bobStorage =
52+
ASAPEngineFS.getASAPStorage(BOB, BOB_APP_FOLDER, CHAT_FORMAT);
53+
54+
bobStorage.add(ALICE_BOB_CHAT_URL, BOB2ALICE_MESSAGE);
55+
bobStorage.add(ALICE_BOB_CHAT_URL, BOB2ALICE_MESSAGE2);
56+
//bobStorage.addRecipient(ALICE_BOB_CHAT_URL, ALICE);
57+
58+
///////////////////////////////////////////////////////////////////////////////////////////////////
59+
// prepare multi engines //
60+
///////////////////////////////////////////////////////////////////////////////////////////////////
61+
62+
ASAPChunkReceivedTester aliceListener = new ASAPChunkReceivedTester();
63+
ASAPPeer aliceEngine = ASAPPeerFS.createASAPPeer(ALICE_ROOT_FOLDER, aliceListener);
64+
65+
ASAPChunkReceivedTester bobListener = new ASAPChunkReceivedTester();
66+
ASAPPeer bobEngine = ASAPPeerFS.createASAPPeer(BOB_ROOT_FOLDER, bobListener);
67+
68+
///////////////////////////////////////////////////////////////////////////////////////////////////
69+
// setup connection //
70+
///////////////////////////////////////////////////////////////////////////////////////////////////
71+
72+
int portNumber = this.getPortNumber();
73+
// create connections for both sides
74+
TCPStream aliceChannel = new TCPStream(portNumber, true, "a2b");
75+
TCPStream bobChannel = new TCPStream(portNumber, false, "b2a");
76+
77+
aliceChannel.start();
78+
bobChannel.start();
79+
80+
// wait to connect
81+
aliceChannel.waitForConnection();
82+
bobChannel.waitForConnection();
83+
84+
///////////////////////////////////////////////////////////////////////////////////////////////////
85+
// run asap connection //
86+
///////////////////////////////////////////////////////////////////////////////////////////////////
87+
88+
// run engine as thread
89+
ASAPPeerHandleConnectionThread aliceEngineThread = new ASAPPeerHandleConnectionThread(aliceEngine,
90+
aliceChannel.getInputStream(), aliceChannel.getOutputStream());
91+
92+
aliceEngineThread.start();
93+
94+
// and better debugging - no new thread
95+
bobEngine.handleConnection(bobChannel.getInputStream(), bobChannel.getOutputStream());
96+
97+
// wait until communication probably ends
98+
System.out.flush();
99+
System.err.flush();
100+
Thread.sleep(2000);
101+
System.out.flush();
102+
System.err.flush();
103+
104+
// close connections: note ASAPEngine does NOT close any connection!!
105+
aliceChannel.close();
106+
bobChannel.close();
107+
System.out.flush();
108+
System.err.flush();
109+
Thread.sleep(1000);
110+
System.out.flush();
111+
System.err.flush();
112+
113+
// check results
114+
115+
// listener must have been informed about new messages
116+
Assert.assertTrue(aliceListener.chunkReceived());
117+
Assert.assertTrue(bobListener.chunkReceived());
118+
119+
120+
///////////////////////////////////////////////////////////////////////////////////////////////////
121+
// open incomming storages //
122+
///////////////////////////////////////////////////////////////////////////////////////////////////
123+
124+
// get messages alice received
125+
ASAPChunkStorage aliceSenderStored =
126+
aliceStorage.getReceivedChunksStorage(aliceListener.getSender());
127+
128+
ASAPChunk aliceReceivedChunk =
129+
aliceSenderStored.getChunk(aliceListener.getUri(),
130+
aliceListener.getEra());
131+
132+
// #1
133+
Iterator<CharSequence> aliceReceivedMessages = aliceReceivedChunk.getMessagesAsCharSequence();
134+
CharSequence aliceReceivedMessage = aliceReceivedMessages.next();
135+
Assert.assertEquals(BOB2ALICE_MESSAGE, aliceReceivedMessage);
136+
// #2
137+
aliceReceivedMessage = aliceReceivedMessages.next();
138+
Assert.assertEquals(BOB2ALICE_MESSAGE2, aliceReceivedMessage);
139+
140+
// get message bob received
141+
ASAPChunkStorage bobSenderStored =
142+
bobStorage.getReceivedChunksStorage(bobListener.getSender());
143+
144+
ASAPChunk bobReceivedChunk =
145+
bobSenderStored.getChunk(bobListener.getUri(),
146+
bobListener.getEra());
147+
148+
// #1
149+
Iterator<CharSequence> bobReceivedMessages = bobReceivedChunk.getMessagesAsCharSequence();
150+
CharSequence bobReceivedMessage = bobReceivedMessages.next();
151+
Assert.assertEquals(ALICE2BOB_MESSAGE, bobReceivedMessage);
152+
// #2
153+
bobReceivedMessage = bobReceivedMessages.next();
154+
Assert.assertEquals(ALICE2BOB_MESSAGE2, bobReceivedMessage);
155+
156+
List<CharSequence> senderList = aliceStorage.getSender();
157+
// expect bob
158+
Assert.assertEquals(1, senderList.size());
159+
Assert.assertTrue(BOB.equalsIgnoreCase(senderList.get(0).toString()));
160+
161+
// simulate a sync
162+
bobStorage = ASAPEngineFS.getASAPStorage(BOB, BOB_APP_FOLDER, CHAT_FORMAT);
163+
Assert.assertEquals(2, bobStorage.getEra());
164+
165+
Thread.sleep(1000);
166+
}
167+
168+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package net.sharksystem.asap;
2+
3+
import org.junit.runner.RunWith;
4+
import org.junit.runners.Suite;
5+
6+
@RunWith(Suite.class)
7+
@Suite.SuiteClasses({
8+
BatchprocessorTest.class,
9+
BasisMethodsTests.class,
10+
Point2PointTests.class,
11+
Point2PointTests2.class,
12+
UsageExamples.class})
13+
public class V1TestSuite {
14+
15+
}

0 commit comments

Comments
 (0)