@Override
 public void sendMessage(Member[] destination, ChannelMessage msg, InterceptorPayload payload)
     throws ChannelException {
   if (!okToProcess(msg.getOptions())) {
     super.sendMessage(destination, msg, payload);
     return;
   }
   ChannelException cx = null;
   for (int i = 0; i < destination.length; i++) {
     try {
       int nr = 0;
       try {
         outLock.writeLock().lock();
         nr = incCounter(destination[i]);
       } finally {
         outLock.writeLock().unlock();
       }
       // reduce byte copy
       msg.getMessage().append(nr);
       try {
         getNext().sendMessage(new Member[] {destination[i]}, msg, payload);
       } finally {
         msg.getMessage().trim(4);
       }
     } catch (ChannelException x) {
       if (cx == null) cx = x;
       cx.addFaultyMember(x.getFaultyMembers());
     }
   } // for
   if (cx != null) throw cx;
 }
  public org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface[] setupForSend(
      Member[] destination) throws ChannelException, RemoteException {
    try {
      ChannelException cx = null;
      org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface[] result =
          new org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface[destination.length];
      for (int i = 0; i < destination.length; i++) {
        org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface sender =
            nioSenders.get(destination[i]);
        try {

          if (sender == null) {
            sender = gerenciadornuvem1.getNioSender();
            gerenciadornuvem1.AbstractSendertransferProperties(this, sender);
            nioSenders.put(destination[i], sender);
          }
          sender.reset();
          sender.setDestination(destination[i]);
          sender.setSelector(selector);
          sender.setUdpBased(isUdpBased());
          result[i] = sender;
        } catch (UnknownHostException x) {
          if (cx == null)
            cx = gerenciadornuvem1.getChannelException("Unable to setup NioSender.", x);
          cx.addFaultyMember(destination[i], x);
        }
      }
      if (cx != null) throw cx;
      else return result;
    } catch (Exception excp) {
      excp.printStackTrace();
    }
    return null;
  }
 @Override
 public void sendMessage(Member[] destination, ChannelMessage msg, InterceptorPayload payload)
     throws ChannelException {
   try {
     super.sendMessage(destination, msg, payload);
   } catch (ChannelException cx) {
     ChannelExceptionFaultyMember[] mbrs = cx.getFaultyMembers();
     for (int i = 0; i < mbrs.length; i++) {
       if (mbrs[i].getCause() != null
           && (!(mbrs[i].getCause()
               instanceof RemoteProcessException))) { // RemoteProcessException's are ok
         this.memberDisappeared(mbrs[i].getMember());
       } // end if
     } // for
     throw cx;
   }
 }
 public void connect(org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface[] senders)
     throws ChannelException, RemoteException {
   try {
     ChannelException x = null;
     for (int i = 0; i < senders.length; i++) {
       try {
         senders[i].connect();
       } catch (IOException io) {
         if (x == null) x = gerenciadornuvem1.getChannelException(io);
         x.addFaultyMember(senders[i].getDestination(), io);
       }
     }
     if (x != null) throw x;
   } catch (Exception excp) {
     excp.printStackTrace();
   }
 }
 public synchronized void close() throws ChannelException, RemoteException {
   try {
     ChannelException x = null;
     Object[] members = nioSenders.keySet().toArray();
     for (int i = 0; i < members.length; i++) {
       Member mbr = (Member) members[i];
       try {
         org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface sender =
             nioSenders.get(mbr);
         sender.disconnect();
       } catch (Exception e) {
         if (x == null) x = gerenciadornuvem1.getChannelException(e);
         x.addFaultyMember(mbr, e);
       }
       nioSenders.remove(mbr);
     }
     if (x != null) throw x;
   } catch (Exception excp) {
     excp.printStackTrace();
   }
 }
 public void sendMessage(Member[] destination, ChannelMessage message) throws ChannelException {
   if (!connected) throw new ChannelException("Sender not connected.");
   ParallelNioSender sender = (ParallelNioSender) getSender();
   if (sender == null) {
     ChannelException cx =
         new ChannelException("Unable to retrieve a data sender, time out error.");
     for (int i = 0; i < destination.length; i++)
       cx.addFaultyMember(
           destination[i],
           new NullPointerException("Unable to retrieve a sender from the sender pool"));
     throw cx;
   } else {
     try {
       sender.sendMessage(destination, message);
       sender.keepalive();
     } catch (ChannelException x) {
       sender.disconnect();
       throw x;
     } finally {
       returnSender(sender);
       if (!connected) disconnect();
     }
   }
 }
    @Override
    public void sendMessage(Member[] destination, ChannelMessage msg, org.apache.catalina.tribes.group.InterceptorPayloadRemoteInterface payload) throws ChannelException, RemoteException {
		try {
		    try {
		        super.sendMessage(destination, msg, payload);
		    }catch ( ChannelException cx ) {
		    	org.apache.catalina.tribes.ChannelExceptionFaultyMemberRemoteInterface[] mbrs = cx.getFaultyMembers();
		        for ( int i=0; i<mbrs.length; i++ ) {
		            if ( mbrs[i].getCause()!=null &&
		                 (!(mbrs[i].getCause() instanceof RemoteProcessException)) ) {//RemoteProcessException's are ok
		                this.memberDisappeared(mbrs[i].getMember());
		            }//end if
		        }//for
		        throw cx;
		    }
		} catch (Exception excp) {
			excp.printStackTrace();
		}
	}
  /**
   * 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;
  }
  @Override
  public synchronized void sendMessage(Member[] destination, ChannelMessage msg)
      throws ChannelException, RemoteException {
    try {
      long start = System.currentTimeMillis();
      this.setUdpBased((msg.getOptions() & Channel.SEND_OPTIONS_UDP) == Channel.SEND_OPTIONS_UDP);
      byte[] data =
          gerenciadornuvem1.XByteBuffercreateDataPackage(
              (org.apache.catalina.tribes.io.ChannelDataRemoteInterface) msg);
      org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface[] senders =
          setupForSend(destination);
      connect(senders);
      setData(senders, data);

      int remaining = senders.length;
      ChannelException cx = null;
      try {
        // loop until complete, an error happens, or we timeout
        long delta = System.currentTimeMillis() - start;
        boolean waitForAck =
            (Channel.SEND_OPTIONS_USE_ACK & msg.getOptions()) == Channel.SEND_OPTIONS_USE_ACK;
        while ((remaining > 0) && (delta < getTimeout())) {
          try {
            remaining -= doLoop(selectTimeout, getMaxRetryAttempts(), waitForAck, msg);
          } catch (Exception x) {
            if (log.isTraceEnabled()) log.trace("Error sending message", x);
            int faulty = (cx == null) ? 0 : cx.getFaultyMembers().length;
            if (cx == null) {
              if (x instanceof ChannelException) cx = (ChannelException) x;
              else cx = gerenciadornuvem1.getChannelException("Parallel NIO send failed.", x);
            } else {
              if (x instanceof ChannelException)
                cx.addFaultyMember(((ChannelException) x).getFaultyMembers());
            }
            // count down the remaining on an error
            if (faulty < cx.getFaultyMembers().length)
              remaining -= (cx.getFaultyMembers().length - faulty);
          }
          // bail out if all remaining senders are failing
          if (cx != null && cx.getFaultyMembers().length == remaining) throw cx;
          delta = System.currentTimeMillis() - start;
        }
        if (remaining > 0) {
          // timeout has occurred
          ChannelException cxtimeout =
              gerenciadornuvem1.getChannelException(
                  "Operation has timed out(" + getTimeout() + " ms.).");
          if (cx == null)
            cx =
                gerenciadornuvem1.getChannelException(
                    "Operation has timed out(" + getTimeout() + " ms.).");
          for (int i = 0; i < senders.length; i++) {
            if (!senders[i].isComplete())
              cx.addFaultyMember(senders[i].getDestination(), cxtimeout);
          }
          throw cx;
        } else if (cx != null) {
          // there was an error
          throw cx;
        }
      } catch (Exception x) {
        try {
          this.disconnect();
        } catch (Exception e) {
          /* Ignore */
        }
        if (x instanceof ChannelException) throw (ChannelException) x;
        else throw new ChannelException(x);
      }

    } catch (Exception excp) {
      excp.printStackTrace();
    }
  }
  public int doLoop(long selectTimeOut, int maxAttempts, boolean waitForAck, ChannelMessage msg)
      throws IOException, ChannelException, RemoteException {
    try {
      int completed = 0;
      int selectedKeys = selector.select(selectTimeOut);

      if (selectedKeys == 0) {
        return 0;
      }

      Iterator<SelectionKey> it = selector.selectedKeys().iterator();
      while (it.hasNext()) {
        SelectionKey sk = it.next();
        it.remove();
        int readyOps = sk.readyOps();
        sk.interestOps(sk.interestOps() & ~readyOps);
        org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface sender =
            (org.apache.catalina.tribes.transport.nio.NioSenderRemoteInterface) sk.attachment();
        try {
          if (sender.process(sk, waitForAck)) {
            completed++;
            sender.setComplete(true);
            if (Logs.getMessages().isTraceEnabled()) {
              Logs.getMessages()
                  .trace(
                      "ParallelNioSender - Sent msg:"
                          + new UniqueId(msg.getUniqueId())
                          + " at "
                          + new java.sql.Timestamp(System.currentTimeMillis())
                          + " to "
                          + sender.getDestination().getName());
            }
            SenderState.getSenderState(sender.getDestination()).setReady();
          } // end if
        } catch (Exception x) {
          if (log.isTraceEnabled()) {
            log.trace("Error while processing send to " + sender.getDestination().getName(), x);
          }
          org.apache.catalina.tribes.transport.SenderStateRemoteInterface state =
              gerenciadornuvem0.SenderStategetSenderState(sender.getDestination());
          int attempt = sender.getAttempt() + 1;
          boolean retry = (sender.getAttempt() <= maxAttempts && maxAttempts > 0);
          synchronized (state) {

            // sk.cancel();
            if (state.isSuspect()) state.setFailing();
            if (state.isReady()) {
              state.setSuspect();
              if (retry)
                log.warn(
                    "Member send is failing for:"
                        + sender.getDestination().getName()
                        + " ; Setting to suspect and retrying.");
              else
                log.warn(
                    "Member send is failing for:"
                        + sender.getDestination().getName()
                        + " ; Setting to suspect.",
                    x);
            }
          }
          if (!isConnected()) {
            log.warn(
                "Not retrying send for:"
                    + sender.getDestination().getName()
                    + "; Sender is disconnected.");
            ChannelException cx =
                gerenciadornuvem1.getChannelException(
                    "Send failed, and sender is disconnected. Not retrying.", x);
            cx.addFaultyMember(sender.getDestination(), x);
            throw cx;
          }

          byte[] data = sender.getMessage();
          if (retry) {
            try {
              sender.disconnect();
              sender.connect();
              sender.setAttempt(attempt);
              sender.setMessage(data);
            } catch (Exception ignore) {
              state.setFailing();
            }
          } else {
            ChannelException cx =
                gerenciadornuvem1.getChannelException(
                    "Send failed, attempt:" + sender.getAttempt() + " max:" + maxAttempts, x);
            cx.addFaultyMember(sender.getDestination(), x);
            throw cx;
          } // end if
        }
      }
      return completed;

    } catch (Exception excp) {
      excp.printStackTrace();
    }
    return 0;
  }