3 import java
.io
.Closeable
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.io
.OutputStream
;
7 import java
.net
.InetSocketAddress
;
8 import java
.net
.Socket
;
9 import java
.nio
.Buffer
;
10 import java
.nio
.ByteBuffer
;
11 import java
.nio
.channels
.ByteChannel
;
12 import java
.nio
.channels
.SocketChannel
;
13 import java
.util
.Arrays
;
14 import java
.util
.Timer
;
15 import java
.util
.TimerTask
;
17 import pursuer
.pxprpc
.AsyncReturn
;
18 import pursuer
.pxprpc
.EventDispatcher
;
19 import pursuer
.pxprpc
.Serilizer2
;
20 import pursuer
.pxprpc
.Utils
;
21 import pursuer
.pxprpc_ex
.TCPBackend
;
25 //Just for test, will ignore session check.
28 //Rpc server handler demo.
29 public static class Handler1
{
30 public int getInt2345() {
33 public String
get1234() {
36 public void printString(String s
) {
37 System
.out
.println(s
);
39 public void print5678() {
40 System
.out
.println("5678");
42 public Closeable
printWhenFree() {
43 return new Closeable() {
45 public void close() throws IOException
{
46 System
.out
.println("free by server gc");
50 public void printArg(int a
,long b
,float c
,double d
,ByteBuffer e
) {
51 System
.out
.println(a
+","+b
+","+c
+","+d
+","+
52 Arrays
.toString(Utils
.bytesGet(e
, 0, 4)));
54 public void printArg2(byte[] bb
){
55 Serilizer2 ser
= new Serilizer2().prepareUnseriling(ByteBuffer
.wrap(bb
));
58 float c
=ser
.getFloat();
59 double d
=ser
.getDouble();
60 String e
=ser
.getString();
61 byte[] f
=ser
.getBytes();
62 System
.out
.println(a
+","+b
+","+c
+","+d
+","+e
+","+ Arrays
.toString(f
));
64 public TickEvent
onTick() {
65 TickEvent te
= new TickEvent();
69 public void waitOneTick(final AsyncReturn
<Object
> asyncRet
) {
71 tm
.schedule(new TimerTask() {
74 asyncRet
.result("one tick done");
78 public void throwError() throws IOException
{
79 throw new IOException("dummy exception");
82 public static class TickEvent
extends EventDispatcher
{
87 tm
.schedule(new TimerTask() {
90 TickEvent
.this.fireEvent("tick");
99 public static void main(String
[] args
) {
102 final TCPBackend pxptcp
= new TCPBackend();
104 pxptcp
.funcMap
.put("test1", new Handler1());
105 pxptcp
.bindAddr
=new InetSocketAddress(listenPort
);
106 Thread th
=new Thread(new Runnable() {
110 pxptcp
.listenAndServe();
111 } catch (IOException e
) {
113 if(!e
.getMessage().contains("Interrupted function call")) {
117 System
.out
.println("server stoped");
123 System
.out
.println("waiting for server start");
126 } catch (InterruptedException e
) {
129 SocketChannel soc
=SocketChannel
.open();
130 soc
.configureBlocking(true);
131 soc
.connect(new InetSocketAddress("localhost",listenPort
));
133 ClientContext client
= new ClientContext();
137 System
.out
.println("##connected server info:\n"+client
.getInfo());
140 // set *12 = getFunc test1.getInt2345
141 client
.getFunc(12, "test1.getInt2345");
142 System
.out
.println("expect print 2345");
143 // set *11 = call *12
144 System
.out
.println(client
.callIntFunc(11, 12, new Object
[0]));
145 // set *11 = getFunc test1.print5678
146 client
.getFunc(11,"test1.print5678");
147 System
.out
.println("expect print 5678");
148 // set *12 = call *11
149 client
.callIntFunc(12, 11, new Object
[0]);
150 //set *11 = getFunc test1.get1234
151 client
.getFunc(11, "test1.get1234");
152 // set *12 = call *11
153 client
.callIntFunc(12,11,new Object
[0]);
154 //set *13 = getFunc test1.printString
155 client
.getFunc(13, "test1.printString");
156 //set *14 = call *13 (*12)
157 //currently, 12 slot store the value return by test1.get1234
158 System
.out
.println("expect print 1234");
159 client
.callIntFunc(14,13,new Object
[] {12});
161 client
.push(12, "pxprpc".getBytes("utf-8"));
162 //should print pxprpc
163 System
.out
.println("expect print pxprpc\\n0");
164 System
.out
.println(client
.callIntFunc(14,13,new Object
[] {12}));
166 //set *11 = getFunc test1.printArg
167 client
.getFunc(11, "test1.printArg");
169 client
.push(12, new byte[] {1,2,3,4});
170 //call *11(111,2222222,3.14,3.1415,*12)
171 System
.out
.println("expect 111,2222222,3.14,3.1415,[1,2,3,4]");
172 client
.callIntFunc(13, 11, new Object
[] {111,2222222l,3.14f
,3.1415d
,12});
175 //set *11 = getFunc test1.printArg2
176 client
.getFunc(11, "test1.printArg2");
179 client
.push(12,Utils
.toBytes(new Serilizer2().prepareSerilizing(64)
180 .putInt(111).putLong(2222222l).putFloat(3.14f
).putDouble(3.1415d
)
181 .putString("abcd").putBytes(new byte[]{1,2,3,4},0,4).build()));
183 System
.out
.println("expect 111,2222222,3.14,3.1415,abcd,[1,2,3,4]");
184 client
.callIntFunc(13, 11, new Object
[] {12});
187 System
.out
.println("sleep 1 tick");
188 //set *11=getFunc test1.waitOneTick
189 client
.getFunc(11, "test1.waitOneTick");
190 client
.callIntFunc(12,11,new Object
[] {});
191 System
.out
.println(new String(client
.pull(12),"utf-8"));
193 //set *11=getFunc test1.onTick
194 client
.getFunc(11, "test1.onTick");
195 //set *12=test1.onTick()
196 System
.out
.println(client
.callIntFunc(12,11,new Object
[0]));
197 //set *13=(*12).next()
198 client
.callIntFunc(13,12,new Object
[0]);
199 System
.out
.println(new String(client
.pull(13),"utf-8"));
200 //set *13=(*12).next()
201 client
.callIntFunc(13,12,new Object
[0]);
202 System
.out
.println(new String(client
.pull(13),"utf-8"));
203 //set *13=(*12).next()
204 client
.callIntFunc(13,12,new Object
[0]);
205 System
.out
.println(new String(client
.pull(13),"utf-8"));
207 System
.out
.println("check exception test,expect '1' 'dummy exception'");
208 //set *11=getFunc test1.throwError
209 client
.getFunc(11, "test1.throwError");
211 System
.out
.print(client
.callIntFunc(12, 11, new Object
[0]));
212 //set *13=getFunc builtIn.checkException
213 if(client
.getFunc(13, "builtin.checkException")==0) {
214 System
.out
.println("builtin.checkException not found");
217 client
.callIntFunc(14, 13, new Object
[] {12});
218 System
.out
.println(new String(client
.pull(14),"utf-8"));
223 //set *11 = getFunc test1.printString
224 client
.getFunc(11,"test1.printWhenFree");
226 client
.callIntFunc(12, 11, new Object
[0]);
227 //push to 12, so *11 should be free if server support.
228 System
.out
.println("expect print 'free by server gc' if server support");
229 client
.push(12, new byte[0]);
232 //should be free when connection close, if server support.
233 client
.callIntFunc(12, 11, new Object
[0]);
235 System
.out
.println("close server...");
239 } catch (InterruptedException e
) {
242 } catch (IOException e
) {
248 public static class ClientContext
{
249 public ByteChannel chan
;
251 public void init(ByteChannel chan
) {
254 public void assert2(boolean b
) {
256 throw new RuntimeException("assert error");
259 public int session
=0x78593<<8;
261 public void push(int addr
,byte[] data
) throws IOException
{
263 Utils
.writeInt32(chan
,op
);
264 Utils
.writeInt32(chan
,addr
);
265 Utils
.writeInt32(chan
, data
.length
);
266 Utils
.writef(chan
,ByteBuffer
.wrap(data
));
267 int op2
=Utils
.readInt32(chan
);
270 public byte[] pull(int addr
) throws IOException
{
272 Utils
.writeInt32(chan
,op
);
273 Utils
.writeInt32(chan
,addr
);
274 assert2(Utils
.readInt32(chan
)==op
);
275 int len
=Utils
.readInt32(chan
);
276 byte[] r
=new byte[len
];
277 Utils
.readf(chan
,ByteBuffer
.wrap(r
));
280 public int callIntFunc(int assignAddr
,int addr
,Object
[] params
) throws IOException
{
282 Utils
.writeInt32(chan
,op
);
283 Utils
.writeInt32(chan
,assignAddr
);
284 Utils
.writeInt32(chan
,addr
);
285 for(Object p
: params
) {
286 if(p
.getClass().equals(Integer
.class)) {
287 Utils
.writeInt32(chan
,(Integer
) p
);
288 }else if(p
.getClass().equals(Long
.class)) {
289 Utils
.writeInt64(chan
,(Long
) p
);
290 }else if(p
.getClass().equals(Float
.class)) {
291 Utils
.writeFloat32(chan
,(Float
) p
);
292 }else if(p
.getClass().equals(Double
.class)) {
293 Utils
.writeFloat64(chan
,(Double
) p
);
295 throw new UnsupportedOperationException();
298 assert2(Utils
.readInt32(chan
)==op
);
299 return Utils
.readInt32(chan
);
301 public int getFunc(int assignAddr
,String name
) throws IOException
{
303 push(1, name
.getBytes());
304 Utils
.writeInt32(chan
,op
);
305 Utils
.writeInt32(chan
,assignAddr
);
306 Utils
.writeInt32(chan
, 1);
307 assert2(Utils
.readInt32(chan
)==op
);
308 return Utils
.readInt32(chan
);
310 public String
getInfo()throws IOException
{
312 Utils
.writeInt32(chan
,op
);
313 assert2(Utils
.readInt32(chan
)==op
);
314 int len
=Utils
.readInt32(chan
);
315 byte[] r
=new byte[len
];
316 Utils
.readf(chan
,ByteBuffer
.wrap(r
));
317 return new String(r
,"utf-8");