Esempio n. 1
0
  protected void handleUpEvent(Message msg, EncryptHeader hdr) {
    // check if we had some sort of encrypt control header if using supplied key we should not
    // process it
    if (suppliedKey) {
      log.warn("we received an encrypt header of %s while in configured mode", hdr.getType());
      return;
    }

    // see what sort of encrypt control message we have received
    switch (hdr.getType()) {
        // if a key request
      case EncryptHeader.KEY_REQUEST:
        log.debug("received a key request from peer %s", msg.getSrc());

        // if a key request send response key back
        try {
          // extract peer's public key
          PublicKey tmpKey = generatePubKey(msg.getBuffer());
          // send back the secret key we have
          sendSecretKey(getSecretKey(), tmpKey, msg.getSrc());
        } catch (Exception e) {
          log.warn("unable to reconstitute peer's public key");
        }
        break;
      case EncryptHeader.SECRETKEY:
        log.debug("received a secretkey response from keyserver %s", msg.getSrc());

        try {
          SecretKey tmp = decodeKey(msg.getBuffer());
          if (tmp == null) sendKeyRequest(); // unable to understand response, let's try again
          else {
            // otherwise lets set the returned key as the shared key
            setKeys(tmp, hdr.getVersion());
            log.debug("decoded secretkey response");
          }
        } catch (Exception e) {
          log.warn("unable to process received public key", e);
        }
        break;
      default:
        log.warn("received ignored encrypt header of %s", hdr.getType());
        break;
    }
  }
Esempio 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) {
    Object body;
    MethodCall method_call;

    if (server_obj == null) {
      if (log.isErrorEnabled()) log.error("no method handler is registered. Discarding request.");
      return null;
    }

    if (req == null || req.getLength() == 0) {
      if (log.isErrorEnabled()) log.error("message or message buffer is null");
      return null;
    }

    try {
      body =
          req_marshaller != null
              ? req_marshaller.objectFromByteBuffer(
                  req.getBuffer(), req.getOffset(), req.getLength())
              : req.getObject();
    } catch (Throwable e) {
      if (log.isErrorEnabled()) log.error("exception marshalling object", e);
      return e;
    }

    if (!(body instanceof MethodCall)) {
      if (log.isErrorEnabled()) log.error("message does not contain a MethodCall object");

      // create an exception to represent this and return it
      return new IllegalArgumentException("message does not contain a MethodCall object");
    }

    method_call = (MethodCall) body;

    try {
      if (log.isTraceEnabled())
        log.trace("[sender=" + req.getSrc() + "], method_call: " + method_call);

      if (method_call.getMode() == MethodCall.ID) {
        if (method_lookup == null)
          throw new Exception(
              "MethodCall uses ID=" + method_call.getId() + ", but method_lookup has not been set");
        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);
    } catch (Throwable x) {
      return x;
    }
  }
Esempio n. 3
0
 public static void testWriteStreamable() throws Exception {
   Message m = new Message(null, null, "Hello");
   ViewId vid2 = new ViewId(Util.createRandomAddress(), 35623);
   ByteArrayOutputStream outstream = new ByteArrayOutputStream();
   DataOutputStream dos = new DataOutputStream(outstream);
   Util.writeGenericStreamable(m, dos);
   Util.writeGenericStreamable(vid2, dos);
   dos.close();
   byte[] buf = outstream.toByteArray();
   ByteArrayInputStream instream = new ByteArrayInputStream(buf);
   DataInputStream dis = new DataInputStream(instream);
   Message m2 = (Message) Util.readGenericStreamable(dis);
   ViewId v3 = (ViewId) Util.readGenericStreamable(dis);
   assert m2.getBuffer() != null;
   Assert.assertEquals(m.getLength(), m2.getLength());
   assert v3 != null;
 }
Esempio n. 4
0
  public Object up(Event evt) {
    switch (evt.getType()) {
      case Event.MSG:
        Message msg = (Message) evt.getArg();
        StateHeader hdr = (StateHeader) msg.getHeader(this.id);
        if (hdr == null) break;

        switch (hdr.type) {
          case StateHeader.STATE_REQ:
            state_requesters.add(msg.getSrc());
            break;
          case StateHeader.STATE_RSP:
            handleStateRsp(hdr.getDigest(), msg.getSrc(), msg.getBuffer());
            break;
          case StateHeader.STATE_EX:
            closeHoleFor(msg.getSrc());
            handleException((Throwable) msg.getObject());
            break;
          default:
            log.error("%s: type %s not known in StateHeader", local_addr, hdr.type);
            break;
        }
        return null;

      case Event.TMP_VIEW:
      case Event.VIEW_CHANGE:
        handleViewChange((View) evt.getArg());
        break;

      case Event.CONFIG:
        Map<String, Object> config = (Map<String, Object>) evt.getArg();
        if (config != null && config.containsKey("state_transfer"))
          log.error(
              Util.getMessage(
                  "ProtocolStackCannotContainTwoStateTransferProtocolsRemoveEitherOneOfThem"));
        break;
    }
    return up_prot.up(evt);
  }