Пример #1
0
  /**
   * Callback method <br>
   * Called by the ProtocolStack when a message is received.
   *
   * @param evt the event carrying the message from the protocol stack
   */
  public Object up(Event evt) {
    switch (evt.getType()) {
      case Event.MSG:
        Message msg = (Message) evt.getArg();
        if (stats) {
          received_msgs++;
          received_bytes += msg.getLength();
        }

        // discard local messages (sent by myself to me)
        if (discard_own_messages
            && local_addr != null
            && msg.getSrc() != null
            && local_addr.equals(msg.getSrc())) return null;
        break;

      case Event.VIEW_CHANGE:
        View tmp = (View) evt.getArg();
        if (tmp instanceof MergeView) my_view = new View(tmp.getViewId(), tmp.getMembers());
        else my_view = tmp;

        // Bela&Vladimir Oct 27th,2006 (JGroups 2.4): we need to set connected=true because a client
        // can
        // call channel.getView() in viewAccepted() callback invoked on this thread (see
        // Event.VIEW_CHANGE handling below)

        // not good: we are only connected when we returned from connect() - bela June 22 2007
        // Changed: when a channel gets a view of which it is a member then it should be
        // connected even if connect() hasn't returned yet ! (bela Noc 2010)
        if (state != State.CONNECTED) state = State.CONNECTED;
        break;

      case Event.CONFIG:
        Map<String, Object> cfg = (Map<String, Object>) evt.getArg();
        if (cfg != null) {
          if (cfg.containsKey("state_transfer")) {
            state_transfer_supported = (Boolean) cfg.get("state_transfer");
          }
          if (cfg.containsKey("flush_supported")) {
            flush_supported = (Boolean) cfg.get("flush_supported");
          }
        }
        break;

      case Event.GET_STATE_OK:
        StateTransferResult result = (StateTransferResult) evt.getArg();
        if (up_handler != null) {
          try {
            Object retval = up_handler.up(evt);
            state_promise.setResult(new StateTransferResult());
            return retval;
          } catch (Throwable t) {
            state_promise.setResult(new StateTransferResult(t));
          }
        }

        if (receiver != null) {
          try {
            if (result.hasBuffer()) {
              byte[] tmp_state = result.getBuffer();
              ByteArrayInputStream input = new ByteArrayInputStream(tmp_state);
              receiver.setState(input);
            }
            state_promise.setResult(result);
          } catch (Throwable t) {
            state_promise.setResult(new StateTransferResult(t));
          }
        }
        break;

      case Event.STATE_TRANSFER_INPUTSTREAM_CLOSED:
        state_promise.setResult((StateTransferResult) evt.getArg());
        break;

      case Event.STATE_TRANSFER_INPUTSTREAM:
        // Oct 13,2006 moved to down() when Event.STATE_TRANSFER_INPUTSTREAM_CLOSED is received
        // state_promise.setResult(is != null? Boolean.TRUE : Boolean.FALSE);

        if (up_handler != null) return up_handler.up(evt);

        InputStream is = (InputStream) evt.getArg();
        if (is != null && receiver != null) {
          try {
            receiver.setState(is);
          } catch (Throwable t) {
            throw new RuntimeException("failed calling setState() in state requester", t);
          }
        }
        break;

      case Event.STATE_TRANSFER_OUTPUTSTREAM:
        if (receiver != null && evt.getArg() != null) {
          try {
            receiver.getState((OutputStream) evt.getArg());
          } catch (Exception e) {
            throw new RuntimeException("failed calling getState() in state provider", e);
          }
        }
        break;

      case Event.GET_LOCAL_ADDRESS:
        return local_addr;

      default:
        break;
    }

    // If UpHandler is installed, pass all events to it and return (UpHandler is e.g. a building
    // block)
    if (up_handler != null) return up_handler.up(evt);

    if (receiver != null) return invokeCallback(evt.getType(), evt.getArg());
    return null;
  }
Пример #2
0
  protected Object handleUpEvent(Event evt) throws Exception {
    switch (evt.getType()) {
      case Event.MSG:
        if (msg_listener != null) msg_listener.receive((Message) evt.getArg());
        break;

      case Event.GET_APPLSTATE: // reply with GET_APPLSTATE_OK
        byte[] tmp_state = null;
        if (msg_listener != null) {
          ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
          msg_listener.getState(output);
          tmp_state = output.toByteArray();
        }
        return new StateTransferInfo(null, 0L, tmp_state);

      case Event.GET_STATE_OK:
        if (msg_listener != null) {
          StateTransferResult result = (StateTransferResult) evt.getArg();
          if (result.hasBuffer()) {
            ByteArrayInputStream input = new ByteArrayInputStream(result.getBuffer());
            msg_listener.setState(input);
          }
        }
        break;

      case Event.STATE_TRANSFER_OUTPUTSTREAM:
        OutputStream os = (OutputStream) evt.getArg();
        if (msg_listener != null && os != null) {
          msg_listener.getState(os);
        }
        break;

      case Event.STATE_TRANSFER_INPUTSTREAM:
        InputStream is = (InputStream) evt.getArg();
        if (msg_listener != null && is != null) msg_listener.setState(is);
        break;

      case Event.VIEW_CHANGE:
        View v = (View) evt.getArg();
        List<Address> new_mbrs = v.getMembers();
        setMembers(new_mbrs);
        if (membership_listener != null) membership_listener.viewAccepted(v);
        break;

      case Event.SET_LOCAL_ADDRESS:
        if (log.isTraceEnabled())
          log.trace("setting local_addr (" + local_addr + ") to " + evt.getArg());
        local_addr = (Address) evt.getArg();
        break;

      case Event.SUSPECT:
        if (membership_listener != null) membership_listener.suspect((Address) evt.getArg());
        break;

      case Event.BLOCK:
        if (membership_listener != null) membership_listener.block();
        break;
      case Event.UNBLOCK:
        if (membership_listener != null) membership_listener.unblock();
        break;
    }

    return null;
  }