forked from booksbyus/zguide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tripping.java
151 lines (124 loc) · 4.7 KB
/
tripping.java
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
import org.zeromq.ZContext;
import org.zeromq.ZFrame;
import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Socket;
import org.zeromq.ZMsg;
/**
* Round-trip demonstrator. Broker, Worker and Client are mocked as separate
* threads.
*
* @author Arkadiusz Orzechowski <[email protected]>
*/
public class tripping {
static class Broker implements Runnable {
@Override
public void run() {
ZContext ctx = new ZContext();
Socket frontend = ctx.createSocket(ZMQ.ROUTER);
Socket backend = ctx.createSocket(ZMQ.ROUTER);
frontend.bind("tcp://*:5555");
backend.bind("tcp://*:5556");
while (!Thread.currentThread().isInterrupted()) {
ZMQ.Poller items = ctx.getContext().poller();
items.register(frontend, ZMQ.Poller.POLLIN);
items.register(backend, ZMQ.Poller.POLLIN);
if (items.poll() == -1)
break; // Interrupted
if (items.pollin(0)) {
ZMsg msg = ZMsg.recvMsg(frontend);
if (msg == null)
break; // Interrupted
ZFrame address = msg.pop();
address.destroy();
msg.addFirst(new ZFrame("W"));
msg.send(backend);
}
if (items.pollin(1)) {
ZMsg msg = ZMsg.recvMsg(backend);
if (msg == null)
break; // Interrupted
ZFrame address = msg.pop();
address.destroy();
msg.addFirst(new ZFrame("C"));
msg.send(frontend);
}
}
ctx.destroy();
}
}
static class Worker implements Runnable {
@Override
public void run() {
ZContext ctx = new ZContext();
Socket worker = ctx.createSocket(ZMQ.DEALER);
worker.setIdentity("W".getBytes());
worker.connect("tcp://localhost:5556");
while (!Thread.currentThread().isInterrupted()) {
ZMsg msg = ZMsg.recvMsg(worker);
msg.send(worker);
}
ctx.destroy();
}
}
static class Client implements Runnable {
private static int SAMPLE_SIZE = 10000;
@Override
public void run() {
ZContext ctx = new ZContext();
Socket client = ctx.createSocket(ZMQ.DEALER);
client.setIdentity("C".getBytes());
client.connect("tcp://localhost:5555");
System.out.println("Setting up test...");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
int requests;
long start;
System.out.println("Synchronous round-trip test...");
start = System.currentTimeMillis();
for (requests = 0; requests < SAMPLE_SIZE; requests++) {
ZMsg req = new ZMsg();
req.addString("hello");
req.send(client);
ZMsg.recvMsg(client).destroy();
}
System.out.printf(" %d calls/second\n",
(1000 * SAMPLE_SIZE) / (System.currentTimeMillis() - start));
System.out.println("Asynchronous round-trip test...");
start = System.currentTimeMillis();
for (requests = 0; requests < SAMPLE_SIZE; requests++) {
ZMsg req = new ZMsg();
req.addString("hello");
req.send(client);
}
for (requests = 0; requests < SAMPLE_SIZE
&& !Thread.currentThread().isInterrupted(); requests++) {
ZMsg.recvMsg(client).destroy();
}
System.out.printf(" %d calls/second\n",
(1000 * SAMPLE_SIZE) / (System.currentTimeMillis() - start));
ctx.destroy();
}
}
public static void main(String[] args) {
if (args.length==1)
Client.SAMPLE_SIZE = Integer.parseInt(args[0]);
Thread brokerThread = new Thread(new Broker());
Thread workerThread = new Thread(new Worker());
Thread clientThread = new Thread(new Client());
brokerThread.setDaemon(true);
workerThread.setDaemon(true);
brokerThread.start();
workerThread.start();
clientThread.start();
try {
clientThread.join();
workerThread.interrupt();
brokerThread.interrupt();
Thread.sleep(200);// give them some time
} catch (InterruptedException closingAnyway) {
}
}
}