| Method from com.sshtools.j2ssh.connection.ConnectionProtocol Detail: |
public void addChannelFactory(String channelName,
ChannelFactory cf) throws IOException {
allowedChannels.put(channelName, cf);
}
|
public void allowGlobalRequest(String requestName,
GlobalRequestHandler handler) {
globalRequests.put(requestName, handler);
}
|
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);
}
|
public boolean containsChannelFactory(String channelName) {
return allowedChannels.containsKey(channelName);
}
|
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);
//}
}
|
protected int[] getAsyncMessageFilter() {
int[] messageFilter = new int[10];
messageFilter[0] = SshMsgGlobalRequest.SSH_MSG_GLOBAL_REQUEST;
messageFilter[3] = SshMsgChannelOpen.SSH_MSG_CHANNEL_OPEN;
messageFilter[4] = SshMsgChannelClose.SSH_MSG_CHANNEL_CLOSE;
messageFilter[5] = SshMsgChannelEOF.SSH_MSG_CHANNEL_EOF;
messageFilter[6] = SshMsgChannelExtendedData.SSH_MSG_CHANNEL_EXTENDED_DATA;
messageFilter[7] = SshMsgChannelData.SSH_MSG_CHANNEL_DATA;
messageFilter[8] = SshMsgChannelRequest.SSH_MSG_CHANNEL_REQUEST;
messageFilter[9] = SshMsgChannelWindowAdjust.SSH_MSG_CHANNEL_WINDOW_ADJUST;
return messageFilter;
}
|
public boolean isConnected() {
return ((transport.getState().getValue() == TransportProtocolState.CONNECTED) ||
(transport.getState().getValue() == TransportProtocolState.PERFORMING_KEYEXCHANGE)) &&
(getState().getValue() == ServiceState.SERVICE_STARTED);
}
|
protected void onGlobalRequest(String requestName,
boolean wantReply,
byte[] requestData) throws IOException {
log.debug("Processing " + requestName + " global request");
if (!globalRequests.containsKey(requestName)) {
sendGlobalRequestFailure();
} else {
GlobalRequestHandler handler = (GlobalRequestHandler) globalRequests.get(requestName);
GlobalRequestResponse response = handler.processGlobalRequest(requestName,
requestData);
if (wantReply) {
if (response.hasSucceeded()) {
sendGlobalRequestSuccess(response.getResponseData());
} else {
sendGlobalRequestFailure();
}
}
}
}
|
protected void onMessageReceived(SshMessage msg) throws IOException {
// Route the message to the correct handling function
switch (msg.getMessageId()) {
case SshMsgGlobalRequest.SSH_MSG_GLOBAL_REQUEST: {
onMsgGlobalRequest((SshMsgGlobalRequest) msg);
break;
}
case SshMsgChannelOpen.SSH_MSG_CHANNEL_OPEN: {
onMsgChannelOpen((SshMsgChannelOpen) msg);
break;
}
case SshMsgChannelClose.SSH_MSG_CHANNEL_CLOSE: {
onMsgChannelClose((SshMsgChannelClose) msg);
break;
}
case SshMsgChannelEOF.SSH_MSG_CHANNEL_EOF: {
onMsgChannelEOF((SshMsgChannelEOF) msg);
break;
}
case SshMsgChannelData.SSH_MSG_CHANNEL_DATA: {
onMsgChannelData((SshMsgChannelData) msg);
break;
}
case SshMsgChannelExtendedData.SSH_MSG_CHANNEL_EXTENDED_DATA: {
onMsgChannelExtendedData((SshMsgChannelExtendedData) msg);
break;
}
case SshMsgChannelRequest.SSH_MSG_CHANNEL_REQUEST: {
onMsgChannelRequest((SshMsgChannelRequest) msg);
break;
}
case SshMsgChannelWindowAdjust.SSH_MSG_CHANNEL_WINDOW_ADJUST: {
onMsgChannelWindowAdjust((SshMsgChannelWindowAdjust) msg);
break;
}
default: {
// If we never registered it why are we getting it?
log.debug("Message not handled");
throw new IOException("Unregistered message received!");
}
}
}
|
protected void onServiceAccept() {
}
|
protected void onServiceInit(int startMode) throws IOException {
log.info("Registering connection protocol messages");
messageStore.registerMessage(SshMsgChannelOpenConfirmation.SSH_MSG_CHANNEL_OPEN_CONFIRMATION,
SshMsgChannelOpenConfirmation.class);
messageStore.registerMessage(SshMsgChannelOpenFailure.SSH_MSG_CHANNEL_OPEN_FAILURE,
SshMsgChannelOpenFailure.class);
messageStore.registerMessage(SshMsgChannelOpen.SSH_MSG_CHANNEL_OPEN,
SshMsgChannelOpen.class);
messageStore.registerMessage(SshMsgChannelClose.SSH_MSG_CHANNEL_CLOSE,
SshMsgChannelClose.class);
messageStore.registerMessage(SshMsgChannelEOF.SSH_MSG_CHANNEL_EOF,
SshMsgChannelEOF.class);
messageStore.registerMessage(SshMsgChannelData.SSH_MSG_CHANNEL_DATA,
SshMsgChannelData.class);
messageStore.registerMessage(SshMsgChannelExtendedData.SSH_MSG_CHANNEL_EXTENDED_DATA,
SshMsgChannelExtendedData.class);
messageStore.registerMessage(SshMsgChannelFailure.SSH_MSG_CHANNEL_FAILURE,
SshMsgChannelFailure.class);
messageStore.registerMessage(SshMsgChannelRequest.SSH_MSG_CHANNEL_REQUEST,
SshMsgChannelRequest.class);
messageStore.registerMessage(SshMsgChannelSuccess.SSH_MSG_CHANNEL_SUCCESS,
SshMsgChannelSuccess.class);
messageStore.registerMessage(SshMsgChannelWindowAdjust.SSH_MSG_CHANNEL_WINDOW_ADJUST,
SshMsgChannelWindowAdjust.class);
messageStore.registerMessage(SshMsgGlobalRequest.SSH_MSG_GLOBAL_REQUEST,
SshMsgGlobalRequest.class);
messageStore.registerMessage(SshMsgRequestFailure.SSH_MSG_REQUEST_FAILURE,
SshMsgRequestFailure.class);
messageStore.registerMessage(SshMsgRequestSuccess.SSH_MSG_REQUEST_SUCCESS,
SshMsgRequestSuccess.class);
}
|
protected void onServiceRequest() {
}
|
protected synchronized void onStop() {
log.info("Closing all active channels");
// synchronized (activeChannels) {
log.info("thread has "+activeChannels.values().size()+" active channels to stop");
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) {
log.error("Unable to close all channels: "+t.getMessage(),t);
}
activeChannels.clear();
// }
}
|
public synchronized boolean openChannel(Channel channel) throws IOException {
return openChannel(channel, null);
}
|
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");
}
//}
}
|
public void removeChannelFactory(String channelName) {
allowedChannels.remove(channelName);
}
|
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;
}
}
}
|
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);
// }
}
|
public synchronized void sendChannelExtData(Channel channel,
int extendedType,
byte[] data) throws IOException {
channel.getRemoteWindow().consumeWindowSpace(data.length);
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);
SshMsgChannelExtendedData msg = new SshMsgChannelExtendedData(channel.getRemoteChannelId(),
extendedType, buffer);
transport.sendMessage(msg, this);
/* if (type != null) {
channel.sendChannelExtData(type.intValue(), buffer);
} else {
channel.sendChannelData(buffer);
}*/
sent += block;
}
}
|
protected void sendChannelFailure(Channel channel) throws IOException {
SshMsgChannelFailure msg = new SshMsgChannelFailure(channel.getRemoteChannelId());
transport.sendMessage(msg, this);
}
|
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);
}
|
protected void sendChannelOpenFailure(long remoteChannelId,
long reasonCode,
String additionalInfo,
String languageTag) throws IOException {
SshMsgChannelOpenFailure msg = new SshMsgChannelOpenFailure(remoteChannelId,
reasonCode, additionalInfo, languageTag);
transport.sendMessage(msg, this);
}
|
public synchronized boolean sendChannelRequest(Channel channel,
String requestType,
boolean wantReply,
byte[] requestData) throws IOException {
boolean success = true;
log.info("Sending " + requestType + " request for the " +
channel.getChannelType() + " channel");
SshMsgChannelRequest msg = new SshMsgChannelRequest(channel.getRemoteChannelId(),
requestType, wantReply, requestData);
transport.sendMessage(msg, this);
// If the user requests a reply then wait for the message and return result
if (wantReply) {
// Set up our message filter
int[] messageIdFilter = new int[2];
messageIdFilter[0] = SshMsgChannelSuccess.SSH_MSG_CHANNEL_SUCCESS;
messageIdFilter[1] = SshMsgChannelFailure.SSH_MSG_CHANNEL_FAILURE;
log.info("Waiting for channel request reply");
try {
// Wait for either success or failure
SshMessage reply = messageStore.getMessage(messageIdFilter);
switch (reply.getMessageId()) {
case SshMsgChannelSuccess.SSH_MSG_CHANNEL_SUCCESS: {
log.info("Channel request succeeded");
success = true;
break;
}
case SshMsgChannelFailure.SSH_MSG_CHANNEL_FAILURE: {
log.info("Channel request failed");
success = false;
break;
}
}
} catch (InterruptedException ex) {
throw new SshException(
"The thread was interrupted whilst waiting for a connection protocol message");
}
}
return success;
}
|
public void sendChannelRequestFailure(Channel channel) throws IOException {
SshMsgChannelFailure msg = new SshMsgChannelFailure(channel.getRemoteChannelId());
transport.sendMessage(msg, this);
}
|
public void sendChannelRequestSuccess(Channel channel) throws IOException {
SshMsgChannelSuccess msg = new SshMsgChannelSuccess(channel.getRemoteChannelId());
transport.sendMessage(msg, this);
}
|
public void sendChannelWindowAdjust(Channel channel,
long bytesToAdd) throws IOException {
log.debug("Increasing window size by " + String.valueOf(bytesToAdd) +
" bytes");
SshMsgChannelWindowAdjust msg = new SshMsgChannelWindowAdjust(channel.getRemoteChannelId(),
bytesToAdd);
transport.sendMessage(msg, this);
}
|
public synchronized byte[] sendGlobalRequest(String requestName,
boolean wantReply,
byte[] requestData) throws IOException {
boolean success = true;
SshMsgGlobalRequest msg = new SshMsgGlobalRequest(requestName, true,
requestData);
transport.sendMessage(msg, this);
if (wantReply) {
// Set up our message filter
int[] messageIdFilter = new int[2];
messageIdFilter[0] = SshMsgRequestSuccess.SSH_MSG_REQUEST_SUCCESS;
messageIdFilter[1] = SshMsgRequestFailure.SSH_MSG_REQUEST_FAILURE;
log.debug("Waiting for global request reply");
try {
// Wait for either success or failure
SshMessage reply = messageStore.getMessage(messageIdFilter);
switch (reply.getMessageId()) {
case SshMsgRequestSuccess.SSH_MSG_REQUEST_SUCCESS: {
log.debug("Global request succeeded");
return ((SshMsgRequestSuccess) reply).getRequestData();
}
case SshMsgRequestFailure.SSH_MSG_REQUEST_FAILURE: {
log.debug("Global request failed");
throw new SshException("The request failed");
}
}
} catch (InterruptedException ex) {
throw new SshException(
"The thread was interrupted whilst waiting for a connection protocol message");
}
}
return null;
}
|
protected void sendGlobalRequestFailure() throws IOException {
SshMsgRequestFailure msg = new SshMsgRequestFailure();
transport.sendMessage(msg, this);
}
|
protected void sendGlobalRequestSuccess(byte[] requestData) throws IOException {
SshMsgRequestSuccess msg = new SshMsgRequestSuccess(requestData);
transport.sendMessage(msg, this);
}
|