Rev 15 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 15 | Rev 73 | ||
|---|---|---|---|
| 1 | /* |
1 | /* |
| 2 | * TcpServerThread.java |
2 | * TcpServerThread.java |
| 3 | * |
3 | * |
| 4 | * Created on den 25 augusti 2003, 22:50 |
4 | * Created on den 25 augusti 2003, 22:50 |
| 5 | */ |
5 | */ |
| 6 | package Macbeth.Net; |
6 | package Macbeth.Net; |
| 7 | 7 | ||
| 8 | import java.net.*; |
8 | import java.net.*; |
| 9 | import java.io.*; |
9 | import java.io.*; |
| 10 | import java.util.List; |
10 | import java.util.List; |
| 11 | 11 | ||
| 12 | /** |
12 | /** |
| 13 | * Server thread for the TcpServer. |
13 | * Server thread for the TcpServer. |
| 14 | * Such a thread will be created for each connected |
14 | * Such a thread will be created for each connected |
| 15 | * client. |
15 | * client. |
| 16 | * @author Jimmy |
16 | * @author Jimmy |
| 17 | */ |
17 | */ |
| 18 | public class TcpServerThread extends Thread { |
18 | public class TcpServerThread extends Thread { |
| 19 | private Socket socket; |
19 | private Socket socket; |
| 20 | private ServerDataHandler dataHandler; |
20 | private ServerDataHandler dataHandler; |
| 21 | private BufferedReader stringInput = null; |
21 | private BufferedReader stringInput = null; |
| 22 | private PrintWriter stringOutput = null; |
22 | private PrintWriter stringOutput = null; |
| 23 | boolean run; |
23 | boolean run; |
| 24 | 24 | ||
| 25 | //reference to the list of server threads (this list is maintained in the TcpServer-class |
25 | //reference to the list of server threads (this list is maintained in the TcpServer-class |
| 26 | private List serverThreads; |
26 | private List serverThreads; |
| 27 | 27 | ||
| 28 | 28 | ||
| 29 | /** |
29 | /** |
| 30 | * Creates a new instance of TcpServerThread. |
30 | * Creates a new instance of TcpServerThread. |
| 31 | * @param socket The socket associated with the client we're serving. |
31 | * @param socket The socket associated with the client we're serving. |
| 32 | * @param datahandler The data handler that should be invoked when the client is sending us data. |
32 | * @param datahandler The data handler that should be invoked when the client is sending us data. |
| 33 | * @param serverThreads The list of server threads maintained in TcpServer. The server thread needs |
33 | * @param serverThreads The list of server threads maintained in TcpServer. The server thread needs |
| 34 | * to be able to delete itself from this list when it has finished running. |
34 | * to be able to delete itself from this list when it has finished running. |
| 35 | * @param TcpNoDelay Sets if socket use TcpNoDelay |
35 | * @param TcpNoDelay Sets if socket use TcpNoDelay |
| 36 | **/ |
36 | **/ |
| 37 | public TcpServerThread(Socket socket, ServerDataHandler datahandler, List serverThreads, boolean TcpNoDelay) { |
37 | public TcpServerThread(Socket socket, ServerDataHandler datahandler, List serverThreads, boolean TcpNoDelay) { |
| 38 | super("TcpServerThread"); |
38 | super("TcpServerThread"); |
| 39 | this.socket = socket; |
39 | this.socket = socket; |
| 40 | this.dataHandler = datahandler; |
40 | this.dataHandler = datahandler; |
| 41 | this.serverThreads = serverThreads; |
41 | this.serverThreads = serverThreads; |
| 42 | try { |
42 | try { |
| 43 | this.socket.setTcpNoDelay(TcpNoDelay); |
43 | this.socket.setTcpNoDelay(TcpNoDelay); |
| 44 | } |
44 | } |
| 45 | catch (SocketException e) { |
45 | catch (SocketException e) { |
| 46 | System.out.println("Socket error: " + e); |
46 | System.out.println("Socket error: " + e); |
| 47 | } |
47 | } |
| 48 | } |
48 | } |
| 49 | 49 | ||
| 50 | /** |
50 | /** |
| 51 | * Starts up the server thread. |
51 | * Starts up the server thread. |
| 52 | */ |
52 | */ |
| 53 | public void startThread() { |
53 | public void startThread() { |
| 54 | try { |
54 | try { |
| 55 | //get input stream from socket |
55 | //get input stream from socket |
| 56 | stringInput = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
56 | stringInput = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
| 57 | //create output stream |
57 | //create output stream |
| 58 | stringOutput = new PrintWriter(socket.getOutputStream()); |
58 | stringOutput = new PrintWriter(socket.getOutputStream()); |
| 59 | //timeout creates a pause in readline so that one can test if client is still connected |
59 | //timeout creates a pause in readline so that one can test if client is still connected |
| 60 | socket.setSoTimeout(35000); |
60 | socket.setSoTimeout(35000); |
| 61 | //does this do anything |
61 | //does this do anything |
| 62 | //socket.setKeepAlive(true); |
62 | //socket.setKeepAlive(true); |
| 63 | } catch (IOException e) { |
63 | } catch (IOException e) { |
| 64 | e.printStackTrace(); |
64 | e.printStackTrace(); |
| 65 | return; |
65 | return; |
| 66 | } |
66 | } |
| 67 | //start new server thread |
67 | //start new server thread |
| 68 | run = true; |
68 | run = true; |
| 69 | super.start(); |
69 | super.start(); |
| 70 | } |
70 | } |
| 71 | 71 | ||
| 72 | /** |
72 | /** |
| 73 | * Stops the server thread. |
73 | * Stops the server thread. |
| 74 | */ |
74 | */ |
| 75 | public void stopThread() { |
75 | public void stopThread() { |
| 76 | if (run) { |
76 | if (run) { |
| 77 | run = false; |
77 | run = false; |
| 78 | try { |
78 | try { |
| 79 | if (stringInput!=null) stringInput.close(); |
79 | if (stringInput!=null) stringInput.close(); |
| 80 | socket.close(); |
80 | socket.close(); |
| 81 | serverThreads.remove(this); |
81 | serverThreads.remove(this); |
| 82 | } catch (IOException e) { |
82 | } catch (IOException e) { |
| 83 | e.printStackTrace(); |
83 | e.printStackTrace(); |
| 84 | } |
84 | } |
| 85 | } |
85 | } |
| 86 | } |
86 | } |
| 87 | /** |
87 | /** |
| 88 | * This will send data to this client. |
88 | * This will send data to this client. |
| 89 | * @param dataToSend String line to send to this client. |
89 | * @param dataToSend String line to send to this client. |
| 90 | */ |
90 | */ |
| 91 | public void sendData(String dataToSend){ |
91 | public void sendData(String dataToSend){ |
| 92 | //borde inte denna generera exception då man skriver till en socket som inte är ansluten? |
92 | //borde inte denna generera exception då man skriver till en socket som inte är ansluten? |
| 93 | stringOutput.println(dataToSend); |
93 | stringOutput.println(dataToSend); |
| 94 | stringOutput.flush(); |
94 | stringOutput.flush(); |
| 95 | } |
95 | } |
| 96 | 96 | ||
| 97 | /** |
97 | /** |
| 98 | * Allows application to ask for this clients address. |
98 | * Allows application to ask for this clients address. |
| 99 | * @return The address of this client. |
99 | * @return The address of this client. |
| 100 | */ |
100 | */ |
| 101 | public String getAddress() { |
101 | public String getAddress() { |
| 102 | return socket.getRemoteSocketAddress().toString(); |
102 | return socket.getRemoteSocketAddress().toString(); |
| 103 | } |
103 | } |
| 104 | 104 | ||
| 105 | /** |
105 | /** |
| 106 | * This will be called from the thread. |
106 | * This will be called from the thread. |
| 107 | */ |
107 | */ |
| 108 | public void run() { |
108 | public void run() { |
| 109 | //tell the datahandler from whom we're receiving data |
109 | //tell the datahandler from whom we're receiving data |
| 110 | dataHandler.setRemoteAddress(socket.getRemoteSocketAddress().toString()); |
110 | dataHandler.setRemoteAddress(socket.getRemoteSocketAddress().toString()); |
| 111 | 111 | ||
| 112 | try { |
112 | try { |
| 113 | //do we have a string input stream? |
113 | //do we have a string input stream? |
| 114 | if (stringInput!=null) { |
114 | if (stringInput!=null) { |
| 115 | //read lines from this stream until connection is closed |
115 | //read lines from this stream until connection is closed |
| 116 | String line; |
116 | String line; |
| 117 | while (run) { |
117 | while (run) { |
| 118 | /* |
118 | /* |
| 119 | //if input buffer is not empty |
119 | //if input buffer is not empty |
| 120 | if (stringInput.ready()) { |
120 | if (stringInput.ready()) { |
| 121 | //read next line |
121 | //read next line |
| 122 | line=stringInput.readLine(); |
122 | line=stringInput.readLine(); |
| 123 | //and let the data handler handle this new received line |
123 | //and let the data handler handle this new received line |
| 124 | dataHandler.handleServerData(line); |
124 | dataHandler.handleServerData(line); |
| 125 | //else, if input buffer is empty |
125 | //else, if input buffer is empty |
| 126 | } else { |
126 | } else { |
| 127 | //get out of the loop |
127 | //get out of the loop |
| 128 | break; |
128 | break; |
| 129 | }*/ |
129 | }*/ |
| 130 | 130 | ||
| 131 | //read next line |
131 | //read next line |
| 132 | try { |
132 | try { |
| 133 | line = stringInput.readLine(); |
133 | line = stringInput.readLine(); |
| 134 | //gör om denna så den läser tecken för tecken tills ett förinställt termineringstecken dyker upp |
134 | //gör om denna så den läser tecken för tecken tills ett förinställt termineringstecken dyker upp |
| 135 | 135 | ||
| 136 | } catch (SocketTimeoutException e) { |
136 | } catch (SocketTimeoutException e) { |
| 137 | //don't care...fully normal since we have a timeout set |
137 | //don't care...fully normal since we have a timeout set |
| 138 | 138 | ||
| 139 | //detta är socketens timeout |
139 | //detta är socketens timeout |
| 140 | //här borde man alltså kolla om det verkligen finns någon på andra sidan |
140 | //här borde man alltså kolla om det verkligen finns någon på andra sidan |
| 141 | //System.out.println("Timeout: " + e); |
141 | //System.out.println("Timeout: " + e); |
| 142 | //e.printStackTrace(); |
142 | //e.printStackTrace(); |
| 143 | //continue; |
143 | //continue; |
| 144 | break; |
144 | break; |
| 145 | } catch (SocketException e) { |
145 | } catch (SocketException e) { |
| 146 | //koppla ner socketen är väl det enda rätta? |
146 | //koppla ner socketen är väl det enda rätta? |
| 147 | //System.out.println("Disconnect: " + e); |
147 | //System.out.println("Disconnect: " + e); |
| 148 | break; |
148 | break; |
| 149 | } |
149 | } |
| 150 | //line = null; |
150 | //line = null; |
| 151 | if (line!=null) { |
151 | if (line!=null) { |
| 152 | dataHandler.handleServerData(line, socket.getRemoteSocketAddress().toString()); |
152 | dataHandler.handleServerData(line, socket.getRemoteSocketAddress().toString()); |
| 153 | } |
153 | } |
| 154 | //until end of stream has been reached |
154 | //until end of stream has been reached |
| 155 | else { |
155 | else { |
| 156 | dataHandler.clientDropped(socket.getRemoteSocketAddress().toString()); |
156 | dataHandler.clientDropped(socket.getRemoteSocketAddress().toString()); |
| 157 | break; |
157 | break; |
| 158 | } |
158 | } |
| 159 | } |
159 | } |
| 160 | } |
160 | } |
| 161 | } catch (IOException e) { |
161 | } catch (IOException e) { |
| 162 | e.printStackTrace(); |
162 | e.printStackTrace(); |
| 163 | } finally { |
163 | } finally { |
| 164 | //stop the thread and close the socket |
164 | //stop the thread and close the socket |
| 165 | stopThread(); |
165 | stopThread(); |
| 166 | } |
166 | } |
| 167 | } |
167 | } |
| 168 | } |
168 | } |