thufir

controller

Sep 19th, 2013
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 KB | None | 0 0
  1. package telnet;
  2.  
  3. import game.Context;
  4. import game.RulesForStrategy;
  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.  
  19. public class TelnetConnection implements Observer {
  20.  
  21.     private static Logger log = Logger.getLogger(TelnetConnection.class.getName());
  22.     private TelnetClient telnetClient = new TelnetClient();
  23.     private InputOutput inputOutput = new InputOutput();
  24.     private TelnetEventProcessor parser = new TelnetEventProcessor();
  25.     private RulesForStrategy rules = null;
  26.     private Context context = null;
  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 newData(GameData data) {
  58.         rules = new RulesForStrategy(data);
  59.         context = rules.getContext();
  60.         Deque<GameAction> gameActions = context.executeStrategy();
  61.         while (!gameActions.isEmpty()) {
  62.             GameAction action = gameActions.remove();
  63.             try {
  64.                 sendAction(action);
  65.             } catch (IOException ex) {
  66.             }
  67.         }
  68.     }
  69.  
  70.     @Override
  71.     public void update(Observable o, Object arg) {
  72.         GameData data = null;
  73.         String line = null;
  74.         if (o instanceof InputOutput) {
  75.             if (arg instanceof String) {
  76.                 line = arg.toString();
  77.                 data = parser.parse(line);
  78.                 if (data != null) {
  79.                     log.info("new data\t" + data.toString());
  80.                 }
  81.             } else if (arg instanceof GameData) {
  82.                 newData((GameData) arg);
  83.             } else {
  84.                 log.info("not a i/o arg");
  85.             }
  86.         } else if (o instanceof TelnetEventProcessor) {
  87.             if (arg instanceof GameData) {
  88.                 newData((GameData) arg);
  89.             } else {
  90.                 log.info("not a telnetevent arg");
  91.             }
  92.         }
  93.     }
  94.  
  95.     public static void main(String[] args) {
  96.         new TelnetConnection();
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment