|
2 | 2 |
|
3 | 3 | import net.sharksystem.asap.*; |
4 | 4 | import net.sharksystem.asap.apps.TCPServerSocketAcceptor; |
| 5 | +import net.sharksystem.utils.Log; |
| 6 | +import net.sharksystem.utils.streams.StreamPair; |
5 | 7 | import net.sharksystem.utils.streams.StreamPairImpl; |
6 | 8 | import net.sharksystem.utils.tcp.SocketFactory; |
| 9 | +import net.sharksystem.utils.tcp.StreamPairCreatedListener; |
7 | 10 | import net.sharksystem.utils.testsupport.TestConstants; |
8 | 11 | import net.sharksystem.utils.testsupport.TestHelper; |
9 | 12 | import org.junit.Test; |
@@ -92,6 +95,75 @@ public void connectAliceAndBob() throws IOException, ASAPException, InterruptedE |
92 | 95 | Thread.sleep(5); |
93 | 96 | } |
94 | 97 |
|
| 98 | + @Test |
| 99 | + public void connectAliceAndBob_2() throws IOException, ASAPException, InterruptedException { |
| 100 | + // supported formats |
| 101 | + Collection<CharSequence> formats = new ArrayList<>(); |
| 102 | + formats.add(EXAMPLE_APP_FORMAT); |
| 103 | + |
| 104 | + // test folder for this test run |
| 105 | + String rootFolder = TestHelper.getFullTempFolderName(TEST_FOLDER, true); |
| 106 | + |
| 107 | + // set up alice |
| 108 | + String aliceFolder = rootFolder + "/" + TestConstants.ALICE_ID; |
| 109 | + ASAPPeerFS alicePeerFS = new ASAPPeerFS(TestConstants.ALICE_ID, aliceFolder, formats); |
| 110 | + // we only need connection handler capabilities in this scenario |
| 111 | + ASAPConnectionHandler alice = alicePeerFS; |
| 112 | + |
| 113 | + // set up bob |
| 114 | + String bobFolder = rootFolder + "/" + TestConstants.BOB_ID; |
| 115 | + ASAPConnectionHandler bob = new ASAPPeerFS(TestConstants.BOB_ID, bobFolder, formats); |
| 116 | + |
| 117 | + /* create a TCP connection. What we do here: |
| 118 | + We create a just single connection. We need a ServerSocket first and a Socket that connects to. |
| 119 | +
|
| 120 | + A bit more complex but maybe more convenient solution can be found here: |
| 121 | + https://github.com/SharedKnowledge/ASAPJava/blob/master/src/main/java/net/sharksystem/asap/apps/testsupport/ASAPTestPeerFS.java |
| 122 | + */ |
| 123 | + |
| 124 | + // get a port number for that test |
| 125 | + int portNumber = TestHelper.getPortNumber(); |
| 126 | + |
| 127 | + /* |
| 128 | + now. We need to accept incoming connection but have to create a socket at the same time... |
| 129 | + We need threads or a helper class. |
| 130 | + */ |
| 131 | + StreamPairCreatedListenerImpl aliceStreamPairCreatedListener = new StreamPairCreatedListenerImpl(alice); |
| 132 | + SocketFactory socketFactory = new SocketFactory(portNumber, aliceStreamPairCreatedListener); |
| 133 | + Thread socketFactoryThread = new Thread(socketFactory); |
| 134 | + socketFactoryThread.start(); |
| 135 | + |
| 136 | + // create a socket |
| 137 | + String addressRemotePeer = "localhost"; // change this in a real scenario |
| 138 | + Socket socket = new Socket(addressRemotePeer, portNumber); |
| 139 | + |
| 140 | + // give server side a moment to connect |
| 141 | + Thread.sleep(1); |
| 142 | + |
| 143 | + // it is an arbitrary choice - Alice takes socket side pairs |
| 144 | + bob.handleConnection(socket.getInputStream(), socket.getOutputStream()); |
| 145 | + |
| 146 | + // give it some time to run an encounter. |
| 147 | + Thread.sleep(5); |
| 148 | + } |
| 149 | + |
| 150 | + private class StreamPairCreatedListenerImpl implements StreamPairCreatedListener { |
| 151 | + private final ASAPConnectionHandler connectionHandler; |
| 152 | + |
| 153 | + StreamPairCreatedListenerImpl(ASAPConnectionHandler connectionHandler) { |
| 154 | + this.connectionHandler = connectionHandler; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void streamPairCreated(StreamPair streamPair) { |
| 159 | + try { |
| 160 | + this.connectionHandler.handleConnection(streamPair.getInputStream(), streamPair.getOutputStream()); |
| 161 | + } catch (IOException | ASAPException e) { |
| 162 | + Log.writeLog(this, "problems handling connection: " + e.getLocalizedMessage()); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
95 | 167 | @Test |
96 | 168 | public void connectAliceAndBobWithEncounterManager_Preferred() throws IOException, ASAPException, InterruptedException { |
97 | 169 | // supported formats |
|
0 commit comments