File: NetServerThread.java

package info (click to toggle)
biogenesis 0.8-3.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 864 kB
  • sloc: java: 6,254; xml: 53; makefile: 51; sh: 4
file content (335 lines) | stat: -rw-r--r-- 10,631 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* Copyright (C) 2006-2010  Joan Queralt Molina
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */
package biogenesis;

import java.io.*;
import java.net.*;
import java.util.*;

public class NetServerThread extends Thread {
	protected InetAddress address;
	protected int port;
	protected ServerSocket serverSocket;
	protected Socket listenSocket;
	protected int state;
	protected int netCode = 0;
	protected int receivedMessage;
	protected boolean isActive;
	protected GeneticCode code;
	protected List<Connection> connections = Collections.synchronizedList(new ArrayList<Connection>());
	protected MainWindow mainWindow;
	private ObjectInputStream ois;
	private ObjectOutputStream oos;
	
	public static final int CONNECT = 1;
	public static final int CONNECTED = 2;
	public static final int ALREADY_CONNECTED = 3;
	public static final int TOO_MANY_CONNECTIONS = 4;
	public static final int DISCONNECT = 5;
	public static final int NOT_ACCEPTING_CONNECTIONS = 6;
	public static final int NOT_CONNECTED = 7;
	public static final int SEND_CODE = 8;
	public static final int WAITING_CODE = 9;
	public static final int CODE_RECEIVED = 10;
	public static final int TOO_MANY_CODES = 11;
	public static final int GAME_PAUSED = 12;
	public static final int KEEP_ALIVE = 13;
	public static final int ACK_KEEP_ALIVE = 14;
	public static final int DISCONNECTED = 15;
	public static final int INCOMPATIBLE_PROGRAM_VERSION = 16;
	
	public static final int STATE_DISCONNECTED = 50;
	public static final int STATE_CONNECTED = 51;
	
	public static String messageToString(int message) {
		switch (message) {
		case CONNECT: return "CONNECT"; //$NON-NLS-1$
		case CONNECTED: return "CONNECTED"; //$NON-NLS-1$
		case ALREADY_CONNECTED: return "ALREADY_CONNECTED"; //$NON-NLS-1$
		case TOO_MANY_CONNECTIONS: return "TOO_MANY_CONNECTIONS"; //$NON-NLS-1$
		case DISCONNECT: return "DISCONNECT"; //$NON-NLS-1$
		case NOT_ACCEPTING_CONNECTIONS: return "NOT_ACCEPTING_CONNECTIONS"; //$NON-NLS-1$
		case NOT_CONNECTED: return "NOT_CONNECTED"; //$NON-NLS-1$
		case SEND_CODE: return "SEND_CODE"; //$NON-NLS-1$
		case WAITING_CODE: return "WAITING_CODE"; //$NON-NLS-1$
		case CODE_RECEIVED: return "CODE_RECEIVED"; //$NON-NLS-1$
		case TOO_MANY_CODES: return "TOO_MANY_CODES"; //$NON-NLS-1$
		case GAME_PAUSED: return "GAME_PAUSED"; //$NON-NLS-1$
		case KEEP_ALIVE: return "KEEP_ALIVE"; //$NON-NLS-1$
		case ACK_KEEP_ALIVE: return "ACK_KEEP_ALIVE"; //$NON-NLS-1$
		case DISCONNECTED: return "DISCONNECTED"; //$NON-NLS-1$
		case INCOMPATIBLE_PROGRAM_VERSION: return "INCOMPATIBLE_PROGRAM_VERSION"; //$NON-NLS-1$
		default: return "Non existant code"; //$NON-NLS-1$
		}
	}
	
	public boolean isActive() {
		return isActive;
	}
	
	public NetServerThread(MainWindow app) {
		mainWindow = app;
	}
	
	public List<Connection> getConnections() {
		return connections;
	}
	
	public Connection newConnection(InetAddress remoteAddress, int remotePort) {
		address = remoteAddress;
		port = remotePort;
		Connection c = checkConnectionDuplicity();
		if (c == null) {
			c = new Connection(mainWindow, remoteAddress, remotePort);
			connections.add(c);
		} else 
			return null;
		return c;
	}
	
