00001 package edu.stanford.hci.r3.actions.remote;
00002
00003 import java.io.IOException;
00004 import java.net.Socket;
00005 import java.net.UnknownHostException;
00006
00007 import edu.stanford.hci.r3.actions.R3Action;
00008 import edu.stanford.hci.r3.util.DebugUtils;
00009 import edu.stanford.hci.r3.util.networking.ClientServerType;
00010
00024 public class ActionSender {
00025
00029 private ActionMessenger messengerOutput;
00030
00034 private Socket socket;
00035
00041 public ActionSender(String serverNameOrIPAddr, int port, ClientServerType type) {
00042 try {
00043 socket = new Socket(serverNameOrIPAddr, port);
00044 if (type == ClientServerType.PLAINTEXT) {
00045 messengerOutput = new ActionPlainTextMessenger(socket);
00046 } else {
00047 messengerOutput = new ActionJavaObjectXMLMessenger(socket);
00048 }
00049 } catch (UnknownHostException e) {
00050 e.printStackTrace();
00051 } catch (IOException e) {
00052 DebugUtils.println(e + " to " + serverNameOrIPAddr);
00053 DebugUtils.println("Perhaps the ActionReceiver is not running on ["
00054 + serverNameOrIPAddr + "]?");
00055 }
00056 }
00057
00061 public synchronized void disconnect() {
00062 try {
00063 if (socket != null) {
00064 socket.close();
00065 socket = null;
00066 }
00067 } catch (IOException e) {
00068 e.printStackTrace();
00069 }
00070 }
00071
00077 public void invokeRemoteAction(R3Action action) {
00078 if (messengerOutput != null) {
00079 messengerOutput.sendAction(action);
00080 } else {
00081
00082
00083 DebugUtils.println("Action Sender's Messenger is NULL!");
00084 }
00085 }
00086
00087 }