View Javadoc
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 2002-2016 <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 class Tic
18  {
19      /**
20       * @param args command line arguments
21       * @throws Sim0MQException on error
22       */
23      public static void main(final String[] args) throws Sim0MQException
24      {
25          long time = System.currentTimeMillis();
26          int nummessages = 100000;
27          try (ZContext context = new ZContext(1))
28          {
29              // Socket to talk to server
30              System.out.println("Connecting to server on port 5556...");
31  
32              ZMQ.Socket requester = context.createSocket(ZMQ.REQ);
33              requester.connect("tcp://localhost:5556");
34  
35              for (int i = 0; i < nummessages; i++)
36              {
37                  // send a request
38                  byte[] message = string2byte("TIC");
39                  requester.send(message, 0);
40  
41                  // wait for reply
42                  byte[] reply = requester.recv(0);
43                  String rs = byte2string(reply);
44                  if (!rs.equals("TOC"))
45                  {
46                      System.err.println("Answer was not TOC");
47                  }
48              }
49  
50              // send stop
51              byte[] message = string2byte("STOP");
52              requester.send(message, 0);
53  
54              requester.close();
55              context.destroy();
56          }
57          long delta = (System.currentTimeMillis() - time);
58          System.out.println("RUNTIME = " + delta + " ms");
59          System.out.println("Transactions/second = " + 1000.0 * nummessages / delta + " tps");
60          System.out.println("Messages/second (req + rep) = " + 2000.0 * nummessages / delta + " mps");
61      }
62  
63      /**
64       * Turn String into byte array with closing zero.
65       * @param s the input string
66       * @return byte array with closing zero byte
67       */
68      public static byte[] string2byte(final String s)
69      {
70          byte[] b = s.getBytes();
71          return Arrays.copyOf(b, b.length + 1);
72      }
73  
74      /**
75       * Turn byte array with closing zero into String.
76       * @param b the byte array with closing zero byte
77       * @return String without closing zero
78       */
79      public static String byte2string(final byte[] b)
80      {
81          return new String(Arrays.copyOf(b, b.length - 1));
82      }
83  
84  }