annotate src/remoteeditor/network/REP.java @ 10:e40c010c703b

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