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