1 package org.cyberiantiger.mudclient.net;
2
3 import java.io.*;
4 import java.net.*;
5 import org.cyberiantiger.telnet.*;
6 import org.cyberiantiger.mudclient.parser.Parser;
7
8 public class MudConnection extends Thread {
9
10 public static final int DISCONNECTED = 0;
11 public static final int CONNECTING = 1;
12 public static final int CONNECTED = 2;
13 public static final int DISCONNECTING = 3;
14
15 private int status = DISCONNECTED;
16 private String destination;
17 private int port;
18 private TelnetSocket sock;
19 private SimpleTelnetSession session;
20 private Display client;
21 private Parser parser;
22
23 /***
24 * Create a new MudConnection for the specified MudClient to the
25 * destination:port.
26 */
27 public MudConnection(Display client) {
28 this.client = client;
29 this.destination = client.getConfiguration().getHost();
30 this.port = client.getConfiguration().getPort();
31 start();
32 }
33
34 /***
35 * Get the current status of the MudConnection.
36 * TODO: Make this watchable, so that listeners can recieve notification
37 * when the state changes.
38 */
39 public int getStatus() {
40 return status;
41 }
42
43 /***
44 * Connect to the mud.
45 * @return True on a sucessful connection, false otherwise.
46 */
47 public synchronized boolean connect() {
48 if(status == CONNECTED) return true;
49 status = CONNECTING;
50 client.connectionStatusChanged(CONNECTING);
51 session = new SimpleTelnetSession(
52 client.getConfiguration().getTerminalType()
53 ) {
54 protected void serverWillEcho(boolean echo) {
55 client.connectionDoLocalEcho(!echo);
56 }
57 };
58 try {
59 sock = new TelnetSocket(destination, port, session);
60 status = CONNECTED;
61 client.connectionStatusChanged(CONNECTED);
62 notify();
63 return true;
64 } catch(IOException ioe) {
65 ioe.printStackTrace();
66 }
67 status = DISCONNECTED;
68 client.connectionStatusChanged(DISCONNECTED);
69 return false;
70 }
71
72 /***
73 * Disconnect from the mud.
74 * @return True on sucessful disconnection.
75 */
76 public synchronized boolean disconnect() {
77 status = DISCONNECTING;
78 client.connectionStatusChanged(DISCONNECTING);
79 if(sock != null) {
80 try {
81 sock.close();
82 } catch (IOException ioe) {
83 ioe.printStackTrace();
84 }
85 }
86 sock = null;
87 status = DISCONNECTED;
88 client.connectionStatusChanged(DISCONNECTED);
89 return true;
90 }
91
92 /***
93 * Set the window size which is reported to the mud via telnet negotiation.
94 */
95 public void setWindowSize(int width, int height) {
96 if(session != null) {
97 try {
98 session.setWindowSize(width,height);
99 } catch (IOException ioe) {
100 disconnect();
101 }
102 }
103 }
104
105 /***
106 * Get whether or not we should echo user input to the console.
107 */
108 public boolean getLocalEcho() {
109 return false;
110 }
111
112 /***
113 * Set the parser to use to parse output from the mud.
114 */
115 public void setParser(Parser parser) {
116 if(this.parser != null) {
117 synchronized(this.parser) {
118 parser.flush(client.getConsoleWriter());
119 this.parser = parser;
120 }
121 } else {
122 this.parser = parser;
123 }
124 }
125
126 public void command(String text) {
127 if(sock != null) {
128 Writer out = sock.getWriter();
129 if(out != null) {
130 try {
131 out.write(text+'\n');
132 out.flush();
133 } catch (IOException ioe) {
134 ioe.printStackTrace();
135 disconnect();
136 }
137 }
138 }
139 }
140
141 /***
142 * Read bytes from the TelnetSocket [if it's connected], parse, and
143 * deliver the appropriate ConsoleActions to the MudClient.
144 */
145 public void run() {
146 while(true) {
147 while(status != CONNECTED) {
148 synchronized(this) {
149 try {
150 wait();
151 } catch(InterruptedException ie) {
152 break;
153 }
154
155 }
156 }
157 try {
158 while(status == CONNECTED) {
159 int ch = sock.getReader().read();
160 if(ch == -1) {
161 disconnect();
162 } else {
163 if(parser != null) {
164 synchronized(parser) {
165 parser.putChar((char)ch);
166 if(parser.changeParser()) {
167 setParser(parser.getNewParser());
168 } else if(!sock.getReader().ready()) {
169 parser.flush(client.getConsoleWriter());
170 }
171 }
172 }
173 }
174 }
175 } catch (Exception e) {
176 disconnect();
177 e.printStackTrace();
178 }
179 }
180 }
181 }
This page was automatically generated by Maven