Adding client-related collaborative changes

The next step is to add the client-specific pieces.

The setupClient() method is fairly trivial, and is even simpler than the process detailed in the collaboration overview, since the sample client doesn't need to worry about remote DataReferences.

To summarize those steps, setupClient() needs to connect to the remote server with ClientServer.connectToServer(hostName, serviceName) then fetch a local copy of the remote Display using ClientServer.getClientDisplay(client, 0).

We'll also add some code in main() which checks the length of the standard args[] array. If the args[] array is empty, it'll call setupServer(). Otherwise, call setupClient, using the first String as the server's host name .

ClientServer.getClientDisplay(client, 0) returns a LocalDisplay rather than a DisplayImpl. This isn't a huge problem, since LocalDisplay is simply an interface which DisplayImpl implements, but it does require us to change a few method and variable signatures.


Prev Next
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.io.IOException;
import java.rmi.RemoteException;

import visad.*;
import visad.data.DefaultFamily;
import visad.java2d.DisplayImplJ2D;
import visad.util.ClientServer;

public class DisplayGIF
{
  // location of GIF file to be displayed
  private static final String fileURL = "http://www.ssec.wisc.edu/~billh/billh.gif";

  // name of service (used by collaborative server/client code)
  private static final String serviceName = "CollabGIF";

  private static LocalDisplay setupServer()
    throws IOException, RemoteException, VisADException
  {
    // create a display
    DisplayImpl dpy = new DisplayImplJ2D("display");

    // get the GIF file
    FlatField image = (FlatField )new DefaultFamily("dflt").open(fileURL);

    // compute ScalarMaps from type components
    FunctionType ftype = (FunctionType )image.getType();
    RealTupleType dtype = ftype.getDomain();
    RealTupleType rtype9 = (RealTupleType )ftype.getRange();
    dpy.addMap(new ScalarMap((RealType )dtype.getComponent(0),
                             Display.XAxis));
    dpy.addMap(new ScalarMap((RealType )dtype.getComponent(1),
                             Display.YAxis));
    dpy.addMap(new ScalarMap((RealType )rtype9.getComponent(0),
                             Display.Red));
    dpy.addMap(new ScalarMap((RealType )rtype9.getComponent(1),
                             Display.Green));
    dpy.addMap(new ScalarMap((RealType )rtype9.getComponent(2),
                             Display.Blue));

    // add the GIF data to the display
    DataReferenceImpl ref_image = new DataReferenceImpl("ref_image");
    ref_image.setData(image);
    dpy.addReference(ref_image, null);

    // set up server and add the display to it
    RemoteServerImpl server = ClientServer.startServer(serviceName);
    server.addDisplay(new RemoteDisplayImpl(dpy));

    return dpy;
  }

  private static LocalDisplay setupClient(String hostName)
    throws RemoteException, VisADException
  {
    // connect to the server
    RemoteServer client = ClientServer.connectToServer(hostName, serviceName,
                                                       true);
 
    // build a local copy of the remote display
    LocalDisplay dpy = ClientServer.getClientDisplay(client, 0);
    if (dpy == null) {
      throw new VisADException("No remote display found!");
    }
 
    return dpy;
  }

  private static void setupUI(LocalDisplay dpy)
  {
    // create a frame
    JFrame jframe = new JFrame("GIF");
    jframe.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) { System.exit(0); }
      });

    // add display to frame
    jframe.setContentPane((Container )dpy.getComponent());

    // make frame visible
    jframe.pack();
    jframe.setVisible(true);
  }

  public static void main(String[] args)
    throws IOException, RemoteException, VisADException
  {
    // check the command-line for a hostname argument
    String hostName = null;
    if (args.length > 0) {
      hostName = args[0];
    }
 
    // get the display
    LocalDisplay local;
    if (hostName == null) {
      // create a local display and start a server
      local = setupServer();
    } else {
      // build a local copy of the remote display at the specified host
      local = setupClient(hostName);
    }
 
    // build the common user interface
    setupUI(local);
  }
}
    

Prev Next
Last modified: Wed Jul 12 10:57:24 CDT 2000