Pen.java

00001 package edu.stanford.hci.r3.pen;
00002 
00003 import edu.stanford.hci.r3.pen.streaming.PenClient;
00004 import edu.stanford.hci.r3.pen.streaming.PenServer;
00005 import edu.stanford.hci.r3.pen.streaming.listeners.PenListener;
00006 import edu.stanford.hci.r3.util.DebugUtils;
00007 import edu.stanford.hci.r3.util.communications.COMPort;
00008 import edu.stanford.hci.r3.util.networking.ClientServerType;
00009 
00027 public class Pen extends PenInput {
00028 
00032         public static final COMPort DEFAULT_COM_PORT = COMPort.COM5;
00033 
00037         private static final String LOCALHOST = "localhost";
00038 
00042         private String defaultPenServer;
00043 
00048         private PenClient livePenClient;
00049 
00053         private COMPort localPenComPort = DEFAULT_COM_PORT;
00054 
00058         private int penServerTcpIpPort = PenServer.DEFAULT_JAVA_PORT;
00059 
00065         public Pen() {
00066                 this("A Pen");
00067         }
00068 
00073         public Pen(String name) {
00074                 this(name, LOCALHOST);
00075         }
00076 
00082         public Pen(String name, String penServerHostName) {
00083                 super(name);
00084                 defaultPenServer = penServerHostName;
00085         }
00086 
00087         /*
00088          * (non-Javadoc)
00089          * 
00090          * @see edu.stanford.hci.r3.pen.PenInput#addLivePenListener(edu.stanford.hci.r3.pen.streaming.listeners.PenListener)
00091          */
00092         public void addLivePenListener(PenListener penListener) {
00093                 // cache if necessary
00094                 super.addLivePenListener(penListener);
00095                 if (isLive()) {
00096                         // if we're live, then listen to the PenClient
00097                         livePenClient.addPenListener(penListener);
00098                 }
00099         }
00100 
00101         /*
00102          * (non-Javadoc)
00103          * 
00104          * @see edu.stanford.hci.r3.pen.PenInput#removeLivePenListener(edu.stanford.hci.r3.pen.streaming.listeners.PenListener)
00105          */
00106         public void removeLivePenListener(PenListener penListener) {
00107                 super.removeLivePenListener(penListener);
00108                 if (isLive()) {
00109                         livePenClient.removePenListener(penListener);
00110                 }
00111         }
00112 
00120         public void setLocalComPort(COMPort port) {
00121                 localPenComPort = port;
00122         }
00123 
00129         public void setPenServerPort(int tcpipPort) {
00130                 penServerTcpIpPort = tcpipPort;
00131         }
00132 
00138         public void startLiveMode() {
00139                 startLiveMode(defaultPenServer);
00140         }
00141 
00149         public void startLiveMode(String hostDomainNameOrIPAddr) {
00150                 if (liveMode) {
00151                         // already started
00152                         return;
00153                 }
00154 
00155                 // if the pen is on the local host...
00156                 // ensure that a java server has been started on this machine
00157                 if (hostDomainNameOrIPAddr.equals(LOCALHOST)) {
00158                         if (!PenServer.javaServerStarted()) {
00159                                 PenServer.startJavaServer(localPenComPort);
00160                         }
00161                 }
00162 
00163                 // start a client to listen to the pen...
00164                 if (livePenClient == null && !isLive()) {
00165                         livePenClient = new PenClient(hostDomainNameOrIPAddr, penServerTcpIpPort, ClientServerType.JAVA);
00166                         livePenClient.connect();
00167                         liveMode = true;
00168 
00169                         // add all the cached listeners now
00170                         for (PenListener pl : penListenersToAdd) {
00171                                 DebugUtils.println("Adding cached pen listeners...");
00172                                 livePenClient.addPenListener(pl);
00173                         }
00174                 } else {
00175                         DebugUtils.println("Pen [" + getName() + "] is already live. " + "We cannot connect again.");
00176                 }
00177         }
00178 
00182         public void stopLiveMode() {
00183                 if (!liveMode) {
00184                         // already stopped
00185                         return;
00186                 }
00187 
00188                 livePenClient.disconnect();
00189                 livePenClient = null;
00190                 liveMode = false;
00191 
00192                 // if the server was started on the localhost, kill it too!
00193                 if (PenServer.javaServerStarted()) {
00194                         PenServer.stopServers();
00195                 }
00196         }
00197 }

Generated on Sat Apr 14 18:21:37 2007 for R3 Paper Toolkit by  doxygen 1.4.7