Skip to content

Commit 73cc3a3

Browse files
committed
working on first version that really works in Android
1 parent 4b1506e commit 73cc3a3

10 files changed

+114
-12
lines changed

src/net/sharksystem/asp3/ASP3Engine.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ private String getLogStart() {
9292
}
9393

9494
@Override
95-
public void handleConnection(InputStream is, OutputStream os) {
95+
public void handleConnection(InputStream is, OutputStream os,
96+
ASP3ReceivedChunkListener listener) {
9697

9798
DataInputStream dis = new DataInputStream(is);
9899
DataOutputStream dos = new DataOutputStream(os);

src/net/sharksystem/asp3/ASP3ProtocolEngine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
* @author thsc
1010
*/
1111
interface ASP3ProtocolEngine {
12-
public void handleConnection(InputStream is, OutputStream os)
13-
throws IOException;
12+
public void handleConnection(InputStream is, OutputStream os,
13+
ASP3ReceivedChunkListener listener) throws IOException;
1414
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package net.sharksystem.asp3;
2+
3+
/**
4+
*
5+
* @author thsc
6+
*/
7+
public interface ASP3ReceivedChunkListener {
8+
public void chunkReceived(String sender, String Uri, int era);
9+
}

test/ASP3ChunkReceiverTester.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
import net.sharksystem.asp3.ASP3ReceivedChunkListener;
3+
4+
/**
5+
*
6+
* @author thsc
7+
*/
8+
public class ASP3ChunkReceiverTester implements ASP3ReceivedChunkListener {
9+
private String sender = null;
10+
private String uri = null;
11+
private int era;
12+
13+
@Override
14+
public void chunkReceived(String sender, String Uri, int era) {
15+
this.sender = sender;
16+
this.uri = Uri;
17+
this.era = era;
18+
}
19+
20+
public boolean chunkReceived() {
21+
return this.sender != null;
22+
}
23+
24+
String getSender() {
25+
return this.sender;
26+
}
27+
28+
String getUri() {
29+
return this.uri;
30+
}
31+
32+
int getEra() {
33+
return this.era;
34+
}
35+
}

test/ASP3EngineThread.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11

2+
import java.io.IOException;
23
import java.io.InputStream;
34
import java.io.OutputStream;
5+
import java.util.logging.Level;
6+
import java.util.logging.Logger;
47
import net.sharksystem.asp3.ASP3Engine;
8+
import net.sharksystem.asp3.ASP3ReceivedChunkListener;
59

610
/**
711
*
@@ -12,15 +16,19 @@ public class ASP3EngineThread extends Thread {
1216
private final ASP3Engine engine;
1317
private final InputStream is;
1418
private final OutputStream os;
19+
private ASP3ReceivedChunkListener listener;
1520

16-
ASP3EngineThread(ASP3Engine engine, InputStream is, OutputStream os) {
21+
ASP3EngineThread(ASP3Engine engine, InputStream is,
22+
OutputStream os, ASP3ReceivedChunkListener listener) {
23+
1724
this.engine = engine;
1825
this.is = is;
1926
this.os = os;
27+
this.listener = listener;
2028
}
2129

2230
@Override
2331
public void run() {
24-
this.engine.handleConnection(is, os);
32+
this.engine.handleConnection(this.is, this.os, this.listener);
2533
}
2634
}

test/BasicTests.java

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
import java.nio.file.Path;
77
import java.nio.file.Paths;
88
import java.util.List;
9+
import net.sharksystem.asp3.ASP3Chunk2Send;
910
import net.sharksystem.asp3.ASP3Engine;
1011
import net.sharksystem.asp3.ASP3EngineFS;
1112
import net.sharksystem.asp3.ASP3Exception;
1213
import net.sharksystem.util.localloop.TCPChannel;
1314
import org.junit.Test;
1415
import net.sharksystem.asp3.ASP3ChunkStorage;
16+
import net.sharksystem.asp3.ASP3ReceivedChunkListener;
17+
import net.sharksystem.asp3.ASP3Storage;
1518
import org.junit.Assert;
1619

1720
/**
@@ -94,14 +97,15 @@ public void usage() throws IOException, ASP3Exception, InterruptedException {
9497
aliceChannel.waitForConnection();
9598
bobChannel.waitForConnection();
9699

97-
// twist and run
100+
// run
98101
ASP3EngineThread aliceEngineThread = new ASP3EngineThread(aliceEngine,
99102
aliceChannel.getInputStream(),
100-
aliceChannel.getOutputStream());
103+
aliceChannel.getOutputStream(), null);
101104

102105
aliceEngineThread.start();
103106

104-
bobEngine.handleConnection(bobChannel.getInputStream(), bobChannel.getOutputStream());
107+
bobEngine.handleConnection(bobChannel.getInputStream(),
108+
bobChannel.getOutputStream(), null);
105109

106110
Thread.sleep(10000);
107111

@@ -124,7 +128,7 @@ public void usage() throws IOException, ASP3Exception, InterruptedException {
124128
}
125129

126130
@Test
127-
public void androidUsage() throws IOException, ASP3Exception {
131+
public void androidUsage() throws IOException, ASP3Exception, InterruptedException {
128132
this.removeDirectory(ALICE_FOLDER); // clean previous version before
129133
this.removeDirectory(BOB_FOLDER); // clean previous version before
130134

@@ -145,18 +149,63 @@ public void androidUsage() throws IOException, ASP3Exception {
145149

146150
ASP3Engine bobEngine = ASP3EngineFS.getASP3Engine("Bob", BOB_FOLDER);
147151

148-
/*
152+
ASP3ChunkReceiverTester aliceListener = new ASP3ChunkReceiverTester();
153+
ASP3ChunkReceiverTester bobListener = new ASP3ChunkReceiverTester();
154+
155+
// create connections for both sides
149156
TCPChannel aliceChannel = new TCPChannel(7777, true, "a2b");
150157
TCPChannel bobChannel = new TCPChannel(7777, false, "b2a");
151158

152159
aliceChannel.start();
153160
bobChannel.start();
154161

162+
// wait to connect
155163
aliceChannel.waitForConnection();
156164
bobChannel.waitForConnection();
157-
*/
158-
165+
166+
// run engine as thread
167+
ASP3EngineThread aliceEngineThread = new ASP3EngineThread(aliceEngine,
168+
aliceChannel.getInputStream(),
169+
aliceChannel.getOutputStream(),
170+
aliceListener);
171+
172+
aliceEngineThread.start();
173+
174+
// and better debugging - no new thread
175+
bobEngine.handleConnection(bobChannel.getInputStream(),
176+
bobChannel.getOutputStream(), bobListener);
177+
178+
// wait until communication end
179+
Thread.sleep(10000);
180+
181+
// listener must have been informed about new messages
182+
Assert.assertTrue(aliceListener.chunkReceived());
183+
Assert.assertTrue(bobListener.chunkReceived());
184+
185+
// get message alice received
186+
ASP3Storage aliceSenderStored =
187+
aliceStorage.getReceivedChunkStorage(aliceListener.getSender());
188+
189+
ASP3Chunk2Send aliceReceivedChunk =
190+
aliceSenderStored.getChunk(aliceListener.getUri(),
191+
aliceListener.getEra());
192+
193+
CharSequence aliceReceivedMessage =
194+
aliceReceivedChunk.getMessages().next();
195+
196+
Assert.assertEquals(BOB2ALICE_MESSAGE, aliceReceivedMessage);
159197

198+
// get message bob received
199+
ASP3Storage bobSenderStored =
200+
bobStorage.getReceivedChunkStorage(bobListener.getSender());
201+
202+
ASP3Chunk2Send bobReceivedChunk =
203+
bobSenderStored.getChunk(bobListener.getUri(),
204+
bobListener.getEra());
205+
206+
CharSequence bobReceivedMessage =
207+
bobReceivedChunk.getMessages().next();
160208

209+
Assert.assertEquals(ALICE2BOB_MESSAGE, bobReceivedMessage);
161210
}
162211
}
11 Bytes
Binary file not shown.
34 Bytes
Binary file not shown.
10 Bytes
Binary file not shown.
34 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)