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.bot.IUDKBot; 16 import cz.cuni.amis.pogamut.udk.bot.IUDKBotController; 17 import cz.cuni.amis.pogamut.udk.bot.impl.UDKBot; 18 import cz.cuni.amis.pogamut.udk.factory.guice.remoteagent.UDKBotFactory; 19 import cz.cuni.amis.pogamut.udk.factory.guice.remoteagent.UDKBotModule; 20 import cz.cuni.amis.utils.NullCheck; 21 import cz.cuni.amis.utils.exception.PogamutException; 22 23 /** 24 * Class used for creating, connecting and starting servers with default settings that are taken from the properties. 25 * <p><p> 26 * The address where the instances will connect are defined either in the constructor 27 * or taken from the properties of the {@link PogamutPlatform}. 28 * <p><p> 29 * For more information about the class see {@link AgentRunner}. 30 * 31 * @author ik 32 * @author Jimmy 33 * 34 * @param <BOT> 35 * @param <PARAMS> 36 */ 37 public class UDKBotRunner<BOT extends IUDKBot, PARAMS extends UDKAgentParameters> extends AgentRunner<BOT, PARAMS> { 38 39 /** 40 * Default host where the instances are going to be connected as defaults, see {@link IAgentParameters#assignDefaults(IAgentParameters)}. 41 */ 42 protected String host; 43 44 /** 45 * Default port where the instances are going to be connected as defaults, see {@link IAgentParameters#assignDefaults(IAgentParameters)}. 46 */ 47 protected int port; 48 49 /** 50 * Default name that will serve as a basis for {@link IAgentId}, see {@link IAgentParameters#assignDefaults(IAgentParameters)}. 51 */ 52 protected String name; 53 54 /** 55 * Construct the runner + specify all defaults. 56 * 57 * @param factory to be used for creating new {@link IUDKBot} instances 58 * @param name default name that serve as a basis for {@link IAgentId} 59 * @param host default host where the instances are going to be connected 60 * @param port default port where the instances are going to be connected 61 */ 62 public UDKBotRunner(IAgentFactory<BOT, PARAMS> factory, String name, String host, int port) { 63 super(factory); 64 this.name = name; 65 this.port = port; 66 this.host = host; 67 } 68 69 /** 70 * Construct the runner + specify the default name, host:port will be taken from the Pogamut platform properties. 71 * 72 * @param factory factory to be used for creating new {@link IUDKBot} instances 73 * @param log used to log stuff 74 * @param name default name that serve as a basis for {@link IAgentId} 75 */ 76 public UDKBotRunner(IAgentFactory<BOT, PARAMS> factory, String name) { 77 this( 78 factory, 79 name, 80 Pogamut.getPlatform().getProperty(PogamutUDKProperty.POGAMUT_UDK_BOT_HOST.getKey()), 81 Pogamut.getPlatform().getIntProperty(PogamutUDKProperty.POGAMUT_UDK_BOT_PORT.getKey()) 82 ); 83 } 84 85 /** 86 * Construct the runner without specifying anything as default. Default name for bots will be "UDKBot" 87 * and host:port will be taken from the Pogamut platform properties. 88 * 89 * @param factory factory to be used for creating new {@link IUDKBot} instances 90 */ 91 public UDKBotRunner(IAgentFactory<BOT, PARAMS> factory) { 92 this(factory, "UDKBot"); 93 } 94 95 /** 96 * Construct the runner + specify all defaults. 97 * 98 * @param module Guice module that is going to be used by the {@link UDKBotFactory} 99 * @param name default name that serve as a basis for {@link IAgentId} 100 * @param host default host where the instances are going to be connected 101 * @param port default port where the instances are going to be connected 102 */ 103 public UDKBotRunner(UDKBotModule module, String name, String host, int port) { 104 this(new UDKBotFactory<BOT, PARAMS>(module), name, host, port); 105 } 106 107 /** 108 * Construct the runner + specify the default name, host:port will be taken from the Pogamut platform properties. 109 * 110 * @param module Guice module that is going to be used by the {@link UDKBotFactory} 111 * @param name default name that serve as a basis for {@link IAgentId} 112 */ 113 public UDKBotRunner(UDKBotModule module, String name) { 114 this( 115 module, 116 name, 117 Pogamut.getPlatform().getProperty(PogamutUDKProperty.POGAMUT_UDK_BOT_HOST.getKey()), 118 Pogamut.getPlatform().getIntProperty(PogamutUDKProperty.POGAMUT_UDK_BOT_PORT.getKey()) 119 ); 120 } 121 122 /** 123 * Construct the runner without specifying anything as default. Default name for bots will be "UDKBot" 124 * and host:port will be taken from the Pogamut platform properties. 125 * 126 * @param module Guice module that is going to be used by the {@link UDKBotFactory} 127 */ 128 public UDKBotRunner(UDKBotModule module) { 129 this(module, "UDKBot"); 130 } 131 132 /** 133 * Construct the runner + specify all defaults. 134 * 135 * @param botControllerClass controller that will be used to instantiate {@link UDKBotModule}, i.e., it will control the {@link UDKBot} instance 136 * @param name default name that serve as a basis for {@link IAgentId} 137 * @param host default host where the instances are going to be connected 138 * @param port default port where the instances are going to be connected 139 */ 140 public UDKBotRunner(Class<? extends IUDKBotController> botControllerClass, String name, String host, int port) { 141 this(new UDKBotModule(botControllerClass), name, host, port); 142 } 143 144 /** 145 * Construct the runner + specify the default name, host:port will be taken from the Pogamut platform properties. 146 * 147 * @param botControllerClass controller that will be used to instantiate {@link UDKBotModule}, i.e., it will control the {@link UDKBot} instance 148 * @param name default name that serve as a basis for {@link IAgentId} 149 */ 150 public UDKBotRunner(Class<? extends IUDKBotController> botControllerClass, String name) { 151 this( 152 new UDKBotModule(botControllerClass), 153 name, 154 Pogamut.getPlatform().getProperty(PogamutUDKProperty.POGAMUT_UDK_BOT_HOST.getKey()), 155 Pogamut.getPlatform().getIntProperty(PogamutUDKProperty.POGAMUT_UDK_BOT_PORT.getKey()) 156 ); 157 } 158 159 /** 160 * Construct the runner without specifying anything as default. Default name for bots will be "UDKBot" 161 * and host:port will be taken from the Pogamut platform properties. 162 * 163 * @param botControllerClass controller that will be used to instantiate {@link UDKBotModule}, i.e., it will control the {@link UDKBot} instance 164 */ 165 public UDKBotRunner(Class<? extends IUDKBotController> botControllerClass) { 166 this(new UDKBotModule(botControllerClass), "UDKBot"); 167 } 168 169 @Override 170 public BOT startAgent() throws PogamutException { 171 return super.startAgent(); 172 } 173 174 @Override 175 public List<BOT> startAgents(int count) throws PogamutException { 176 return super.startAgents(count); 177 } 178 179 @Override 180 public List<BOT> startAgents(PARAMS... agentParameters) throws PogamutException { 181 return super.startAgents(agentParameters); 182 }; 183 184 /** 185 * Returns name that is going to be used to form new {@link IAgentId} of the bots. 186 * 187 * @return name used for the newly started bots 188 */ 189 public String getName() { 190 return name; 191 } 192 193 /** 194 * Sets name that is going to be used to form new {@link IAgentId} of the bots. 195 * <p><p> 196 * If null is passed, generic "UDKBot" will be set. 197 * 198 * @param name name used for the newly started bots 199 * @return this instance 200 */ 201 public UDKBotRunner<BOT, PARAMS> setName(String name) { 202 if (name == null) name = "UDKBot"; 203 this.name = name; 204 return this; 205 } 206 207 /** 208 * Returns host, where newly launched bots will be connected to. 209 * 210 * @return host running GBUDK server 211 */ 212 public String getHost() { 213 return host; 214 } 215 216 /** 217 * Sets host, where newly launched bots will be connected to. 218 * 219 * @param host host running GBUDK server (can't be null) 220 * @return this instance 221 */ 222 public UDKBotRunner<BOT, PARAMS> setHost(String host) { 223 this.host = host; 224 NullCheck.check(this.host, "host"); 225 return this; 226 } 227 228 /** 229 * Returns port, where newly launched bots will be connected to. 230 * 231 * @return port at the host where GBUDK server is listening for bot connections 232 */ 233 public int getPort() { 234 return port; 235 } 236 237 /** 238 * Sets port, where newly launched bots will be connected to. 239 * 240 * @param port at the host where GBUDK server is listening for bot connections 241 * @return this instance 242 */ 243 public UDKBotRunner<BOT, PARAMS> setPort(int port) { 244 this.port = port; 245 return this; 246 } 247 248 /** 249 * Provides default parameters that is, {@link IAgentId} using {@link UDKBotRunner#name} and {@link SocketConnectionAddress} 250 * using {@link UDKBotRunner#host} and {@link UDKBotRunner#port}. 251 */ 252 @Override 253 protected IAgentParameters newDefaultAgentParameters() { 254 return new UDKAgentParameters().setAgentId(new AgentId(name)).setWorldAddress(new SocketConnectionAddress(host, port)); 255 } 256 257 }