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.bot.IUT2004Bot;
13  import cz.cuni.amis.pogamut.ut2004.bot.IUT2004BotController;
14  import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
15  import cz.cuni.amis.pogamut.ut2004.bot.params.UT2004BotParameters;
16  import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004BotFactory;
17  import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004BotModule;
18  import cz.cuni.amis.utils.NullCheck;
19  import cz.cuni.amis.utils.exception.PogamutException;
20  
21  /**
22   * Class used for creating, connecting and starting servers with default settings that are taken from the properties.
23   * <p><p>
24   * The address where the instances will connect are defined either in the constructor
25   * or taken from the properties of the {@link PogamutPlatform}.
26   * <p><p>
27   * For more information about the class see {@link AgentRunner}.
28   * 
29   * @author ik
30   * @author Jimmy
31   *
32   * @param <BOT>
33   * @param <PARAMS>
34   */
35  public abstract class UTBotRunner<BOT extends IUT2004Bot, PARAMS extends UT2004BotParameters> extends AgentRunner<BOT, PARAMS> {
36  
37  	/**
38  	 * Default host where the instances are going to be connected as defaults, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
39  	 */
40      protected String host;
41      
42      /**
43  	 * Default port where the instances are going to be connected as defaults, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
44  	 */
45      protected int port;
46      
47      /**
48  	 * Default name that will serve as a basis for {@link IAgentId}, see {@link IAgentParameters#assignDefaults(IAgentParameters)}.
49  	 */
50  	protected String name;
51  
52  	/** 
53  	 * Construct the runner + specify all defaults.
54  	 * 
55  	 * @param factory to be used for creating new {@link IUT2004Bot} instances
56  	 * @param name default name that serve as a basis for {@link IAgentId}
57  	 * @param host default host where the instances are going to be connected
58  	 * @param port default port where the instances are going to be connected
59  	 */
60  	public UTBotRunner(IAgentFactory<BOT, PARAMS> factory, String name, String host, int port) {
61          super(factory);
62          this.name = name;
63          this.port = port;
64          this.host = host;        
65      }
66  
67  	/**
68  	 * Construct the runner + specify the default name, host:port will be taken from the Pogamut platform properties.
69  	 * 
70  	 * @param factory factory to be used for creating new {@link IUT2004Bot} instances
71  	 * @param log used to log stuff
72  	 * @param name default name that serve as a basis for {@link IAgentId}
73  	 */
74      public UTBotRunner(IAgentFactory<BOT, PARAMS> factory, String name) {
75          this(
76          	factory, 
77          	name, 
78          	Pogamut.getPlatform().getProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_HOST.getKey()) == null ? 
79          				"localhost" 
80          			:	Pogamut.getPlatform().getProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_HOST.getKey()), 
81          	Pogamut.getPlatform().getIntProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_PORT.getKey()) == 0 ?
82          				3000
83          			:	Pogamut.getPlatform().getIntProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_PORT.getKey())
84          );
85      }
86      
87      /**
88       * Construct the runner without specifying anything as default. Default name for bots will be "UT2004Bot"
89       * and host:port will be taken from the Pogamut platform properties.
90       * 
91       * @param factory factory to be used for creating new {@link IUT2004Bot} instances
92       */
93      public UTBotRunner(IAgentFactory<BOT, PARAMS> factory) {
94          this(factory, "UT2004Bot");
95      }
96  
97      
98      /**
99       * Returns name that is going to be used to form new {@link IAgentId} of the bots.
100      *     
101      * @return name used for the newly started bots
102      */
103     public String getName() {
104 		return name;
105 	}
106 
107     /**
108      * Sets name that is going to be used to form new {@link IAgentId} of the bots.
109      * <p><p>
110      * If null is passed, generic "UT2004Bot" will be set.
111      *     
112      * @param name name used for the newly started bots
113      * @return this instance
114      */
115 	public UTBotRunner<BOT, PARAMS> setName(String name) {
116 		if (name == null) name = "UTBot";
117 		this.name = name;
118 		return this;
119 	}
120 
121 	/**
122      * Returns host, where newly launched bots will be connected to.
123      * 
124      * @return host running GB2004 server
125      */
126     public String getHost() {
127 		return host;
128 	}
129 
130     /**
131      * Sets host, where newly launched bots will be connected to.
132      * 
133      * @param host host running GB2004 server (can't be null)
134      * @return this instance
135      */
136 	public UTBotRunner<BOT, PARAMS> setHost(String host) {
137 		this.host = host;
138 		NullCheck.check(this.host, "host");
139 		return this;
140 	}
141 
142 	/**
143      * Returns port, where newly launched bots will be connected to.
144      * 
145      * @return port at the host where GB2004 server is listening for bot connections
146      */
147 	public int getPort() {
148 		return port;
149 	}
150 
151 	/**
152      * Sets port, where newly launched bots will be connected to.
153      * 
154      * @param port at the host where GB2004 server is listening for bot connections
155      * @return this instance
156      */
157 	public UTBotRunner<BOT, PARAMS> setPort(int port) {
158 		this.port = port;
159 		return this;
160 	}
161 
162     /**
163      * Provides default parameters that is, {@link IAgentId} using {@link UTBotRunner#name} and {@link SocketConnectionAddress}
164      * using {@link UTBotRunner#host} and {@link UTBotRunner#port}.
165      */
166 	@Override
167 	protected abstract IAgentParameters newDefaultAgentParameters();
168 	
169 }