View Javadoc
1   package org.sim0mq.test;
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          try (ZContext context = new ZContext(1))
35          {
36              // Socket to talk to server
37              System.out.println("Connecting to server on port 5556...");
38  
39              ZMQ.Socket requester = context.createSocket(SocketType.REQ);
40              requester.connect("tcp://localhost:5556");
41  
42              for (int i = 0; i < nummessages; i++)
43              {
44                  // send a request
45                  byte[] message = string2byte("TIC");
46                  requester.send(message, 0);
47  
48                  // wait for reply
49                  byte[] reply = requester.recv(0);
50                  String rs = byte2string(reply);
51                  if (!rs.equals("TOC"))
52                  {
53                      System.err.println("Answer was not TOC");
54                  }
55              }
56  
57              // send stop
58              byte[] message = string2byte("STOP");
59              requester.send(message, 0);
60  
61              requester.close();
62              context.destroy();
63          }
64          long delta = (System.currentTimeMillis() - time);
65          System.out.println("RUNTIME = " + delta + " ms");
66          System.out.println("Transactions/second = " + 1000.0 * nummessages / delta + " tps");
67          System.out.println("Messages/second (req + rep) = " + 2000.0 * nummessages / delta + " mps");
68      }
69  
70      /**
71       * Turn String into byte array with closing zero.
72       * @param s the input string
73       * @return byte array with closing zero byte
74       */
75      public static byte[] string2byte(final String s)
76      {
77          byte[] b = s.getBytes();
78          return Arrays.copyOf(b, b.length + 1);
79      }
80  
81      /**
82       * Turn byte array with closing zero into String.
83       * @param b the byte array with closing zero byte
84       * @return String without closing zero
85       */
86      public static String byte2string(final byte[] b)
87      {
88          return new String(Arrays.copyOf(b, b.length - 1));
89      }
90  
91  }