comparison rep/Forwarder.java @ 317:c83a3faec487

*** empty log message ***
author kono
date Tue, 07 Oct 2008 21:48:31 +0900
parents 77f443f6dc9f
children dc57e24ea3df
comparison
equal deleted inserted replaced
316:77f443f6dc9f 317:c83a3faec487
1 package rep; 1 package rep;
2
3 import java.util.LinkedList;
4 2
5 import rep.channel.REPLogger; 3 import rep.channel.REPLogger;
6 import rep.channel.REPSocketChannel; 4 import rep.channel.REPSocketChannel;
5 import rep.handler.PacketSet;
7 6
8 public class Forwarder { 7 public class Forwarder {
9 int eid; // unique id in a session 8 int eid; // unique id in a session
10 REPSocketChannel<REPCommand> myChannel; 9 REPSocketChannel<REPCommand> myChannel;
11 // REPCommands we sent to the next editor 10 // REPCommands we sent to the next editor
12 LinkedList<REPCommand> writeQueue = new LinkedList<REPCommand>();
13 final int limit=100; 11 final int limit=100;
14 REPLogger ns = REPLogger.singleton(); 12 REPLogger ns = REPLogger.singleton();
13 SessionManager manager;
15 14
15 public Forwarder(SessionManager manager) {
16 this.manager = manager;
17 }
18
16 public int getEID() { 19 public int getEID() {
17 return eid; 20 return eid;
18 } 21 }
19 22
20 public void setEID(int eid) { 23 public void setEID(int eid) {
21 this.eid = eid; 24 this.eid = eid;
22 } 25 }
23 26
24 public void send(REPCommand command) { 27 public void send(REPCommand command) {
25 writeQueue.add(command); 28 manager.addWriteQueue(new PacketSet(myChannel, null, command));
26 assert(writeQueue.size()<limit);
27 } 29 }
28 30
29 public REPSocketChannel<REPCommand> getChannel() { 31 public REPSocketChannel<REPCommand> getChannel() {
30 return myChannel; 32 return myChannel;
31 } 33 }
32 34
33 public void setChannel(REPSocketChannel<REPCommand> channel) { 35 public void setChannel(REPSocketChannel<REPCommand> channel) {
34 myChannel = channel; 36 myChannel = channel;
35 } 37 }
36 38
37 public boolean doWaitingWrite() {
38 // 一気に送ると、向こう側(Editor)で、dead lock する可能性がある。
39 // select loop の中で一つ一つ送るしかない。Editor側から割り込まれる可能性も
40 //  ある。その時に複数のコマンドを送っていると、どこに割り込まれたかを判断する
41 // ことが出来ない。そこで、一つ一つReturnを確認する必要がある。つまり、
42 // select loop で送るしかない。
43 REPCommand cmd;
44 if (writeQueue.size()>0) {
45 cmd = new REPCommand(writeQueue.remove(0));
46 ns.writeLog("SessionManager write to "+myChannel+" cmd="+cmd);
47 myChannel.write(cmd);
48 return true;
49 }
50 return false;
51 }
52 } 39 }