예제 #1
0
  public void sendMessage(Message message, IP remoteAddress, int remotePort) throws IOException {
    message.setId(JGN.getUniqueLong());

    if (message instanceof CertifiedMessage) {
      getMessageCertifier().enqueue((CertifiedMessage) message, remoteAddress, remotePort);
    }
    if (message instanceof OrderedMessage) {
      if (((OrderedMessage) message).getOrderId() == -1) {
        ((OrderedMessage) message)
            .setOrderId(OrderedMessage.createUniqueId(((OrderedMessage) message).getOrderGroup()));
      }
    }
    resendMessage(message, remoteAddress, remotePort);
  }
  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);
  }