Mercurial > hg > blitz_stable
comparison src/org/dancres/blitz/remote/nio/Server.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 package org.dancres.blitz.remote.nio; | |
2 | |
3 import org.dancres.blitz.SpaceImpl; | |
4 | |
5 import java.net.ServerSocket; | |
6 import java.net.Socket; | |
7 import java.net.InetSocketAddress; | |
8 import java.io.IOException; | |
9 import java.nio.channels.SelectionKey; | |
10 import java.nio.channels.Selector; | |
11 import java.nio.channels.SocketChannel; | |
12 import java.nio.channels.ServerSocketChannel; | |
13 | |
14 /** | |
15 * To make this scale we would have multiple selectors each serviced by a | |
16 * separate event processor thread. That's way easier than trying to handle | |
17 * multiple threads doing selects on the same selector. Each processor | |
18 * thread would handle IO transfer into ControlBlock which would then figure | |
19 * out when to dispatch a request into the thread pool for execution. | |
20 * We'd also need to pass over the SocketChannel or whatever so the worker | |
21 * knows where to post the response to. Probably the best thing is to pass | |
22 * the selection key so that we can set it for write and post the data to write | |
23 * into the control block. When the control block sees no more data to write | |
24 * it clears down that setting on the selection key. | |
25 */ | |
26 public class Server implements Runnable { | |
27 private InetSocketAddress _address; | |
28 private ServerSocketChannel _rootSocket; | |
29 private Thread _acceptor; | |
30 private Selector _selector; | |
31 private EventProcessor _processor; | |
32 | |
33 /** | |
34 * Warning: Do not use more than one selector, not thread safe yet | |
35 */ | |
36 public Server(InetSocketAddress anAddr, | |
37 DispatcherFactory aFactory) throws IOException { | |
38 _address = anAddr; | |
39 _rootSocket = ServerSocketChannel.open(); | |
40 | |
41 _rootSocket.socket().bind(anAddr); | |
42 | |
43 _acceptor = new Thread(this, "Selector"); | |
44 _selector = Selector.open(); | |
45 _processor = new EventProcessor(_selector, aFactory); | |
46 _processor.start(); | |
47 _acceptor.start(); | |
48 } | |
49 | |
50 public FastSpace getEndpoint() { | |
51 return new Invoker(new InetSocketAddress(_address.getAddress(), | |
52 _rootSocket.socket().getLocalPort())); | |
53 } | |
54 | |
55 public void run() { | |
56 while (true) { | |
57 try { | |
58 SocketChannel mySocket = _rootSocket.accept(); | |
59 | |
60 // System.err.println("Connection received: " + mySocket); | |
61 _processor.add(mySocket); | |
62 } catch (IOException anIOE) { | |
63 System.err.println("Error during accept"); | |
64 anIOE.printStackTrace(System.err); | |
65 } | |
66 } | |
67 } | |
68 | |
69 public static void main(String args[]) { | |
70 try { | |
71 | |
72 SpaceImpl mySpace = new SpaceImpl(null); | |
73 | |
74 new Server(new InetSocketAddress(12345), | |
75 new DispatcherFactoryImpl(mySpace)); | |
76 | |
77 } catch (Exception anE) { | |
78 System.err.println("Server error"); | |
79 anE.printStackTrace(System.err); | |
80 } | |
81 } | |
82 } |