0
|
1 package remoteeditor.network;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.net.InetSocketAddress;
|
|
5 import java.nio.ByteBuffer;
|
|
6 import java.nio.channels.SocketChannel;
|
14
|
7 import java.util.LinkedList;
|
|
8 import java.util.List;
|
0
|
9
|
|
10 import org.eclipse.jface.dialogs.InputDialog;
|
|
11 import org.eclipse.jface.window.Window;
|
|
12 import org.eclipse.swt.widgets.Shell;
|
|
13
|
17
|
14 import sample.merge.Rep_Cmd;
|
21
|
15 import sample.merge.Translate;
|
17
|
16
|
0
|
17
|
|
18
|
|
19 public class REP implements Runnable{
|
|
20
|
|
21 SocketChannel sc;
|
8
|
22 int cmd;
|
|
23 int eid;
|
31
|
24 int eid2;
|
8
|
25 int lineno;
|
|
26 int sid;
|
|
27 int seqid;
|
|
28 int textsiz;
|
0
|
29
|
15
|
30 public static final int REP_INSERT_CMD = 6;
|
18
|
31 public static final int REP_INSERT_ACK_CMD = 7;
|
|
32 public static final int REP_DELETE_CMD = 9;
|
|
33 public static final int REP_DELETE_ACK_CMD = 10;
|
|
34 public static final int REP_REPLACE_CMD = 13;
|
|
35 public static final int REP_REPLACE_ACK_CMD = 14;
|
|
36 public static final int REP_JOIN_CMD = 41;
|
|
37 public static final int REP_JOIN_ACK_CMD = 42;
|
|
38 public static final int REP_PUT_CMD = 45;
|
|
39 public static final int REP_PUT_ACK_CMD = 46;
|
|
40 public static final int REP_SELECT_CMD = 47;
|
|
41 public static final int REP_SELECT_ACK_CMD = 48;
|
|
42 public static final int REP_QUIT_CMD = 53;
|
|
43 public static final int REP_QUIT_ACK_CMD = 54;
|
0
|
44
|
21
|
45 static final int HEADER_SIZE = 24;
|
0
|
46
|
|
47 String string;
|
20
|
48 public RSocketListener socketListener;
|
|
49 Shell shell;
|
0
|
50
|
20
|
51 List <Rep_Cmd> myCmdList = new LinkedList<Rep_Cmd>();
|
|
52 List <Rep_Cmd> othersCmdList = new LinkedList<Rep_Cmd>();
|
19
|
53 private String filename;
|
14
|
54
|
21
|
55 public REP() {
|
0
|
56 String host = "localhost";
|
20
|
57 int port = 8765;
|
0
|
58
|
|
59 InputDialog dialog = new InputDialog(shell, "REP", "host", "localhost", null);
|
|
60 if(dialog.open() == Window.OK){
|
|
61 host = dialog.getValue();
|
|
62 }
|
|
63
|
33
|
64 dialog = new InputDialog(shell, "REP", "port", "8765", null);
|
0
|
65 if (dialog.open() == Window.OK) {
|
|
66 try {
|
|
67 port = Integer.parseInt(dialog.getValue());
|
|
68 } catch (NumberFormatException x) {
|
|
69 }
|
|
70 }
|
|
71
|
|
72
|
|
73 InetSocketAddress addr = new InetSocketAddress(host, port);
|
21
|
74 try {
|
|
75 sc = SocketChannel.open();
|
|
76
|
0
|
77 sc.configureBlocking(true);
|
|
78 sc.connect(addr);
|
|
79 while(!sc.finishConnect()){
|
|
80 System.out.println("afro");
|
|
81 }
|
|
82 join();
|
21
|
83 } catch (IOException e) {
|
|
84 e.printStackTrace();
|
|
85 }
|
0
|
86
|
|
87 dialog = new InputDialog(shell, "repput", "read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string, "hugo", null);
|
|
88 if(dialog.open() == Window.OK){
|
19
|
89 filename = dialog.getValue();
|
0
|
90 put();
|
|
91 }
|
|
92
|
8
|
93 dialog = new InputDialog(shell, "repselect", "read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string ,Integer.toString(sid), null);
|
0
|
94 if(dialog.open() == Window.OK){
|
|
95 try {
|
|
96 sid = (byte) Integer.parseInt(dialog.getValue());
|
|
97 } catch (NumberFormatException x) {
|
|
98 }
|
|
99 select();
|
|
100 }
|
|
101 }
|
|
102
|
21
|
103 public void join() {
|
|
104 rWrite(pack( REP_JOIN_CMD, sid, eid, seqid, lineno, "afro"));
|
18
|
105 unpack();
|
31
|
106 eid2 = eid;
|
0
|
107 System.out.println("read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string);
|
|
108 }
|
|
109
|
21
|
110 public void put() {
|
|
111 rWrite(pack(REP_PUT_CMD, sid, eid, seqid, lineno, filename));
|
18
|
112 unpack();
|
0
|
113 System.out.println("read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string);
|
|
114 }
|
|
115
|
21
|
116 public void select() {
|
|
117 rWrite(pack(REP_SELECT_CMD, sid, eid, seqid, lineno, "afro"));
|
18
|
118 unpack();
|
0
|
119 System.out.println("read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string);
|
|
120 }
|
|
121
|
24
|
122 /* public void insert(int offset, int length, String text) {
|
18
|
123 cmd = REP_INSERT_CMD;
|
0
|
124 seqid = (byte)offset;
|
20
|
125 try {
|
|
126 sc.write(pack(cmd, sid, eid, seqid, lineno, text));
|
|
127 } catch (IOException e1) {
|
|
128 e1.printStackTrace();
|
|
129 }
|
17
|
130 try {
|
18
|
131 myCmdList.add(new Rep_Cmd(cmd, sid, eid, seqid, lineno, length, text));
|
|
132 System.out.println("myCmdList : " + myCmdList.toString());
|
17
|
133 } catch (Exception e) {
|
|
134 e.printStackTrace();
|
|
135 }
|
0
|
136 }
|
|
137
|
18
|
138 public void replace(int offset, int length, String text) throws IOException {
|
|
139 cmd = REP_REPLACE_CMD;
|
|
140 seqid = (byte)offset;
|
|
141 sc.write(pack(cmd, sid, eid, seqid, lineno, text));
|
|
142 try {
|
|
143 myCmdList.add(new Rep_Cmd(cmd, sid, eid, seqid, lineno, length, text));
|
|
144 System.out.println("myCmdList : " + myCmdList.toString());
|
|
145 } catch (Exception e) {
|
|
146 e.printStackTrace();
|
|
147 }
|
|
148 }
|
8
|
149
|
18
|
150 public void delete(int offset, int length, String text) throws IOException {
|
|
151 cmd = REP_DELETE_CMD;
|
|
152 seqid = (byte)offset;
|
|
153 sc.write(pack(cmd, sid, eid, seqid, lineno, text));
|
|
154 try {
|
|
155 myCmdList.add(new Rep_Cmd(cmd, sid, eid, seqid, lineno, length, text));
|
|
156 System.out.println("myCmdList : " + myCmdList.toString());
|
|
157 } catch (Exception e) {
|
|
158 e.printStackTrace();
|
|
159 }
|
24
|
160 }*/
|
18
|
161
|
|
162 public ByteBuffer pack(int cmd, int sid, int eid, int seqid, int lineno, String text ) {
|
9
|
163
|
18
|
164 ByteBuffer buffer = ByteBuffer.allocateDirect(24+text.length()*2);
|
9
|
165 buffer.clear(); // position = 0
|
32
|
166 buffer.putInt(cmd); buffer.putInt(sid); buffer.putInt(eid2);
|
8
|
167 buffer.putInt(seqid); buffer.putInt(lineno);
|
9
|
168 buffer.putInt(text.length()*2);
|
8
|
169 //buffer.putString(text);
|
24
|
170 //System.out.println("pack:" + cmd +", "+ "length="+ text.length() +" "+ sid +", "+ eid +", "+ seqid +", "+ lineno);
|
8
|
171
|
|
172 for(int i=0;i<text.length();i++) {
|
|
173 buffer.putChar(text.charAt(i));
|
|
174 }
|
9
|
175 buffer.flip(); // limit = current position, position = 0
|
8
|
176 return buffer;
|
|
177 }
|
9
|
178
|
21
|
179 public void unpack() {
|
17
|
180 ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE);
|
21
|
181 long len = 0;
|
9
|
182 header.clear();
|
21
|
183 try {
|
|
184 len = sc.read(header);
|
|
185 } catch (IOException e1) {
|
|
186 e1.printStackTrace();
|
|
187 } // limit = read length
|
9
|
188 if (len !=HEADER_SIZE) {
|
18
|
189 System.out.println("‚Ä‚·");
|
9
|
190 // this can't happen
|
0
|
191 }
|
9
|
192 header.rewind(); // position = 0
|
8
|
193 /*for(int i = 0; i < 24; i++){
|
9
|
194 readbyte[i] = header.get();
|
0
|
195 //System.out.println(readbyte[i]);
|
8
|
196 }*/
|
|
197 String text = "";
|
9
|
198 cmd = header.getInt();
|
|
199 sid = header.getInt();
|
31
|
200 eid = header.getInt();
|
9
|
201 seqid = header.getInt();
|
|
202 lineno = header.getInt();
|
|
203 textsiz = header.getInt()/2;
|
|
204
|
18
|
205 ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz*2);
|
17
|
206 //buffer.limit(textsiz*2);
|
|
207 //buffer.rewind();
|
21
|
208 try {
|
|
209 len = sc.read(textBuffer);
|
|
210 } catch (IOException e1) {
|
|
211 e1.printStackTrace();
|
|
212 } // limit = read length
|
9
|
213 if (len !=HEADER_SIZE) {
|
|
214 // this can't happen
|
|
215 }
|
18
|
216 textBuffer.rewind();
|
8
|
217 for(int i=0;i<textsiz;i++) {
|
18
|
218 text +=textBuffer.getChar();
|
0
|
219 }
|
8
|
220 string = text;
|
17
|
221 try {
|
29
|
222 if(eid2 != eid) othersCmdList.add(new Rep_Cmd(cmd, sid, eid, seqid, lineno, textsiz, text));
|
17
|
223 } catch (Exception e) {
|
|
224 e.printStackTrace();
|
|
225 }
|
0
|
226 }
|
|
227
|
|
228 public void addSocketListener(RSocketListener socketListener){
|
|
229 Thread th = new Thread(this);
|
|
230 th.start();
|
|
231 this.socketListener = socketListener;
|
|
232 }
|
|
233
|
|
234 public void run() {
|
|
235 while(sc.isConnected()){
|
18
|
236 unpack();
|
0
|
237 System.out.println("read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string);
|
29
|
238 for(Rep_Cmd othersCmd:othersCmdList){
|
|
239 socketListener.packetReceived(new RSocketEvent(othersCmd.cmd, othersCmd.sid, othersCmd.eid, othersCmd.seq, othersCmd.lineno, othersCmd.string.length(), othersCmd.string));
|
|
240 othersCmdList.remove(othersCmd);
|
|
241 }
|
|
242 //socketListener.packetReceived(new RSocketEvent(cmd, sid, eid, seqid, lineno, textsiz, string)); //
|
0
|
243 }
|
|
244
|
|
245 }
|
21
|
246 public void rRead(ByteBuffer buffer){
|
|
247 try {
|
|
248 sc.read(buffer);
|
|
249 } catch (IOException e) {
|
|
250 e.printStackTrace();
|
|
251 }
|
|
252 }
|
|
253 public void rWrite(ByteBuffer buffer){
|
|
254 try {
|
|
255 sc.write(buffer);
|
|
256 } catch (IOException e) {
|
|
257 e.printStackTrace();
|
|
258 }
|
|
259 }
|
0
|
260 public void dispose() {
|
|
261 try {
|
|
262 sc.close();
|
|
263 } catch (IOException e) {
|
|
264 e.printStackTrace();
|
|
265 }
|
|
266 }
|
18
|
267
|
31
|
268 public void sendCmd(int cmd2, int line, int length, String lineText) {
|
25
|
269 //cmd = REP_INSERT_CMD;
|
29
|
270 //rWrite(pack(cmd2, sid, eid, seqid, line, lineText));
|
21
|
271 try {
|
|
272 myCmdList.add(new Rep_Cmd(cmd2, sid, eid, seqid, line, length, lineText));
|
30
|
273 //System.out.println("myCmdList : " + myCmdList.toString());
|
|
274 //System.out.println("othersCmdList : " + othersCmdList.toString());
|
21
|
275 new Translate(myCmdList, othersCmdList);
|
29
|
276 for(Rep_Cmd userCmd:myCmdList){
|
|
277 rWrite(pack(userCmd.cmd, userCmd.sid, userCmd.eid, userCmd.seq, userCmd.lineno, userCmd.string));
|
|
278 myCmdList.remove(userCmd);
|
|
279 }
|
21
|
280 } catch (Exception e) {
|
|
281 e.printStackTrace();
|
|
282 }
|
|
283 }
|
|
284
|
0
|
285 }
|