thufir

data changes frequently; strategy depends upon data content

Sep 18th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. package telnet;
  2.  
  3. import game.LogicalContext;
  4. import game.TargetStrategy;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.OutputStream;
  8. import java.net.InetAddress;
  9. import java.net.SocketException;
  10. import java.util.Deque;
  11. import java.util.Observable;
  12. import java.util.Observer;
  13. import java.util.Properties;
  14. import java.util.logging.Logger;
  15. import org.apache.commons.net.telnet.TelnetClient;
  16. import model.GameAction;
  17. import model.GameData;
  18. import model.TelnetEventProcessor;
  19.  
  20. public class TelnetConnection implements Observer {
  21.  
  22.     private static Logger log = Logger.getLogger(TelnetConnection.class.getName());
  23.     private TelnetClient telnetClient = new TelnetClient();
  24.     private InputOutput inputOutput = new InputOutput();
  25.     private TelnetEventProcessor parser = new TelnetEventProcessor();
  26.     private LogicalContext logic;// = new LogicalContext();
  27.  
  28.     public TelnetConnection() {
  29.         try {
  30.             init();
  31.         } catch (SocketException ex) {
  32.         } catch (FileNotFoundException ex) {
  33.         } catch (IOException ex) {
  34.         }
  35.     }
  36.  
  37.     private void init() throws SocketException, FileNotFoundException, IOException {
  38.         Properties props = PropertiesReader.getProps();
  39.         InetAddress host = InetAddress.getByName(props.getProperty("host"));
  40.         int port = Integer.parseInt(props.getProperty("port"));
  41.         telnetClient.connect(host, port);
  42.         inputOutput.readWriteParse(telnetClient.getInputStream(), telnetClient.getOutputStream());
  43.         inputOutput.addObserver(this);
  44.         parser.addObserver(this);
  45.     }
  46.  
  47.     private void sendAction(GameAction action) throws IOException {
  48.         log.fine(action.toString());
  49.         byte[] actionBytes = action.getAction().getBytes();
  50.         OutputStream outputStream = telnetClient.getOutputStream();
  51.         outputStream.write(actionBytes);
  52.         outputStream.write(13);
  53.         outputStream.write(10);
  54.         outputStream.flush();
  55.     }
  56.  
  57.     private void sendActions(Deque<GameAction> gameActions) {
  58.         while (!gameActions.isEmpty()) {
  59.             GameAction action = gameActions.remove();
  60.             try {
  61.                 sendAction(action);
  62.             } catch (IOException ex) {
  63.             }
  64.         }
  65.     }
  66.  
  67.     @Override
  68.     public void update(Observable o, Object arg) {
  69.         GameData data = null;
  70.         String line = null;
  71.         Deque<GameAction> gameActions;
  72.         if (o instanceof InputOutput) {
  73.             if (arg instanceof String) {
  74.                 line = arg.toString();
  75.                 parser.parse(line);
  76.             } else if (arg instanceof GameData) {
  77.                 data = (GameData) arg;
  78.                 logic = new LogicalContext(new TargetStrategy());
  79.                 logic.setGameData(data);  //data changes frequently!
  80.                 //strategy depends on data
  81.                 //put all this in another class?
  82.                 gameActions = logic.executeStrategy();
  83.                 sendActions(gameActions);
  84.             } else {
  85.                 log.info("not a i/o arg");
  86.             }
  87.         } else if (o instanceof TelnetEventProcessor) {
  88.             if (arg instanceof GameData) {
  89.                 log.info("game data arg");
  90.                 data = (GameData) arg;
  91.                 gameActions = logic.executeStrategy();
  92.                 sendActions(gameActions);
  93.             } else {
  94.                 log.info("not a telnetevent arg");
  95.             }
  96.         }
  97.     }
  98.  
  99.     public static void main(String[] args) {
  100.         new TelnetConnection();
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment