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