Skip to content

Commit 90a284c

Browse files
committed
add connection establishment example
1 parent 78c12f8 commit 90a284c

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

src/test/java/howto/ConnectPeers.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package howto;
22

3-
import net.sharksystem.asap.ASAPConnectionHandler;
4-
import net.sharksystem.asap.ASAPException;
5-
import net.sharksystem.asap.ASAPPeer;
6-
import net.sharksystem.asap.ASAPPeerFS;
7-
import net.sharksystem.utils.streams.StreamPair;
8-
import net.sharksystem.utils.streams.StreamPairImpl;
3+
import net.sharksystem.asap.*;
94
import net.sharksystem.utils.tcp.SocketFactory;
105
import net.sharksystem.utils.testsupport.TestConstants;
116
import net.sharksystem.utils.testsupport.TestHelper;
@@ -33,8 +28,14 @@ public class ConnectPeers {
3328

3429
private static final String TEST_FOLDER = "ConnectPeers";
3530
private CharSequence EXAMPLE_APP_FORMAT = "shark/x-connectPeersExample";
36-
private int PORT_NUMBER = 7777;
3731

32+
/**
33+
* A single connection is set up and two peers run an ASAP encounter. It can work in test scenarios. It is not
34+
* a blueprint for a real application though - better use our encounter manager.
35+
* @throws IOException
36+
* @throws ASAPException
37+
* @throws InterruptedException
38+
*/
3839
@Test
3940
public void connectAliceAndBob() throws IOException, ASAPException, InterruptedException {
4041
// supported formats
@@ -61,7 +62,10 @@ public void connectAliceAndBob() throws IOException, ASAPException, InterruptedE
6162
https://github.com/SharedKnowledge/ASAPJava/blob/master/src/main/java/net/sharksystem/asap/apps/testsupport/ASAPTestPeerFS.java
6263
*/
6364

64-
ServerSocket serverSocket = new ServerSocket(PORT_NUMBER);
65+
// get a port number for that test
66+
int portNumber = TestHelper.getPortNumber();
67+
68+
ServerSocket serverSocket = new ServerSocket(portNumber);
6569

6670
/*
6771
now. We need to accept incoming connection but have to create a socket at the same time...
@@ -73,7 +77,7 @@ public void connectAliceAndBob() throws IOException, ASAPException, InterruptedE
7377

7478
// create a socket
7579
String addressRemotePeer = "localhost"; // change this in a real scenario
76-
Socket socket = new Socket(addressRemotePeer, PORT_NUMBER);
80+
Socket socket = new Socket(addressRemotePeer, portNumber);
7781

7882
// give server side a moment to connect
7983
Thread.sleep(1);
@@ -84,6 +88,5 @@ public void connectAliceAndBob() throws IOException, ASAPException, InterruptedE
8488

8589
// give it some time to run an encounter.
8690
Thread.sleep(5);
87-
8891
}
8992
}

src/test/java/net/sharksystem/V1TestSuite.java

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

3+
import howto.ConnectPeers;
34
import net.sharksystem.asap.crypto.CryptoUsage;
45
import junit5Tests.release_1.MultipleEncounterTests;
6+
import net.sharksystem.asap.encounter.EncounterManagerTests;
57
import net.sharksystem.asap.engine.*;
68
import net.sharksystem.asap.helper.HelperTester;
79
import net.sharksystem.asap.peer.Point2Point2Test2;
@@ -27,7 +29,9 @@
2729
MultihopTests.class,
2830
CommandlineTests.class,
2931
TransientMessages.class,
30-
Point2Point2Test2.class//,
32+
Point2Point2Test2.class,
33+
EncounterManagerTests.class,
34+
ConnectPeers.class, //
3135
//MultipleEncounterTests.class
3236
//E2EStreamPairLinkTestVersion2.class TODO
3337
})

0 commit comments

Comments
 (0)