public static ServerBridge createServerConnection(
      int port, SeedValidator validator, Class... registeredClasses) {

    if (AbstractServer.getInstance() == null) {
      try {
        throw new Exception("Cannot create a server connection if AbstractServer is null.");
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    ServerBridge bridge = ServerBridge.create(validator);

    Server server = new Server(16384, 8192);
    NetworkManager networkManager =
        new NetworkManager(ConnectionType.SERVER, ServerBridge.getInstance());
    server.addListener(new NetworkListener(validator, networkManager));
    try {
      ServerConnectionProvider provider = new ServerConnectionProvider(server, registeredClasses);
      provider.connect(); // server.start()
      server.bind(port);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      System.out.println("Error : Could not connect / create the server.");
      e.printStackTrace();
    }

    AbstractServer.getInstance().setBridge(bridge);
    return bridge;
  }
예제 #2
0
  public void testRMI() throws IOException {
    Server server = new Server();
    register(server.getKryo());
    startEndPoint(server);
    server.bind(tcpPort);

    final ObjectSpace serverObjectSpace = new ObjectSpace();
    final TestObjectImpl serverTestObject = new TestObjectImpl(4321);
    serverObjectSpace.register((short) 42, serverTestObject);

    server.addListener(
        new Listener() {
          public void connected(final Connection connection) {
            serverObjectSpace.addConnection(connection);
            runTest(connection, 12, 1234);
          }

          public void received(Connection connection, Object object) {
            if (!(object instanceof MessageWithTestObject)) return;
            MessageWithTestObject m = (MessageWithTestObject) object;
            System.out.println(serverTestObject.value);
            System.out.println(((TestObjectImpl) m.testObject).value);
            assertEquals(4321f, m.testObject.other());
            stopEndPoints(2000);
          }
        });

    // ----

    Client client = new Client();
    register(client.getKryo());

    ObjectSpace clientObjectSpace = new ObjectSpace(client);
    final TestObjectImpl clientTestObject = new TestObjectImpl(1234);
    clientObjectSpace.register((short) 12, clientTestObject);

    startEndPoint(client);
    client.addListener(
        new Listener() {
          public void connected(final Connection connection) {
            RmiTest.runTest(connection, 42, 4321);
          }

          public void received(Connection connection, Object object) {
            if (!(object instanceof MessageWithTestObject)) return;
            MessageWithTestObject m = (MessageWithTestObject) object;
            System.out.println(clientTestObject.value);
            System.out.println(((TestObjectImpl) m.testObject).value);
            assertEquals(1234f, m.testObject.other());
            stopEndPoints(2000);
          }
        });
    client.connect(5000, host, tcpPort);

    waitForThreads();
  }
예제 #3
0
  public GameServer() throws IOException {
    server = new Server() {};

    server.addListener(
        new Listener() {
          @Override
          public void received(Connection connection, Object object) {
            LOG.log(
                Level.INFO,
                "New connection to server ={0}, recieved ={1}",
                new Object[] {connection, object});
            // We know all connections for this server are actually ChatConnections.
            if (object instanceof Request) {
              Request request = (Request) object;
              System.out.println("Get from client: " + request);
              Request response = player.getRequest(request.getGameField());
              System.out.println("Send to client: " + response);
              connection.sendTCP(response);
            }
          }
        });
  }