Listening for events

Top  Previous  Next

For an object to listen for events published by the Ssh class the following steps are required:

 

1. Set object to implement SshListener or extend SshAdapter class.

2. Overload event handling methods.

3. Subscribe object to receive events published by Ssh instance.

 

The SshListener class is a pure interface and can be used in cases where the class you defined to listen for events already extends another class type. The SshAdapter class implements the SshListener interface and can be used in cases where there is no need for class inheritance.

 

Note

 

Unless your class requires inheritance it is generally easier to use the SshAdapter class as it provides default implementations for all the event handler methods defined in the SshListener interface. This allows you to overload only the event handler methods that you are interested in.

 

Example

 

The example below demonstrates using the SshListener class.

 

import com.jscape.inet.ssh.*;

 

public class MySshListener implements SshListener {

 

public void connected(SshConnectedEvent event) {

  // process event

 

 }

 

public void disconnected(SshDisconnectedEvent event) {

  // process event

 

 }

 

public void dataReceived(SshDataReceivedEvent event) {

  // process event

 }

public static void main(String[] args) {

  try {

     Ssh ssh = new Ssh("hostname","username","password");

     ssh.addSshListener(new MySshListener());    

   } catch(SshException se) {    

   }

 }

}

 

Example

 

The example below demonstrates using the SshAdapter class.

 

import com.jscape.inet.ssh.*;

 

public class MySshAdapter extends SshAdapter {

public void connected(SshConnectedEvent event) {

  // process event  

 }

 

public void disconnected(SshDisconnectedEvent event) {

  // process event

 }

 

public void dataReceived(SshDataReceivedEvent event) {

  // process event

 }

public static void main(String[] args) {

  try {

     Ssh ssh = new Ssh("hostname","username","password");

     ssh.addSshListener(new MySshAdapter());    

   } catch(SshException se) {    

   }

 }

}





Home | Company | Products | Solutions | Purchase | Support | Services | Blog

© 2021 JSCAPE LLC