Collaborative Applications and the Java Shared Data Toolkit
by Joshua Fox


Listing One
import com.sun.media.jsdt.Connection;
import com.sun.media.jsdt.JSDTException;
 . . .
public static void main (String [] args) {
    // . . .
    // Register a Connection Listener, which will receive 
    // notification when the connection fails:
    try {
        Connection.addConnectionListener("www.my-jsdt-server.com", "socket",
                                         new KeepAlive());
    } catch (JSDTException jsdte) {
    }
    // . . .
}
/* Class that cleans up and tries to reconnect when the connection is lost. */
import com.sun.media.jsdt.Connection;
import com.sun.media.jsdt.JSDTException;
import com.sun.media.jsdt.Session;
import com.sun.media.jsdt.event.ConnectionEvent;
import com.sun.media.jsdt.event.ConnectionListener;

public class KeepAlive implements ConnectionListener {
    /* Call-back method from ConnectionListener interface. The connection has 
     * failed--let's hope that it is restored eventually. Try to reconnect 
     * at 20 second intervals. */
    public void connectionFailed(ConnectionEvent event) {
       disconnect();  // clean up just in case
       boolean succeeded = false;
       while (!succeeded) {
          try {
               connect();
               succeeded = true;
          } catch (JSDTException jsdte) {
               succeeded = false;
          }
          try {
             Thread.sleep(20 * 1000L);
          } catch (InterruptedException ie) {
          }
       }
    }
     private void connect() {
       // . . .
    }
    private void disconnect() {
       // . . .
    }
    // . . .
}


Listing Two
/** This is a JSDT Client which registers a * Client Listener, incrementing 
 * its port number if the port it tries is already bound. 
 */
import java.net.InetAddress;
import com.sun.media.jsdt.AuthenticationInfo;
import com.sun.media.jsdt.Client;
import com.sun.media.jsdt.URLString;
import com.sun.media.jsdt.ClientFactory;
import com.sun.media.jsdt.PortInUseException;
import com.sun.media.jsdt.event.ClientEvent;

import com.sun.media.jsdt.event.ClientAdaptor;

public class MyClient implements Client {
    private String myName;
    public int clientListenerPort = 5661;
    // ...
    private void registerClientListener(Client client) {
      while (true) {
          try {
              // The last parameter for the Client Listener
              // URLString MUST be the same as the name of the Client object.
              URLString clientListenerUrl= URLString.createClientURL(
                               "www.my-jsdt-server.com", clientListenerPort,
                               "socket", this.getName());
              ClientFactory.createClient(this, 
                                    clientListenerUrl, new MyClientAdaptor());
              break;
          } catch (PortInUseException piue) {
              clientListenerPort++; // Retry after incrementing port number
          } catch (Exception  e) {
              break;
          }
       }
    }
    public String getName() {
        return myName;
    }
    public Object authenticate(AuthenticationInfo ai){
        return null;
    }
    /* Implementation of ClientListener. It listens for commands on behalf 
     * of your client applications. Like the Swing Adaptor classes, the JSDT 
     * Adaptors provide an empty implementation of all methods of Listener, 
     * so that you can implement just those methods that interest you.
     */
    private class MyClientAdaptor extends ClientAdaptor {
        /* Examples of commands that can be sent to the Client Listener. */
         public void sessionInvited(ClientEvent event) {
                // Now that you've been invited, you'll 
                // probably want to connect to the Session, 
                // . . .
         }
         public void sessionExpelled(ClientEvent event) {
                // You've been expelled from the Session. Unlike
                // sessionInvited, you do not need to do anything
                // to leave the Session, since you have already been expelled.
         }
    }
}

2


