Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

This MUD client is a simpler version of Mudlet in Java. Before going too far with the code for the client, I'm interested in feedback on any design problems with the current organization and data flow.

The targeting, happens in Alias:

package telnet;

import java.util.logging.Logger;

public class Alias {

    private static final Logger log = Logger.getLogger(Alias.class.getName());

    public Alias() {
    }

    public String parse(String line) throws StringIndexOutOfBoundsException {
        char c = line.charAt(0);
        String s = String.valueOf(c);
        if ("/".equals(s)) {
            line = getAlias(line);
        }
        return line;
    }

    private String getAlias(String line) {
        String sentence = line.subSequence(1, line.length()).toString();
        String[] words = sentence.split(" ");
        String action = words[0];
        String target = words[1];

        if ("t".equals(action)) {
            log.info("targetting correct\t\t" + target);
        }
        return "";
    }
}

Once Alias has the targeting data, it will pass that data back to InputOutput:

package telnet;

import player.TelnetEventProcessor;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Observable;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.output.TeeOutputStream;

public class InputOutput extends Observable {

    private static final Logger log = Logger.getLogger(InputOutput.class.getName());
    private Alias a = new Alias();

    public InputOutput() {
    }

    private void readFromConsole(final OutputStream outputStream) {
        Thread read = new Thread() {

            @Override
            public void run() {
                String line;
                byte[] bytes;
                Scanner scanner;
                while (true) {
                    scanner = new Scanner(System.in);
                    line = scanner.nextLine();
                    try {
                        line = a.parse(line);
                    } catch (StringIndexOutOfBoundsException e) {
                        log.fine(line);
                    }
                    log.fine(line);
                    bytes = line.getBytes();
                    try {
                        outputStream.write(bytes);
                        outputStream.write(10);
                        outputStream.flush();
                    } catch (IOException ex) {
                        log.info(ex.toString());
                    }
                }
            }
        };
        read.start();
    }

    private void readInput(final InputStream inputStream) throws FileNotFoundException, IOException {
        Thread readInput = new Thread() {

            @Override
            public void run() {
                char ch = 0;
                int intVal = 0;
                StringBuilder sb = new StringBuilder();
                TelnetEventProcessor rx = new TelnetEventProcessor();

                try {
                    while ((intVal = inputStream.read()) != -1) {
                        ch = (char) intVal;
                        printToConsole(ch);
                        //logToFile(ch);
                        sb.append(ch);
                        if (intVal == 13) {
                            setChanged();
                            notifyObservers(sb.toString());
                            sb = new StringBuilder();
                        }
                    }
                } catch (IOException ex) {
                    Logger.getLogger(InputOutput.class.getName()).log(Level.SEVERE, null, ex);
                }


            }

            private void logToFile(char c) throws IOException {
                String fname = "weather.log";
                File f = new File(fname);
                f.createNewFile();
                try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fname, true)))) {
                    out.print(c);
                    out.flush();
                }
            }

            private void printToConsole(char c) {
                System.out.print(c);
            }
        };
        readInput.start();
    }

    public void readWriteParse(final InputStream inputStream, final OutputStream outputStream) throws FileNotFoundException, IOException {
        readFromConsole(outputStream);
        readInput(inputStream);
    }

    //                TeeOutputStream tee = new TeeOutputStream(inputStream, bis);   
    private void tee(FileOutputStream fos) {
        TeeOutputStream tee = new TeeOutputStream(System.out, fos);

    }
}

which will then send the targeting data back to the controller, TelnetConnection:

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.TelnetEventProcessor;

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 TelnetEventProcessor parser = new TelnetEventProcessor();
    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);
        parser.addObserver(this);
    }

    private void sendAction(GameAction action) throws IOException {
        log.fine(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 TelnetEventProcessor) {
            data = (GameData) arg;
            Deque<GameAction> gameActions = logic.getActions(data);
            sendActions(gameActions);
        }
    }

    public static void main(String[] args) {
        new TelnetConnection();
    }
}
share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.