00001 package edu.stanford.hci.r3.actions.remote; 00002 00003 import java.io.BufferedOutputStream; 00004 import java.io.IOException; 00005 import java.net.Socket; 00006 00007 import edu.stanford.hci.r3.actions.R3Action; 00008 import edu.stanford.hci.r3.util.DebugUtils; 00009 import edu.stanford.hci.r3.util.SystemUtils; 00010 00023 public abstract class ActionMessenger { 00024 00028 protected static final String LINE_SEPARATOR = SystemUtils.LINE_SEPARATOR; 00029 00033 private BufferedOutputStream bos; 00034 00038 private Socket sock; 00039 00043 public ActionMessenger(Socket s) { 00044 try { 00045 sock = s; 00046 bos = new BufferedOutputStream(s.getOutputStream()); 00047 } catch (IOException e) { 00048 e.printStackTrace(); 00049 } 00050 } 00051 00055 public void destroy() { 00056 try { 00057 if (bos != null) { 00058 bos.close(); 00059 bos = null; 00060 } 00061 if (sock != null) { 00062 sock.close(); 00063 sock = null; 00064 } 00065 } catch (IOException ioe) { 00066 DebugUtils.println("Got exception when destroying messenger: " 00067 + ioe.getLocalizedMessage()); 00068 } 00069 } 00070 00077 public abstract byte[] getMessage(R3Action action); 00078 00084 public void sendAction(R3Action action) { 00085 try { 00086 bos.write(getMessage(action)); 00087 bos.flush(); 00088 } catch (IOException e) { 00089 e.printStackTrace(); 00090 } 00091 } 00092 00093 }