public static void main(String[] args) throws Exception { // Create our first server MessageServer server1 = new TCPMessageServer(new InetSocketAddress(InetAddress.getLocalHost(), 1000)); JGN.createThread(server1).start(); // Create our second server MessageServer server2 = new TCPMessageServer(new InetSocketAddress(InetAddress.getLocalHost(), 2000)); JGN.createThread(server2).start(); // Establish a MessageClient from server2 to server1 MessageClient client = server2.connectAndWait(new InetSocketAddress(InetAddress.getLocalHost(), 1000), 5000); // Create the RemoteActionImplementation for server2 and register it RemoteAction impl = new RemoteActionImplementation(); impl.setMessage("Initial Message"); RemoteObjectManager.registerRemoteObject(RemoteAction.class, impl, server1); // Get an instance of RemoteAction on server1 pointing back to server2 RemoteAction action = RemoteObjectManager.createRemoteObject(RemoteAction.class, client, 5000); // Invoke methods on RemoteAction that get invoked on server2 System.out.println("Message: " + action.getMessage()); action.setMessage("Testing"); System.out.println("Message: " + action.getMessage()); }
protected void tearDown() throws IOException, InterruptedException { server1.closeAndWait(5000); server2.closeAndWait(5000); }
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); }
public static void main(String[] args) throws Exception { JGN.register(BasicMessage.class); final MessageServer server1 = new TCPMessageServer(new InetSocketAddress(InetAddress.getLocalHost(), 1000)); server1.addConnectionListener( new ConnectionListener() { public void connected(MessageClient client) { // System.out.println("S1> Connected: " + client); LOG.log(Level.FINE, "S1> Connected: {0}", client); client1 = client; ClientMonitor m1 = new ClientMonitor("C1>", client1, 1000); JGN.createThread(m1).start(); } public void negotiationComplete(MessageClient client) { LOG.log(Level.FINE, "S1> Negotiation completed with: {0}", client); // System.out.println("S1> Negotiation completed successfully with: " + client); } public void disconnected(MessageClient client) { LOG.log(Level.FINE, "S1> Disconnected: {0}", client); // System.out.println("S1> Disconnected: " + client); } public void kicked(MessageClient client, String reason) {} }); server1.addMessageListener( new MessageAdapter() { private long time; public void messageReceived(Message message) { if (message instanceof BasicMessage) { if (receiveCount == 0) time = System.currentTimeMillis(); receiveCount++; // System.out.println("Count: " + receiveCount + ", " + // ((BasicMessage)message).getValue()); // if (receiveCount > 2000) System.out.println("Receive Count: " + receiveCount); if (receiveCount >= MAX) { LOG.log( Level.INFO, "S1> Completed in: {0} ms", (System.currentTimeMillis() - time)); // System.out.println("Completed in: " + (System.currentTimeMillis() - time) + // "ms"); System.exit(0); } } } public void messageSent(Message message) { LOG.log(Level.FINE, "S1> Message sent: {0}", message); // System.out.println("S1> Message Sent: " + message); } }); final MessageServer server2 = new TCPMessageServer(new InetSocketAddress(InetAddress.getLocalHost(), 2000)); JGN.createThread(server1, server2).start(); server2.addConnectionListener( new ConnectionListener() { public void connected(MessageClient client) { LOG.log(Level.FINE, "S2> Connected: {0}", client); // System.out.println("S2> Connected: " + client); client2 = client; ClientMonitor m2 = new ClientMonitor("C2>", client2, 1000); JGN.createThread(m2).start(); } public void negotiationComplete(MessageClient client) { LOG.log(Level.FINE, "S2> Negotiation completed with: {0}", client); // System.out.println("S2> Negotiation completed successfully with: " + client); } public void disconnected(MessageClient client) { LOG.log(Level.FINE, "S2> Disconnected: {0}", client); // System.out.println("S2> Disconnected: " + client); } public void kicked(MessageClient client, String reason) {} }); MessageClient client = server2.connectAndWait(new InetSocketAddress(InetAddress.getLocalHost(), 1000), 5000); if (client != null) { LOG.log(Level.INFO, "Connection S2 --> S1 established! using client: {0}", client); // System.out.println("Connection established!"); BasicMessage message = new BasicMessage(); long time = System.currentTimeMillis(); for (int i = 0; i < MAX; i++) { message.setValue(i); try { client.sendMessage(message); } catch (QueueFullException exc) { i--; try { Thread.sleep(1); } catch (InterruptedException ie) { // aha } } } LOG.log(Level.INFO, "S2> Enqueued in: {0} ms", (System.currentTimeMillis() - time)); // System.out.println("Enqueued in: " + (System.currentTimeMillis() - time) + "ms"); } else { LOG.warning("Connection timed out!"); // System.out.println("Connection timed out!"); } }