390
|
1 package rep;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.net.InetSocketAddress;
|
|
5 import java.net.SocketException;
|
|
6 import java.nio.channels.ClosedChannelException;
|
|
7 import java.nio.channels.SelectionKey;
|
|
8 import java.util.LinkedList;
|
|
9 import java.util.Set;
|
|
10 import java.util.concurrent.BlockingQueue;
|
|
11 import java.util.concurrent.LinkedBlockingQueue;
|
|
12
|
|
13 import rep.channel.REPLogger;
|
|
14 import rep.channel.REPSelectionKey;
|
|
15 import rep.channel.REPSelector;
|
|
16 import rep.channel.REPServerSocketChannel;
|
|
17 import rep.channel.REPSocketChannel;
|
|
18 import rep.gui.SessionManagerEvent;
|
|
19 import rep.gui.SessionManagerGUI;
|
|
20 import rep.handler.FirstConnector;
|
|
21 import rep.handler.REPNode;
|
|
22
|
411
|
23 /**
|
|
24 * @author kono
|
|
25 * Single Threaded Server Main Loop
|
|
26 * maintain multiple connections
|
|
27 * gui interface is provided.
|
|
28 * Protocols are handled by our manager.
|
412
|
29 * We believe this is an protocol independent server.
|
411
|
30 */
|
390
|
31 public class ServerMainLoop {
|
|
32
|
|
33 public static REPLogger logger = REPLogger.singleton();
|
|
34 public SessionManager manager;
|
|
35 protected SessionManagerGUI gui;
|
|
36 protected REPSelector<REPCommand> selector;
|
|
37 private BlockingQueue<SessionManagerEvent> waitingEventQueue
|
|
38 = new LinkedBlockingQueue<SessionManagerEvent>();
|
|
39 public String myHost;
|
|
40 private LinkedList<PacketSet> writeQueue = new LinkedList<PacketSet>();
|
|
41 protected int receive_port;
|
|
42 protected int parent_port;
|
|
43 protected static final int DEFAULT_PORT = 8766;
|
|
44 private SessionManagerEvent execAfterConnect = null;
|
430
|
45 private boolean running = true;
|
390
|
46
|
|
47
|
|
48 public void setReceivePort(int port) {
|
|
49 receive_port = port;
|
|
50 }
|
|
51
|
|
52 void mainLoop(SessionManager sessionManager, int port, SessionManagerGUI gui) throws IOException,
|
|
53 SocketException, ClosedChannelException {
|
|
54 this.gui = gui;
|
|
55 manager = sessionManager;
|
|
56 receive_port = port;
|
|
57 serverInit();
|
|
58 mainLoop();
|
|
59 }
|
|
60
|
|
61 public void mainLoop() throws IOException {
|
430
|
62 while(running){
|
399
|
63 manager.checkWaitingCommandInMerge();
|
390
|
64 if (checkInputEvent() ||
|
|
65 checkWaitingWrite()) {
|
420
|
66 continue;
|
390
|
67 // try to do fair execution for waiting task
|
420
|
68 //if(selector.selectNow() > 0) select();
|
|
69 //continue;
|
390
|
70 }
|
|
71 // now we can wait for input packet or event
|
|
72 selector.select();
|
|
73 select();
|
|
74 }
|
|
75 }
|
|
76
|
|
77 void serverInit() throws IOException, SocketException,
|
|
78 ClosedChannelException {
|
|
79 selector = REPSelector.<REPCommand>create();
|
|
80 REPServerSocketChannel<REPCommand> ssc = REPServerSocketChannel.<REPCommand>open(new REPCommandPacker());
|
|
81 ssc.configureBlocking(false); // Selector requires this
|
|
82 ssc.socket().setReuseAddress(true); //reuse address 必須
|
|
83 //getAllByNameで取れた全てのアドレスに対してbindする
|
|
84 try {
|
|
85 ssc.socket().bind(new InetSocketAddress("::",receive_port));
|
|
86 } catch (SocketException e) {
|
391
|
87 // for some IPv6 implementation
|
390
|
88 ssc.socket().bind(new InetSocketAddress(receive_port));
|
|
89 }
|
|
90 ssc.register(selector, SelectionKey.OP_ACCEPT,null);
|
|
91 }
|
|
92
|
|
93 private boolean checkInputEvent() {
|
|
94 SessionManagerEvent e;
|
|
95 if((e = waitingEventQueue.poll())!=null){
|
|
96 e.exec(manager);
|
|
97 return true;
|
|
98 }
|
|
99 return false;
|
|
100 }
|
430
|
101
|
|
102 public void serverStop() {
|
|
103 running = false;
|
|
104 selector.wakeup();
|
|
105 }
|
390
|
106
|
411
|
107 /**
|
|
108 * To avoid dead locks, we write a command one at a time
|
|
109 * during select().
|
|
110 * @return
|
|
111 * @throws IOException
|
|
112 */
|
390
|
113 private boolean checkWaitingWrite() throws IOException {
|
|
114 PacketSet p = writeQueue.poll();
|
|
115 if (p!=null) {
|
442
|
116 // sendLog(p);
|
390
|
117 p.channel.write(p.command);
|
|
118 return true;
|
|
119 }
|
|
120 return false;
|
|
121 }
|
|
122
|
411
|
123 /**
|
|
124 * Debug message
|
|
125 * @param p
|
|
126 */
|
442
|
127 @SuppressWarnings("unused")
|
401
|
128 private void sendLog(PacketSet p) {
|
|
129 REPNode to;
|
|
130 String s;
|
450
|
131 to = manager.editorList.editorByChannel(p.channel.channel);
|
401
|
132 if (to==null)
|
|
133 s = p.channel.toString();
|
|
134 else
|
|
135 s = to.toString();
|
|
136 logger.writeLog("writing: "+p.command+" to: " + s);
|
|
137 }
|
|
138
|
390
|
139
|
399
|
140 public void close(REPSocketChannel<REPCommand> channel) {
|
390
|
141 REPSelectionKey<REPCommand>key = channel.keyFor1(selector);
|
|
142 REPNode handler = (REPNode)key.attachment();
|
|
143 key.cancel();
|
|
144 handler.cancel(channel);
|
|
145 // we have to remove session/editor
|
|
146 }
|
|
147
|
411
|
148 /**
|
|
149 * Main Select routing
|
|
150 * check incoming connection request and incoming packet
|
|
151 * A request is handled by a handler object which is attached
|
|
152 * to the SelectionKey.
|
|
153 * @throws IOException
|
|
154 */
|
390
|
155 private void select() throws IOException {
|
|
156
|
|
157 Set<REPSelectionKey<REPCommand>> keys = selector.selectedKeys1();
|
|
158 for(REPSelectionKey<REPCommand> key : keys){
|
|
159 if(key.isAcceptable()){
|
|
160 /*
|
|
161 * Incoming connection. We don't know which, editor or
|
|
162 * session manager. Assign FirstConnector to distinguish.
|
|
163 */
|
|
164 REPSocketChannel<REPCommand> channel = key.accept(new REPCommandPacker());
|
|
165 logger.writeLog("SessionManager.select() : key.isAcceptable : channel = " + channel);
|
|
166 registerChannel(channel, new FirstConnector(manager,channel));
|
|
167 } else if(key.isReadable()){
|
|
168 /*
|
|
169 * Incoming packets are handled by a various forwarder.
|
|
170 * A handler throw IOException() in case of a trouble to
|
|
171 * close the channel.
|
|
172 */
|
|
173 REPNode handler = (REPNode)key.attachment();
|
|
174 try {
|
|
175 REPCommand command = key.channel1().read();
|
|
176 handler.handle(command, key);
|
|
177 } catch (IOException e) {
|
|
178 key.cancel();
|
|
179 handler.cancel(key.channel1());
|
|
180 }
|
|
181 }
|
|
182 }
|
|
183 }
|
|
184
|
|
185 public void registerChannel(REPSocketChannel<REPCommand> channel, REPNode handler) throws IOException {
|
|
186 if(channel == null) {
|
|
187 return;
|
|
188 }
|
|
189 // handler.setChannel(channel);
|
|
190 channel.configureBlocking(false);
|
|
191 channel.register(selector, SelectionKey.OP_READ, handler);
|
|
192 }
|
|
193
|
|
194 public void setMyHostName(String localHostName) {
|
|
195 myHost = localHostName + receive_port;
|
|
196 setHostToEditor(myHost);
|
|
197 }
|
|
198
|
|
199 public String myHost() {
|
|
200 return myHost;
|
|
201 }
|
|
202
|
|
203 private void setHostToEditor(String myHost2) {
|
|
204 for(REPNode editor : manager.editorList.values()){
|
|
205 if (editor.channel!=null)
|
|
206 editor.setHost(myHost2);
|
|
207 }
|
|
208 }
|
|
209
|
|
210
|
|
211 public void buttonPressed(SessionManagerEvent event) {
|
|
212 try {
|
|
213 waitingEventQueue.put(event);
|
|
214 } catch (InterruptedException e) {}
|
|
215 selector.wakeup();
|
|
216 }
|
|
217
|
|
218 public void syncExec(SessionManagerEvent event) {
|
|
219 try {
|
|
220 waitingEventQueue.put(event);
|
|
221 } catch (InterruptedException e) {
|
|
222 }
|
|
223 }
|
|
224
|
|
225 public void addWriteQueue(PacketSet packetSet) {
|
|
226 writeQueue.addLast(packetSet);
|
|
227 assert(writeQueue.size()<SessionManager.packetLimit) ;
|
|
228 }
|
|
229
|
|
230 public void setParentPort(int port) {
|
|
231 parent_port = port;
|
|
232 }
|
|
233
|
|
234 public int getParentPort() {
|
|
235 return parent_port;
|
|
236 }
|
|
237
|
|
238 public int getPort() {
|
|
239 return receive_port;
|
|
240 }
|
|
241
|
|
242 public void execAfterConnect(SessionManagerEvent sessionManagerEvent) {
|
|
243 execAfterConnect = sessionManagerEvent;
|
|
244 }
|
|
245
|
|
246 public void afterConnect() {
|
|
247 SessionManagerEvent e = execAfterConnect;
|
|
248 execAfterConnect = null;
|
|
249 if (e!=null) e.exec(manager);
|
|
250 }
|
|
251
|
|
252 void removeChannel(SessionManager sessionManager, REPNode channel) {
|
|
253 REPSelectionKey<REPCommand> key = channel.channel.keyFor1(selector);
|
|
254 key.cancel();
|
|
255 try {
|
|
256 channel.channel.close1();
|
|
257 } catch (IOException e) {
|
|
258 }
|
|
259 }
|
|
260
|
|
261 public String toString() {
|
|
262 return ""+myHost+":"+receive_port;
|
|
263 }
|
|
264
|
|
265
|
|
266 public void setGUI(SessionManagerGUI gui) {
|
|
267 this.gui = gui;
|
|
268 }
|
|
269
|
|
270 public void setManager(SessionManager sessionManager) {
|
|
271 manager = sessionManager;
|
|
272 }
|
|
273
|
|
274 } |