/**
   * @param channel
   * @param eventListener
   * @return
   * @throws IOException
   * @throws SshException
   */
  public synchronized boolean openChannel(Channel channel, ChannelEventListener eventListener)
      throws IOException {
    synchronized (activeChannels) {
      Long channelId = getChannelId();

      // Create the message
      SshMsgChannelOpen msg =
          new SshMsgChannelOpen(
              channel.getChannelType(),
              channelId.longValue(),
              channel.getLocalWindow().getWindowSpace(),
              channel.getLocalPacketSize(),
              channel.getChannelOpenData());

      // Send the message
      transport.sendMessage(msg, this);

      // Wait for the next message to confirm the open channel (or not)
      int[] messageIdFilter = new int[2];
      messageIdFilter[0] = SshMsgChannelOpenConfirmation.SSH_MSG_CHANNEL_OPEN_CONFIRMATION;
      messageIdFilter[1] = SshMsgChannelOpenFailure.SSH_MSG_CHANNEL_OPEN_FAILURE;

      try {
        SshMessage result = messageStore.getMessage(messageIdFilter);

        if (result.getMessageId()
            == SshMsgChannelOpenConfirmation.SSH_MSG_CHANNEL_OPEN_CONFIRMATION) {
          SshMsgChannelOpenConfirmation conf = (SshMsgChannelOpenConfirmation) result;
          activeChannels.put(channelId, channel);
          log.debug("Initiating channel");
          channel.init(
              this,
              channelId.longValue(),
              conf.getSenderChannel(),
              conf.getInitialWindowSize(),
              conf.getMaximumPacketSize(),
              eventListener);
          channel.open();
          log.info(
              "Channel "
                  + String.valueOf(channel.getLocalChannelId())
                  + " is open ["
                  + channel.getName()
                  + "]");

          return true;
        } else {
          // Make sure the channels state is closed
          channel.getState().setValue(ChannelState.CHANNEL_CLOSED);

          return false;
        }
      } catch (MessageStoreEOFException mse) {
        throw new IOException(mse.getMessage());
      } catch (InterruptedException ex) {
        throw new SshException(
            "The thread was interrupted whilst waiting for a connection protocol message");
      }
    }
  }
Exemple #2
0
 /**
  * @param persoLevel - Mettre 0 si on ne veut pas prendre en compte les limitations de range de
  *     niveau.
  * @param state - L'état du channel de combat (Preparing, Active, Looting)
  * @return
  */
 public StringBuilder getChannelsList(int persoLevel, byte state) {
   StringBuilder sb =
       new StringBuilder(channels.size() * 10); // jai mit 10 completement au hasard, à refaire.
   for (Channel c : channels.values()) {
     if (c.getState() == state) {
       if (persoLevel == 0 || c.getLevelMin() <= persoLevel && c.getLevelMax() >= persoLevel) {
         if (sb.length() != 0) {
           sb.append("|");
         }
         sb.append(c.toString());
       }
     }
   }
   return sb;
 }
  /**
   * @param channel
   * @param data
   * @throws IOException
   */
  public synchronized void sendChannelData(Channel channel, byte[] data) throws IOException {
    synchronized (channel.getState()) {
      if (log.isDebugEnabled()) {
        log.debug(
            "Sending "
                + String.valueOf(data.length)
                + " bytes for channel id "
                + String.valueOf(channel.getLocalChannelId()));
      }

      int sent = 0;
      int block;
      int remaining;
      long max;
      byte[] buffer;
      ChannelDataWindow window = channel.getRemoteWindow();

      while (sent < data.length) {
        remaining = data.length - sent;
        max =
            ((window.getWindowSpace() < channel.getRemotePacketSize())
                    && (window.getWindowSpace() > 0))
                ? window.getWindowSpace()
                : channel.getRemotePacketSize();
        block = (max < remaining) ? (int) max : remaining;
        channel.remoteWindow.consumeWindowSpace(block);
        buffer = new byte[block];
        System.arraycopy(data, sent, buffer, 0, block);

        SshMsgChannelData msg = new SshMsgChannelData(channel.getRemoteChannelId(), buffer);
        transport.sendMessage(msg, this);

        /*                if (type != null) {
        channel.sendChannelExtData(type.intValue(), buffer);
                   } else {
                       channel.sendChannelData(buffer);
                   }*/
        sent += block;
      }
    }
  }
  private void onMsgChannelClose(SshMsgChannelClose msg) throws IOException {
    Channel channel = getChannel(msg.getRecipientChannel());

    // If we have not already closed it then inform the subclasses
    if (channel == null) {
      throw new IOException(
          "Remote computer tried to close a "
              + "non existent channel "
              + String.valueOf(msg.getRecipientChannel()));
    }

    log.info(
        "Remote computer has closed channel "
            + String.valueOf(channel.getLocalChannelId())
            + "["
            + channel.getName()
            + "]");

    // If the channel is not already closed then close it
    if (channel.getState().getValue() != ChannelState.CHANNEL_CLOSED) {
      channel.remoteClose();
    }
  }