-
Notifications
You must be signed in to change notification settings - Fork 489
/
Copy pathlruqueue3.java
186 lines (151 loc) · 5.39 KB
/
lruqueue3.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
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
package guide;
import java.util.LinkedList;
import java.util.Queue;
import org.zeromq.*;
import org.zeromq.ZMQ.PollItem;
import org.zeromq.ZMQ.Socket;
class ClientThread3 extends Thread
{
@Override
public void run()
{
// Prepare our context and sockets
try (ZContext context = new ZContext()) {
Socket client = context.createSocket(SocketType.REQ);
// Initialize random number generator
client.connect("ipc://frontend.ipc");
// Send request, get reply
while (true) {
client.send("HELLO".getBytes(ZMQ.CHARSET), 0);
byte[] data = client.recv(0);
if (data == null)
break;
String reply = new String(data, ZMQ.CHARSET);
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
}
System.out.println(
Thread.currentThread().getName() + " Client Sent HELLO"
);
}
}
}
}
class WorkerThread3 extends Thread
{
@Override
public void run()
{
// Prepare our context and sockets
try (ZContext context = new ZContext()) {
Socket worker = context.createSocket(SocketType.REQ);
worker.connect("ipc://backend.ipc");
ZFrame frame = new ZFrame(lruqueue3.LRU_READY);
// Tell backend we're ready for work
frame.send(worker, 0);
while (true) {
ZMsg msg = ZMsg.recvMsg(worker);
if (msg == null)
break;
msg.getLast().reset("OK".getBytes(ZMQ.CHARSET));
msg.send(worker);
System.out.println(
Thread.currentThread().getName() + " Worker Sent OK"
);
}
}
}
}
//Our LRU queue structure, passed to reactor handlers
class LRUQueueArg
{
Socket frontend; // Listen to clients
Socket backend; // Listen to workers
Queue<ZFrame> workers; // List of ready workers
}
//In the reactor design, each time a message arrives on a socket, the
//reactor passes it to a handler function. We have two handlers; one
//for the frontend, one for the backend:
class FrontendHandler implements ZLoop.IZLoopHandler
{
@Override
public int handle(ZLoop loop, PollItem item, Object arg_)
{
LRUQueueArg arg = (LRUQueueArg) arg_;
ZMsg msg = ZMsg.recvMsg(arg.frontend);
if (msg != null) {
msg.wrap(arg.workers.poll());
msg.send(arg.backend);
// Cancel reader on frontend if we went from 1 to 0 workers
if (arg.workers.isEmpty()) {
PollItem poller = new PollItem(arg.frontend, ZMQ.Poller.POLLIN);
loop.removePoller(poller);
}
}
return 0;
}
}
class BackendHandler implements ZLoop.IZLoopHandler
{
@Override
public int handle(ZLoop loop, PollItem item, Object arg_)
{
LRUQueueArg arg = (LRUQueueArg) arg_;
ZMsg msg = ZMsg.recvMsg(arg.backend);
if (msg != null) {
ZFrame address = msg.unwrap();
// Queue worker address for LRU routing
arg.workers.add(address);
// Enable reader on frontend if we went from 0 to 1 workers
if (arg.workers.size() == 1) {
PollItem poller = new PollItem(arg.frontend, ZMQ.Poller.POLLIN);
loop.addPoller(poller, lruqueue3.handle_frontend, arg);
}
// Forward message to client if it's not a READY
ZFrame frame = msg.getFirst();
if (new String(frame.getData(), ZMQ.CHARSET).equals(lruqueue3.LRU_READY))
msg.destroy();
else msg.send(arg.frontend);
}
return 0;
}
}
public class lruqueue3
{
public final static String LRU_READY = "\001";
protected final static FrontendHandler handle_frontend = new FrontendHandler();
protected final static BackendHandler handle_backend = new BackendHandler();
public static void main(String[] args)
{
// Prepare our context and sockets
try (ZContext context = new ZContext()) {
LRUQueueArg arg = new LRUQueueArg();
Socket frontend = context.createSocket(SocketType.ROUTER);
Socket backend = context.createSocket(SocketType.ROUTER);
arg.frontend = frontend;
arg.backend = backend;
frontend.bind("ipc://frontend.ipc");
backend.bind("ipc://backend.ipc");
int client_nbr;
for (client_nbr = 0; client_nbr < 10; client_nbr++)
new ClientThread3().start();
int worker_nbr;
for (worker_nbr = 0; worker_nbr < 3; worker_nbr++)
new WorkerThread3().start();
// Queue of available workers
arg.workers = new LinkedList<>();
// Prepare reactor and fire it up
ZLoop reactor = new ZLoop(context);
reactor.verbose(true);
PollItem poller = new PollItem(arg.backend, ZMQ.Poller.POLLIN);
reactor.addPoller(poller, handle_backend, arg);
reactor.start();
for (ZFrame frame : arg.workers) {
frame.destroy();
}
}
System.exit(0);
}
}