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
{
private static final String fileURL = "http://www.ssec.wisc.edu/~billh/billh.gif";
private static final String serviceName = "CollabGIF";
private static LocalDisplay setupServer()
throws IOException, RemoteException, VisADException
{
DisplayImpl dpy = new DisplayImplJ2D("display");
FlatField image = (FlatField )new DefaultFamily("dflt").open(fileURL);
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));
DataReferenceImpl ref_image = new DataReferenceImpl("ref_image");
ref_image.setData(image);
dpy.addReference(ref_image, null);
RemoteServerImpl server = ClientServer.startServer(serviceName);
server.addDisplay(new RemoteDisplayImpl(dpy));
return dpy;
}
private static LocalDisplay setupClient(String hostName)
throws RemoteException, VisADException
{
RemoteServer client = ClientServer.connectToServer(hostName, serviceName,
true);
LocalDisplay dpy = ClientServer.getClientDisplay(client, 0);
if (dpy == null) {
throw new VisADException("No remote display found!");
}
return dpy;
}
private static void setupUI(LocalDisplay dpy)
{
JFrame jframe = new JFrame("GIF");
jframe.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { System.exit(0); }
});
jframe.setContentPane((Container )dpy.getComponent());
jframe.pack();
jframe.setVisible(true);
}
public static void main(String[] args)
throws IOException, RemoteException, VisADException
{
String hostName = null;
if (args.length > 0) {
hostName = args[0];
}
LocalDisplay local;
if (hostName == null) {
local = setupServer();
} else {
local = setupClient(hostName);
}
setupUI(local);
}
}
Prev
Next
Last modified: Wed Jul 12 10:57:24 CDT 2000