|
1 | 1 | package howto; |
2 | 2 |
|
3 | 3 | import net.sharksystem.asap.*; |
| 4 | +import net.sharksystem.asap.apps.TCPServerSocketAcceptor; |
| 5 | +import net.sharksystem.utils.streams.StreamPairImpl; |
4 | 6 | import net.sharksystem.utils.tcp.SocketFactory; |
5 | 7 | import net.sharksystem.utils.testsupport.TestConstants; |
6 | 8 | import net.sharksystem.utils.testsupport.TestHelper; |
@@ -89,4 +91,53 @@ public void connectAliceAndBob() throws IOException, ASAPException, InterruptedE |
89 | 91 | // give it some time to run an encounter. |
90 | 92 | Thread.sleep(5); |
91 | 93 | } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void connectAliceAndBobWithEncounterManager_Preferred() throws IOException, ASAPException, InterruptedException { |
| 97 | + // supported formats |
| 98 | + Collection<CharSequence> formats = new ArrayList<>(); |
| 99 | + formats.add(EXAMPLE_APP_FORMAT); |
| 100 | + |
| 101 | + // test folder for this test run |
| 102 | + String rootFolder = TestHelper.getFullTempFolderName(TEST_FOLDER, true); |
| 103 | + |
| 104 | + ////////////////////////// set up peers |
| 105 | + // set up alice |
| 106 | + String aliceFolder = rootFolder + "/" + TestConstants.ALICE_ID; |
| 107 | + ASAPConnectionHandler alice = new ASAPPeerFS(TestConstants.ALICE_ID, aliceFolder, formats); |
| 108 | + // set up bob |
| 109 | + String bobFolder = rootFolder + "/" + TestConstants.BOB_ID; |
| 110 | + ASAPConnectionHandler bob = new ASAPPeerFS(TestConstants.BOB_ID, bobFolder, formats); |
| 111 | + |
| 112 | + ////////////////////////// encounter manager |
| 113 | + ASAPEncounterManager aliceEncounterManager = new ASAPEncounterManagerImpl(alice); |
| 114 | + ASAPEncounterManager bobEncounterManager = new ASAPEncounterManagerImpl(bob); |
| 115 | + |
| 116 | + ////////////////////////// set up server socket and handle connection requests |
| 117 | + int portNumberAlice = TestHelper.getPortNumber(); |
| 118 | + TCPServerSocketAcceptor aliceTcpServerSocketAcceptor = |
| 119 | + new TCPServerSocketAcceptor(portNumberAlice, aliceEncounterManager); |
| 120 | + |
| 121 | + int portNumberBob = TestHelper.getPortNumber(); |
| 122 | + TCPServerSocketAcceptor bobTcpServerSocketAcceptor = |
| 123 | + new TCPServerSocketAcceptor(portNumberBob, bobEncounterManager); |
| 124 | + |
| 125 | + // give it a moment to settle |
| 126 | + Thread.sleep(5); |
| 127 | + |
| 128 | + // now, both side wit for connection establishment. Example |
| 129 | + |
| 130 | + // open connection to Bob |
| 131 | + Socket socket = new Socket("localhost", portNumberBob); |
| 132 | + |
| 133 | + // let Alice handle it |
| 134 | + aliceEncounterManager.handleEncounter( |
| 135 | + StreamPairImpl.getStreamPair(socket.getInputStream(), socket.getOutputStream()), |
| 136 | + EncounterConnectionType.INTERNET); |
| 137 | + |
| 138 | + // give it a moment to run ASAP session |
| 139 | + Thread.sleep(5); |
| 140 | + |
| 141 | + // There is just one peer in a real app. |
| 142 | + } |
92 | 143 | } |
0 commit comments