	private Connection newConnection() {
		Connection c = checkConnectionDuplicity();
		if (c == null) {
			c = new Connection(mainWindow, address, port, netCode);
			connections.add(c);
		} else
			return null;
		return c;
	}
	
	public void removeConnection(Connection c) {
		connections.remove(c);
	}

	public void closeServer() {
		isActive = false;
		// Remove all connections
		Connection c;
		synchronized (connections) {
			for (Iterator<Connection> it = connections.iterator();it.hasNext();) {
				c = it.next();
				c.send(DISCONNECT);
				c.setState(DISCONNECTED);
			}
		}
		connections = Collections.synchronizedList(new ArrayList<Connection>());
	}
	
	@Override
	public void run() {
		isActive = true;
		try {
			serverSocket=new ServerSocket(Utils.LOCAL_PORT);
			mainWindow.setStatusMessage(Messages.getString("T_NET_SERVER_LISTENING_ON_PORT", Integer.toString(Utils.LOCAL_PORT))); //$NON-NLS-1$
		} catch (IOException e) {
			if (e instanceof BindException) {
				mainWindow.setStatusMessage(Messages.getString("T_PORT_ALREADY_IN_USE", Integer.toString(Utils.LOCAL_PORT))); //$NON-NLS-1$
			} else
				e.printStackTrace();
			isActive = false;
		}
		while (isActive) {
			try {
				listenSocket = serverSocket.accept();
				ois = new ObjectInputStream(listenSocket.getInputStream());
				oos = new ObjectOutputStream(listenSocket.getOutputStream());
				receivedMessage = ois.readInt();
				System.out.println(messageToString(receivedMessage));
				switch (receivedMessage) {
				case CONNECT:
					handleConnect();
					break;
				case SEND_CODE:
					handleSendCode();
					break;
				case KEEP_ALIVE:
					handleKeepAlive();
					break;
				case DISCONNECT:
					handleDisconnect();
					break;
				}
				ois.close();
				oos.close();
			} catch (IOException e) {
				System.out.println(e.getMessage());
			} finally {
				if (listenSocket != null)
					try {
						listenSocket.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
			}	
		}
	}
	
	private void handleDisconnect() {
		Connection c = null;
		try {
			netCode = ois.readInt();
			c = checkConnectionNetCode();
			if (c != null && c.getState() == Connection.STATE_CONNECTED) {
				oos.writeInt(DISCONNECTED);
				oos.flush();
				System.out.println("->DISCONNECTED"); //$NON-NLS-1$
				c.setState(Connection.STATE_DISCONNECTED);
			} else {
				System.out.println("->NOT_CONNECTED"); //$NON-NLS-1$
				oos.writeInt(NOT_CONNECTED);
				oos.flush();
			}
		} catch (IOException e) {
			System.out.println("handleDisconnect: "+e.getMessage()); //$NON-NLS-1$
			if (c!=null) {
				c.setState(Connection.STATE_DISCONNECTED);
				System.out.println("Connection closed with "+c.remoteAddress+":"+c.remotePort);  //$NON-NLS-1$//$NON-NLS-2$
			}
		}
	}
	
	private void handleKeepAlive() {
		Connection c = null;
		try {
			netCode = ois.readInt();
			c = checkConnectionNetCode();
			if (c != null && c.getState() == Connection.STATE_CONNECTED) {
				oos.writeInt(ACK_KEEP_ALIVE);
				oos.flush();
				System.out.println("->ACK_KEEP_ALIVE"); //$NON-NLS-1$
			} else {
				System.out.println("->NOT_CONNECTED"); //$NON-NLS-1$
				oos.writeInt(NOT_CONNECTED);
				oos.flush();
			}
		} catch (IOException e) {
			System.out.println("handleKeepAlive: "+e.getMessage()); //$NON-NLS-1$
			if (c!=null) {
				c.setState(Connection.STATE_DISCONNECTED);
				System.out.println("Connection closed with "+c.remoteAddress+":"+c.remotePort); //$NON-NLS-1$ //$NON-NLS-2$
			}
		}
	}
	
	private void handleConnect() {
		try {
			int program_version = ois.readInt();
			port = ois.readInt();
			address = listenSocket.getInetAddress();
			netCode = ois.readInt();
			Connection c = checkConnectionNetCode();
			if (c != null) {
				oos.writeInt(ALREADY_CONNECTED);
				oos.flush();
				System.out.println("->ALREADY_CONNECTED"); //$NON-NLS-1$
			} else {
				if (mainWindow.isAcceptingConnections()) {
					if (connections.size() < Utils.MAX_CONNECTIONS) {
						if (Utils.VERSION == program_version) {
							Connection newConnection = newConnection();
							if (newConnection != null) {
								oos.writeInt(CONNECTED);
								newConnection.setState(Connection.STATE_CONNECTED);
								System.out.println("->CONNECTED"); //$NON-NLS-1$
							} else {
								oos.writeInt(ALREADY_CONNECTED);
								System.out.println("->ALREADY_CONNECTED"); //$NON-NLS-1$
							}
							oos.flush();
						} else {
							oos.writeInt(INCOMPATIBLE_PROGRAM_VERSION);
							oos.flush();
							System.out.println("->INCOMPATIBLE_PROGRAM_VERSION"); //$NON-NLS-1$
						}
					} else {
						oos.writeInt(TOO_MANY_CONNECTIONS);
						oos.flush();
						System.out.println("->TOO_MANY_CONNECTIONS"); //$NON-NLS-1$
					}
				} else {
					oos.writeInt(NOT_ACCEPTING_CONNECTIONS);
					oos.flush();
					System.out.println("->NOT_ACCEPTING_CONNECTIONS"); //$NON-NLS-1$
				}
			}
		} catch (IOException ex) {
			System.out.println(ex.getMessage());
		}
	}
	
	private void handleSendCode() {
		Connection c = null;
		try {
			netCode = ois.readInt();
			c = checkConnectionNetCode();
			if (c != null && c.getState() == Connection.STATE_CONNECTED) {
				oos.writeInt(WAITING_CODE);
				oos.flush();
				System.out.println("->WAITING_CODE"); //$NON-NLS-1$
				code = (GeneticCode) ois.readObject();
				System.out.println("Genetic code"); //$NON-NLS-1$
				oos.writeInt(CODE_RECEIVED);
				oos.flush();
				System.out.println("->CODE_RECEIVED"); //$NON-NLS-1$
				c.inCorridor.receiveOrganism(code);
			} else {
				System.out.println("->NOT_CONNECTED"); //$NON-NLS-1$
				oos.writeInt(NOT_CONNECTED);
				oos.flush();
			}
		} catch (IOException e) {
			System.out.println("handleSendCode: "+e.getMessage()); //$NON-NLS-1$
			if (c!=null) {
				c.setState(Connection.STATE_DISCONNECTED);
				System.out.println("Connection closed with "+c.remoteAddress+":"+c.remotePort);  //$NON-NLS-1$//$NON-NLS-2$
			}
		} catch (ClassNotFoundException e) {
			System.out.println("handleSendCode: "+e.getMessage()); //$NON-NLS-1$
			if (c!=null) {
				c.setState(Connection.STATE_DISCONNECTED);
				System.out.println("Connection closed with "+c.remoteAddress+":"+c.remotePort);  //$NON-NLS-1$//$NON-NLS-2$
			}
		}
	}
	
	private Connection checkConnectionNetCode() {
		Connection c;
		synchronized (connections) {
			for (Iterator<Connection> it = connections.iterator();it.hasNext();) {
				c = it.next();
				if (c.netCode == netCode)
					return c;
			}
		}
		return null;
	}
	
	private Connection checkConnectionDuplicity() {
		Connection c;
		synchronized (connections) {
			for (Iterator<Connection> it = connections.iterator();it.hasNext();) {
				c = it.next();
				if (c.remoteAddress.equals(address) && c.remotePort == port) {
					System.out.println("Duplicated connection"); //$NON-NLS-1$
					return c;
				}
			}
		}
		return null;
	}
}