-
Notifications
You must be signed in to change notification settings - Fork 489
/
Copy pathmtrelay.java
112 lines (89 loc) · 2.74 KB
/
mtrelay.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
package guide;
import org.zeromq.SocketType;
import org.zeromq.ZContext;
import org.zeromq.ZMQ.Socket;
/**
* Multithreaded relay
*/
public class mtrelay
{
private static class Step1 extends Thread
{
private final ZContext context;
private Step1(ZContext context)
{
this.context = context;
}
@Override
public void run()
{
// Signal downstream to step 2
Socket xmitter = context.createSocket(SocketType.PAIR);
xmitter.connect("inproc://step2");
System.out.println("Step 1 ready, signaling step 2");
xmitter.send("READY", 0);
xmitter.close();
}
}
private static class Step2 extends Thread
{
private final ZContext context;
private Step2(ZContext context)
{
this.context = context;
}
@Override
public void run()
{
// Bind to inproc: endpoint, then start upstream thread
Socket receiver = context.createSocket(SocketType.PAIR);
receiver.bind("inproc://step2");
// Wait for signal
receiver.recv(0);
receiver.close();
// Connect to step3 and tell it we're ready
Socket xmitter = context.createSocket(SocketType.PAIR);
xmitter.connect("inproc://step3");
System.out.println("Step 2 ready, signaling step 3");
xmitter.send("READY", 0);
xmitter.close();
}
}
private static class Step3 extends Thread
{
private final ZContext context;
private Step3(ZContext context)
{
this.context = context;
}
@Override
public void run()
{
// Bind to inproc: endpoint, then start upstream thread
Socket receiver = context.createSocket(SocketType.PAIR);
receiver.bind("inproc://step3");
// Wait for signal
receiver.recv(0);
receiver.close();
System.out.println("Step 3 ready");
}
}
public static void main(String[] args) throws InterruptedException
{
try (ZContext context = new ZContext()) {
// Step 1 signals to step 2
Thread step1 = new Step1(context);
step1.start();
// Step 2 relays the signal from step 1 to step 3
Thread step2 = new Step2(context);
step2.start();
// Step 3 waits for signal from step 2
Thread step3 = new Step3(context);
step3.start();
step1.join();
step2.join();
step3.join();
System.out.println("Test successful!");
}
}
}