-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathTestIPC.java
More file actions
240 lines (223 loc) · 6.66 KB
/
Copy pathTestIPC.java
File metadata and controls
240 lines (223 loc) · 6.66 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
229
230
231
232
233
234
235
236
237
238
239
240
package javaforce.tests;
import java.util.*;
import javaforce.*;
import javaforce.ffm.*;
import javaforce.ipc.*;
import javaforce.ipc.transport.*;
/** Test IPC (DBus)
*
* @author pquiring
*/
public class TestIPC {
private static boolean debug = false;
private static boolean debug_callback = false;
private static boolean use_tcp = false;
private static int tcp_port = 8001;
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("usage:TestIPC {server | client} [options]");
System.out.println(" client options : [--threads=#] [--delay=#]");
return;
}
switch (args[0]) {
case "server": server(); break;
case "client": client(args); break;
default: System.out.println("Unknown option:" + args[0]);
}
}
private static void server() {
DBusTransport transport;
try {
if (use_tcp) {
transport = new TCPTransport(tcp_port);
} else {
transport = DBus.createTransport();
}
TestEndPoint ep = new TestEndPoint("javaforce.TestIPC.Server");
IPC ipc = new DBus(ep, transport);
ep.setIPC(ipc);
if (!ipc.connect()) {
JFLog.log("IPC.connect() failed");
return;
}
while (true) {
JF.sleep(1000);
}
} catch (Exception e) {
JFLog.log(e);
}
}
private static long success = 0;
private static long error = 0;
private static void client(String[] args) {
int threads = 1;
int delay = 1000;
for(String arg : args) {
if (arg.startsWith("--threads=")) {
threads = Integer.valueOf(arg.substring(10));
} else if (arg.startsWith("--delay=")) {
delay = Integer.valueOf(arg.substring(8));
}
}
for(int a=0;a<threads;a++) {
Client client = new Client(delay);
client.start();
}
while (true) {
JF.sleep(1000);
System.out.print(String.format("\rsuccess = %d : error = %d", success, error));
}
}
private static class Client extends Thread {
private int delay;
private Random r = new Random();
private IPC ipc;
private TestEndPoint ep;
public Client(int delay) {
this.delay = delay;
}
public void run() {
DBusTransport transport;
try {
if (use_tcp) {
transport = new TCPTransport(tcp_port);
} else {
transport = DBus.createTransport();
}
ep = new TestEndPoint(null);
ipc = new DBus(ep, transport);
if (!ipc.connect()) {
JFLog.log("IPC.connect() failed");
return;
}
ep.setIPC(ipc);
while (true) {
try {
ping();
modify();
process();
getString();
} catch (Exception e) {
JFLog.log(e);
}
JF.sleep(delay);
}
} catch (Exception e) {
JFLog.log(e);
}
}
/** Test pinging server. */
public void ping() throws Exception {
int value = r.nextInt();
if (debug) JFLog.log(String.format("ping(0x%x)", value));
Object result = ipc.invoke("javaforce.TestIPC.Server", "ping", value);
if (result == null) {
if (debug) JFLog.log("result == null");
error++;
} else {
if (debug) JFLog.log("result = " + result);
success++;
}
}
/** Test sending/receiving byte array. */
public void modify() throws Exception {
byte[] data = new byte[3];
data[0] = 0x11;
data[1] = 0x22;
data[2] = 0x33;
if (debug) JFLog.log(String.format("modify(byte[] {%x %x %x})", data[0], data[1], data[2]));
byte[] result = (byte[])ipc.invoke("javaforce.TestIPC.Server", "modify", data);
if (result == null) {
if (debug) JFLog.log("result == null");
error++;
} else {
if (debug) JFLog.log(String.format("result = %x %x %x", result[0], result[1], result[2]));
success++;
}
}
private static final int callbacks = 3;
/** Test long process on server with callbacks. */
public void process() throws Exception {
ep.callback_count = 0;
Object result = ipc.invoke("javaforce.TestIPC.Server", "process", ipc.getBusName(), callbacks);
if (result == null) {
if (debug) JFLog.log("result == null");
error++;
} else {
if (ep.callback_count != callbacks) {
if (debug) JFLog.log("callback_count != " + callbacks);
error++;
} else {
if (debug) JFLog.log("result = " + result);
success++;
}
}
}
public void getString() throws Exception {
Object result = ipc.invoke("javaforce.TestIPC.Server", "getString");
}
}
public static class TestEndPoint implements EndPoint {
public TestEndPoint(String name) {
this.name = name;
dispatcher = new Dispatcher(new Methods());
}
private String name;
private Dispatcher dispatcher;
private IPC ipc;
public int callback_count;
public String getEndPointName() {
return name;
}
public Object dispatch(String method, Object[] args) throws Exception {
return dispatcher.dispatch(method, args);
}
public void setIPC(IPC ipc) {
this.ipc = ipc;
}
public class Methods {
/** ping */
public boolean ping(int value) {
if (debug) JFLog.log(String.format("ping(0x%x)", value));
return true;
}
/** Modifies the byte array and returns it. */
public byte[] modify(byte[] data) {
if (debug) JFLog.log(String.format("modify(byte[] {%x %x %x})", data[0], data[1], data[2]));
data[0] = 0x44;
data[1] = 0x55;
data[2] = 0x66;
return data;
}
/** Process is a long process that will call callback() on the caller.
* This method MUST return before the timeout occurs on the caller
* or the caller will generate an exception
* and will not receive the return value (default = 30 seconds).
*/
public boolean process(String name, int cnt) {
if (ipc != null) {
for(int a=0;a<cnt;a++) {
try {
ipc.invoke(name, "callback", "count=" + a);
} catch (Exception e) {
JFLog.log(e);
}
}
}
return true;
}
/** callback to the caller from process() */
public boolean callback(String value) {
if (debug_callback) JFLog.log("callback:" + value);
callback_count++;
return true;
}
private StringBuilder str = new StringBuilder();
/** Returns String that grows with each call. */
public String getString() {
str.append("0123456789abcdef");
return str.toString();
}
}
}
}