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;
  }
  public void init() throws IOException {
    server.start();
    server.bind(Network.TCP_PORT, Network.UDP_PORT);

    // For consistency, the classes to be sent over the network are
    // registered by the same method for both the client and server.
    Network.register(server);
  }
  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();
  }
Exemple #4
0
 public void start() {
   try {
     this.server =
         new Server(Globals.PACKET_MAX_SIZE * Globals.PACKET_MAX_PER_CON, Globals.PACKET_MAX_SIZE);
     Kryo kyro = this.server.getKryo();
     kyro.register(byte[].class);
     this.receiver = new PacketReceiver();
     this.server.addListener(new Listener.ThreadedListener(this.receiver));
     server.bind(Globals.SERVER_TCP_PORT);
     server.start();
     Globals.log(
         GameServer.class,
         "Server listening on port TCP: " + Globals.SERVER_TCP_PORT,
         Globals.LOG_TYPE_DATA,
         true);
     // Globals.log(GameServer.class, "Server listening on port UDP: " + Globals.SERVER_UDP_PORT,
     // Globals.LOG_TYPE_DATA, true);
   } catch (IOException ex) {
     Globals.logError(ex.getStackTrace()[0].toString(), ex, true);
     System.exit(1);
   }
 }