[Pogamut-list] Sending commands to Control Server

michal.bida pogamut-forum at artemis.ms.mff.cuni.cz
Tue Nov 17 12:03:06 CET 2009


Sending commands to Control Server

Author: michal.bida

Recently we have received some questions concerning sending commands through ControlConnection from Java code. 

This is possible in Pogamut2, however it is not entirely straightforward. 
The problem with this is that it takes some time for the UTServer to initialize and during that time all the commands that are sent to the UTServer are thrown out. The solution is to wait until the UTServer is initialized. This can be done for example like this: 

The example shows how to add two original epic bots through control connection to the GameBots server. Any other supported GameBots command/or control server method may be called.

package testtest;

import cz.cuni.pogamut.MessageObjects.Triple;
import cz.cuni.pogamut.exceptions.PogamutException;
import cz.cuni.pogamut.server.UTServer;
import cz.cuni.pogamut.server.UTServerState;
import cz.cuni.utils.FlagListener;
import java.io.IOException;
import java.net.URI;
import java.util.concurrent.CountDownLatch;

public class MainClass {

       public static void main(String[] args) throws IOException,
InterruptedException {
               final CountDownLatch latch =  new CountDownLatch(1);

               try {
                       UTServer s = new UTServer();
                       s.setGamebotsBotURI(URI.create("ut://localhost:3000"));

s.setGamebotsControlConnectionURI(URI.create("ut://localhost:3001"));

                       // setup listener that will raise the latch
when server will be in
RUNNING state
                       s.getServerStateFlag().addListener(new
FlagListener() {
                               public void flagChanged(UTServerState
changedValue, int listenerParam) {

if(changedValue.equals(UTServerState.RUNNING)) {
                                               latch.countDown();
                                       }
                               }
                       });

                       // Wait for latch since commands can be send
only when the server is
                       // RUNNING state
                       latch.await();

                       System.out.println("ADDING BOTS");
                       //s.sendToGB("ADDBOT");
                       for (int i = 0; i < 2; i++) {
                               s.connectOriginalBot("original", new
Triple(0, 0, 0), 1, 1);
                       }
               } catch (PogamutException e) {
                       throw new RuntimeException(e);
               }

       }
}


Best,
Michal





More information about the Pogamut-list mailing list