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