Loading...
 

PogamutUT2004


CustomControlServer in Pogamut 3.0.11

Hi all,

it is possible to instantiate and use custom ControlServer in Pogamut 3.0.11. Currently it is not straightforward, hence I am posting an example of working code that creates my own control server and register listener for AliveMessage.

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

Guice module


package cz.cuni.amis.pogamut.emonewt.storycontrol;

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 StoryControlServerModule 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(StoryControlServer.class); //Here we tell guice it should create our custom Control server class
			}

		});
	}
}


ControlServer code


package cz.cuni.amis.pogamut.emonewt.storycontrol;

import com.google.inject.Inject;
import cz.cuni.amis.pogamut.base.agent.IAgentId;
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.communication.messages.gbinfomessages.AliveMessage;
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.factory.guice.remoteagent.UT2004ServerModule;
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
 * for the {@link WorldScene} visualizator.
 *
 * @author Knight
 */
public class StoryControlServer extends UT2004Server implements IUT2004Server {

    private double currentUTTime;

    IWorldObjectEventListener myAliveListener = new IWorldObjectEventListener() {

        public void notify(WorldObjectUpdatedEvent event) {
            currentUTTime = event.getObject().getTime();
            System.out.println("ALIVE: " + event.getObject().toString());            
        }
    };

    @Inject
    public StoryControlServer(IAgentId agentId, IAgentLogger agentLogger, IComponentBus bus, SocketConnection connection, UT2004WorldView worldView, IAct act) {
        super(agentId, agentLogger, bus, connection, worldView, act);
    }

    public void initialize() {
        getWorldView().addObjectListener(AliveMessage.class, WorldObjectUpdatedEvent.class, myAliveListener);
        System.out.print("StoryControlServer initialized.");
    }


    /**
     * Creates control server, connect it to the environemnt and returns the instance.
     * 
     * @return StoryControlServer instance
     * @throws PogamutException
     */
    public static StoryControlServer createServer() throws PogamutException {
        UT2004ServerModule module = new StoryControlServerModule();
        UT2004ServerFactory factory = new UT2004ServerFactory(module);

        IAgentId agentID = new AgentId("StoryControlServer");
        StoryControlServer server = (StoryControlServer) factory.newAgent(agentID, new SocketConnectionAddress("127.0.0.1", 3001));
        server.start();
        server.initialize();

        return server;        
    }

    /**
     * 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 {
        UT2004ServerModule module = new StoryControlServerModule();
        UT2004ServerFactory factory = new UT2004ServerFactory(module);

        IAgentId agentID = new AgentId("PogamutObserver");
        StoryControlServer server = (StoryControlServer) factory.newAgent(agentID, new SocketConnectionAddress("127.0.0.1", 3001));
        server.start();
        server.initialize();
        //new SingleUT2004BotRunner(PogamutObserver.class, "PogamutObserver").startAgent();
    }
}


Best,
Michal
 

News

News RSS RSS feed for News link



Pogamut

Quarterly RSS RSS feed for quarterly reports

Acknowledgement

This work is supported by GA UK 1053/2007/A-INF/MFF (2007-8), GA UK 351/2006/A-INF/MFF (2006-8), the Ministry of Education of the Czech Republic (grant MSM0021620838) (2008-9), by the Program "Information Society" under project 1ET100300517 (2006-9), and the project Integration of IT Tools into Education of Humanities (2006-8) and by the project CZ.2.17/3.1.00/31162, which are financed by the European Social Fund, the state budget of the Czech Republic, and by the budget of Municipal House Prague.