net.schmizz.sshj.transport
Class TransportImpl

java.lang.Object
  extended by net.schmizz.sshj.transport.TransportImpl
All Implemented Interfaces:
SSHPacketHandler, Transport

public final class TransportImpl
extends Object
implements Transport

A thread-safe Transport implementation.


Constructor Summary
TransportImpl(Config config)
           
 
Method Summary
 void addHostKeyVerifier(HostKeyVerifier hkv)
          Adds the specified verifier.
 void disconnect()
          Send a disconnection packet with reason as DisconnectReason.BY_APPLICATION, and closes this transport.
 void disconnect(DisconnectReason reason)
          Send a disconnect packet with the given reason, and closes this transport.
 void disconnect(DisconnectReason reason, String message)
          Send a disconnect packet with the given reason and message, and closes this transport.
 void doKex()
          Do key exchange and algorithm negotiation.
 String getClientVersion()
           
 Config getConfig()
           
 DisconnectListener getDisconnectListener()
           
 int getHeartbeatInterval()
           
 String getRemoteHost()
           
 int getRemotePort()
           
 String getServerVersion()
          Returns the version string as sent by the SSH server for identification purposes, e.g.
 Service getService()
           
 byte[] getSessionID()
           
 int getTimeout()
           
 void handle(Message msg, SSHPacket buf)
          This is where all incoming packets are handled.
 void init(String remoteHost, int remotePort, InputStream in, OutputStream out)
          Sets the host information and the streams to be used by this transport.
 boolean isAuthenticated()
           
 boolean isKexDone()
           
 boolean isRunning()
           
 void join()
          Joins the thread calling this method to the transport's death.
 void join(int timeout, TimeUnit unit)
          Joins the thread calling this method to the transport's death.
 void reqService(Service service)
          Request a SSH service represented by a Service instance.
 long sendUnimplemented()
          Sends SSH_MSG_UNIMPLEMENTED in response to the last packet received.
 void setAuthenticated()
          Informs this transport that authentication has been completed.
 void setDisconnectListener(DisconnectListener listener)
          Specify a listener that will be notified upon disconnection.
 void setHeartbeatInterval(int interval)
           
 void setService(Service service)
          Sets the currently active Service.
 void setTimeout(int timeout)
          Set a timeout for methods that may block.
 long write(SSHPacket payload)
          Write a packet over this transport.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TransportImpl

public TransportImpl(Config config)
Method Detail

init

public void init(String remoteHost,
                 int remotePort,
                 InputStream in,
                 OutputStream out)
          throws TransportException
Description copied from interface: Transport
Sets the host information and the streams to be used by this transport. Identification information is exchanged with the server. A TransportException is thrown in case of SSH protocol version incompatibility.

Specified by:
init in interface Transport
Parameters:
remoteHost - server's hostname
remotePort - server's port
in - input stream for the connection
out - output stream for the connection
Throws:
TransportException - if there is an error during exchange of identification information

addHostKeyVerifier

public void addHostKeyVerifier(HostKeyVerifier hkv)
Description copied from interface: Transport
Adds the specified verifier.

Specified by:
addHostKeyVerifier in interface Transport
Parameters:
hkv - the host key verifier

doKex

public void doKex()
           throws TransportException
Description copied from interface: Transport
Do key exchange and algorithm negotiation. This can be the initial one or for algorithm renegotiation.

Specified by:
doKex in interface Transport
Throws:
TransportException - if there was an error during key exchange

isKexDone

public boolean isKexDone()

getTimeout

public int getTimeout()
Specified by:
getTimeout in interface Transport
Returns:
the timeout that is currently set for blocking operations.

setTimeout

public void setTimeout(int timeout)
Description copied from interface: Transport
Set a timeout for methods that may block.

Specified by:
setTimeout in interface Transport
Parameters:
timeout - the timeout in seconds

getHeartbeatInterval

public int getHeartbeatInterval()
Specified by:
getHeartbeatInterval in interface Transport
Returns:
the interval in seconds at which a heartbeat message is sent to the server

setHeartbeatInterval

public void setHeartbeatInterval(int interval)
Specified by:
setHeartbeatInterval in interface Transport
Parameters:
interval - the interval in seconds, 0 means no hearbeat

getRemoteHost

public String getRemoteHost()
Specified by:
getRemoteHost in interface Transport
Returns:
the hostname to which this transport is connected.

getRemotePort

public int getRemotePort()
Specified by:
getRemotePort in interface Transport
Returns:
the port number on the remote host to which this transport is connected.

getClientVersion

public String getClientVersion()
Specified by:
getClientVersion in interface Transport
Returns:
the version string used by this client to identify itself to an SSH server, e.g. "SSHJ_3_0"

getConfig

public Config getConfig()
Specified by:
getConfig in interface Transport
Returns:
the Config associated with this transport.

getServerVersion

public String getServerVersion()
Description copied from interface: Transport
Returns the version string as sent by the SSH server for identification purposes, e.g. "OpenSSH_$version".

