|
| 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 net.sharksystem.utils.ASAPSerialization; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import java.io.ByteArrayInputStream; |
| 11 | +import java.io.ByteArrayOutputStream; |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.Iterator; |
| 14 | + |
| 15 | +import static net.sharksystem.asap.ASAPPeer.DEFAULT_MAX_PROCESSING_TIME; |
| 16 | + |
| 17 | +public class LongerMessages { |
| 18 | + public static final String ALICE_BOB_CHAT_URL = "content://aliceAndBob.talk"; |
| 19 | + public static final String CHAT_FORMAT = "application/x-sn2-makan"; |
| 20 | + public static final String ALICE_ROOT_FOLDER = "longmessage/Alice"; |
| 21 | + public static final String ALICE_APP_FOLDER = ALICE_ROOT_FOLDER + "/appFolder"; |
| 22 | + public static final String BOB_ROOT_FOLDER = "longmessage/Bob"; |
| 23 | + public static final String BOB_APP_FOLDER = BOB_ROOT_FOLDER + "/appFolder"; |
| 24 | + public static final String ALICE = "Alice"; |
| 25 | + public static final String BOB = "Bob"; |
| 26 | + // 200 |
| 27 | + public static final String ALICE2BOB_MESSAGE = "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"; |
| 28 | + //public static final String ALICE2BOB_MESSAGE = "Hi Bob"; |
| 29 | + //public static final String ALICE2BOB_MESSAGE2 = "HiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgainHiYouAgain"; |
| 30 | + public static final String ALICE2BOB_MESSAGE2 = "Hi Again"; |
| 31 | + |
| 32 | + private static int portnumber = 7777; |
| 33 | + |
| 34 | + private int getPortNumber() { |
| 35 | + portnumber++; |
| 36 | + return portnumber; |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void serializeDeserializeTest() throws IOException, ASAPException { |
| 41 | + long writeLong = 0x9ABCDEF012345678L; |
| 42 | + //long writeLong = 0x9999999999999999L; |
| 43 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 44 | + ASAPSerialization.writeLongParameter(writeLong, baos); |
| 45 | + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); |
| 46 | + long readLong = ASAPSerialization.readLongParameter(bais); |
| 47 | + |
| 48 | + Assert.assertEquals(writeLong, readLong); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void longerMessagesAlice2Bob() throws IOException, ASAPException, InterruptedException { |
| 53 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 54 | + // prepare storages // |
| 55 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 56 | + |
| 57 | + ASAPEngineFS.removeFolder(ALICE_ROOT_FOLDER); // clean previous version before |
| 58 | + ASAPEngineFS.removeFolder(BOB_ROOT_FOLDER); // clean previous version before |
| 59 | + |
| 60 | + // alice writes a message into chunkStorage |
| 61 | + ASAPStorage aliceStorage = ASAPEngineFS.getASAPStorage(ALICE, ALICE_APP_FOLDER, CHAT_FORMAT); |
| 62 | + |
| 63 | + int aliceInitialEra = aliceStorage.getEra(); |
| 64 | + |
| 65 | + aliceStorage.createChannel(ALICE_BOB_CHAT_URL, BOB); |
| 66 | + |
| 67 | + // content changed - next change in topology should increase alice era. |
| 68 | + aliceStorage.add(ALICE_BOB_CHAT_URL, ALICE2BOB_MESSAGE); |
| 69 | + |
| 70 | + // bob does the same |
| 71 | + ASAPStorage bobStorage = ASAPEngineFS.getASAPStorage(BOB, BOB_APP_FOLDER, CHAT_FORMAT); |
| 72 | + |
| 73 | + int bobInitialEra = bobStorage.getEra(); |
| 74 | + |
| 75 | + bobStorage.createChannel(ALICE_BOB_CHAT_URL, ALICE); |
| 76 | + |
| 77 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 78 | + // prepare multi engines // |
| 79 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 80 | + |
| 81 | + ASAPChunkReceivedTester aliceListener = new ASAPChunkReceivedTester(); |
| 82 | + ASAPPeer aliceEngine = ASAPPeerFS.createASAPPeer( |
| 83 | + ALICE, ALICE_ROOT_FOLDER, DEFAULT_MAX_PROCESSING_TIME, aliceListener); |
| 84 | + |
| 85 | + ASAPChunkReceivedTester bobListener = new ASAPChunkReceivedTester(); |
| 86 | + ASAPPeer bobEngine = ASAPPeerFS.createASAPPeer( |
| 87 | + BOB, BOB_ROOT_FOLDER, DEFAULT_MAX_PROCESSING_TIME, bobListener); |
| 88 | + |
| 89 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 90 | + // prepare asap immediate bypass // |
| 91 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 92 | + |
| 93 | + ASAPAbstractOnlineMessageSender aliceBypass = new ASAPSingleProcessOnlineMessageSender(aliceEngine, aliceStorage); |
| 94 | + ASAPAbstractOnlineMessageSender bobBypass = new ASAPSingleProcessOnlineMessageSender(bobEngine, bobStorage); |
| 95 | + |
| 96 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 97 | + // setup connection // |
| 98 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 99 | + |
| 100 | + int portNumber = this.getPortNumber(); |
| 101 | + // create connections for both sides |
| 102 | + TCPStream aliceChannel = new TCPStream(portNumber, true, "a2b"); |
| 103 | + TCPStream bobChannel = new TCPStream(portNumber, false, "b2a"); |
| 104 | + |
| 105 | + aliceChannel.start(); |
| 106 | + bobChannel.start(); |
| 107 | + |
| 108 | + // wait to connect |
| 109 | + aliceChannel.waitForConnection(); |
| 110 | + bobChannel.waitForConnection(); |
| 111 | + |
| 112 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 113 | + // run asap connection // |
| 114 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 115 | + |
| 116 | + // run engine as thread |
| 117 | + ASAPPeerHandleConnectionThread aliceEngineThread = new ASAPPeerHandleConnectionThread(aliceEngine, |
| 118 | + aliceChannel.getInputStream(), aliceChannel.getOutputStream()); |
| 119 | + |
| 120 | + aliceEngineThread.start(); |
| 121 | + |
| 122 | + // and better debugging - no new thread |
| 123 | + bobEngine.handleConnection(bobChannel.getInputStream(), bobChannel.getOutputStream()); |
| 124 | + |
| 125 | + // give handleConnection some time. |
| 126 | + Thread.sleep(1000); |
| 127 | + // create second message after creating a connection - should be bypassed. |
| 128 | + //aliceStorage.add(ALICE_BOB_CHAT_URL, ALICE2BOB_MESSAGE2); |
| 129 | + |
| 130 | + // wait until communication probably ends |
| 131 | + System.out.flush(); |
| 132 | + System.err.flush(); |
| 133 | + Thread.sleep(2000); |
| 134 | + System.out.flush(); |
| 135 | + System.err.flush(); |
| 136 | + |
| 137 | + // close connections: note ASAPEngine does NOT close any connection!! |
| 138 | + aliceChannel.close(); |
| 139 | + bobChannel.close(); |
| 140 | + System.out.flush(); |
| 141 | + System.err.flush(); |
| 142 | + Thread.sleep(1000); |
| 143 | + System.out.flush(); |
| 144 | + System.err.flush(); |
| 145 | + |
| 146 | + // check results |
| 147 | + |
| 148 | + // listener must have been informed about new messages |
| 149 | + Assert.assertTrue(bobListener.chunkReceived()); |
| 150 | + |
| 151 | + |
| 152 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 153 | + // open incoming storages // |
| 154 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 155 | + |
| 156 | + // get message bob received |
| 157 | + ASAPChunkStorage bobIncomingAliceStorage = bobStorage.getReceivedChunksStorage(ALICE); |
| 158 | + ASAPChunk bobReceivedChunk = bobIncomingAliceStorage.getChunk(ALICE_BOB_CHAT_URL, aliceInitialEra); |
| 159 | + |
| 160 | + // #1 |
| 161 | + Iterator<CharSequence> bobReceivedMessages = bobReceivedChunk.getMessagesAsCharSequence(); |
| 162 | + CharSequence bobReceivedMessage = bobReceivedMessages.next(); |
| 163 | + Assert.assertEquals(ALICE2BOB_MESSAGE, bobReceivedMessage); |
| 164 | + |
| 165 | + // #2 - in next era |
| 166 | + /* |
| 167 | + bobReceivedChunk = bobIncomingAliceStorage.getChunk(ALICE_BOB_CHAT_URL, ASAP.nextEra(aliceInitialEra)); |
| 168 | + bobReceivedMessages = bobReceivedChunk.getMessagesAsCharSequence(); |
| 169 | + bobReceivedMessage = bobReceivedMessages.next(); |
| 170 | + Assert.assertEquals(ALICE2BOB_MESSAGE2, bobReceivedMessage); |
| 171 | +
|
| 172 | + */ |
| 173 | + |
| 174 | + Thread.sleep(1000); |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments