|
| 1 | +package net.sharksystem.asap; |
| 2 | + |
| 3 | +import net.sharksystem.asap.*; |
| 4 | +import net.sharksystem.asap.util.ASAPChunkReceivedTester; |
| 5 | +import net.sharksystem.asap.util.ASAPPeerHandleConnectionThread; |
| 6 | +import net.sharksystem.cmdline.TCPStream; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.util.Iterator; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +public class CreateNewChannelFromOutsideTest { |
| 15 | + public static final String ALICE_BOB_CHAT_URL = "content://aliceAndBob.talk"; |
| 16 | + public static final String CHAT_FORMAT = "application/x-sn2-makan"; |
| 17 | + public static final String ALICE_ROOT_FOLDER = "tests/Alice"; |
| 18 | + public static final String ALICE_APP_FOLDER = ALICE_ROOT_FOLDER + "/appFolder"; |
| 19 | + public static final String BOB_ROOT_FOLDER = "tests/Bob"; |
| 20 | + public static final String BOB_APP_FOLDER = BOB_ROOT_FOLDER + "/appFolder"; |
| 21 | + public static final String ALICE = "Alice"; |
| 22 | + public static final String BOB = "Bob"; |
| 23 | + public static final String ALICE2BOB_MESSAGE = "Hi Bob"; |
| 24 | + public static final String ALICE2BOB_MESSAGE2 = "Hi Bob again"; |
| 25 | + public static final String BOB2ALICE_MESSAGE = "Hi Alice"; |
| 26 | + public static final String BOB2ALICE_MESSAGE2 = "Hi Alice again"; |
| 27 | + |
| 28 | + private static int portnumber = 7777; |
| 29 | + |
| 30 | + private int getPortNumber() { |
| 31 | + portnumber++; |
| 32 | + return portnumber; |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void openMessageExchange() throws IOException, ASAPException, InterruptedException { |
| 37 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 38 | + // prepare storages // |
| 39 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 40 | + ASAPEngineFS.removeFolder(ALICE_ROOT_FOLDER); // clean previous version before |
| 41 | + ASAPEngineFS.removeFolder(BOB_ROOT_FOLDER); // clean previous version before |
| 42 | + |
| 43 | + // alice writes a message into chunkStorage |
| 44 | + ASAPStorage aliceStorage = |
| 45 | + ASAPEngineFS.getASAPStorage(ALICE, ALICE_APP_FOLDER, CHAT_FORMAT); |
| 46 | + |
| 47 | + aliceStorage.add(ALICE_BOB_CHAT_URL, ALICE2BOB_MESSAGE); |
| 48 | + aliceStorage.add(ALICE_BOB_CHAT_URL, ALICE2BOB_MESSAGE2); |
| 49 | + //aliceStorage.addRecipient(ALICE_BOB_CHAT_URL, BOB); |
| 50 | + |
| 51 | + // bob does the same |
| 52 | + ASAPStorage bobStorage = |
| 53 | + ASAPEngineFS.getASAPStorage(BOB, BOB_APP_FOLDER, CHAT_FORMAT); |
| 54 | + |
| 55 | + // there is only Bobs storage - nothing else. That's important. |
| 56 | + |
| 57 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 58 | + // prepare peers // |
| 59 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 60 | + |
| 61 | + ASAPPeer aliceEngine = ASAPPeerFS.createASAPPeer(ALICE_ROOT_FOLDER, null); |
| 62 | + ASAPPeer bobEngine = ASAPPeerFS.createASAPPeer(BOB_ROOT_FOLDER, null); |
| 63 | + |
| 64 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 65 | + // setup connection // |
| 66 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 67 | + |
| 68 | + int portNumber = this.getPortNumber(); |
| 69 | + // create connections for both sides |
| 70 | + TCPStream aliceChannel = new TCPStream(portNumber, true, "a2b"); |
| 71 | + TCPStream bobChannel = new TCPStream(portNumber, false, "b2a"); |
| 72 | + |
| 73 | + aliceChannel.start(); |
| 74 | + bobChannel.start(); |
| 75 | + |
| 76 | + // wait to connect |
| 77 | + aliceChannel.waitForConnection(); |
| 78 | + bobChannel.waitForConnection(); |
| 79 | + |
| 80 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 81 | + // run asap connection // |
| 82 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 83 | + |
| 84 | + // run engine as thread |
| 85 | + ASAPPeerHandleConnectionThread aliceEngineThread = new ASAPPeerHandleConnectionThread(aliceEngine, |
| 86 | + aliceChannel.getInputStream(), aliceChannel.getOutputStream()); |
| 87 | + |
| 88 | + aliceEngineThread.start(); |
| 89 | + |
| 90 | + // and better debugging - no new thread |
| 91 | + bobEngine.handleConnection(bobChannel.getInputStream(), bobChannel.getOutputStream()); |
| 92 | + |
| 93 | + // wait until communication probably ends |
| 94 | + System.out.flush(); |
| 95 | + System.err.flush(); |
| 96 | + Thread.sleep(2000); |
| 97 | + System.out.flush(); |
| 98 | + System.err.flush(); |
| 99 | + |
| 100 | + // close connections: note ASAPEngine does NOT close any connection!! |
| 101 | + aliceChannel.close(); |
| 102 | + bobChannel.close(); |
| 103 | + System.out.flush(); |
| 104 | + System.err.flush(); |
| 105 | + Thread.sleep(1000); |
| 106 | + System.out.flush(); |
| 107 | + System.err.flush(); |
| 108 | + |
| 109 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 110 | + // tests // |
| 111 | + /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 112 | + |
| 113 | + // there is a new channel |
| 114 | + List<CharSequence> channelURIs = bobStorage.getChannelURIs(); |
| 115 | + Assert.assertEquals(1, channelURIs.size()); |
| 116 | + |
| 117 | + // and the one and only is... |
| 118 | + Assert.assertEquals(ALICE_BOB_CHAT_URL, channelURIs.get(0)); |
| 119 | + } |
| 120 | +} |
0 commit comments