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.DoGUIUpdate;
|
|
19 import rep.gui.SessionManagerEvent;
|
|
20 import rep.gui.SessionManagerGUI;
|
|
21 import rep.handler.FirstConnector;
|
|
22 import rep.handler.REPNode;
|
|
23
|
|
24 public class ServerMainLoop {
|
|
25
|
|
26 public static REPLogger logger = REPLogger.singleton();
|
|
27 public SessionManager manager;
|
|
28 protected SessionManagerGUI gui;
|
|
29 protected REPSelector<REPCommand> selector;
|
|
30 private BlockingQueue<SessionManagerEvent> waitingEventQueue
|
|
31 = new LinkedBlockingQueue<SessionManagerEvent>();
|
|
32 public String myHost;
|
|
33 private LinkedList<PacketSet> writeQueue = new LinkedList<PacketSet>();
|
|
34 protected int receive_port;
|
|
35 protected int parent_port;
|
|
36 protected static final int DEFAULT_PORT = 8766;
|
|
37 private SessionManagerEvent execAfterConnect = null;
|
|
38
|
|
39
|
|
40 public void setReceivePort(int port) {
|
|
41 receive_port = port;
|
|
42 }
|
|
43
|
|
44 void mainLoop(SessionManager sessionManager, int port, SessionManagerGUI gui) throws IOException,
|
|
45 SocketException, ClosedChannelException {
|
|
46 this.gui = gui;
|
|
47 manager = sessionManager;
|
|
48 receive_port = port;
|
|
49 serverInit();
|
|
50 mainLoop();
|
|
51 }
|
|
52
|
|
53 public void mainLoop() throws IOException {
|
|
54 while(true){
|
399
|
55 manager.checkWaitingCommandInMerge();
|
390
|
56 if (checkInputEvent() ||
|
|
57 checkWaitingWrite()) {
|
|
58 // try to do fair execution for waiting task
|
|
59 if(selector.selectNow() > 0) select();
|
|
60 continue;
|
|
61 }
|
|
62 // now we can wait for input packet or event
|
|
63 selector.select();
|
|
64 select();
|
|
65 }
|
|
66 }
|
|
67
|
|
68 void serverInit() throws IOException, SocketException,
|
|
69 ClosedChannelException {
|
|
70 selector = REPSelector.<REPCommand>create();
|
|
71 REPServerSocketChannel<REPCommand> ssc = REPServerSocketChannel.<REPCommand>open(new REPCommandPacker());
|
|
72 ssc.configureBlocking(false); // Selector requires this
|
|
73 ssc.socket().setReuseAddress(true); //reuse address 必須
|
|
74 //getAllByNameで取れた全てのアドレスに対してbindする
|
|
75 try {
|
|
76 ssc.socket().bind(new InetSocketAddress("::",receive_port));
|
|
77 } catch (SocketException e) {
|
391
|
78 // for some IPv6 implementation
|
390
|
79 ssc.socket().bind(new InetSocketAddress(receive_port));
|
|
80 }
|
|
81 ssc.register(selector, SelectionKey.OP_ACCEPT,null);
|
|
82 }
|
|
83
|
|
84 private boolean checkInputEvent() {
|
|
85 SessionManagerEvent e;
|
|
86 if((e = waitingEventQueue.poll())!=null){
|
|
87 e.exec(manager);
|
|
88 return true;
|
|
89 }
|
|
90 return false;
|
|
91 }
|
|
92
|
|
93 private boolean checkWaitingWrite() throws IOException {
|
|
94 PacketSet p = writeQueue.poll();
|
|
95 if (p!=null) {
|
391
|
96 logger.writeLog("writing: "+p.command+" to: "
|
|
97 +manager.editorList.editorByChannel(p.channel));
|
390
|
98 p.channel.write(p.command);
|
|
99 return true;
|
|
100 }
|
|
101 return false;
|
|
102 }
|
|
103
|
|
104
|
399
|
105 public void close(REPSocketChannel<REPCommand> channel) {
|
390
|
106 REPSelectionKey<REPCommand>key = channel.keyFor1(selector);
|
|
107 REPNode handler = (REPNode)key.attachment();
|
|
108 key.cancel();
|
|
109 handler.cancel(channel);
|
|
110 // we have to remove session/editor
|
|
111 }
|
|
112
|
|
113 private void select() throws IOException {
|
|
114
|
|
115 Set<REPSelectionKey<REPCommand>> keys = selector.selectedKeys1();
|
|
116 for(REPSelectionKey<REPCommand> key : keys){
|
|
117 if(key.isAcceptable()){
|
|
118 /*
|
|
119 * Incoming connection. We don't know which, editor or
|
|
120 * session manager. Assign FirstConnector to distinguish.
|
|
121 */
|
|
122 REPSocketChannel<REPCommand> channel = key.accept(new REPCommandPacker());
|
|
123 logger.writeLog("SessionManager.select() : key.isAcceptable : channel = " + channel);
|
|
124 registerChannel(channel, new FirstConnector(manager,channel));
|
|
125 } else if(key.isReadable()){
|
|
126 /*
|
|
127 * Incoming packets are handled by a various forwarder.
|
|
128 * A handler throw IOException() in case of a trouble to
|
|
129 * close the channel.
|
|
130 */
|
|
131 REPNode handler = (REPNode)key.attachment();
|
|
132 try {
|
|
133 REPCommand command = key.channel1().read();
|
|
134 handler.handle(command, key);
|
|
135 } catch (IOException e) {
|
|
136 key.cancel();
|
|
137 handler.cancel(key.channel1());
|
|
138 }
|
|
139 }
|
|
140 }
|
|
141 }
|
|
142
|
|
143 public void registerChannel(REPSocketChannel<REPCommand> channel, REPNode handler) throws IOException {
|
|
144 if(channel == null) {
|
|
145 return;
|
|
146 }
|
|
147 // handler.setChannel(channel);
|
|
148 channel.configureBlocking(false);
|
|
149 channel.register(selector, SelectionKey.OP_READ, handler);
|
|
150 }
|
|
151
|
|
152 protected void updateGUI() {
|
|
153 //リストのコピーをGUIに渡す
|
|
154 LinkedList<Session> sList = new LinkedList<Session>(manager.sessionList.values());
|
|
155 LinkedList<REPNode> eList;
|
|
156 if (false) {
|
|
157 // local editor only
|
|
158 eList = new LinkedList<REPNode>();
|
|
159 for(REPNode e:manager.editorList.values()) {
|
|
160 if (manager.getSMID(e.eid)==manager.smList.sessionManagerID()) {
|
|
161 eList.add(e);
|
|
162 }
|
|
163 }
|
|
164 } else {
|
|
165 eList = new LinkedList<REPNode>(manager.editorList.values());
|
|
166 }
|
|
167 //GUIに反映
|
|
168 Runnable doRun = new DoGUIUpdate(sList, eList, gui);
|
|
169 gui.invokeLater(doRun);
|
|
170 }
|
|
171
|
|
172 public void setMyHostName(String localHostName) {
|
|
173 myHost = localHostName + receive_port;
|
|
174 setHostToEditor(myHost);
|
|
175 }
|
|
176
|
|
177 public String myHost() {
|
|
178 return myHost;
|
|
179 }
|
|
180
|
|
181 private void setHostToEditor(String myHost2) {
|
|
182 for(REPNode editor : manager.editorList.values()){
|
|
183 if (editor.channel!=null)
|
|
184 editor.setHost(myHost2);
|
|
185 }
|
|
186 }
|
|
187
|
|
188
|
|
189 public void buttonPressed(SessionManagerEvent event) {
|
|
190 try {
|
|
191 waitingEventQueue.put(event);
|
|
192 } catch (InterruptedException e) {}
|
|
193 selector.wakeup();
|
|
194 }
|
|
195
|
|
196 public void syncExec(SessionManagerEvent event) {
|
|
197 try {
|
|
198 waitingEventQueue.put(event);
|
|
199 } catch (InterruptedException e) {
|
|
200 }
|
|
201 }
|
|
202
|
|
203 public void addWriteQueue(PacketSet packetSet) {
|
|
204 writeQueue.addLast(packetSet);
|
|
205 assert(writeQueue.size()<SessionManager.packetLimit) ;
|
|
206 }
|
|
207
|
|
208 public void setParentPort(int port) {
|
|
209 parent_port = port;
|
|
210 }
|
|
211
|
|
212 public int getParentPort() {
|
|
213 return parent_port;
|
|
214 }
|
|
215
|
|
216 public int getPort() {
|
|
217 return receive_port;
|
|
218 }
|
|
219
|
|
220 public void execAfterConnect(SessionManagerEvent sessionManagerEvent) {
|
|
221 execAfterConnect = sessionManagerEvent;
|
|
222 }
|
|
223
|
|
224 public void afterConnect() {
|
|
225 SessionManagerEvent e = execAfterConnect;
|
|
226 execAfterConnect = null;
|
|
227 if (e!=null) e.exec(manager);
|
|
228 }
|
|
229
|
|
230 void removeChannel(SessionManager sessionManager, REPNode channel) {
|
|
231 REPSelectionKey<REPCommand> key = channel.channel.keyFor1(selector);
|
|
232 key.cancel();
|
|
233 try {
|
|
234 channel.channel.close1();
|
|
235 } catch (IOException e) {
|
|
236 }
|
|
237 }
|
|
238
|
|
239 public String toString() {
|
|
240 return ""+myHost+":"+receive_port;
|
|
241 }
|
|
242
|
|
243
|
|
244 public void setGUI(SessionManagerGUI gui) {
|
|
245 this.gui = gui;
|
|
246 }
|
|
247
|
|
248 public void setManager(SessionManager sessionManager) {
|
|
249 manager = sessionManager;
|
|
250 }
|
|
251
|
|
252 } |