-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPxpRpc.java
More file actions
228 lines (210 loc) · 6.43 KB
/
PxpRpc.java
File metadata and controls
228 lines (210 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package pxprpc.test;
import java.io.Closeable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.*;
import pxprpc.backend.TCPBackend;
import pxprpc.base.*;
import pxprpc.extend.*;
public class PxpRpc {
//Just for test, will ignore session check.
//Rpc server handler demo.
public static class Handler1 {
public Object get1234(){
return "1234";
}
public void printString(Object s){
System.out.println(s);
}
@MethodTypeDecl("cilfdb->il")
public Object[] testPrintArg(AsyncReturn<Object[]> r,boolean a,int b,long c,float d,double e,ByteBuffer f){
System.out.println(""+a+","+b+","+c+","+d+","+e+","+new String(Utils.toBytes(f), ServerContext.charset));
r.resolve(new Object[]{100,1234567890l});
return null;
}
public void testUnser(ByteBuffer buf){
Serializer2 ser = new Serializer2().prepareUnserializing(buf);
System.out.println(","+ser.getInt()+","+ser.getLong()+","+ser.getFloat()+","+ser.getDouble()+","+ser.getString()+","+ser.getString());
}
public void testTableUnser(ByteBuffer buf){
List<Map<String, Object>> maparr = new TableSerializer().load(buf).toMapArray();
for(Map<String, Object> e:maparr){
for(Map.Entry<String, Object> kv:e.entrySet()){
System.out.print(kv.getKey()+":");
System.out.print(kv.getValue());
System.out.print(" ");
}
System.out.println();
}
}
public Object testNone(AsyncReturn<Object> r,Object para){
System.out.print("expect null:");
System.out.println(para);
r.resolve(null);
return new Object();
}
public TickEvent onTick() {
TickEvent te = new TickEvent();
te.start();
return te;
}
public static Timer tm=new Timer();
//as Parameter class is support since 1.8, We have to use return type to identify the typedecl even for async callable.
public String wait1Sec(final AsyncReturn<Object> asyncRet) {
tm.schedule(new TimerTask() {
@Override
public void run() {
asyncRet.resolve("tick");
}
}, 1000);
return null;
}
public void raiseError1() throws IOException {
throw new IOException("dummy exception");
}
public int testSignal(final AsyncReturn<Integer> aret,String s){
System.out.println("test signal:"+s);
tm.schedule(new TimerTask() {
@Override
public void run() {
aret.resolve(0);
}
},3000);
return 0;
}
public Closeable autoCloseable(){
return new Closeable(){
@Override
public void close() throws IOException {
System.out.println("auto closeable closed");
}
};
}
}
public static class TickEvent extends EventDispatcher implements Closeable {
boolean closed=false;
public TickEvent() {
setEventType(String.class);
}
public void start() {
Handler1.tm.schedule(new TimerTask() {
@Override
public void run() {
TickEvent.this.fireEvent("tick");
}
}, 1000,1000);
}
}
public static class Cfg{
public int id;
public long size;
public String name;
public boolean isdir;
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("id:");
sb.append(id);
sb.append(",size:");
sb.append(size);
sb.append(",name:");
sb.append(name);
sb.append(",isdir:");
sb.append(isdir);
return sb.toString();
}
}
public static void tabserTest() {
Cfg t1 = new Cfg();
t1.id=12;
t1.isdir=false;
t1.name="myfile.txt";
t1.size=1231l;
ArrayList<Cfg> t2 = new ArrayList<Cfg>();
t2.add(t1);
ByteBuffer sered = new TableSerializer().fromTypedObjectArray(t2).build();
System.out.println(new TableSerializer().load(sered).toTypedObjectArray(Cfg.class));
}
public static void main(String[] args) {
tabserTest();
final TCPBackend pxptcp = new TCPBackend();
int listenPort=1064;
DefaultFuncMap.registered.put("test1", new Handler1());
pxptcp.bindAddr=new InetSocketAddress(listenPort);
Thread th=new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("ready...");
pxptcp.listenAndServe();
} catch (IOException e) {
e.printStackTrace();
if(!e.getMessage().contains("Interrupted function call")) {
e.printStackTrace();
}
}
System.out.println("server stoped");
}
});
th.start();
try {
/* client simple test */
Thread.sleep(500);
AbstractIo clientIo = pxptcp.clientConnect(new InetSocketAddress(InetAddress.getByAddress(new byte[]{127, 0, 0, 1}), listenPort));
RpcExtendClient1 client1 = new RpcExtendClient1(new ClientContext().backend1(clientIo)).init();
RpcExtendClientCallable get1234 = client1.getFunc("test1.get1234").typedecl("->o");
RpcExtendClientCallable printString = client1.getFunc("test1.printString").typedecl("o->");;
Object[] s1234 = get1234.callBlock();
printString.callBlock(s1234);
RpcExtendClientCallable testTableUnser=client1.getFunc("test1.testTableUnser").typedecl("b->");
Cfg t1 = new Cfg();
t1.id=12;
t1.isdir=false;
t1.name="myfile.txt";
t1.size=1231l;
ArrayList<Cfg> t2 = new ArrayList<Cfg>();
t2.add(t1);
ByteBuffer sered = new TableSerializer().fromTypedObjectArray(t2).build();
testTableUnser.callBlock(sered);
RpcExtendClientCallable wait1Sec=client1.getFunc("test1.wait1Sec");
wait1Sec.typedecl("->s");
System.out.println("print tick after 1 seconds");
System.out.println("wait done"+wait1Sec.callBlock()[0]);
RpcExtendClientCallable getOnTick=client1.getFunc("test1.onTick");
getOnTick.typedecl("->o");
RpcExtendClientCallable onTick = ((RpcExtendClientObject) getOnTick.callBlock()[0]).asCallable();
onTick.typedecl("->s");
System.out.println("tick 3 times");
onTick.poll(new RpcExtendClientCallable.Ret() {
@Override
public void cb(Object[] r, RemoteError err) {
if(err!=null){
err.printStackTrace();
System.out.println("poll stoped.");
}else {
System.out.println(r[0]);
}
}
});
Thread.sleep(3500);
onTick.free();
Thread.sleep(1000);
RpcExtendClientCallable testSignal=client1.getFunc("test1.testSignal");
testSignal.typedecl("s->");
testSignal.signal("signal");
try{
RpcExtendClientCallable raiseError1 = client1.getFunc("test1.raiseError1").typedecl("->");
raiseError1.callBlock(new Object[]{});
}catch(RemoteError e){
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}