If the transport has not yet been initialized via Transport.init(java.lang.String, int, java.io.InputStream, java.io.OutputStream), it will be null.

Specified by:
getServerVersion in interface Transport
Returns:
server's version string (may be null)

getSessionID

public byte[] getSessionID()
Specified by:
getSessionID in interface Transport
Returns:
the session identifier assigned by server

getService

public Service getService()
Specified by:
getService in interface Transport
Returns:
the currently active Service instance.

setService

public void setService(Service service)
Description copied from interface: Transport
Sets the currently active Service. Handling of non-transport-layer packets is delegated to that service.

For this method to be successful, at least one service request via Transport.reqService(net.schmizz.sshj.Service) must have been successful (not necessarily for the service being set).

Specified by:
setService in interface Transport
Parameters:
service - (null-ok) the Service

reqService

public void reqService(Service service)
                throws TransportException
Description copied from interface: Transport
Request a SSH service represented by a Service instance. A separate call to Transport.setService(net.schmizz.sshj.Service) is not needed.

Specified by:
reqService in interface Transport
Parameters:
service - the SSH service to be requested
Throws:
TransportException - if the request failed for any reason

setAuthenticated

public void setAuthenticated()
Description copied from interface: Transport
Informs this transport that authentication has been completed. This method must be called after successful authentication, so that delayed compression may become effective if applicable.

Specified by:
setAuthenticated in interface Transport

isAuthenticated

public boolean isAuthenticated()
Specified by:
isAuthenticated in interface Transport
Returns:
whether the transport thinks it is authenticated.

sendUnimplemented

public long sendUnimplemented()
                       throws TransportException
Description copied from interface: Transport
Sends SSH_MSG_UNIMPLEMENTED in response to the last packet received.

Specified by:
sendUnimplemented in interface Transport
Returns:
the sequence number of the packet sent
Throws:
TransportException - if an error occured sending the packet

join

public void join()
          throws TransportException
Description copied from interface: Transport
Joins the thread calling this method to the transport's death.

Specified by:
join in interface Transport
Throws:
TransportException - if the transport dies of an exception

join

public void join(int timeout,
                 TimeUnit unit)
          throws TransportException
Description copied from interface: Transport
Joins the thread calling this method to the transport's death.

Specified by:
join in interface Transport
Throws:
TransportException - if the transport dies of an exception

isRunning

public boolean isRunning()
Specified by:
isRunning in interface Transport
Returns:
whether this transport is active.

The transport is considered to be running if it has been initialized without error via Transport.init(java.lang.String, int, java.io.InputStream, java.io.OutputStream) and has not been disconnected.


disconnect

public void disconnect()
Description copied from interface: Transport
Send a disconnection packet with reason as DisconnectReason.BY_APPLICATION, and closes this transport.

Specified by:
disconnect in interface Transport

disconnect

public void disconnect(DisconnectReason reason)
Description copied from interface: Transport
Send a disconnect packet with the given reason, and closes this transport.

Specified by:
disconnect in interface Transport
Parameters:
reason - reason for disconnecting

disconnect

public void disconnect(DisconnectReason reason,
                       String message)
Description copied from interface: Transport
Send a disconnect packet with the given reason and message, and closes this transport.

Specified by:
disconnect in interface Transport
Parameters:
reason - the reason code for this disconnect
message - the text message

setDisconnectListener

public void setDisconnectListener(DisconnectListener listener)
Description copied from interface: Transport
Specify a listener that will be notified upon disconnection.

Specified by:
setDisconnectListener in interface Transport

getDisconnectListener

public DisconnectListener getDisconnectListener()
Specified by:
getDisconnectListener in interface Transport
Returns:
the current disconnect listener.

write

public long write(SSHPacket payload)
           throws TransportException
Description copied from interface: Transport
Write a packet over this transport.

The payload SSHPacket should have 5 bytes free at the beginning to avoid a performance penalty associated with making space for header bytes (packet length, padding length).

Specified by:
write in interface Transport
Parameters:
payload - the SSHPacket containing data to send
Returns:
sequence number of the sent packet
Throws:
TransportException - if an error occurred sending the packet

handle

public void handle(Message msg,
                   SSHPacket buf)
            throws SSHException
This is where all incoming packets are handled. If they pertain to the transport layer, they are handled here; otherwise they are delegated to the active service instance if any via SSHPacketHandler.handle(net.schmizz.sshj.common.Message, net.schmizz.sshj.common.SSHPacket).

Even among the transport layer specific packets, key exchange packets are delegated to KeyExchanger.handle(net.schmizz.sshj.common.Message, net.schmizz.sshj.common.SSHPacket).

This method is called in the context of the reader thread via Decoder.received(byte[], int) when a full packet has been decoded.

Specified by:
handle in interface SSHPacketHandler
Parameters:
msg - the message identifer
buf - buffer containg rest of the packet
Throws:
SSHException - if an error occurs during handling (unrecoverable)


Copyright © 2009-2012. All Rights Reserved.