11package cs455 .scaling .server ;
22
3+ import cs455 .scaling .threadpool .ThreadPoolManager ;
4+ import cs455 .scaling .tasks .ServerTask ;
5+ import java .nio .channels .Selector ;
6+ import java .nio .channels .SelectionKey ;
7+ import java .nio .channels .ServerSocketChannel ;
8+ import java .nio .channels .SocketChannel ;
9+ import java .nio .channels .ClosedChannelException ;
10+ import java .nio .ByteBuffer ;
11+ import java .net .InetSocketAddress ;
12+ import java .io .IOException ;
13+ import java .util .Set ;
14+
315public class Server {
16+
17+ private static boolean debug ;
18+ private static ThreadPoolManager tpm ;
19+ private static ServerSocketChannel ssChannel ;
20+ private static Selector serverSelector ;
21+ //private final int clientInterestSet = SelectionKey.OP_READ | SelectionKey.OP_WRITE;
422
523 public static void main (String [] args ) {
624
@@ -14,11 +32,68 @@ public static void main(String[] args) {
1432
1533 int portNumber = Integer .parseInt (args [0 ]);
1634 int threadPoolSize = Integer .parseInt (args [1 ]);
17- boolean debug = false ;
35+ debug = false ;
1836
1937 if (args .length == 3 )
2038 debug = true ;
39+
40+ tpm = new ThreadPoolManager (threadPoolSize , debug );
2141
42+ try {
43+ s .setupServerSocket (portNumber );
44+ serverSelector = Selector .open ();
45+ s .executeServerLoop ();
46+ } catch (IOException ioe ) {
47+ System .out .println (ioe .getMessage ());
48+ }
49+
50+ }
51+
52+ private void setupServerSocket (int portNumber ) throws IOException {
53+ System .out .println ("Setting up server..." );
54+ ssChannel = ServerSocketChannel .open ();
55+ ssChannel .socket ().bind (new InetSocketAddress (portNumber ));
56+ ssChannel .configureBlocking (false );
57+ }
58+
59+ private void executeServerLoop () throws IOException {
60+ while (true ) {
61+ // Get new sockets
62+ SocketChannel socketChannel = ssChannel .accept ();
63+ // Register new sockets
64+ if (socketChannel != null )
65+ registerIncomingKey (socketChannel );
66+ // Find read ready channels
67+ if (serverSelector .selectNow () > 0 ) {
68+ Set <SelectionKey > selectedKeys = serverSelector .selectedKeys ();
69+
70+ for (SelectionKey key : selectedKeys ) {
71+ if (key .isReadable ()) {
72+ // create and enqueue a task to read message from client
73+ createTask (key );
74+ tpm .assignTask ();
75+ }
76+ selectedKeys .remove (key );
77+ }
78+
79+ }
80+ }
81+ }
82+
83+ private void createTask (SelectionKey key ) throws IOException {
84+ ServerTask task = new ServerTask (key );
85+ tpm .addTaskToQueue (task );
86+ }
87+
88+ private void registerIncomingKey (SocketChannel socketChannel ) throws IOException {
89+ if (debug )
90+ System .out .println ("Registering new client..." );
91+ try {
92+ socketChannel .configureBlocking (false );
93+ SelectionKey key = socketChannel .register (serverSelector , SelectionKey .OP_READ );
94+ } catch (ClosedChannelException cce ) {
95+ System .out .println (cce .getMessage ());
96+ }
2297 }
2398
2499}
0 commit comments