Mercurial > hg > RemoteEditor > REPSessionManager
comparison test/sematest/TestEditor.java @ 297:9c553308b918
*** empty log message ***
author | kono |
---|---|
date | Tue, 30 Sep 2008 19:16:06 +0900 |
parents | d93b062eadaa |
children | cf9328e66d25 |
comparison
equal
deleted
inserted
replaced
296:c35f01b5060f | 297:9c553308b918 |
---|---|
12 import rep.channel.REPSelector; | 12 import rep.channel.REPSelector; |
13 import rep.channel.REPSocketChannel; | 13 import rep.channel.REPSocketChannel; |
14 import test.Text; | 14 import test.Text; |
15 | 15 |
16 | 16 |
17 /** | |
18 * @author kono | |
19 * Basic Temote Editor client implementation | |
20 * should support multi-session | |
21 * currently multi-session requires new channel, that is | |
22 * only one session for this editor. | |
23 */ | |
17 public class TestEditor extends Thread{ | 24 public class TestEditor extends Thread{ |
18 private InetSocketAddress semaIP; | 25 private InetSocketAddress semaIP; |
19 private REPLogger ns; | 26 private REPLogger ns; |
20 private boolean master; | |
21 private int myid ; | |
22 private int seq = 0; | 27 private int seq = 0; |
23 public static int editorNo = 0; | |
24 public Text text; | 28 public Text text; |
25 public LinkedList<REPCommand> cmds; | 29 public LinkedList<REPCommand> cmds; |
26 private int eid = 0; | 30 private int eid = 0; |
27 private int sid = 0; | 31 private int sid = 0; |
28 REPSocketChannel<REPCommand> channel; | 32 REPSocketChannel<REPCommand> channel; |
29 boolean running = true; | 33 boolean running = true; |
30 long timeout = 1; | 34 long timeout = 1; |
35 private String name; | |
31 | 36 |
32 static private String[] text1d = { | 37 static private String[] text1d = { |
33 "aaa", "bbb", "ccc", "ddd", "eee", | 38 "aaa", "bbb", "ccc", "ddd", "eee", |
34 }; | 39 }; |
35 | 40 |
36 public TestEditor(String name, String _host,int _port, boolean master){ | 41 public TestEditor(String name, String _host,int _port, boolean master){ |
37 super(name); | 42 super(name); |
38 semaIP = new InetSocketAddress(_host, _port); | 43 semaIP = new InetSocketAddress(_host, _port); |
39 ns = REPLogger.singleton(); | 44 ns = REPLogger.singleton(); |
40 this.master = master; | 45 this.name = name; |
41 myid = editorNo++; | |
42 cmds = new LinkedList<REPCommand>(); | 46 cmds = new LinkedList<REPCommand>(); |
43 if (master) { | 47 if (master) { |
44 text = new Text(text1d); | 48 text = new Text(text1d); |
49 cmds.add(new REPCommand(REP.SMCMD_PUT,0,0,seq++,0,name+"-file")); | |
45 cmds.add(new REPCommand(REP.REPCMD_INSERT,0,0,0,0,"m0")); | 50 cmds.add(new REPCommand(REP.REPCMD_INSERT,0,0,0,0,"m0")); |
46 cmds.add(new REPCommand(REP.REPCMD_DELETE,0,0,0,0,"m0")); | 51 cmds.add(new REPCommand(REP.REPCMD_DELETE,0,0,0,0,"m0")); |
47 cmds.add(new REPCommand(REP.SMCMD_QUIT,0,0,0,0,"")); | 52 cmds.add(new REPCommand(REP.SMCMD_QUIT,0,0,0,0,"")); |
48 } else { | 53 } else { |
49 text = new Text(new String[0]); | 54 text = new Text(new String[0]); |
55 cmds.add(new REPCommand(REP.SMCMD_JOIN,0,0,seq++,0,name)); | |
50 cmds.add(new REPCommand(REP.REPCMD_INSERT,0,0,0,0,"c0")); | 56 cmds.add(new REPCommand(REP.REPCMD_INSERT,0,0,0,0,"c0")); |
51 cmds.add(new REPCommand(REP.REPCMD_DELETE,0,0,0,0,"c0")); | 57 cmds.add(new REPCommand(REP.REPCMD_DELETE,0,0,0,0,"c0")); |
52 cmds.add(new REPCommand(REP.SMCMD_QUIT,0,0,0,0,"")); | 58 //cmds.add(new REPCommand(REP.SMCMD_QUIT,0,0,0,0,"")); |
53 } | 59 } |
54 } | 60 } |
55 | 61 |
56 public void run(){ | 62 public void run(){ |
57 try { | 63 try { |
58 channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker()); | 64 channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker()); |
59 } catch (IOException e) { return; } | 65 } catch (IOException e) { return; } |
60 | 66 |
61 ns.writeLog("try to connect to SessionManager whose ip is "+semaIP+" editor-"+myid, 1); | 67 ns.writeLog("try to connect to SessionManager whose ip is "+semaIP+" "+name, 1); |
62 try { | 68 try { |
63 while (!channel.connect(semaIP)){ | 69 while (!channel.connect(semaIP)){ |
64 ns.writeLog("SeMa not listen to socket yet, wait", 1); | 70 ns.writeLog("SeMa not listen to socket yet, wait", 1); |
65 } | 71 } |
66 } catch (IOException e) { return; } | 72 } catch (IOException e) { return; } |
67 ns.writeLog("successes to connect editor-"+myid, 1); | 73 ns.writeLog("successes to connect "+name); |
68 REPCommand command; | |
69 /* | 74 /* |
70 * public REPCommand(REP cmd,int sid,int eid, int seq, int lineno, String string) | 75 * public REPCommand(REP cmd,int sid,int eid, int seq, int lineno, String string) |
71 */ | 76 */ |
72 if (master) | |
73 command = new REPCommand(REP.SMCMD_PUT,0,0,seq++,0,"master-file"); | |
74 else | |
75 command = new REPCommand(REP.SMCMD_JOIN,0,0,seq++,0,"editor-"+myid); | |
76 channel.write(command); | |
77 | |
78 try { | 77 try { |
79 mainloop(); | 78 mainloop(); |
80 } catch (IOException e) { | 79 } catch (IOException e) { |
81 } | 80 } |
82 } | 81 } |
96 } | 95 } |
97 | 96 |
98 private void userInput() { | 97 private void userInput() { |
99 REPCommand cmd = cmds.poll(); | 98 REPCommand cmd = cmds.poll(); |
100 if (cmd!=null) { | 99 if (cmd!=null) { |
101 text.edit(cmd); | 100 switch(cmd.cmd) { |
102 sendCommand(cmd); | 101 case REPCMD_INSERT: |
102 text.insert(cmd.lineno, cmd.string); | |
103 sendCommand(cmd); | |
104 break; | |
105 case REPCMD_DELETE: | |
106 String del = text.delete(cmd.lineno); | |
107 cmd.setString(del); | |
108 sendCommand(cmd); | |
109 break; | |
110 case SMCMD_QUIT: | |
111 cmds.clear(); | |
112 sendCommand(cmd); | |
113 break; | |
114 default: | |
115 assert(false); | |
116 } | |
103 } else { | 117 } else { |
104 // no more command to send | 118 // no more command to send |
105 timeout = 0; | 119 timeout = 0; |
106 } | 120 } |
107 } | 121 } |
109 | 123 |
110 private void sendCommand(REPCommand cmd) { | 124 private void sendCommand(REPCommand cmd) { |
111 cmd.setSEQID(seq++); | 125 cmd.setSEQID(seq++); |
112 cmd.setEID(eid); | 126 cmd.setEID(eid); |
113 cmd.setSID(sid); | 127 cmd.setSID(sid); |
114 ns.writeLog("editor-"+myid+" send "+cmd); | 128 ns.writeLog(name +" send "+cmd); |
115 channel.write(cmd); | 129 channel.write(cmd); |
116 } | 130 } |
117 | 131 |
118 private void handle(REPCommand cmd) { | 132 private void handle(REPCommand cmd) { |
119 ns.writeLog("editor-"+myid+" read "+cmd); | 133 ns.writeLog(name +": read "+cmd); |
120 switch(cmd.cmd) { | 134 switch(cmd.cmd) { |
121 case REPCMD_INSERT : | 135 case REPCMD_INSERT : |
122 break; | 136 text.insert(cmd.lineno, cmd.string); |
123 case REPCMD_INSERT_ACK : | 137 sendCommand(cmd); |
124 break; | 138 break; |
125 case REPCMD_DELETE : | 139 case REPCMD_INSERT_ACK : |
126 break; | 140 break; |
141 case REPCMD_DELETE : | |
142 String del = text.delete(cmd.lineno); | |
143 cmd.setString(del); | |
144 sendCommand(cmd); | |
145 break; | |
127 case REPCMD_DELETE_ACK : | 146 case REPCMD_DELETE_ACK : |
128 break; | 147 break; |
129 case REPCMD_CLOSE : | 148 case REPCMD_CLOSE : |
130 case REPCMD_CLOSE_2 : | 149 case REPCMD_CLOSE_2 : |
131 break; | 150 break; |