1 package cz.cuni.amis.pogamut.ut2004.utils; 2 3 import java.util.List; 4 import java.util.logging.Level; 5 6 import cz.cuni.amis.pogamut.base.agent.IAgentId; 7 import cz.cuni.amis.pogamut.base.agent.params.IAgentParameters; 8 import cz.cuni.amis.pogamut.base.agent.utils.runner.IAgentDescriptor; 9 import cz.cuni.amis.pogamut.base.agent.utils.runner.impl.MultipleAgentRunner; 10 import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.SocketConnectionAddress; 11 import cz.cuni.amis.pogamut.base.factory.IAgentFactory; 12 import cz.cuni.amis.pogamut.base.utils.Pogamut; 13 import cz.cuni.amis.pogamut.ut2004.bot.IUT2004Bot; 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 * This class has only one purpose - execute ONE OR MORE BOTS inside 'main' method. You can't use it for anything else! 23 * It is THE SHORTCUT of all SHORTCUTS to execute multiple bots, wait till they finishe and close the whole Pogamut. 24 * <p><p> 25 * Designed especially for the usage inside NetBeans projects. 26 * <p><p> 27 * NOTE: by default, all bots get paused after they start and they are resumed after all bots are present in UT2004. To 28 * change this behaviour pass 'false' through {@link MultipleUT2004BotRunner#setPausing(boolean)}. 29 * <p><p> 30 * NOTE: It's not even meant to be instantiated twice for two different batch of bots and consequently executed in two different threads! 31 * Single-purpose class only ;-) 32 * <p><p> 33 * NOTE: It might be very interesting for you to check out the source of method {@link MultipleUT2004BotRunner#startAgent()} to 34 * see how the agent should be instantiated via {@link UT2004BotFactory} using {@link UT2004BotModule}. 35 * <p><p> 36 * 37 * 38 * @author Jimmy 39 */ 40 public class MultipleUT2004BotRunner<BOT extends UT2004Bot, PARAMS extends UT2004BotParameters, MODULE extends UT2004BotModule> extends MultipleAgentRunner<BOT, PARAMS, MODULE> { 41 42 /** 43 * Default host where the instances are going to be connected as defaults, see {@link IAgentParameters#assignDefaults(IAgentParameters)}. 44 */ 45 protected String host; 46 47 /** 48 * Default port where the instances are going to be connected as defaults, see {@link IAgentParameters#assignDefaults(IAgentParameters)}. 49 */ 50 protected int port; 51 52 /** 53 * Default name that will serve as a basis for {@link IAgentId}, see {@link IAgentParameters#assignDefaults(IAgentParameters)}. 54 */ 55 protected String name; 56 57 /** 58 * Construct the runner + specify all defaults. 59 * 60 * @param name default name that serve as a basis for {@link IAgentId} 61 * @param host default host where the instances are going to be connected 62 * @param port default port where the instances are going to be connected 63 */ 64 public MultipleUT2004BotRunner(String name, String host, int port) { 65 this.name = name; 66 this.port = port; 67 this.host = host; 68 } 69 70 /** 71 * Construct the runner + specify the default name, host:port will be taken from the Pogamut platform properties. 72 * 73 * @param name default name that serve as a basis for {@link IAgentId} 74 */ 75 public MultipleUT2004BotRunner(String name) { 76 this( 77 name, 78 Pogamut.getPlatform().getProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_HOST.getKey()), 79 Pogamut.getPlatform().getIntProperty(PogamutUT2004Property.POGAMUT_UT2004_BOT_PORT.getKey()) 80 ); 81 } 82 83 /** 84 * Returns name that is going to be used to form new {@link IAgentId} of the bots. 85 * 86 * @return name used for the newly started bots 87 */ 88 public String getName() { 89 return name; 90 } 91 92 /** 93 * Sets name that is going to be used to form new {@link IAgentId} of the bots. 94 * <p><p> 95 * If null is passed, generic "UT2004Bot" will be set. 96 * 97 * @param name name used for the newly started bots 98 * @return this instance 99 */ 100 public MultipleUT2004BotRunner<BOT, PARAMS, MODULE> setName(String name) { 101 if (name == null) name = "UT2004Bot"; 102 this.name = name; 103 return this; 104 } 105 106 /** 107 * Returns host, where newly launched bots will be connected to. 108 * 109 * @return host running GB2004 server 110 */ 111 public String getHost() { 112 return host; 113 } 114 115 /** 116 * Sets host, where newly launched bots will be connected to. 117 * 118 * @param host host running GB2004 server (can't be null) 119 * @return this instance 120 */ 121 public MultipleUT2004BotRunner<BOT, PARAMS, MODULE> setHost(String host) { 122 this.host = host; 123 NullCheck.check(this.host, "host"); 124 return this; 125 } 126 127 /** 128 * Returns port, where newly launched bots will be connected to. 129 * 130 * @return port at the host where GB2004 server is listening for bot connections 131 */ 132 public int getPort() { 133 return port; 134 } 135 136 /** 137 * Sets port, where newly launched bots will be connected to. 138 * 139 * @param port at the host where GB2004 server is listening for bot connections 140 * @return this instance 141 */ 142 public MultipleUT2004BotRunner<BOT, PARAMS, MODULE> setPort(int port) { 143 this.port = port; 144 return this; 145 } 146 147 /** 148 * We're setting the logging level to {@link Level#WARNING} here so the bot won't log much. 149 */ 150 @Override 151 protected void preStartHook(BOT agent) throws PogamutException { 152 //agent.getLogger().setLevel(Level.WARNING); 153 } 154 155 /** 156 * Provides default parameters that is, {@link IAgentId} using {@link MultipleUT2004BotRunner#name} and {@link SocketConnectionAddress} 157 * using {@link MultipleUT2004BotRunner#host} and {@link MultipleUT2004BotRunner#port}. 158 */ 159 @Override 160 protected IAgentParameters newDefaultAgentParameters() { 161 return new UT2004BotParameters().setAgentId(newAgentId(name)).setWorldAddress(new SocketConnectionAddress(host, port)); 162 } 163 164 /** 165 * Uses {@link UT2004BotFactory} for agent construction. 166 */ 167 @Override 168 protected IAgentFactory newAgentFactory(MODULE agentModule) { 169 return new UT2004BotFactory<IUT2004Bot, UT2004BotParameters>(agentModule); 170 } 171 172 public List<BOT> startAgents(IAgentDescriptor<PARAMS,MODULE>... agentDescriptors) { 173 return super.startAgents(agentDescriptors); 174 }; 175 176 }