1 package cz.cuni.amis.pogamut.udk.utils;
2
3 import java.util.List;
4
5 import cz.cuni.amis.pogamut.base.agent.IAgentId;
6 import cz.cuni.amis.pogamut.base.agent.impl.AgentId;
7 import cz.cuni.amis.pogamut.base.agent.params.IAgentParameters;
8 import cz.cuni.amis.pogamut.base.agent.utils.runner.impl.AgentRunner;
9 import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.SocketConnectionAddress;
10 import cz.cuni.amis.pogamut.base.factory.IAgentFactory;
11 import cz.cuni.amis.pogamut.base.utils.Pogamut;
12 import cz.cuni.amis.pogamut.base.utils.PogamutPlatform;
13 import cz.cuni.amis.pogamut.base.utils.PogamutProperty;
14 import cz.cuni.amis.pogamut.udk.agent.params.UDKAgentParameters;
15 import cz.cuni.amis.pogamut.udk.observer.IUDKObserver;
16 import cz.cuni.amis.utils.exception.PogamutException;
17
18 /**
19 * Class used for creating, connecting and starting observers with default settings that are taken from the properties.
20 * <p><p>
21 * The address where the instances will connect are defined either in the constructor
22 * or taken from the properties of the {@link PogamutPlatform}.
23 * <p><p>
24 * For more information about the class see {@link AgentRunner}.
25 *
26 * @author ik
27 * @author Jimmy
28 */
29 public class UDKObserverRunner<OBSERVER extends IUDKObserver, PARAMS extends UDKAgentParameters> extends AgentRunner<OBSERVER, PARAMS> {
30
31 /**
32 * Default host where the instances are going to be connected as defaults, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
33 */
34 protected String host;
35
36 /**
37 * Default port where the instances are going to be connected as defaults, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
38 */
39 protected int port;
40
41 /**
42 * Default name that will serve as a basis for {@link IAgentId}, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
43 */
44 protected String name;
45
46 /**
47 * Construct the runner + specify all defaults.
48 *
49 * @param factory to be used for creating new {@link IUDKObserver} instances
50 * @param name default name that serve as a basis for {@link IAgentId}
51 * @param host default host where the instances are going to be connected
52 * @param port default port where the instances are going to be connected
53 */
54 public UDKObserverRunner(IAgentFactory<OBSERVER, PARAMS> factory, String name, String host, int port) {
55 super(factory);
56 this.name = name;
57 this.port = port;
58 this.host = host;
59 }
60
61 /**
62 * Construct the runner + specify the default name, host:port will be taken from the Pogamut platform properties.
63 *
64 * @param factory factory to be used for creating new {@link IUDKObserver} instances
65 * @param name default name that serve as a basis for {@link IAgentId}
66 */
67 public UDKObserverRunner(IAgentFactory<OBSERVER, PARAMS> factory, String name) {
68 this(
69 factory,
70 name,
71 Pogamut.getPlatform().getProperty(PogamutUDKProperty.POGAMUT_UDK_OBSERVER_HOST.getKey()),
72 Pogamut.getPlatform().getIntProperty(PogamutUDKProperty.POGAMUT_UDK_OBSERVER_PORT.getKey())
73 );
74 }
75
76 /**
77 * Construct the runner without specifying anything as default. Default name for server agents will be "UT2004Observer"
78 * and host:port will be taken from the Pogamut platform properties.
79 *
80 * @param factory factory to be used for creating new {@link IUDKObserver} instances
81 */
82 public UDKObserverRunner(IAgentFactory<OBSERVER, PARAMS> factory) {
83 this(factory, "UDKObserver");
84 }
85
86 @Override
87 public OBSERVER startAgent() throws PogamutException {
88 return super.startAgent();
89 }
90
91 @Override
92 public List<OBSERVER> startAgents(int count) throws PogamutException {
93 return super.startAgents(count);
94 }
95
96 @Override
97 public List<OBSERVER> startAgents(PARAMS... agentParameters) throws PogamutException {
98 return super.startAgents(agentParameters);
99 };
100
101 /**
102 * Provides default parameters that is, {@link IAgentId} using {@link UDKObserverRunner#name} and {@link SocketConnectionAddress}
103 * using {@link UDKObserverRunner#host} and {@link UDKObserverRunner#port}.
104 */
105 @Override
106 protected IAgentParameters newDefaultAgentParameters() {
107 return new UDKAgentParameters().setAgentId(new AgentId(name)).setWorldAddress(new SocketConnectionAddress(host, port));
108 }
109
110 }