Skip to content

Commit 37c2b80

Browse files
committed
added connection establishment to asapJavaApp test - actual test are missing
1 parent d4020c3 commit 37c2b80

File tree

3 files changed

+46
-49
lines changed

3 files changed

+46
-49
lines changed

src/net/sharksystem/asap/apps/ASAPJavaApplicationFS.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,15 @@ private ASAPJavaApplicationFS(CharSequence owner, CharSequence rootFolder, Colle
3636

3737
private MultiASAPEngineFS getMulitEngine() throws IOException, ASAPException {
3838
// TODO: re-create any time - keep track of potential changes in external storage (file system)?
39-
return MultiASAPEngineFS_Impl.createMultiEngine(owner, rootFolder,
40-
MultiASAPEngineFS.DEFAULT_MAX_PROCESSING_TIME, null);
39+
MultiASAPEngineFS multiEngine = MultiASAPEngineFS_Impl.createMultiEngine(owner, rootFolder,
40+
MultiASAPEngineFS.DEFAULT_MAX_PROCESSING_TIME, null);
41+
42+
for(CharSequence format : this.messageReceivedListener.keySet()) {
43+
ASAPMessageReceivedListener listener = this.messageReceivedListener.get(format);
44+
multiEngine.setASAPChunkReceivedListener(format, new MessageChunkReceivedListenerWrapper(listener));
45+
}
46+
47+
return multiEngine;
4148
}
4249

4350
@Override
@@ -85,7 +92,7 @@ public void chunkReceived(String format, String sender, String uri, int era) {
8592
System.out.println(getLogStart() + "chunk received - convert to asap message received");
8693
try {
8794
ASAPEngine engine = ASAPJavaApplicationFS.this.multiEngine.getEngineByFormat(format);
88-
ASAPMessages messages = engine.getChannel(uri).getMessages();
95+
ASAPMessages messages = engine.getIncomingChunkStorage(sender).getASAPChunkCache(uri, era);
8996
this.listener.asapMessagesReceived(messages);
9097
} catch (ASAPException | IOException e) {
9198
System.out.println(getLogStart() + e.getLocalizedMessage());

src/net/sharksystem/asap/protocol/ASAPPersistentConnection.java

+20-42
Original file line numberDiff line numberDiff line change
@@ -211,51 +211,29 @@ public void run() {
211211
System.out.println(this.getLogStart() + "read valid pdu");
212212
this.setRemotePeer(asappdu.getPeer());
213213

214-
// process received pdu
215-
boolean pduExecuted = false;
216-
/*
217-
if(asappdu.getFormat().equalsIgnoreCase(ASAP_1_0.ASAP_MANAGEMENT_FORMAT)) {
218-
System.out.println(this.getLogStart()
219-
+ "got asap management message - let multi-engine handle this one");
220-
214+
try {
215+
this.executor = new ASAPPDUExecutor(asappdu,
216+
this.is, this.os,
217+
this.multiASAPEngineFS.getEngineSettings(asappdu.getFormat()),
218+
protocol,this);
219+
220+
// get exclusive access to streams
221+
System.out.println(this.getLogStart() + "asap pdu executor going to wait for stream access");
222+
this.wait4ExclusiveStreamsAccess();
221223
try {
222-
// if return true - message was handled. No further actions required
223-
pduExecuted = this.multiASAPEngineFS.handleASAPManagementPDU(asappdu, protocol, is);
224-
} catch (ASAPException e) {
225-
System.err.println("asap management pdu processing failed - go ahead in read/process loop: "
226-
+ e.getLocalizedMessage());
227-
pduExecuted = false; // failed to execute - forget and go ahead
228-
} catch (IOException e) {
229-
this.terminate("asap management pdu processing failed", e);
224+
System.out.println(this.getLogStart() + "asap pdu executor got stream access - process pdu");
225+
this.runObservedThread(executor, maxExecutionTime);
226+
} catch (ASAPExecTimeExceededException e) {
227+
System.out.println(this.getLogStart() + "asap pdu processing took longer than allowed");
228+
this.terminate("asap pdu processing took longer than allowed", e);
230229
break;
230+
} finally {
231+
// wake waiting thread if any
232+
this.releaseStreamsLock();
233+
System.out.println(this.getLogStart() + "asap pdu executor release locks");
231234
}
232-
}
233-
*/
234-
if(!pduExecuted) { // not (completely executed by multi engine
235-
try {
236-
this.executor = new ASAPPDUExecutor(asappdu,
237-
this.is, this.os,
238-
this.multiASAPEngineFS.getEngineSettings(asappdu.getFormat()),
239-
protocol,this);
240-
241-
// get exclusive access to streams
242-
System.out.println(this.getLogStart() + "asap pdu executor going to wait for stream access");
243-
this.wait4ExclusiveStreamsAccess();
244-
try {
245-
System.out.println(this.getLogStart() + "asap pdu executor got stream access - process pdu");
246-
this.runObservedThread(executor, maxExecutionTime);
247-
} catch (ASAPExecTimeExceededException e) {
248-
System.out.println(this.getLogStart() + "asap pdu processing took longer than allowed");
249-
this.terminate("asap pdu processing took longer than allowed", e);
250-
break;
251-
} finally {
252-
// wake waiting thread if any
253-
this.releaseStreamsLock();
254-
System.out.println(this.getLogStart() + "asap pdu executor release locks");
255-
}
256-
} catch (ASAPException e) {
257-
System.out.println(this.getLogStart() + " problem when executing asap received pdu: " + e);
258-
}
235+
} catch (ASAPException e) {
236+
System.out.println(this.getLogStart() + " problem when executing asap received pdu: " + e);
259237
}
260238
}
261239
}

test/net/sharksystem/asap/ASAPJavaApplicationTests.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.sharksystem.asap.apps.*;
44
import net.sharksystem.asap.util.ASAPEngineThread;
55
import net.sharksystem.cmdline.TCPChannel;
6+
import org.junit.Assert;
67
import org.junit.Test;
78

89
import java.io.IOException;
@@ -12,14 +13,16 @@
1213
public class ASAPJavaApplicationTests {
1314
public static final String ALICE = "Alice";
1415
public static final String BOB = "Bob";
15-
public static final String ALICE_ROOT_FOLDER = "tests/Alice";
16-
public static final String BOB_ROOT_FOLDER = "tests/Bob";
16+
public static final String TESTS_ROOT_FOLDER = "tests/";
17+
public static final String ALICE_ROOT_FOLDER = TESTS_ROOT_FOLDER + "Alice";
18+
public static final String BOB_ROOT_FOLDER = TESTS_ROOT_FOLDER + "Bob";
1719
private static final CharSequence APP_FORMAT = "TEST_FORMAT";
1820
private static final byte[] TESTMESSAGE = "TestMessage".getBytes();
1921
private static final int PORT = 7777;
2022

2123
@Test
2224
public void usageTest() throws IOException, ASAPException, InterruptedException {
25+
ASAPEngineFS.removeFolder(TESTS_ROOT_FOLDER);
2326
Collection<CharSequence> formats = new HashSet<>();
2427
formats.add(APP_FORMAT);
2528

@@ -40,7 +43,8 @@ public void usageTest() throws IOException, ASAPException, InterruptedException
4043
ASAPJavaApplication asapJavaApplicationBob =
4144
ASAPJavaApplicationFS.createASAPJavaApplication(BOB, BOB_ROOT_FOLDER, formats);
4245

43-
asapJavaApplicationBob.setASAPMessageReceivedListener(APP_FORMAT, new ListenerExample());
46+
ListenerExample listenerBob = new ListenerExample();
47+
asapJavaApplicationBob.setASAPMessageReceivedListener(APP_FORMAT, listenerBob);
4448

4549
// create connections for both sides
4650
TCPChannel aliceChannel = new TCPChannel(PORT, true, "a2b");
@@ -86,18 +90,26 @@ public void usageTest() throws IOException, ASAPException, InterruptedException
8690
// test results //
8791
///////////////////////////////////////////////////////////////////////////////////////////////////
8892

89-
// TODO: should test something
93+
// received?
94+
Assert.assertTrue(listenerBob.hasReceivedMessage());
9095
}
9196

9297
private class ListenerExample implements ASAPMessageReceivedListener {
9398

99+
private boolean hasReceivedMessage = false;
100+
94101
@Override
95102
public void asapMessagesReceived(ASAPMessages messages) {
96103
try {
97104
System.out.println("#message == " + messages.size());
105+
this.hasReceivedMessage = true;
98106
} catch (IOException e) {
99107
// do something with it.
100108
}
101109
}
110+
111+
public boolean hasReceivedMessage() {
112+
return this.hasReceivedMessage;
113+
}
102114
}
103115
}

0 commit comments

Comments
 (0)