Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- running the program, output:
- 482 636 [n,s,w,e]say confusing the hell out of bob.
- Sep 16, 2013 8:06:07 AM player.GameData <init>
- INFO: bob
- Sep 16, 2013 8:06:07 AM player.TelnetParser ifs
- INFO: new data object bob
- 482 636 [n,s,w,e]
- TelnetParser seems to create a GameData object, but that object never ?seems? to make it to the Observer.
- package player;
- import java.util.Observable;
- import java.util.logging.Logger;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class TelnetParser extends Observable {
- private static Logger log = Logger.getLogger(TelnetParser.class.getName());
- private String string = null;
- public TelnetParser() {
- }
- private void stripAnsiColors() {
- Pattern regex = Pattern.compile("\\e\\[[0-9;]*m");
- Matcher regexMatcher = regex.matcher(string);
- string = regexMatcher.replaceAll(""); // *3 ??
- }
- public void parse(String string) {
- this.string = string;
- ifs();
- }
- // [\w]+(?=\.)
- private void ifs() {
- log.fine("checking..");
- if (string.contains("confusing the hell out of")) {
- Pattern pattern = Pattern.compile("[\\w]+(?=\\.)"); //(\w+)\.
- Matcher matcher = pattern.matcher(string);
- String enemy = null;
- GameData data = null;
- while (matcher.find()) {
- enemy = matcher.group();
- }
- data = new GameData.Builder().enemy(enemy).build();
- log.info("new data object\t\t" + data.getEnemy());
- setChanged();
- notifyObservers(data);
- } else if (string.contains("Enter 3-letter city code:")) {
- log.fine("found enter city code");
- } else {
- }
- }
- }
- package telnet;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.InetAddress;
- import java.net.SocketException;
- import java.util.Deque;
- import java.util.Observable;
- import java.util.Observer;
- import java.util.Properties;
- import java.util.logging.Logger;
- import org.apache.commons.net.telnet.TelnetClient;
- import player.GameAction;
- import player.GameData;
- import player.TelnetParser;
- public class TelnetConnection implements Observer {
- private static Logger log = Logger.getLogger(TelnetConnection.class.getName());
- private TelnetClient telnetClient = new TelnetClient();
- private InputOutput inputOutput = new InputOutput();
- private TelnetParser parser = new TelnetParser();
- private Logic logic = new Logic();
- public TelnetConnection() {
- try {
- init();
- } catch (SocketException ex) {
- } catch (FileNotFoundException ex) {
- } catch (IOException ex) {
- }
- }
- private void init() throws SocketException, FileNotFoundException, IOException {
- Properties props = PropertiesReader.getProps();
- InetAddress host = InetAddress.getByName(props.getProperty("host"));
- int port = Integer.parseInt(props.getProperty("port"));
- telnetClient.connect(host, port);
- inputOutput.readWriteParse(telnetClient.getInputStream(), telnetClient.getOutputStream());
- inputOutput.addObserver(this);
- }
- private void sendAction(GameAction action) throws IOException {
- log.info(action.toString());
- byte[] actionBytes = action.getAction().getBytes();
- OutputStream outputStream = telnetClient.getOutputStream();
- outputStream.write(actionBytes);
- outputStream.write(13);
- outputStream.write(10);
- outputStream.flush();
- }
- private void sendActions(Deque<GameAction> gameActions) {
- while (!gameActions.isEmpty()) {
- GameAction action = gameActions.remove();
- try {
- sendAction(action);
- } catch (IOException ex) {
- }
- }
- }
- @Override
- public void update(Observable o, Object arg) {
- GameData data = null;
- String line = null;
- if (o instanceof InputOutput) {
- line = arg.toString();
- parser.parse(line);
- } else if (o instanceof TelnetParser) {
- log.info("trying..");
- data = (GameData) arg;
- log.info("hmm, never get here");
- Deque<GameAction> gameActions = logic.getActions(data);
- sendActions(gameActions);
- }
- }
- public static void main(String[] args) {
- new TelnetConnection();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment