Esempio n. 1
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();
  }
Esempio n. 2
0
 private void startNetworkClient() {
   client.start();
   client.addListener(new NetworkListener(this));
   try {
     client.connect(5000, ipAddress, PORT1, PORT2);
   } catch (IOException e) {
     System.out.print(e);
     Gdx.app.debug("Test", e.toString());
     showDCdialog("Can not connect to server!");
   }
 }
  // Connect to the server
  protected Client doInBackground(final String... arg0) {
    client = new Client();
    client.start();

    Network.register(client);

    client.addListener(
        new Listener() {
          public void connected(Connection connection) { // send the player id to the server
            Connect player = new Connect();
            player.id = arg0[0];
            client.sendTCP(player);

            return;
          }

          public void received(
              Connection connection,
              Object
                  object) { // found a game, save the player color and game id and finish the task
            if (object instanceof GameData) {

              GameData data = (GameData) object;
              playerColor = data.playerColor;
              gameId = data.gameId;

              finished = true;

              return;
            }
          }
        });

    try { // attempt to connect to the server
      client.connect(5000, Network.IP, Network.PORT);
    } catch (IOException e) {
      e.printStackTrace();
    }

    // wait untill a game has been made
    while (!finished) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

    return client;
  }
  public void connet(String host) {

    client = new Client();
    client.start();

    config.register(client);
    networkMessageAdapter.init(client);

    if (sync) {
      queuedListener =
          new WaitingQueuedListener(new NetworkListener(config, networkMessageAdapter));
      client.addListener(queuedListener);
    } else
      client.addListener(
          new Listener.ThreadedListener(new NetworkListener(config, networkMessageAdapter)));
    try {
      client.connect(5000, host, config.getServerTCPPort(), config.getServerUDPPort());
    } catch (IOException e) {
      logger.log(Level.SEVERE, "Can not bind port", e);
      // e.printStackTrace();
    }
  }
  public RemoteMinecraft(String address) {
    client = new Client(65536, 65536);
    Kryo kryo = client.getKryo();
    kryo.register(NetCreateWorldRequest.class);
    kryo.register(NetGetBiomeDataRequest.class);
    kryo.register(NetGetBiomeDataResult.class);
    kryo.register(NetBiome.class);
    kryo.register(NetBiome[].class);
    kryo.register(NetInfoRequest.class);
    kryo.register(int[].class);

    client.addListener(
        new Listener() {
          @Override
          public void received(Connection connection, Object object) {
            if (object instanceof NetGetBiomeDataResult) {
              currentResults = (NetGetBiomeDataResult) object;
              // Log.i("Received NetGetBiomeDataResult: " + currentResults);
            } else if (object instanceof NetBiome[]) {
              NetBiome[] biomes = (NetBiome[]) object;
              for (int i = 0; i < biomes.length; i++) {
                if (biomes[i] != null) {
                  new Biome(biomes[i].name, biomes[i].id, biomes[i].color | 0xFF000000, true);
                }
              }
            }
          }
        });

    client.start();
    try {
      client.connect(5000, address, 54580, 54580);
    } catch (IOException e) {
      e.printStackTrace();
    }

    client.sendTCP(new NetInfoRequest());
  }
Esempio n. 6
0
  // Start Game
  public void init() {
    // map size
    level = new Level();

    MapProperties prop = level.map.getProperties();
    int mapWidth = prop.get("width", Integer.class);
    int mapHeight = prop.get("height", Integer.class);
    System.out.println("mapWidth: " + mapWidth + ", " + "mapHeight: " + mapHeight);

    tilePixelWidth = prop.get("tilewidth", Integer.class);
    tilePixelHeight = prop.get("tileheight", Integer.class);

    System.out.println(
        "tilePixelWidth: " + tilePixelWidth + ", " + "tilePixelHeight: " + tilePixelHeight);

    mapPixelWidth = mapWidth * tilePixelWidth;
    mapPixelHeight = mapHeight * tilePixelHeight;

    System.out.println(
        "mapPixelWidth: " + mapPixelWidth + ", " + "mapPixelHeight: " + mapPixelHeight);

    // set bounding boxes for tilemap sprites
    // stones
    TiledMapTileLayer layer = (TiledMapTileLayer) level.map.getLayers().get("stones");
    System.out.println("Layer: " + layer);

    freeCells = new boolean[layer.getWidth()][layer.getHeight()];
    mapCellWidth = layer.getWidth();
    mapCellHeight = layer.getHeight();
    for (int x = 0; x < layer.getWidth(); x++) {
      for (int y = 0; y < layer.getHeight(); y++) {
        freeCells[x][y] = true;
        if (layer.getCell(x, y) != null) {
          // Spawn Walls
          WallGameObject wall =
              new WallGameObject(x * tilePixelWidth + 25, y * tilePixelWidth + 25, 100, 100);
          this.objs.addObject(wall);

          // safe blocked cell coordinates for random player position
          freeCells[x][y] = false;
        }
      }
    }

    // bushs
    layer = (TiledMapTileLayer) level.map.getLayers().get("bushs");

    for (int x = 0; x < layer.getWidth(); x++) {
      for (int y = 0; y < layer.getHeight(); y++) {
        if (layer.getCell(x, y) != null) {
          // Spawn Bush
          BushGameObject bush =
              new BushGameObject(x * tilePixelWidth + 25, y * tilePixelWidth + 25, 90, 90);
          this.objs.addObject(bush);
        }
      }
    }

    // checkpoints
    layer = (TiledMapTileLayer) level.map.getLayers().get("checkpoint");

    int ci = 0;
    for (int x = 0; x < layer.getWidth(); x++) {
      for (int y = 0; y < layer.getHeight(); y++) {
        if (layer.getCell(x, y) != null) {
          // Spawn Checkpoint
          CheckpointGameObject checkpoint =
              new CheckpointGameObject(x * tilePixelWidth, y * tilePixelWidth, 125, 125);
          checkpoint.client = this.client;
          checkpoint.checkpointID = ci;
          this.objs.addObject(checkpoint);
          checkpointsNeeded++;
          ci++;
        }
      }
    }

    this.client.start();

    // For consistency, the classes to be sent over the network are registered by the same method
    // for both the client and server
    TurirunNetwork.register(client);

    client.addListener(
        new ThreadedListener(
            new Listener() {
              public void connected(Connection connection) {}

              public void received(Connection connection, Object object) {
                WorldController.events.add(object);
              }

              public void disconnected(Connection connection) {}
            }));

    try {
      // Block for max. 3000ms // 172.18.12.25
      client.connect(3000, this.game.host, this.game.port, TurirunNetwork.udpPort);
      // Server communication after connection can go here, or in Listener#connected()
    } catch (IOException e) {
      Gdx.app.error("Could not connect to server", e.getMessage());

      // Create local player as fallback
      TouriCharacterObject playerObj = new TouriCharacterObject(10, 10);
      playerObj.setNick(game.nickname);
      objs.addObject(playerObj);
      controller.setPlayerObj(playerObj);
    }

    Register register = new Register();

    register.nick = this.game.nickname;
    register.type = 0;

    client.sendTCP(register);
  }