1 |
ajm |
1.1 |
import javax.swing.*; |
2 |
|
|
import javax.swing.border.*; |
3 |
|
|
import java.awt.Color; |
4 |
|
|
import uk.ac.ukc.iscream.util.*; |
5 |
|
|
|
6 |
|
|
import java.awt.*; |
7 |
|
|
import java.awt.event.*; |
8 |
|
|
import java.net.*; |
9 |
|
|
import java.io.*; |
10 |
|
|
import java.util.Date; |
11 |
|
|
import java.text.DateFormat; |
12 |
|
|
import java.util.Locale; |
13 |
|
|
import java.util.HashMap; |
14 |
|
|
import javax.swing.border.*; |
15 |
|
|
|
16 |
|
|
public class ConnectionHandler extends Thread { |
17 |
|
|
|
18 |
|
|
public static final int DONOTHING = 0; |
19 |
|
|
public static final int CONNECT = 1; |
20 |
|
|
public ConnectionHandler(DataPanel data, Queue actionQueue) { |
21 |
|
|
_data = data; |
22 |
|
|
_actionQueue = actionQueue; |
23 |
|
|
_myQueue = _actionQueue.getQueue(); |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
public void run() { |
27 |
|
|
|
28 |
|
|
while(true) { |
29 |
|
|
// we wait for a call... |
30 |
|
|
int action = 0; |
31 |
|
|
try { |
32 |
|
|
action = ((Integer) _actionQueue.get(_myQueue)).intValue(); |
33 |
|
|
} catch (InvalidQueueException e) { |
34 |
|
|
// we 're never going to get this... |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
// examine our action... |
38 |
|
|
// if it was to connect...then we connect... |
39 |
|
|
if(action == CONNECT) { |
40 |
|
|
Object response = null; |
41 |
|
|
response = JOptionPane.showInputDialog(null, "Please enter the name of a server running the I-Scream client interface:", "I-Scream Server", JOptionPane.INFORMATION_MESSAGE); |
42 |
|
|
if (response instanceof String) { |
43 |
|
|
_server = (String) response; |
44 |
|
|
SwingClient.setStatus("Connecting to " + _server); |
45 |
|
|
try { |
46 |
|
|
_controlLink = new Socket(_server, _port); |
47 |
|
|
_inBound = new BufferedReader(new InputStreamReader(_controlLink.getInputStream())); |
48 |
|
|
_outBound = new PrintWriter(_controlLink.getOutputStream()); |
49 |
|
|
SwingClient.setStatus("Connection Established - " + _server); |
50 |
|
|
SwingClient.addMessage("Handshaking"); |
51 |
|
|
SwingClient.addMessage("Getting Configuration"); |
52 |
|
|
// here we will do some connect stuff. |
53 |
|
|
// open another socket |
54 |
|
|
// pass the reader to the data reader...but we won't ;-) |
55 |
|
|
Queue theQueue = new Queue(); |
56 |
|
|
DataReader dataReader = new DataReader(_inBound, theQueue); |
57 |
|
|
_data.setQueue(theQueue); |
58 |
|
|
new Thread(_data).start(); |
59 |
|
|
dataReader.start(); |
60 |
|
|
// finished for us.... |
61 |
|
|
|
62 |
|
|
} catch (IOException e) { |
63 |
|
|
SwingClient.addMessage("Control Link Error: " + e); |
64 |
|
|
SwingClient.setStatus("Disconnected"); |
65 |
|
|
} |
66 |
|
|
} |
67 |
|
|
} |
68 |
|
|
} |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
|
72 |
|
|
SwingClient SwingClient; |
73 |
|
|
DataPanel _data; |
74 |
|
|
Queue _actionQueue; |
75 |
|
|
int _myQueue; |
76 |
|
|
String _server; |
77 |
|
|
int _port = SwingClient.PORT; |
78 |
|
|
Socket _controlLink; |
79 |
|
|
BufferedReader _inBound; |
80 |
|
|
PrintWriter _outBound; |
81 |
|
|
} |