Skip to content

Commit 78c12f8

Browse files
committed
add connection establishment example
1 parent 1987adc commit 78c12f8

2 files changed

Lines changed: 89 additions & 1 deletion

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package howto;
2+
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;
9+
import net.sharksystem.utils.tcp.SocketFactory;
10+
import net.sharksystem.utils.testsupport.TestConstants;
11+
import net.sharksystem.utils.testsupport.TestHelper;
12+
import org.junit.Test;
13+
14+
import java.io.IOException;
15+
import java.net.ServerSocket;
16+
import java.net.Socket;
17+
import java.util.ArrayList;
18+
import java.util.Collection;
19+
20+
/**
21+
* Problem: ASAPPeers can handle a connection. They are not meant to open a connection be their own. That's task of
22+
* another component. Our framework offers an ASAPTestPeerFS which allows setting up an encounter based on a
23+
* local loop (IP 127.0.0.1) TCP connection
24+
*
25+
* Real scenarios need to set up connections between two peers in different processes or most probably
26+
* different engines. You find examples here.
27+
*
28+
* See also https://github.com/SharedKnowledge/ASAPJava/wiki/EncounterManager
29+
*
30+
* @see net.sharksystem.asap.apps.testsupport.ASAPTestPeerFS
31+
*/
32+
public class ConnectPeers {
33+
34+
private static final String TEST_FOLDER = "ConnectPeers";
35+
private CharSequence EXAMPLE_APP_FORMAT = "shark/x-connectPeersExample";
36+
private int PORT_NUMBER = 7777;
37+
38+
@Test
39+
public void connectAliceAndBob() throws IOException, ASAPException, InterruptedException {
40+
// supported formats
41+
Collection<CharSequence> formats = new ArrayList<>();
42+
formats.add(EXAMPLE_APP_FORMAT);
43+
44+
// test folder for this test run
45+
String rootFolder = TestHelper.getFullTempFolderName(TEST_FOLDER, true);
46+
47+
// set up alice
48+
String aliceFolder = rootFolder + "/" + TestConstants.ALICE_ID;
49+
ASAPPeerFS alicePeerFS = new ASAPPeerFS(TestConstants.ALICE_ID, aliceFolder, formats);
50+
// we only need connection handler capabilities in this scenario
51+
ASAPConnectionHandler alice = alicePeerFS;
52+
53+
// set up bob
54+
String bobFolder = rootFolder + "/" + TestConstants.BOB_ID;
55+
ASAPConnectionHandler bob = new ASAPPeerFS(TestConstants.BOB_ID, bobFolder, formats);
56+
57+
/* create a TCP connection. What we do here:
58+
We create a just single connection. We need a ServerSocket first and a Socket that connects to.
59+
60+
A bit more complex but maybe more convenient solution can be found here:
61+
https://github.com/SharedKnowledge/ASAPJava/blob/master/src/main/java/net/sharksystem/asap/apps/testsupport/ASAPTestPeerFS.java
62+
*/
63+
64+
ServerSocket serverSocket = new ServerSocket(PORT_NUMBER);
65+
66+
/*
67+
now. We need to accept incoming connection but have to create a socket at the same time...
68+
We need threads or a helper class.
69+
*/
70+
SocketFactory socketFactory = new SocketFactory(serverSocket);
71+
Thread socketFactoryThread = new Thread(socketFactory);
72+
socketFactoryThread.start();
73+
74+
// create a socket
75+
String addressRemotePeer = "localhost"; // change this in a real scenario
76+
Socket socket = new Socket(addressRemotePeer, PORT_NUMBER);
77+
78+
// give server side a moment to connect
79+
Thread.sleep(1);
80+
81+
// it is an arbitrary choice - Alice takes socket side pairs
82+
alice.handleConnection(socket.getInputStream(), socket.getOutputStream());
83+
bob.handleConnection(socketFactory.getInputStream(), socketFactory.getOutputStream());
84+
85+
// give it some time to run an encounter.
86+
Thread.sleep(5);
87+
88+
}
89+
}

src/test/java/junit5Tests/release_1/MultipleEncounterTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import net.sharksystem.asap.ASAPException;
77
import net.sharksystem.asap.ASAPPeer;
88
import net.sharksystem.asap.apps.testsupport.ASAPTestPeerFS;
9-
import net.sharksystem.asap.engine.ASAPEngineFS;
109
import net.sharksystem.asap.mockAndTemplates.ASAPMessageReceivedListenerExample;
1110
import net.sharksystem.asap.mockAndTemplates.TestUtils;
1211
import org.junit.jupiter.api.Assertions;

0 commit comments

Comments
 (0)