View Javadoc
1   package org.opentrafficsim.imb.connector;
2   
3   import nl.tudelft.simulation.language.Throw;
4   
5   import org.opentrafficsim.base.modelproperties.CompoundProperty;
6   import org.opentrafficsim.base.modelproperties.IntegerProperty;
7   import org.opentrafficsim.base.modelproperties.Property;
8   import org.opentrafficsim.base.modelproperties.PropertyException;
9   import org.opentrafficsim.base.modelproperties.StringProperty;
10  import org.opentrafficsim.imb.IMBException;
11  
12  /**
13   * <p>
14   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
16   * </p>
17   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
18   * initial version Aug 28, 2016 <br>
19   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
21   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
22   */
23  public class OTSIMBConnector extends IMBConnector
24  {
25      /** Key for compound property with IMB settings. */
26      public static String PROPERTY_KEY = "IMBProperties";
27  
28      /**
29       * Create a new OTSIMBConnector by specifying all the details.
30       * @param host String; the host that runs the IMB hub server
31       * @param port int; the port number on the IMB hub host
32       * @param modelName String; the name used to register on the hub host
33       * @param modelId integer; usually 1
34       * @param federation string; usually "OTS_RT"
35       * @throws IMBException when a connection to the IMB hub could not be established
36       */
37      public OTSIMBConnector(final String host, final int port, final String modelName, final int modelId,
38              final String federation) throws IMBException
39      {
40          super(host, port, modelName, modelId, federation);
41      }
42  
43      /**
44       * Create a new OTSIMBConnector expecting the IMB hub on localhost port 4000.
45       * @param modelName String; the name used to register on the hub host
46       * @return OTSIMBConnector; a new OTSIMBConnector expecting the IMB hub on localhost port 4000.
47       * @throws IMBException when a connection to the IMB hub could not be established
48       */
49      public static OTSIMBConnector create(final String modelName) throws IMBException
50      {
51          return new OTSIMBConnector("localhost", 4000, modelName, 1, "OTS_RT");
52      }
53  
54      /**
55       * Construct a new OTSIMBConnector from a CompoundProperty (preferably constructed with the
56       * <cite>standardIMBProperties</cite> method of this class.
57       * @param compoundProperty CompoundProperty; the compound property with the settings
58       * @param modelName String; the name used to register on the hub host
59       * @return OTSIMBConnector; a new OTSIMBConnector expecting the IMB hub on localhost port 4000.
60       * @throws IMBException when a connection to the IMB hub could not be established
61       */
62      public static OTSIMBConnector create(final CompoundProperty compoundProperty, final String modelName) throws IMBException
63      {
64          String host = null;
65          int port = -1;
66          int modelId = 1;
67          String federation = "OTS_RT";
68  
69          for (Property<?> ap : compoundProperty)
70          {
71              switch (ap.getKey())
72              {
73                  case "IMBHost":
74                      host = ((StringProperty) ap).getValue();
75                      break;
76  
77                  case "IMBPort":
78                      port = ((IntegerProperty) ap).getValue();
79                      break;
80  
81                  case "IMBModelId":
82                      modelId = ((IntegerProperty) ap).getValue();
83                      break;
84  
85                  case "IMBFederation":
86                      federation = ((StringProperty) ap).getValue();
87                      break;
88  
89                  default:
90                      System.err.println("Ignoring property " + ap);
91              }
92          }
93          Throw.when(host == null, IMBException.class, "host may not be null");
94          System.out.println("IMB: connecting to " + host + ":" + port);
95          return new OTSIMBConnector(host, port, modelName, modelId, federation);
96      }
97  
98      /**
99       * Create a CompoundProperty with the settings for an IMB connection.
100      * @param displayPriority int; the displayPriority of the created CompoundProperty
101      * @return CompoundProperty the default settings
102      */
103     public static CompoundProperty standardIMBProperties(final int displayPriority)
104     {
105         try
106         {
107             CompoundProperty result =
108                     new CompoundProperty("IMBProperties", "IMB properties", "IMB properties", null, false, displayPriority);
109             result.add(new StringProperty("IMBHost", "IMB hub host", "Name of the IMB hub", "localhost", false, 0));
110             result.add(new IntegerProperty("IMBPort", "IMB hub port", "Port on the IMB hub", 4000, 0, 65535, "%d", false, 1));
111             result.add(new IntegerProperty("IMBModelId", "IMB model id", "Model id", 1, 0, 9999, "%d", false, 2));
112             result.add(new StringProperty("IMBFederation", "IMB federation", "Federation on the IMB hub", "OTS_RT", false, 3));
113             return result;
114         }
115         catch (PropertyException exception)
116         {
117             exception.printStackTrace();
118         }
119         return null;
120     }
121 }