Listening for events

Top  Previous  Next

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

 

1. Set object to implement TelnetListener or extend TelnetAdapter class.

2. Overload event handling methods.

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

 

The TelnetListener 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 TelnetAdapter class implements the TelnetListener 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 TelnetAdapter class as it provides default implementations for all the event handler methods defined in the TelnetListener interface. This allows you to overload only the event handler methods that you are interested in.

 

Example

 

The example below demonstrates using the TelnetListener class.

 

import com.jscape.inet.telnet.*;

 

public class MyTelnetListener implements TelnetListener {

 

 private Telnet telnet = null;

 

 public MyTelnetListener(Telnet telnet) {

         this.telnet = telnet;                

 }

 

 public void connected(TelnetConnectedEvent event) {

         System.out.println("Connected to host: " + event.getHost());

 }

 

 public void disconnected(TelnetDisconnectedEvent event) {

         System.out.println("Disconnected from host: " + event.getHost());

 }

 

 public void doOption(DoOptionEvent event) {

         // refuse all options

         telnet.sendWontOption(event.getOption());                

 }

 

 public void dontOption(DontOptionEvent event) {                

 

 }

 

 public void willOption(WillOptionEvent event) {

         // refuse all options

         telnet.sendDontOption(event.getOption());

 }

 

 public void wontOption(WontOptionEvent event) {                

 

 }

 

 public void doSubOption(DoSubOptionEvent event) {                

 

 }

 

 public void dataReceived(TelnetDataReceivedEvent event) {                

         // print data received to console

         System.out.print(event.getData());

 }

 

 public static void main(String[] args) {

         try {

                 // create new telnet instance                        

                 Telnet telnet = new Telnet("hostname");

                 

                 // subscribe event listener

                 telnet.addTelnetListener(new MyTelnetListener(telnet));

                 

                 // establish connection

                 telnet.connect();

                 

                 // disconnect

                 telnet.disconnect();

                 

         } catch(Exception e) {

                 e.printStackTrace();

         }

 }

}

 

 

Example

 

The example below demonstrates using the TelnetAdapter class.

 

import com.jscape.inet.telnet.*;

 

public class MyTelnetAdapter extends TelnetAdapter {

 

 private Telnet telnet;

 

 public MyTelnetAdapter(Telnet telnet) {

         this.telnet = telnet;                

 }

 

 public void connected(TelnetConnectedEvent event) {

         System.out.println("Connected to host: " + event.getHost());

 }

 

 public void disconnected(TelnetDisconnectedEvent event) {

         System.out.println("Disconnected from host: " + event.getHost());

 }

 

 public void doOption(DoOptionEvent event) {

         // refuse all options

         telnet.sendWontOption(event.getOption());                

 }

 

 public void willOption(WillOptionEvent event) {

         // refuse all options

         telnet.sendDontOption(event.getOption());

 }

 

 

 public static void main(String[] args) {

         try {

                 // create new telnet instance                        

                 Telnet telnet = new Telnet("hostname");

                 

                 // subscribe event listener

                 telnet.addTelnetListener(new MyTelnetAdapter(telnet));

                 

                 // establish connection

                 telnet.connect();

                 

                 // disconnect

                 telnet.disconnect();

                 

         } catch(Exception e) {

                 e.printStackTrace();

         }

 }

}





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

© 2021 JSCAPE LLC