public void messageReceived(ChannelMessage msg) {
   if (okToProcess(msg.getOptions())) {
     if (msg.getMessage().getLength()
             == (START_DATA.length + msg.getUniqueId().length + END_DATA.length)
         && Arrays.contains(msg.getMessage().getBytesDirect(), 0, START_DATA, 0, START_DATA.length)
         && Arrays.contains(
             msg.getMessage().getBytesDirect(),
             START_DATA.length + msg.getUniqueId().length,
             END_DATA,
             0,
             END_DATA.length)) {
       UniqueId id =
           new UniqueId(
               msg.getMessage().getBytesDirect(), START_DATA.length, msg.getUniqueId().length);
       MapEntry original = (MapEntry) messages.get(id);
       if (original != null) {
         super.messageReceived(original.msg);
         messages.remove(id);
       } else
         log.warn(
             "Received a confirmation, but original message is missing. Id:"
                 + Arrays.toString(id.getBytes()));
     } else {
       UniqueId id = new UniqueId(msg.getUniqueId());
       MapEntry entry =
           new MapEntry((ChannelMessage) msg.deepclone(), id, System.currentTimeMillis());
       messages.put(id, entry);
     }
   } else {
     super.messageReceived(msg);
   }
 }
 public String toString() {
   StringBuffer buf = new StringBuffer("RpcMessage[");
   buf.append(super.toString());
   buf.append("] rpcId=");
   buf.append(Arrays.toString(rpcId));
   buf.append("; uuid=");
   buf.append(Arrays.toString(uuid));
   buf.append("; msg=");
   buf.append(message);
   return buf.toString();
 }
  /**
   * @param destination Member[] - destination.length > 0
   * @param msg Serializable - the message to send
   * @param options int - sender options, options can trigger guarantee levels and different
   *     interceptors to react to the message see class documentation for the <code>Channel</code>
   *     object.<br>
   * @param handler - callback object for error handling and completion notification, used when a
   *     message is sent asynchronously using the <code>Channel.SEND_OPTIONS_ASYNCHRONOUS</code>
   *     flag enabled.
   * @return UniqueId - the unique Id that was assigned to this message
   * @throws ChannelException - if an error occurs processing the message
   * @see org.apache.catalina.tribes.Channel
   */
  @Override
  public UniqueIdRemoteInterface send(
      Member[] destination, Serializable msg, int options, ErrorHandler handler)
      throws ChannelException, RemoteException, RemoteException {
    if (msg == null) throw new ChannelException("Cant send a NULL message");
    XByteBufferRemoteInterface buffer = null;
    try {
      if (destination == null || destination.length == 0)
        throw new ChannelException("No destination given");
      ChannelDataRemoteInterface data =
          gerenciadornuvem1.getChannelData(true); // generates a unique Id
      data.setAddress(getLocalMember(false));
      data.setTimestamp(System.currentTimeMillis());
      byte[] b = null;
      if (msg instanceof ByteMessageRemoteInterface) {
        b = ((ByteMessageRemoteInterface) msg).getMessage();
        options = options | SEND_OPTIONS_BYTE_MESSAGE;
      } else {
        b = gerenciadornuvem1.XByteBufferserialize(msg);
        options = options & (~SEND_OPTIONS_BYTE_MESSAGE);
      }
      data.setOptions(options);
      // XByteBuffer buffer = new XByteBuffer(b.length+128,false);
      buffer = BufferPool.getBufferPool().getBuffer(b.length + 128, false);
      buffer.append(b, 0, b.length);
      data.setMessage(buffer);
      InterceptorPayloadRemoteInterface payload = null;
      if (handler != null) {
        payload = gerenciadornuvem0.getInterceptorPayload();
        payload.setErrorHandler(handler);
      }
      getFirstInterceptor().sendMessage(destination, data, payload);
      if (Logs.getMessages().isTraceEnabled()) {
        Logs.getMessages()
            .trace(
                "GroupChannel - Sent msg:"
                    + new UniqueId(data.getUniqueId())
                    + " at "
                    + new java.sql.Timestamp(System.currentTimeMillis())
                    + " to "
                    + Arrays.toNameString(destination));
        Logs.getMessages()
            .trace(
                "GroupChannel - Send Message:" + new UniqueId(data.getUniqueId()) + " is " + msg);
      }

      return gerenciadornuvem1.getUniqueId(data.getUniqueId());
    } catch (Exception x) {
      if (x instanceof ChannelException) throw (ChannelException) x;
      throw new ChannelException(x);
    } finally {
      if (buffer != null) BufferPool.getBufferPool().returnBuffer(buffer);
    }
  }
 /**
  * Send a message to one or more members in the cluster
  *
  * @param destination Member[] - the destinations, null or zero length means all
  * @param msg ClusterMessage - the message to send
  * @param payload TBA
  */
 @Override
 public void sendMessage(Member[] destination, ChannelMessage msg, InterceptorPayload payload)
     throws ChannelException {
   if (destination == null) destination = membershipService.getMembers();
   if ((msg.getOptions() & Channel.SEND_OPTIONS_MULTICAST) == Channel.SEND_OPTIONS_MULTICAST) {
     membershipService.broadcast(msg);
   } else {
     clusterSender.sendMessage(msg, destination);
   }
   if (Logs.MESSAGES.isTraceEnabled()) {
     Logs.MESSAGES.trace(
         "ChannelCoordinator - Sent msg:"
             + new UniqueId(msg.getUniqueId())
             + " at "
             + new java.sql.Timestamp(System.currentTimeMillis())
             + " to "
             + Arrays.toNameString(destination));
   }
 }
  /**
   * publish info about a map pair (key/value) to other nodes in the cluster
   *
   * @param key Object
   * @param value Object
   * @return Member - the backup node
   * @throws ChannelException
   */
  @Override
  public Member[] publishEntryInfo(Object key, Object value)
      throws ChannelException, RemoteException {
    try {
      if (!(key instanceof Serializable && value instanceof Serializable)) return new Member[0];
      Member[] members = getMapMembers();
      int firstIdx = getNextBackupIndex();
      int nextIdx = firstIdx;
      Member[] backup = new Member[0];

      // there are no backups
      if (members.length == 0 || firstIdx == -1) return backup;

      boolean success = false;
      do {
        // select a backup node
        Member next = members[nextIdx];

        // increment for the next round of back up selection
        nextIdx = nextIdx + 1;
        if (nextIdx >= members.length) nextIdx = 0;

        if (next == null) {
          continue;
        }
        org.apache.catalina.tribes.tipis.AbstractReplicatedMapMapMessageRemoteInterface msg = null;
        try {
          backup = wrap(next);
          // publish the backup data to one node
          msg =
              gerenciadornuvem1.getAbstractReplicatedMapMapMessage(
                  getMapContextName(),
                  gerenciadornuvem1.AbstractReplicatedMapMapMessagegetMsgBackup(),
                  false,
                  (Serializable) key,
                  (Serializable) value,
                  null,
                  getChannel().getLocalMember(false),
                  backup);
          if (log.isTraceEnabled())
            log.trace("Publishing backup data:" + msg + " to: " + next.getName());
          org.apache.catalina.tribes.UniqueIdRemoteInterface id =
              getChannel().send(backup, msg, getChannelSendOptions());
          if (log.isTraceEnabled()) log.trace("Data published:" + msg + " msg Id:" + id);
          // we published out to a backup, mark the test success
          success = true;
        } catch (ChannelException x) {
          log.error(
              "Unable to replicate backup key:"
                  + key
                  + " to backup:"
                  + next
                  + ". Reason:"
                  + x.getMessage(),
              x);
        }
        try {
          // publish the data out to all nodes
          Member[] proxies = excludeFromSet(backup, getMapMembers());
          if (success && proxies.length > 0) {
            msg =
                gerenciadornuvem1.getAbstractReplicatedMapMapMessage(
                    getMapContextName(),
                    gerenciadornuvem1.AbstractReplicatedMapMapMessagegetMsgProxy(),
                    false,
                    (Serializable) key,
                    null,
                    null,
                    getChannel().getLocalMember(false),
                    backup);
            if (log.isTraceEnabled())
              log.trace("Publishing proxy data:" + msg + " to: " + Arrays.toNameString(proxies));
            getChannel().send(proxies, msg, getChannelSendOptions());
          }
        } catch (ChannelException x) {
          // log the error, but proceed, this should only happen if a node went down,
          // and if the node went down, then it can't receive the message, the others
          // should still get it.
          log.error(
              "Unable to replicate proxy key:"
                  + key
                  + " to backup:"
                  + next
                  + ". Reason:"
                  + x.getMessage(),
              x);
        }
      } while (!success && (firstIdx != nextIdx));
      return backup;
    } catch (Exception excp) {
      excp.printStackTrace();
    }
    return null;
  }
Exemple #6
0
 public void setDomain(String domain) {
   if (domain == null) return;
   if (domain.startsWith("{")) setDomain(Arrays.fromString(domain));
   else setDomain(Arrays.convert(domain));
 }