View Javadoc
1   package org.sim0mq.demo;
2   
3   import org.sim0mq.Sim0MQException;
4   import org.sim0mq.message.MessageStatus;
5   import org.sim0mq.message.SimulationMessage;
6   import org.zeromq.ZMQ;
7   
8   /**
9    * Server example for JeroMQ / ZeroMQ.
10   * <p>
11   * (c) copyright 2002-2016 <a href="http://www.simulation.tudelft.nl">Delft University of Technology</a>. <br>
12   * BSD-style license. See <a href="http://www.simulation.tudelft.nl/dsol/3.0/license.html">DSOL License</a>. <br>
13   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
14   * @version Oct 21, 2016
15   */
16  public class Server
17  {
18      /**
19       * @param args command line arguments
20       * @throws Sim0MQException on error
21       */
22      public static void main(String[] args) throws Sim0MQException
23      {
24          ZMQ.Context context = ZMQ.context(1);
25  
26          // Socket to talk to clients
27          ZMQ.Socket responder = context.socket(ZMQ.REP);
28          responder.bind("tcp://*:5556");
29  
30          while (!Thread.currentThread().isInterrupted())
31          {
32              // Wait for next request from the client
33              byte[] request = responder.recv(0);
34              Object[] message = SimulationMessage.decode(request);
35              System.out.println("Received " + SimulationMessage.print(message));
36  
37              // send a reply
38              Object[] reply = new Object[] { true, -28.2, 77000, "Bangladesh" };
39              responder.send(SimulationMessage.encodeUTF8("IDVV14.2", "MC.1", "MM1.4", "TEST.2", 1201L, MessageStatus.NEW, reply), 0);
40          }
41          responder.close();
42          context.term();
43      }
44  }