Source code: jcsq/CSServer.java
1 package jcsq;
2
3 import java.net.DatagramPacket;
4 import java.net.DatagramSocket;
5 import java.net.SocketException;
6 import java.net.UnknownHostException;
7 import java.net.InetAddress;
8 import java.util.Vector;
9 import java.util.Arrays;
10
11 import jcsq.serverinfo.*;
12 import jcsq.player.*;
13 import jcsq.util.*;
14 import jcsq.rules.*;
15
16
17
18 /**
19 * This represent the CS-Server. It will return objects that contains data regarding, server, players and rules.
20 *
21 *@author Per Frödeberg, per@abelbaker.se
22 */
23
24 public class CSServer implements Cloneable{
25
26
27 private String ip;
28 private int port;
29 private InetAddress host;
30 private DatagramPacket sendPacket;
31 private DatagramPacket receivePacket;
32 private DatagramSocket socket;
33 private int timeout = 10000;
34 private byte[] buffer = new byte[900];
35 private Vector playerList = null;
36 private byte playerCount = 0;
37
38
39 private static final String INFO_STRING = "info";
40 private static final int INFO_STRING_LENGTH = INFO_STRING.getBytes().length;
41
42 private static final String PLAYERS_STRING = "players";
43 private static final int PLAYERS_STRING_LENGTH = PLAYERS_STRING.getBytes().length;
44
45 private static final String RULES_STRING = "rules";
46 private static final int RULES_STRING_LENGTH = RULES_STRING.getBytes().length;
47
48 private static final String PING_STRING = "ping";
49 private static final int PING_STRING_LENGTH = PING_STRING.getBytes().length;
50
51 /**
52 * Creates a CS-Server with specified host.
53 * host should be formatted: <host>:<port>
54 *
55 */
56 public CSServer(String host) throws SocketException,CSPortException,UnknownHostException{
57 splitHost(host);
58 try{
59 this.host = InetAddress.getByName(this.ip);
60 }
61 catch(Exception ce){
62 throw new UnknownHostException("Unable to find host");
63 }
64
65 try{
66 this.socket = new DatagramSocket();
67 this.socket.setSoTimeout(5000);
68 }
69 catch(Exception e){
70 throw new SocketException("Unable to bind port: "+this.port);
71 }
72
73
74 }
75
76 /**
77 * Creates a CS-Server with specified host and socket.
78 * Host should be formatted: <host>:<port>
79 *
80 */
81 public CSServer(String host, DatagramSocket socket) throws SocketException,CSPortException,UnknownHostException{
82 splitHost(host);
83 try{
84 this.host = InetAddress.getByName(this.ip);
85 }
86 catch(Exception ce){
87 throw new UnknownHostException("Unable to find host: "+this.ip);
88 }
89
90 try{
91 this.socket = socket;
92 this.socket.setSoTimeout(5000);
93 }
94 catch(Exception e){
95 throw new SocketException("Unable to bind port: "+this.port);
96 }
97
98
99 }
100 /**
101 * Creates a CS-Server with specified host and port (OBS! This port is not port on the server you are connecting to, it´s the port from it will
102 * open it from.).
103 * Host should be formatted: <host>:<port>
104 */
105 public CSServer(String host, int port) throws SocketException,CSPortException,UnknownHostException{
106 splitHost(host);
107 try{
108 this.host = InetAddress.getByName(this.ip);
109 }
110 catch(Exception ce){
111 throw new UnknownHostException("Unable to find host: "+this.ip);
112 }
113
114 try{
115 this.socket = new DatagramSocket(port);
116 this.socket.setSoTimeout(5000);
117 }
118 catch(Exception e){
119 throw new UnknownHostException("Unable to find host: "+this.ip);
120 }
121
122
123
124 }
125
126 /**
127 * Closes the server, after this you will not be able to call the getPlayers, getRules methods-
128 */
129 public void closeServer(){
130 this.socket.close();
131 this.socket = null;
132 }
133
134 /**
135 Clones (makes an exact copy) of the object
136 */
137 public Object clone()
138 {
139 try{
140 CSServer cloned = (CSServer)super.clone();
141 return cloned;
142 }
143 catch( CloneNotSupportedException e)
144 {
145 return null;
146 }
147 }
148
149 private InfoPacketStripper getInfo() {//throws Exception{
150
151 InfoPacketStripper inf;
152 DatagramPacket sendPacket = CSPacket.getDatagramPacket(INFO_STRING,this.host,this.port);
153
154 byte[] buffer = new byte[255];
155 Arrays.fill(buffer,(byte)0);
156 try{
157 socket.send(sendPacket);
158 DatagramPacket receivePacket = new DatagramPacket(buffer,buffer.length);
159 receivePacket.setPort(port);
160 socket.receive(receivePacket);
161 byte[] dataFromServer = receivePacket.getData();
162 inf = new InfoPacketStripper(dataFromServer);
163 dataFromServer = null;
164 }
165 catch(Exception e){
166 byte ti = -9;
167 byte[] tmp = {ti};
168 inf = new InfoPacketStripper(tmp);
169 inf.setTimedOut(true);
170
171
172 }
173
174 return inf;
175 }
176 /**
177 * Returns a ServerInfo object
178 */
179 public ServerInfo getServerInfo() throws Exception{
180
181 InfoPacketStripper inf = getInfo();
182 ServerInfo srv = inf.getServerInfo();
183 srv.setIp(this.ip);
184 return srv;
185
186 }
187 public Vector getPlayerList() {
188 byte[] buffer = new byte[900];
189 try{
190 DatagramPacket sendPacket = CSPacket.getDatagramPacket(PLAYERS_STRING,this.host,this.port);
191 socket.send(sendPacket);
192 DatagramPacket receivePacket = new DatagramPacket(buffer,buffer.length);
193 receivePacket.setPort(port);
194 socket.receive(receivePacket);
195
196 byte[] dataFromServer = receivePacket.getData();
197
198 PlayerPacketStripper pl = new PlayerPacketStripper(dataFromServer);
199 this.playerCount = pl.getPlayerCount();
200 return pl.getPlayerList();
201 }
202
203 catch(Exception e){
204 e.printStackTrace();
205 return null;
206 }
207
208
209 }
210 public Rules getServerRules(){
211 byte buffer[] = new byte[2000];
212 try{
213 DatagramPacket sendPacket = CSPacket.getDatagramPacket(RULES_STRING,this.host,this.port);
214 socket.send(sendPacket);
215 DatagramPacket receivePacket = new DatagramPacket(buffer,buffer.length);
216 receivePacket.setPort(port);
217 socket.receive(receivePacket);
218
219 byte[] dataFromServer = receivePacket.getData();
220 RulesPacketStripper pl = new RulesPacketStripper(dataFromServer);
221 return pl.getServerRules();
222 }
223
224 catch(Exception e){
225 e.printStackTrace();
226 return null;
227 }
228
229 }
230
231 public long ping(){
232 long t1;
233 long t2;
234 try{
235 DatagramPacket sendPacket = CSPacket.getDatagramPacket(PING_STRING,this.host,this.port);
236 t1 = System.currentTimeMillis();
237 socket.send(sendPacket);
238 DatagramPacket receivePacket = new DatagramPacket(buffer,buffer.length);
239 receivePacket.setPort(port);
240 socket.receive(receivePacket);
241 t2 = System.currentTimeMillis() - t1;
242 byte[] dataFromServer = receivePacket.getData();
243 return t2;
244
245 }
246 catch(Exception e){
247 t1 = -1;
248 return t1;
249 }
250
251
252 }
253
254
255
256 private void splitHost(String ip_port) throws CSPortException{
257 int split = ip_port.indexOf(":");
258 if(split<1){
259 this.port = 27015;
260 this.ip = ip_port;
261 }
262 else{
263 this.ip = ip_port.substring(0,split);
264 try{
265 this.port = Integer.parseInt(ip_port.substring(split+1,ip_port.length() ));
266 }
267 catch(Exception e){
268 throw new CSPortException(ip_port);
269 }
270 }
271
272 }
273
274
275 public void setTimeout(int timeout){
276 this.timeout = timeout;
277 }
278 public int getTimeout(){
279 return this.timeout;
280 }
281
282 public byte getPlayerCount(){
283 return this.playerCount;
284 }
285 public String getIp(){
286 return this.ip;
287 }
288
289
290
291 }