示例#1
0
  /** @param channel */
  protected void freeChannel(Channel channel) {
    synchronized (activeChannels) {
      log.info(
          "Freeing channel "
              + String.valueOf(channel.getLocalChannelId())
              + " ["
              + channel.getName()
              + "]");

      Long channelId = new Long(channel.getLocalChannelId());
      activeChannels.remove(channelId);

      // reusableChannels.add(channelId);
    }
  }
示例#2
0
  protected void onStop() {
    log.info("Closing all active channels");

    try {
      Channel channel;

      for (Iterator x = activeChannels.values().iterator(); x.hasNext(); ) {
        channel = (Channel) x.next();

        if (channel != null) {
          if (log.isDebugEnabled()) {
            log.debug(
                "Closing "
                    + channel.getName()
                    + " id="
                    + String.valueOf(channel.getLocalChannelId()));
          }

          channel.close();
        }
      }
    } catch (Throwable t) {
    }

    activeChannels.clear();
  }
示例#3
0
  /**
   * @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");
      }
    }
  }
示例#4
0
 /**
  * @param channel
  * @throws IOException
  */
 protected void sendChannelOpenConfirmation(Channel channel) throws IOException {
   SshMsgChannelOpenConfirmation msg =
       new SshMsgChannelOpenConfirmation(
           channel.getRemoteChannelId(),
           channel.getLocalChannelId(),
           channel.getLocalWindow().getWindowSpace(),
           channel.getLocalPacketSize(),
           channel.getChannelConfirmationData());
   transport.sendMessage(msg, this);
 }
示例#5
0
 /**
  * @param channel
  * @throws IOException
  */
 protected void closeChannel(Channel channel) throws IOException {
   SshMsgChannelClose msg = new SshMsgChannelClose(channel.getRemoteChannelId());
   log.info(
       "Local computer has closed channel "
           + String.valueOf(channel.getLocalChannelId())
           + "["
           + channel.getName()
           + "]");
   transport.sendMessage(msg, this);
 }
示例#6
0
  /**
   * @param channel
   * @throws IOException
   */
  public void sendChannelEOF(Channel channel) throws IOException {
    synchronized (activeChannels) {
      if (!activeChannels.containsValue(channel)) {
        throw new IOException(
            "Attempt to send EOF for a non existent channel "
                + String.valueOf(channel.getLocalChannelId()));
      }

      log.info(
          "Local computer has set channel "
              + String.valueOf(channel.getLocalChannelId())
              + " to EOF ["
              + channel.getName()
              + "]");

      SshMsgChannelEOF msg = new SshMsgChannelEOF(channel.getRemoteChannelId());
      transport.sendMessage(msg, this);
    }
  }
示例#7
0
  /**
   * @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;
      }
    }
  }
示例#8
0
  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();
    }
  }