/** * @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"); } } }
private void onMsgChannelOpen(SshMsgChannelOpen msg) throws IOException { synchronized (activeChannels) { log.info("Request for " + msg.getChannelType() + " channel recieved"); // Try to get the channel implementation from the allowed channels ChannelFactory cf = (ChannelFactory) allowedChannels.get(msg.getChannelType()); if (cf == null) { sendChannelOpenFailure( msg.getSenderChannelId(), SshMsgChannelOpenFailure.SSH_OPEN_CONNECT_FAILED, "The channel type is not supported", ""); log.info("Request for channel type " + msg.getChannelType() + " refused"); return; } try { log.info("Creating channel " + msg.getChannelType()); Channel channel = cf.createChannel(msg.getChannelType(), msg.getChannelData()); // Initialize the channel log.info("Initiating channel"); Long channelId = getChannelId(); channel.init( this, channelId.longValue(), msg.getSenderChannelId(), msg.getInitialWindowSize(), msg.getMaximumPacketSize()); activeChannels.put(channelId, channel); log.info("Sending channel open confirmation"); // Send the confirmation message sendChannelOpenConfirmation(channel); // Open the channel for real channel.open(); } catch (InvalidChannelException ice) { sendChannelOpenFailure( msg.getSenderChannelId(), SshMsgChannelOpenFailure.SSH_OPEN_CONNECT_FAILED, ice.getMessage(), ""); } } }
@Override public Connection<Address> open(Listener<Address, X> listener) throws InterruptedException, IOException { return new MarshallConnection(inner.open(new UnmarshallListener(listener))); }