|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Server.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
|
|
3 |
VRMoo Server - Virtual Reality Object Oriented MUD Server
|
|
4 |
Copyright (C) 2001 - 2003 VRMoo Development Team
|
|
5 |
|
|
6 |
|
|
7 |
This program is free software; you can redistribute it and/or modify
|
|
8 |
it under the terms of the GNU General Public License as published by
|
|
9 |
the Free Software Foundation; either version 2 of the License, or
|
|
10 |
(at your option) any later version.
|
|
11 |
|
|
12 |
This program is distributed in the hope that it will be useful,
|
|
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 |
GNU General Public License for more details.
|
|
16 |
|
|
17 |
You should have received a copy of the GNU General Public License
|
|
18 |
along with this program; if not, write to the Free Software
|
|
19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
20 |
|
|
21 |
|
|
22 |
For information about VRMoo and its authors, please visit the website:
|
|
23 |
http://www.vrmoo.org/
|
|
24 |
|
|
25 |
*/
|
|
26 |
|
|
27 |
package org.vrmoo.server;
|
|
28 |
|
|
29 |
import java.io.IOException;
|
|
30 |
import java.net.ServerSocket;
|
|
31 |
import java.net.Socket;
|
|
32 |
|
|
33 |
import org.vrmoo.server.client.ClientHandler;
|
|
34 |
import org.vrmoo.server.data.DataManager;
|
|
35 |
|
|
36 |
/**
|
|
37 |
* This class in the main entry point for the VRMoo Server application. The main
|
|
38 |
* method constructs an instance of this class instructing it to listen for
|
|
39 |
* connections on port 9500. This class monitors the specified port for clients
|
|
40 |
* connecting to the server. A thread is started specifically for this task.
|
|
41 |
*
|
|
42 |
* @author Jeff Weston
|
|
43 |
*/
|
|
44 |
public class Server |
|
45 |
{ |
|
46 |
/**
|
|
47 |
* The entry point for the VRMoo Server. This method starts a server thread
|
|
48 |
* listening on the specified port, and then monitors the console window
|
|
49 |
* to determine when to exit the application.
|
|
50 |
*
|
|
51 |
* @param args the command-line arguments
|
|
52 |
*/
|
|
53 | 0 |
public static void main( String[ ] args ) |
54 |
{ |
|
55 | 0 |
int iInput;
|
56 |
|
|
57 | 0 |
printCopyright( ); |
58 |
|
|
59 | 0 |
int port = 9500;
|
60 |
|
|
61 | 0 |
if ( args.length == 1 )
|
62 |
{ |
|
63 | 0 |
try
|
64 |
{ |
|
65 | 0 |
port = Integer.parseInt( args[ 0 ] ); |
66 |
} |
|
67 |
catch ( NumberFormatException e )
|
|
68 |
{ |
|
69 | 0 |
System.out.println( "The specified port is not a number." );
|
70 | 0 |
printUsage( ); |
71 | 0 |
return;
|
72 |
} |
|
73 |
} |
|
74 |
|
|
75 | 0 |
if ( args.length > 1 )
|
76 |
{ |
|
77 | 0 |
System.out.println( "Too many arguments." );
|
78 | 0 |
printUsage( ); |
79 | 0 |
return;
|
80 |
} |
|
81 |
|
|
82 | 0 |
new Server( port );
|
83 |
|
|
84 | 0 |
while ( true ) |
85 |
{ |
|
86 | 0 |
try
|
87 |
{ |
|
88 | 0 |
iInput = System.in.read( ); |
89 |
} |
|
90 |
catch ( IOException e )
|
|
91 |
{ |
|
92 | 0 |
iInput = -2; |
93 |
} |
|
94 | 0 |
if ( iInput == 113 || iInput < 0 )
|
95 |
{ |
|
96 | 0 |
System.out.println( ); |
97 | 0 |
System.out.println( "VRMoo Sever is exiting..." );
|
98 | 0 |
DataManager.saveData( ); |
99 | 0 |
System.exit( 0 ); |
100 |
} |
|
101 | 0 |
System.out.println( "You typed: " + iInput );
|
102 |
} |
|
103 |
} |
|
104 |
|
|
105 |
/**
|
|
106 |
* Prints out the VRMoo Server copyright header to the console.
|
|
107 |
*/
|
|
108 | 0 |
private static void printCopyright( ) |
109 |
{ |
|
110 | 0 |
System.out.println( |
111 |
"VRMoo Server, Copyright (C) 2001 - 2003 " +
|
|
112 |
"VRMoo Development Team" );
|
|
113 | 0 |
System.out.println( |
114 |
"VRMoo Server comes with ABSOLUTELY NO WARRANTY." );
|
|
115 | 0 |
System.out.println( ); |
116 | 0 |
System.out.println( |
117 |
"This is free software, and you are welcome to redistribute" );
|
|
118 | 0 |
System.out.println( |
119 |
"it under certain conditions; see LICENSE.txt for details." );
|
|
120 | 0 |
System.out.println( ); |
121 |
} |
|
122 |
|
|
123 |
/**
|
|
124 |
* Prints out usage information to the console.
|
|
125 |
*/
|
|
126 | 0 |
private static void printUsage( ) |
127 |
{ |
|
128 | 0 |
System.out.println( |
129 |
"usage: server [<port>]" );
|
|
130 | 0 |
System.out.println( ); |
131 | 0 |
System.out.println( |
132 |
"<port> - the port to listen for connections on" );
|
|
133 | 0 |
System.out.println( |
134 |
" (default is 9500)" );
|
|
135 |
} |
|
136 |
|
|
137 |
/**
|
|
138 |
* The port to monitor.
|
|
139 |
*/
|
|
140 |
private int iPort; |
|
141 |
|
|
142 |
/**
|
|
143 |
* This constructor starts a new thread listening for connections on the
|
|
144 |
* specified port.
|
|
145 |
*
|
|
146 |
* @param iListenPort the port to monitor
|
|
147 |
*/
|
|
148 | 0 |
public Server( int iListenPort ) |
149 |
{ |
|
150 | 0 |
DataManager.init( ); |
151 | 0 |
iPort = iListenPort; |
152 | 0 |
new Thread( new Loop( ), "Server" ).start( ); |
153 |
} |
|
154 |
|
|
155 |
/**
|
|
156 |
* Private inner class implementing Runnable to make this class thread-safe.
|
|
157 |
* The thread that is started can only be called from the constructor.
|
|
158 |
*/
|
|
159 |
private class Loop implements Runnable |
|
160 |
{ |
|
161 |
/**
|
|
162 |
* Continuously checks the specified port for connections. When a
|
|
163 |
* connection is detected, a <code>ClientReader</code> and a
|
|
164 |
* <code>ClientWriter</code> are started up with their own threads to
|
|
165 |
* deal with the new client.
|
|
166 |
*/
|
|
167 | 0 |
public void run( ) |
168 |
{ |
|
169 | 0 |
ServerSocket serverSocket; |
170 | 0 |
Socket clientSocket; |
171 |
|
|
172 | 0 |
System.out.println( ); |
173 | 0 |
System.out.println( "Attempting to listen on port " + iPort + "." ); |
174 |
|
|
175 | 0 |
serverSocket = null;
|
176 |
|
|
177 | 0 |
try
|
178 |
{ |
|
179 | 0 |
serverSocket = new ServerSocket( iPort );
|
180 |
} |
|
181 |
catch ( IOException e )
|
|
182 |
{ |
|
183 | 0 |
System.out.println( "Could not listen on port " + iPort );
|
184 | 0 |
e.printStackTrace( ); |
185 | 0 |
return;
|
186 |
} |
|
187 |
|
|
188 | 0 |
while ( true ) |
189 |
{ |
|
190 | 0 |
try
|
191 |
{ |
|
192 | 0 |
clientSocket = serverSocket.accept( ); |
193 | 0 |
new ClientHandler( clientSocket );
|
194 |
} |
|
195 |
catch ( IOException e )
|
|
196 |
{ |
|
197 | 0 |
System.out.println( "Accept failed." );
|
198 | 0 |
e.printStackTrace( ); |
199 |
} |
|
200 |
} |
|
201 |
} |
|
202 |
} |
|
203 |
} |
|
204 |
|
|