View Javadoc

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