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
14
15
16
17
18
19
20
21
22
23 public class OTSIMBConnector extends IMBConnector
24 {
25
26 public static String PROPERTY_KEY = "IMBProperties";
27
28
29
30
31
32
33
34
35
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
45
46
47
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
56
57
58
59
60
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
100
101
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 }