|
| 1 | +package net.sharksystem.asap; |
| 2 | + |
| 3 | +import net.sharksystem.asap.util.ASAPChunkReceivedTester; |
| 4 | +import net.sharksystem.asap.util.ASAPPeerHandleConnectionThread; |
| 5 | +import net.sharksystem.cmdline.TCPStream; |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.util.Iterator; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +public class Point2PointTests2 { |
| 14 | + public static final String ALICE_BOB_CHAT_URL = "content://aliceAndBob.talk"; |
| 15 | + public static final String CHAT_FORMAT = "application/x-sn2-makan"; |
| 16 | + public static final String ALICE_ROOT_FOLDER = "tests/Alice"; |
| 17 | + public static final String ALICE_APP_FOLDER = ALICE_ROOT_FOLDER + "/appFolder"; |
| 18 | + public static final String BOB_ROOT_FOLDER = "tests/Bob"; |
| 19 | + public static final String BOB_APP_FOLDER = BOB_ROOT_FOLDER + "/appFolder"; |
| 20 | + public static final String ALICE = "Alice"; |
| 21 | + public static final String BOB = "Bob"; |
| 22 | + public static final String ALICE2BOB_MESSAGE = "Hi Bob"; |
| 23 | + public static final String ALICE2BOB_MESSAGE2 = "Hi Bob again"; |
| 24 | + public static final String BOB2ALICE_MESSAGE = "Hi Alice"; |
| 25 | + public static final String BOB2ALICE_MESSAGE2 = "Hi Alice again"; |
| 26 | + |
| 27 | + private static int portnumber = 7777; |
| 28 | + |
| 29 | + private int getPortNumber() { |
| 30 | + portnumber++; |
| 31 | + return portnumber; |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void openMessageExchange() throws IOException, ASAPException, InterruptedException { |
| 36 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 37 | + // prepare storages // |
| 38 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 39 | + ASAPEngineFS.removeFolder(ALICE_ROOT_FOLDER); // clean previous version before |
| 40 | + ASAPEngineFS.removeFolder(BOB_ROOT_FOLDER); // clean previous version before |
| 41 | + |
| 42 | + // alice writes a message into chunkStorage |
| 43 | + ASAPStorage aliceStorage = |
| 44 | + ASAPEngineFS.getASAPStorage(ALICE, ALICE_APP_FOLDER, CHAT_FORMAT); |
| 45 | + |
| 46 | + aliceStorage.add(ALICE_BOB_CHAT_URL, ALICE2BOB_MESSAGE); |
| 47 | + aliceStorage.add(ALICE_BOB_CHAT_URL, ALICE2BOB_MESSAGE2); |
| 48 | + //aliceStorage.addRecipient(ALICE_BOB_CHAT_URL, BOB); |
| 49 | + |
| 50 | + // bob does the same |
| 51 | + ASAPStorage bobStorage = |
| 52 | + ASAPEngineFS.getASAPStorage(BOB, BOB_APP_FOLDER, CHAT_FORMAT); |
| 53 | + |
| 54 | + bobStorage.add(ALICE_BOB_CHAT_URL, BOB2ALICE_MESSAGE); |
| 55 | + bobStorage.add(ALICE_BOB_CHAT_URL, BOB2ALICE_MESSAGE2); |
| 56 | + //bobStorage.addRecipient(ALICE_BOB_CHAT_URL, ALICE); |
| 57 | + |
| 58 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 59 | + // prepare multi engines // |
| 60 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 61 | + |
| 62 | + ASAPChunkReceivedTester aliceListener = new ASAPChunkReceivedTester(); |
| 63 | + ASAPPeer aliceEngine = ASAPPeerFS.createASAPPeer(ALICE_ROOT_FOLDER, aliceListener); |
| 64 | + |
| 65 | + ASAPChunkReceivedTester bobListener = new ASAPChunkReceivedTester(); |
| 66 | + ASAPPeer bobEngine = ASAPPeerFS.createASAPPeer(BOB_ROOT_FOLDER, bobListener); |
| 67 | + |
| 68 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 69 | + // setup connection // |
| 70 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 71 | + |
| 72 | + int portNumber = this.getPortNumber(); |
| 73 | + // create connections for both sides |
| 74 | + TCPStream aliceChannel = new TCPStream(portNumber, true, "a2b"); |
| 75 | + TCPStream bobChannel = new TCPStream(portNumber, false, "b2a"); |
| 76 | + |
| 77 | + aliceChannel.start(); |
| 78 | + bobChannel.start(); |
| 79 | + |
| 80 | + // wait to connect |
| 81 | + aliceChannel.waitForConnection(); |
| 82 | + bobChannel.waitForConnection(); |
| 83 | + |
| 84 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 85 | + // run asap connection // |
| 86 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 87 | + |
| 88 | + // run engine as thread |
| 89 | + ASAPPeerHandleConnectionThread aliceEngineThread = new ASAPPeerHandleConnectionThread(aliceEngine, |
| 90 | + aliceChannel.getInputStream(), aliceChannel.getOutputStream()); |
| 91 | + |
| 92 | + aliceEngineThread.start(); |
| 93 | + |
| 94 | + // and better debugging - no new thread |
| 95 | + bobEngine.handleConnection(bobChannel.getInputStream(), bobChannel.getOutputStream()); |
| 96 | + |
| 97 | + // wait until communication probably ends |
| 98 | + System.out.flush(); |
| 99 | + System.err.flush(); |
| 100 | + Thread.sleep(2000); |
| 101 | + System.out.flush(); |
| 102 | + System.err.flush(); |
| 103 | + |
| 104 | + // close connections: note ASAPEngine does NOT close any connection!! |
| 105 | + aliceChannel.close(); |
| 106 | + bobChannel.close(); |
| 107 | + System.out.flush(); |
| 108 | + System.err.flush(); |
| 109 | + Thread.sleep(1000); |
| 110 | + System.out.flush(); |
| 111 | + System.err.flush(); |
| 112 | + |
| 113 | + // check results |
| 114 | + |
| 115 | + // listener must have been informed about new messages |
| 116 | + Assert.assertTrue(aliceListener.chunkReceived()); |
| 117 | + Assert.assertTrue(bobListener.chunkReceived()); |
| 118 | + |
| 119 | + |
| 120 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 121 | + // open incomming storages // |
| 122 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 123 | + |
| 124 | + // get messages alice received |
| 125 | + ASAPChunkStorage aliceSenderStored = |
| 126 | + aliceStorage.getReceivedChunksStorage(aliceListener.getSender()); |
| 127 | + |
| 128 | + ASAPChunk aliceReceivedChunk = |
| 129 | + aliceSenderStored.getChunk(aliceListener.getUri(), |
| 130 | + aliceListener.getEra()); |
| 131 | + |
| 132 | + // #1 |
| 133 | + Iterator<CharSequence> aliceReceivedMessages = aliceReceivedChunk.getMessagesAsCharSequence(); |
| 134 | + CharSequence aliceReceivedMessage = aliceReceivedMessages.next(); |
| 135 | + Assert.assertEquals(BOB2ALICE_MESSAGE, aliceReceivedMessage); |
| 136 | + // #2 |
| 137 | + aliceReceivedMessage = aliceReceivedMessages.next(); |
| 138 | + Assert.assertEquals(BOB2ALICE_MESSAGE2, aliceReceivedMessage); |
| 139 | + |
| 140 | + // get message bob received |
| 141 | + ASAPChunkStorage bobSenderStored = |
| 142 | + bobStorage.getReceivedChunksStorage(bobListener.getSender()); |
| 143 | + |
| 144 | + ASAPChunk bobReceivedChunk = |
| 145 | + bobSenderStored.getChunk(bobListener.getUri(), |
| 146 | + bobListener.getEra()); |
| 147 | + |
| 148 | + // #1 |
| 149 | + Iterator<CharSequence> bobReceivedMessages = bobReceivedChunk.getMessagesAsCharSequence(); |
| 150 | + CharSequence bobReceivedMessage = bobReceivedMessages.next(); |
| 151 | + Assert.assertEquals(ALICE2BOB_MESSAGE, bobReceivedMessage); |
| 152 | + // #2 |
| 153 | + bobReceivedMessage = bobReceivedMessages.next(); |
| 154 | + Assert.assertEquals(ALICE2BOB_MESSAGE2, bobReceivedMessage); |
| 155 | + |
| 156 | + List<CharSequence> senderList = aliceStorage.getSender(); |
| 157 | + // expect bob |
| 158 | + Assert.assertEquals(1, senderList.size()); |
| 159 | + Assert.assertTrue(BOB.equalsIgnoreCase(senderList.get(0).toString())); |
| 160 | + |
| 161 | + // simulate a sync |
| 162 | + bobStorage = ASAPEngineFS.getASAPStorage(BOB, BOB_APP_FOLDER, CHAT_FORMAT); |
| 163 | + Assert.assertEquals(2, bobStorage.getEra()); |
| 164 | + |
| 165 | + Thread.sleep(1000); |
| 166 | + } |
| 167 | + |
| 168 | +} |
0 commit comments