Device.java

00001 package edu.stanford.hci.r3.devices;
00002 
00003 import java.io.File;
00004 import java.net.InetAddress;
00005 import java.net.MalformedURLException;
00006 import java.net.URL;
00007 import java.net.UnknownHostException;
00008 
00009 import edu.stanford.hci.r3.actions.R3Action;
00010 import edu.stanford.hci.r3.actions.remote.ActionReceiver;
00011 import edu.stanford.hci.r3.actions.remote.ActionSender;
00012 import edu.stanford.hci.r3.actions.types.OpenFileAction;
00013 import edu.stanford.hci.r3.actions.types.OpenURLAction;
00014 import edu.stanford.hci.r3.actions.types.PlaySoundAction;
00015 import edu.stanford.hci.r3.actions.types.TextToSpeechAction;
00016 import edu.stanford.hci.r3.devices.channels.ActionChannel;
00017 import edu.stanford.hci.r3.devices.channels.AudioChannel;
00018 import edu.stanford.hci.r3.devices.channels.DisplayChannel;
00019 import edu.stanford.hci.r3.util.DebugUtils;
00020 import edu.stanford.hci.r3.util.networking.ClientServerType;
00021 
00039 public class Device {
00040 
00041         // ////////////////////////////////////////////////////////////////////////////////////////
00042         // The series of doXXX methods are convenience methods for the application to execute local
00043         // actions from the actions.* package. When called, they implicitly ask the local device
00044         // to invoke Actions.
00045         // ////////////////////////////////////////////////////////////////////////////////////////
00046 
00050         // private static LinkedList<PlaySoundAction> queuedSounds = new LinkedList<PlaySoundAction>();
00051         //
00052 
00056         // public static PlaySoundAction doPlaySound(File soundFile) {
00057         // final PlaySoundAction playSoundAction = new PlaySoundAction(soundFile);
00058         // playSoundAction.addStopListener(new PlaySoundAction.PlaySoundListener() {
00059         // public void soundStopped() {
00060         // queuedSounds.remove(playSoundAction); // remove myself
00061         // if (queuedSounds.size() > 0) {
00062         // // if there are any left... play the next guy
00063         // queuedSounds.getFirst().invoke();
00064         // }
00065         // }
00066         // });
00067         // queuedSounds.addLast(playSoundAction);
00068         // if (queuedSounds.size() == 1) {
00069         // playSoundAction.invoke();
00070         // }
00071         //
00072         // return playSoundAction;
00073         // }
00074 
00078         private static PlaySoundAction playSoundAction;
00079 
00086         public static PlaySoundAction doPlaySound(File soundFile) {
00087                 if (playSoundAction != null) {
00088                         playSoundAction.stop();
00089                 }
00090                 playSoundAction = new PlaySoundAction(soundFile);
00091                 playSoundAction.invoke();
00092                 return playSoundAction;
00093         }
00094 
00098         private ActionChannel actionChannel;
00099 
00103         private AudioChannel audioChannel;
00104 
00108         private DisplayChannel displayChannel;
00109 
00113         private String hostNameOrIPAddr;
00114 
00118         private String name;
00119 
00123         private ActionSender sender;
00124 
00128         public Device(String theHostNameOrIPAddr, String descriptiveName) {
00129                 hostNameOrIPAddr = theHostNameOrIPAddr;
00130                 name = descriptiveName;
00131         }
00132 
00136         public void connect() {
00137                 sender = new ActionSender(hostNameOrIPAddr, ActionReceiver.DEFAULT_JAVA_PORT,
00138                                 ClientServerType.JAVA);
00139         }
00140 
00144         public void disconnect() {
00145                 sender.disconnect();
00146                 sender = null;
00147         }
00148 
00152         public ActionChannel getActionChannel() {
00153                 if (actionChannel == null) {
00154                         actionChannel = new ActionChannel(this);
00155                 }
00156                 return actionChannel;
00157         }
00158 
00166         public AudioChannel getAudioChannel() {
00167                 if (audioChannel == null) {
00168                         audioChannel = new AudioChannel(this);
00169                 }
00170                 return audioChannel;
00171         }
00172 
00178         public DisplayChannel getDisplayChannel() {
00179                 if (displayChannel == null) {
00180                         displayChannel = new DisplayChannel(this);
00181                 }
00182                 return displayChannel;
00183         }
00184 
00188         public String getName() {
00189                 return name;
00190         }
00191 
00197         public void invokeAction(R3Action action) {
00198                 if (sender != null) {
00199                         sender.invokeRemoteAction(action);
00200                 } else {
00201                         DebugUtils.println("Sender is null.");
00202                 }
00203         }
00204 
00210         public boolean isAlive() {
00211                 try {
00212                         DebugUtils.println("Checking if Device is Alive and Reachable...");
00213                         final InetAddress address = InetAddress.getByName(hostNameOrIPAddr);
00214                         DebugUtils.println("Device Hostname: " + address.getHostName());
00215                         DebugUtils.println("Device Address: " + address.getHostAddress());
00216                         return true;
00217                 } catch (UnknownHostException e) {
00218                         DebugUtils.println(e);
00219                 }
00220                 return false;
00221         }
00222 
00226         public static void doSpeakText(String textToSpeak) {
00227                 TextToSpeechAction.getInstance().speak(textToSpeak);
00228         }
00229 
00233         public static void doOpenURL(String urlString) {
00234                 try {
00235                         final URL u = new URL(urlString);
00236                         new OpenURLAction(u).invoke();
00237                 } catch (MalformedURLException e) {
00238                         e.printStackTrace();
00239                 }
00240         }
00241 
00248         public static OpenFileAction doOpenFile(File file) {
00249                 final OpenFileAction ofa = new OpenFileAction(file);
00250                 ofa.invoke();
00251                 return ofa;
00252         }
00253 }

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