View Javadoc
1   package org.sim0mq.test;
2   
3   import org.sim0mq.Sim0MQException;
4   import org.zeromq.ZContext;
5   import org.zeromq.ZMQ;
6   
7   /**
8    * Server example for JeroMQ / ZeroMQ.
9    * <p>
10   * (c) copyright 2002-2016 <a href="http://www.simulation.tudelft.nl">Delft University of Technology</a>. <br>
11   * BSD-style license. See <a href="http://www.simulation.tudelft.nl/dsol/3.0/license.html">DSOL License</a>. <br>
12   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
13   * @version Oct 21, 2016
14   */
15  public class Toc
16  {
17      /**
18       * @param args command line arguments
19       * @throws Sim0MQException on error
20       */
21      public static void main(final String[] args) throws Sim0MQException
22      {
23          try (ZContext context = new ZContext(1))
24          {
25              // Socket to talk to clients
26              ZMQ.Socket responder = context.createSocket(ZMQ.REP);
27              responder.bind("tcp://*:5556");
28  
29              while (true)
30              {
31                  // Wait for next request from the client
32                  byte[] request = responder.recv(0);
33                  String rs = Tic.byte2string(request);
34                  if (rs.equals("STOP"))
35                  {
36                      break;
37                  }
38                  if (!rs.equals("TIC"))
39                  {
40                      System.err.println("Request was not TIC");
41                  }
42  
43                  // send a reply
44                  byte[] reply = Tic.string2byte("TOC");
45                  responder.send(reply, 0);
46              }
47              responder.close();
48              context.destroy();
49          }
50      }
51  }