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