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