public void handleUpdatePacket(Packet p, String from) {
    // dont update packets that came from this device
    if (p.getSource().equals(localDevice.getBluetoothAddress())) return;

    // get the md5 of the payload of the packet and store it
    // if the previous md5 is the same with the new one
    // then this packet is redundant
    String hash = md5(p.getPayload());
    if (history.containsKey(p.getSource())) {
      String old = history.get(p.getSource());
      if (old.equals(hash)) return;
    }

    ByteBuffer buffer = ByteBuffer.allocate(p.getPayload().length);
    buffer.put(p.getPayload());
    routingTable.remove(p.getSource());
    routingTable.add(p.getSource());
    byte[] tmp = new byte[12];
    while (buffer.hasRemaining()) {
      buffer.get(tmp, 0, 12);
      routingTable.add(p.getSource(), new String(tmp));
    }

    for (String address : connections.keySet()) {
      if (!address.equals(from)) {
        connections.get(address).offer(p);
      }
    }
    history.put(p.getSource(), hash);
  }
Exemplo n.º 2
0
 protected void print(Packet p) {
   if (paperStock > 0) {
     System.out.printf("%s: impression de «%s»\n", name, p.getPayload());
     paperStock--;
   } else {
     System.out.printf("%s: bac vide!\n", name);
   }
 }
  @Override
  public void encode(IoSession session, Object in, ProtocolEncoderOutput out) throws Exception {
    Packet p = (Packet) in;

    /*
     * Check what type the packet is.
     */
    if (p.isRaw()) {
      /*
       * If the packet is raw, send its payload.
       */
      out.write(p.getPayload());
    } else {
      /*
       * If not, get the out ISAAC cipher.
       */
      // ISAACCipher outCipher = ((Player)
      // session.getAttribute("player")).getOutCipher();

      /*
       * Get the packet attributes.
       */
      int opcode = p.getOpcode();
      Packet.Type type = p.getType();
      int length = p.getLength();

      /*
       * Encrypt the packet opcode.
       */
      // opcode += outCipher.getNextValue();

      /*
       * Compute the required size for the buffer.
       */
      int finalLength = length + 1;
      switch (type) {
        case VARIABLE:
          finalLength += 1;
          break;
        case VARIABLE_SHORT:
          finalLength += 2;
          break;
      }

      /*
       * Create the buffer and write the opcode (and length if the packet
       * is variable-length).
       */
      IoBuffer buffer = IoBuffer.allocate(finalLength);
      buffer.put((byte) opcode);
      switch (type) {
        case VARIABLE:
          buffer.put((byte) length);
          break;
        case VARIABLE_SHORT:
          buffer.putShort((short) length);
          break;
      }

      /*
       * Write the payload itself.
       */
      buffer.put(p.getPayload());

      /*
       * Flip and dispatch the packet.
       */
      out.write(buffer.flip());
    }
  }