View Javadoc
1   package org.opentrafficsim.demo;
2   
3   import java.io.BufferedReader;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.io.InputStreamReader;
7   import java.io.OutputStream;
8   import java.net.Socket;
9   import java.net.URLConnection;
10  import java.nio.charset.StandardCharsets;
11  import java.util.stream.Collectors;
12  
13  import org.opentrafficsim.aimsun.proto.AimsunControlProtoBuf;
14  
15  import nl.tudelft.simulation.language.io.URLResource;
16  
17  /**
18   * Test client for AimsunController.
19   * <p>
20   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
21   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
22   * <p>
23   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Apr 18, 2017 <br>
24   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
26   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
27   */
28  public final class TestController
29  {
30      /**
31       * Cannot be instantiated.
32       */
33      private TestController()
34      {
35          // Do not instantiate.
36      }
37  
38      /**
39       * Test client for AimsunControl.
40       * <p>
41       * (c) copyright 2002-2017 <a href="http://www.simulation.tudelft.nl">Delft University of Technology</a>. <br>
42       * BSD-style license. See <a href="http://www.simulation.tudelft.nl/dsol/3.0/license.html">DSOL License</a>. <br>
43       * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
44       * @version Oct 21, 2016
45       */
46      /**
47       * @param args command line arguments
48       * @throws IOException when communication fails
49       */
50      public static void main(final String[] args) throws IOException
51      {
52          String ip = null;
53          Integer port = null;
54  
55          for (String arg : args)
56          {
57              int equalsPos = arg.indexOf("=");
58              if (equalsPos < 0)
59              {
60                  System.err.println("Unhandled argument \"" + arg + "\"");
61              }
62              String key = arg.substring(0, equalsPos);
63              String value = arg.substring(equalsPos + 1);
64              switch (key.toUpperCase())
65              {
66                  case "IP":
67                      ip = value;
68                      break;
69                  case "PORT":
70                      try
71                      {
72                          port = Integer.parseInt(value);
73                      }
74                      catch (NumberFormatException exception)
75                      {
76                          System.err.println("Bad port number \"" + value + "\"");
77                          System.exit(1);
78                      }
79                      break;
80                  default:
81                      System.err.println("Unhandled argument \"" + arg + "\"");
82              }
83          }
84          if (null == ip || null == port)
85          {
86              System.err.println("Missing required argument(s) ip=<ip-number_or_hostname> port=<port-number>");
87              System.exit(1);
88          }
89          // Socket to talk to server
90          System.out.println("Connecting to server...");
91          Socket socket = new Socket(ip, port);
92          System.out.println("Connected");
93          OutputStream outputStream = socket.getOutputStream();
94          InputStream inputStream = socket.getInputStream();
95          // Send a build network command
96          System.out.println("Sending 1st test message");
97          AimsunControlProtoBuf.CreateSimulation.Builder createSimulationBuilder =
98                  AimsunControlProtoBuf.CreateSimulation.newBuilder();
99          createSimulationBuilder.setRunTime(3600d);
100         createSimulationBuilder.setWarmUpTime(0d);
101         // String network = URLResource.getResource("/aimsun/singleRoad.xml").toString(); // wrong; fix later
102         String network = null; // IOUtils.toString(URLResource.getResource("/aimsun/singleRoad.xml"));
103         URLConnection conn = URLResource.getResource("/aimsun/singleRoad.xml").openConnection();
104         try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)))
105         {
106             network = reader.lines().collect(Collectors.joining("\n"));
107         }
108 
109         createSimulationBuilder.setNetworkXML(network);
110         AimsunControlProtoBuf.OTSMessage command =
111                 AimsunControlProtoBuf.OTSMessage.newBuilder().setCreateSimulation(createSimulationBuilder.build()).build();
112         // for (byte b : command.toByteArray())
113         // {
114         // System.out.print(String.format("%02x ", b));
115         // }
116         // System.out.println("");
117         command.writeDelimitedTo(outputStream);
118         // Send a simulate until command with time 0 to retrieve the initial gtu positions
119         System.out.println("Sending simulate until 0 command to retrieve the initial GTU positions");
120         AimsunControlProtoBuf.SimulateUntil simulateUntil =
121                 AimsunControlProtoBuf.SimulateUntil.newBuilder().setTime(0d).build();
122         command = AimsunControlProtoBuf.OTSMessage.newBuilder().setSimulateUntil(simulateUntil).build();
123         command.writeDelimitedTo(outputStream);
124         // Receive a reply
125         System.out.println("Waiting for / reading reply");
126         AimsunControlProtoBuf.OTSMessage reply = AimsunControlProtoBuf.OTSMessage.parseDelimitedFrom(inputStream);
127         System.out.println("Received " + reply);
128         for (int step = 1; step <= 20; step++)
129         {
130             simulateUntil = AimsunControlProtoBuf.SimulateUntil.newBuilder().setTime(0.5d * step).build();
131             System.out.println("Simulate step " + step);
132             command = AimsunControlProtoBuf.OTSMessage.newBuilder().setSimulateUntil(simulateUntil).build();
133             command.writeDelimitedTo(outputStream);
134             System.out.println("Waiting for / reading reply");
135             reply = AimsunControlProtoBuf.OTSMessage.parseDelimitedFrom(inputStream);
136             System.out.println("Received " + reply);
137         }
138         try
139         {
140             Thread.sleep(5000);
141         }
142         catch (InterruptedException exception)
143         {
144             exception.printStackTrace();
145         }
146         socket.close();
147     }
148 }