1 package org.sim0mq.test;
2
3 import java.util.Arrays;
4
5 import org.sim0mq.Sim0MQException;
6 import org.zeromq.ZContext;
7 import org.zeromq.ZMQ;
8
9 /**
10 * Client example for JeroMQ / ZeroMQ.
11 * <p>
12 * (c) copyright 2015-2019 <a href="http://www.simulation.tudelft.nl">Delft University of Technology</a>. <br>
13 * BSD-style license. See <a href="http://www.simulation.tudelft.nl/dsol/3.0/license.html">DSOL License</a>. <br>
14 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
15 * @version Oct 21, 2016
16 */
17 public final class TicPush
18 {
19 /** */
20 private TicPush()
21 {
22 // Utility Class.
23 }
24
25 /**
26 * @param args command line arguments
27 * @throws Sim0MQException on error
28 */
29 public static void main(final String[] args) throws Sim0MQException
30 {
31 long time = System.currentTimeMillis();
32 try (ZContext context = new ZContext(1))
33 {
34 // Socket to talk to server
35 System.out.println("Connecting to server on port 5556...");
36
37 ZMQ.Socket requester = context.createSocket(ZMQ.REQ);
38 requester.connect("tcp://localhost:5556");
39
40 for (int i = 0; i < 100000; i++)
41 {
42 // send a request
43 byte[] message = string2byte("TIC");
44 requester.send(message, 0);
45
46 // wait for reply
47 byte[] reply = requester.recv(0);
48 String rs = byte2string(reply);
49 if (!rs.equals("TOC"))
50 {
51 System.err.println("Answer was not TOC");
52 }
53 }
54
55 // send stop
56 byte[] message = string2byte("STOP");
57 requester.send(message, 0);
58
59 requester.close();
60 context.destroy();
61 }
62 System.out.println("RUNTIME = " + (System.currentTimeMillis() - time) + " ms");
63 }
64
65 /**
66 * Turn String into byte array with closing zero.
67 * @param s the input string
68 * @return byte array with closing zero byte
69 */
70 public static byte[] string2byte(final String s)
71 {
72 byte[] b = s.getBytes();
73 return Arrays.copyOf(b, b.length + 1);
74 }
75
76 /**
77 * Turn byte array with closing zero into String.
78 * @param b the byte array with closing zero byte
79 * @return String without closing zero
80 */
81 public static String byte2string(final byte[] b)
82 {
83 return new String(Arrays.copyOf(b, b.length - 1));
84 }
85
86 }