Exemplo n.º 1
0
  /** When receive a message, analyze message content and then execute the command: Draw or Clear */
  public void receive(Message msg) {
    byte[] buf = msg.getRawBuffer();
    if (buf == null) {
      System.err.println(
          "["
              + channel.getAddress()
              + "] received null buffer from "
              + msg.getSrc()
              + ", headers: "
              + msg.printHeaders());
      return;
    }

    try {
      DrawCommand comm =
          (DrawCommand)
              Util.streamableFromByteBuffer(
                  DrawCommand.class, buf, msg.getOffset(), msg.getLength());
      switch (comm.mode) {
        case DrawCommand.DRAW:
          if (drawPanel != null) drawPanel.drawPoint(comm);
          break;
        case DrawCommand.CLEAR:
          clearPanel();
        default:
          System.err.println("***** received invalid draw command " + comm.mode);
          break;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 2
0
  /**
   * Message contains MethodCall. Execute it against *this* object and return result. Use
   * MethodCall.invoke() to do this. Return result.
   */
  public Object handle(Message req) throws Exception {
    if (server_obj == null) {
      log.error(Util.getMessage("NoMethodHandlerIsRegisteredDiscardingRequest"));
      return null;
    }

    if (req == null || req.getLength() == 0) {
      log.error(Util.getMessage("MessageOrMessageBufferIsNull"));
      return null;
    }

    Object body =
        req_marshaller != null
            ? req_marshaller.objectFromBuffer(req.getRawBuffer(), req.getOffset(), req.getLength())
            : req.getObject();

    if (!(body instanceof MethodCall))
      throw new IllegalArgumentException("message does not contain a MethodCall object");

    MethodCall method_call = (MethodCall) body;

    if (log.isTraceEnabled()) log.trace("[sender=%s], method_call: %s", req.getSrc(), method_call);

    if (method_call.getMode() == MethodCall.ID) {
      if (method_lookup == null)
        throw new Exception(
            String.format(
                "MethodCall uses ID=%d, but method_lookup has not been set", method_call.getId()));
      Method m = method_lookup.findMethod(method_call.getId());
      if (m == null) throw new Exception("no method found for " + method_call.getId());
      method_call.setMethod(m);
    }

    return method_call.invoke(server_obj);
  }
Exemplo n.º 3
0
  private void encryptAndSend(Message msg) throws Exception {
    EncryptHeader hdr = new EncryptHeader(EncryptHeader.ENCRYPT, getSymVersion());
    if (this.encrypt_entire_message) hdr.type |= EncryptHeader.ENCRYPT_ENTIRE_MSG;

    if (encrypt_entire_message) {
      if (msg.getSrc() == null) msg.setSrc(local_addr);

      Buffer serialized_msg = Util.streamableToBuffer(msg);
      byte[] encrypted_msg =
          code(
              serialized_msg.getBuf(),
              serialized_msg.getOffset(),
              serialized_msg.getLength(),
              false);

      // exclude existing headers, they will be seen again when we decrypt and unmarshal the msg at
      // the receiver
      Message tmp = msg.copy(false, false).setBuffer(encrypted_msg).putHeader(this.id, hdr);
      down_prot.down(new Event(Event.MSG, tmp));
      return;
    }

    // copy neeeded because same message (object) may be retransmitted -> no double encryption
    Message msgEncrypted =
        msg.copy(false)
            .putHeader(this.id, hdr)
            .setBuffer(code(msg.getRawBuffer(), msg.getOffset(), msg.getLength(), false));
    down_prot.down(new Event(Event.MSG, msgEncrypted));
  }
Exemplo n.º 4
0
  private Message _decrypt(final Cipher cipher, Message msg, boolean decrypt_entire_msg)
      throws Exception {
    byte[] decrypted_msg;
    if (cipher == null)
      decrypted_msg = code(msg.getRawBuffer(), msg.getOffset(), msg.getLength(), true);
    else decrypted_msg = cipher.doFinal(msg.getRawBuffer(), msg.getOffset(), msg.getLength());

    if (!decrypt_entire_msg) {
      msg.setBuffer(decrypted_msg);
      return msg;
    }

    Message ret = Util.streamableFromBuffer(Message.class, decrypted_msg, 0, decrypted_msg.length);
    if (ret.getDest() == null) ret.setDest(msg.getDest());
    if (ret.getSrc() == null) ret.setSrc(msg.getSrc());
    return ret;
  }
Exemplo n.º 5
0
    public Message visit(Message msg, MessageBatch batch) {
      EncryptHeader hdr;

      if (msg == null
          || (msg.getLength() == 0 && !encrypt_entire_message)
          || ((hdr = (EncryptHeader) msg.getHeader(id)) == null)) return null;

      if (hdr.getType() == EncryptHeader.ENCRYPT) {
        // if queueing then pass into queue to be dealt with later
        if (queue_up) {
          queueUpMessage(msg, batch);
          return null;
        }

        // make sure we pass up any queued messages first
        if (!suppliedKey) drainUpQueue();

        if (lock == null) {
          int index = getNextIndex();
          lock = decoding_locks[index];
          cipher = decoding_ciphers[index];
          lock.lock();
        }

        try {
          Message tmpMsg = decryptMessage(cipher, msg.copy()); // need to copy for possible xmits
          if (tmpMsg != null) batch.replace(msg, tmpMsg);
        } catch (Exception e) {
          log.error(
              "failed decrypting message from %s (offset=%d, length=%d, buf.length=%d): %s, headers are %s",
              msg.getSrc(),
              msg.getOffset(),
              msg.getLength(),
              msg.getRawBuffer().length,
              e,
              msg.printHeaders());
        }
      } else {
        batch.remove(
            msg); // a control message will get handled by ENCRYPT and should not be passed up
        handleUpEvent(msg, hdr);
      }
      return null;
    }
Exemplo n.º 6
0
  @SuppressWarnings("unchecked")
  public Object up(Event evt) {
    switch (evt.getType()) {
      case Event.MSG:
        Message msg = (Message) evt.getArg();
        PingHeader hdr = (PingHeader) msg.getHeader(this.id);
        if (hdr == null) return up_prot.up(evt);

        if (is_leaving) return null; // prevents merging back a leaving member
        // (https://issues.jboss.org/browse/JGRP-1336)

        PingData data = readPingData(msg.getRawBuffer(), msg.getOffset(), msg.getLength());
        Address logical_addr = data != null ? data.getAddress() : msg.src();

        switch (hdr.type) {
          case PingHeader.GET_MBRS_REQ: // return Rsp(local_addr, coord)
            if (cluster_name == null || hdr.cluster_name == null) {
              log.warn(
                  "cluster_name (%s) or cluster_name of header (%s) is null; passing up discovery "
                      + "request from %s, but this should not be the case",
                  cluster_name, hdr.cluster_name, msg.src());
            } else {
              if (!cluster_name.equals(hdr.cluster_name)) {
                log.warn(
                    "%s: discarding discovery request for cluster '%s' from %s; "
                        + "our cluster name is '%s'. Please separate your clusters properly",
                    logical_addr, hdr.cluster_name, msg.src(), cluster_name);
                return null;
              }
            }

            // add physical address and logical name of the discovery sender (if available) to the
            // cache
            if (data != null) {
              addDiscoveryResponseToCaches(
                  logical_addr, data.getLogicalName(), data.getPhysicalAddr());
              discoveryRequestReceived(msg.getSrc(), data.getLogicalName(), data.getPhysicalAddr());
              addResponse(data, false);
            }

            if (return_entire_cache) {
              Map<Address, PhysicalAddress> cache =
                  (Map<Address, PhysicalAddress>)
                      down(new Event(Event.GET_LOGICAL_PHYSICAL_MAPPINGS));
              if (cache != null) {
                for (Map.Entry<Address, PhysicalAddress> entry : cache.entrySet()) {
                  Address addr = entry.getKey();
                  // JGRP-1492: only return our own address, and addresses in view.
                  if (addr.equals(local_addr) || members.contains(addr)) {
                    PhysicalAddress physical_addr = entry.getValue();
                    sendDiscoveryResponse(
                        addr, physical_addr, UUID.get(addr), msg.getSrc(), isCoord(addr));
                  }
                }
              }
              return null;
            }

            // Only send a response if hdr.mbrs is not empty and contains myself. Otherwise always
            // send my info
            Collection<? extends Address> mbrs = data != null ? data.mbrs() : null;
            boolean send_response = mbrs == null || mbrs.contains(local_addr);
            if (send_response) {
              PhysicalAddress physical_addr =
                  (PhysicalAddress) down(new Event(Event.GET_PHYSICAL_ADDRESS, local_addr));
              sendDiscoveryResponse(
                  local_addr, physical_addr, UUID.get(local_addr), msg.getSrc(), is_coord);
            }
            return null;

          case PingHeader.GET_MBRS_RSP:
            // add physical address (if available) to transport's cache
            if (data != null) {
              log.trace("%s: received GET_MBRS_RSP from %s: %s", local_addr, msg.src(), data);
              handleDiscoveryResponse(data, msg.src());
            }
            return null;

          default:
            log.warn("got PING header with unknown type %d", hdr.type);
            return null;
        }

      case Event.FIND_MBRS:
        return findMembers(
            (List<Address>) evt.getArg(), false, true); // this is done asynchronously
    }

    return up_prot.up(evt);
  }