private static void startSender() { Thread t = new Thread() { public void run() { try { DatagramChannel channel = DatagramChannel.open(); channel.configureBlocking(false); // channel.socket().connect(new InetSocketAddress(InetAddress.getLocalHost(), 1000)); ByteBuffer buffer = ByteBuffer.allocate(512 * 1000); byte[] buf; long time = JGN.getNanoTime(); for (int i = 0; i < 1000; i++) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); byte[] textBytes = ("Testing " + (i + 1)).getBytes(); dos.writeInt(textBytes.length); dos.write(textBytes); buf = baos.toByteArray(); baos.close(); buffer.clear(); buffer.put(buf); buffer.flip(); // channel.write(buffer); channel.send(buffer, new InetSocketAddress(InetAddress.getLocalHost(), 1000)); Thread.sleep(5); } System.out.println("Took " + ((JGN.getNanoTime() - time) / 1000000) + "ms to send."); } catch (Throwable t) { t.printStackTrace(); } } }; t.start(); }
private static void startReceiver() { Thread t = new Thread() { public void run() { try { DatagramChannel server = DatagramChannel.open(); server.configureBlocking(false); server.socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), 1000)); // Selector selector = Selector.open(); // server.register(selector, SelectionKey.OP_READ); ByteBuffer buffer = ByteBuffer.allocate(512 * 1000); byte[] buf = new byte[512 * 1000]; long time = 0; while (true) { Thread.sleep(1); SocketAddress address = server.receive(buffer); if (address != null) { int len = buffer.position(); buffer.rewind(); buffer.get(buf, 0, len); buffer.clear(); ByteArrayInputStream bais = new ByteArrayInputStream(buf, 0, len); DataInputStream dis = new DataInputStream(bais); int bytesLength; byte[] bytes; int packets = 0; try { while ((bytesLength = dis.readInt()) > -1) { dis.read(bytes = new byte[bytesLength]); packets++; String s = new String(bytes); if (s.equals("Testing 1000")) { System.out.println( "Took " + ((JGN.getNanoTime() - time) / 1000000) + "ms to receive " + packets + " packets."); System.exit(0); } } } catch (EOFException exc) { // System.out.println("End of stream reached!"); } bais.close(); } } } catch (Throwable t) { t.printStackTrace(); } } }; t.start(); }
protected void setUp() throws IOException, InterruptedException { JGN.register(MyCertifiedMessage.class); JGN.register(MyRealtimeMessage.class); JGN.register(MyUniqueMessage.class); JGN.register(MySerializableMessage.class); // Create first MessageServer serverAddress1 = new InetSocketAddress(InetAddress.getLocalHost(), 1000); if (tcp) { server1 = new TCPMessageServer(serverAddress1); } else { server1 = new UDPMessageServer(serverAddress1); } if (debug) { server1.addMessageListener(new DebugListener("Server1")); server1.addConnectionListener(new DebugListener("Server1")); } server1.addConnectionListener( new ConnectionListener() { public void connected(MessageClient client) { client1Disconnected = false; } public void negotiationComplete(MessageClient client) { client1to2 = client; } public void disconnected(MessageClient client) { System.out.println("Disconnected1"); client1Disconnected = true; } public void kicked(MessageClient client, String reason) {} }); JGN.createThread(server1).start(); // Create second MessageServer serverAddress2 = new InetSocketAddress(InetAddress.getLocalHost(), 2000); if (tcp) { server2 = new TCPMessageServer(serverAddress2); } else { server2 = new UDPMessageServer(serverAddress2); } if (debug) { server2.addMessageListener(new DebugListener("Server2")); server2.addConnectionListener(new DebugListener("Server1")); } server2.addConnectionListener( new ConnectionListener() { public void connected(MessageClient client) { client2Disconnected = false; } public void negotiationComplete(MessageClient client) { client2to1 = client; } public void disconnected(MessageClient client) { System.out.println("Disconnected2"); client2Disconnected = true; } public void kicked(MessageClient client, String reason) {} }); JGN.createThread(server2).start(); // Connect server2 to server1 MessageClient client = server2.connectAndWait(serverAddress1, 5000); if (client == null) { System.err.println("Unable to establish connection!"); } else { System.out.println("Connection established successfully"); } long time = System.currentTimeMillis(); while (System.currentTimeMillis() < time + 5000) { if ((client1to2 != null) && (client2to1 != null)) break; Thread.sleep(1); } assertTrue(client1to2 != null); assertTrue(client2to1 != null); }