[Pogamut-list] Custom ControlServer in 3.1

michal.bida pogamut-forum at diana.ms.mff.cuni.cz
Tue Jan 18 16:44:15 CET 2011


Custom ControlServer in 3.1

Author: michal.bida

Hi all,

follow this guide when having troubles with running custom control server in Pogamut 3.1.

Basically you need to write two classes. A module with information for guice and the control server itself. 

!Guice module

{CODE()}
package pogamutcontrolserver;

import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
import cz.cuni.amis.pogamut.base.agent.IAgent;
import cz.cuni.amis.pogamut.base.communication.translator.IWorldMessageTranslator;
import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencies;
import cz.cuni.amis.pogamut.base.server.IWorldServer;
import cz.cuni.amis.pogamut.base3d.worldview.IVisionWorldView;
import cz.cuni.amis.pogamut.ut2004.communication.translator.server.ServerFSM;
import cz.cuni.amis.pogamut.ut2004.communication.worldview.UT2004WorldView;
import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004ServerModule;

import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server;

/**
 *
 * @author Knight
 */
public class PogamutControlServerModule extends UT2004ServerModule {

	@Override
	protected void configureModules() {
		super.configureModules();
		addModule(new AbstractModule() {

			@Override
			public void configure() {
				bind(IWorldMessageTranslator.class).to(ServerFSM.class);
				bind(IWorldView.class).to(IVisionWorldView.class);
				bind(IVisionWorldView.class).to(UT2004WorldView.class);
				bind(ComponentDependencies.class).annotatedWith(Names.named(UT2004WorldView.WORLDVIEW_DEPENDENCY)).toProvider(worldViewDependenciesProvider);
				bind(IAgent.class).to(IWorldServer.class);
				bind(IWorldServer.class).to(IUT2004Server.class);
				bind(IUT2004Server.class).to(PogamutControlServer.class); //Here we tell guice it should create our custom Control server class
			}

		});
	}
}

{CODE}

!ControlServer code

{CODE()}
package pogamutcontrolserver;

import com.google.inject.Inject;
import cz.cuni.amis.pogamut.base.agent.impl.AgentId;
import cz.cuni.amis.pogamut.base.communication.command.IAct;
import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.SocketConnection;
import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.SocketConnectionAddress;
import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEventListener;
import cz.cuni.amis.pogamut.base.communication.worldview.object.event.WorldObjectUpdatedEvent;
import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
import cz.cuni.amis.pogamut.ut2004.agent.params.UT2004AgentParameters;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.AliveMessage;

import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.FlagInfo;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPoint;
import cz.cuni.amis.pogamut.ut2004.communication.worldview.UT2004WorldView;
import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004ServerFactory;

import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server;
import cz.cuni.amis.pogamut.ut2004.server.impl.UT2004Server;
import cz.cuni.amis.utils.exception.PogamutException;

/**
 * Control server connected to UT environment. Through this connection we get information
 * about the game.
 *
 * @author Knight
 */
public class PogamutControlServer extends UT2004Server implements IUT2004Server {

    private double currentUTTime;

    IWorldObjectEventListener myAliveListener = new IWorldObjectEventListener() {
        public void notify(WorldObjectUpdatedEvent event) {
            currentUTTime = event.getObject().getTime();
        }
    };

    IWorldObjectEventListener myFlagInfoListener = new IWorldObjectEventListener() {
        public void notify(WorldObjectUpdatedEvent event) {
            System.out.print("FLG: ");
            System.out.println(event.getObject().toString());
            System.out.println("--------------------------");
        }
    };

    IWorldObjectEventListener myDominationPointListener = new IWorldObjectEventListener() {
        public void notify(WorldObjectUpdatedEvent event) {
            if (event.getObject().isDomPoint()) {
                System.out.print("DOM: ");
                System.out.println(event.getObject().toString());
                System.out.println("--------------------------");
            }
        }
    };

    @Inject
    public PogamutControlServer(UT2004AgentParameters params, IAgentLogger agentLogger, IComponentBus bus, SocketConnection connection, UT2004WorldView worldView, IAct act) {
        super(params, agentLogger, bus, connection, worldView, act);
    }


    public void initialize() {
        getWorldView().addObjectListener(AliveMessage.class, WorldObjectUpdatedEvent.class, myAliveListener);
        getWorldView().addObjectListener(FlagInfo.class, WorldObjectUpdatedEvent.class, myFlagInfoListener);
        getWorldView().addObjectListener(NavPoint.class, WorldObjectUpdatedEvent.class, myDominationPointListener);
        System.out.print("PogamutControlServer initialized.");
    }


    /**
     * This method is called when the server is started either from IDE or from command line.
     * It connects the server to the game.
     * @param args
     */
    public static void main(String args[]) throws PogamutException {
        //creating agent parameters - setting module name and connection adress
        UT2004AgentParameters params = new UT2004AgentParameters();
        params.setAgentId(new AgentId("PogamutControlServer"));
        params.setWorldAddress(new SocketConnectionAddress("127.0.0.1", 3001));

        //create module that tells guice it should instantiate OUR (this) class
        PogamutControlServerModule module = new PogamutControlServerModule();

        //creating pogamut factory
        UT2004ServerFactory fac = new UT2004ServerFactory(module);
        PogamutControlServer cts = (PogamutControlServer) fac.newAgent(params);

        //starting the connection - connecting to the server
        cts.start();
        //launching our custom method
        cts.initialize();
    }
}
{CODE}

This control servers updates its current time variable and is also posting information from synchronous batches about FlagInfo and domination point objects it is receiving as a console log (note that you get FlagInfo only in BotCTFGame and domination points in BotDoubleDomination game type).

Best,
Michal

-- 
Reply Link: <http://diana.ms.mff.cuni.cz/main/tiki-view_forum_thread.php?forumId=4&comments_parentId=545#form>





More information about the Pogamut-list mailing list