Ejemplo n.º 1
0
  void turnOnPowerReceiver() throws IOException {
    Global.log("Turning on power receiver...");
    while (true) {

      if (isDisposed()) throw new IOException("Client game disposed");
      Global.log("Connecting to port " + (Global.powerPort()) + "...");
      try {
        powerStream = new ClientByteStream(ip, Global.powerPort(), Power.bufferSize());
        break;
      } catch (IOException ex) {
      }
    }
    Global.log("Connected!");
    timers.add(
        new FixedTimer(
            new FixedTask() {
              public boolean fixedRate() {
                return false;
              }

              public float FPS() {
                return Global.ReceiveFPS;
              }

              public void run() {
                byte[] data = null;
                try {
                  data = powerStream.read();
                } catch (IOException ex) {
                  Global.onException();
                  stop();
                  return;
                }
                if (data == null) return;
                addPower(Power.fromBytes(data));
              }
            }));
    Global.log("Turning on power remover...");
    while (true) {
      if (isDisposed()) throw new IOException("Client game disposed");
      Global.log("Connecting to port " + Global.powerRemoverPort() + "...");
      try {
        powerRemover = new ClientByteStream(ip, Global.powerRemoverPort(), 2);
        break;
      } catch (IOException ex) {
      }
    }
    Global.log("Connected!");
    timers.add(
        new FixedTimer(
            new FixedTask() {
              public boolean fixedRate() {
                return false;
              }

              public float FPS() {
                return Global.ReceiveFPS;
              }

              public void run() {
                byte[] data = null;
                try {
                  data = powerRemover.read();
                } catch (IOException ex) {
                  System.err.println("Cannot read info from power remover");
                  Global.onException();
                  stop();
                  return;
                }
                if (data == null) return;
                ByteBuffer bb = ByteBuffer.wrap(data);
                short id = bb.getShort();
                for (Power power : powers) {
                  if (power.ID == id) {
                    removePower(power);
                    break;
                  }
                }
              }
            }));
  }