View Javadoc

1   package cz.cuni.amis.pogamut.udk.agent.module.sensomotoric;
2   
3   import java.util.logging.Logger;
4   
5   import cz.cuni.amis.pogamut.base.agent.module.SensomotoricModule;
6   import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
7   import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
8   import cz.cuni.amis.pogamut.udk.bot.IUDKBotController;
9   import cz.cuni.amis.pogamut.udk.bot.impl.UDKBot;
10  import cz.cuni.amis.pogamut.udk.communication.messages.gbcommands.Ping;
11  import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.Pong;
12  import cz.cuni.amis.utils.flag.Flag;
13  
14  /**
15   * Memory module specialized on requests to the map.
16   * <p><p>
17   * It is designed to be initialized inside {@link IUDKBotController#initializeController(UDKBot)} method call
18   * and may be used since {@link IUDKBotController#botInitialized(cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.GameInfo, cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.ConfigChange, cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.InitedMessage)}
19   * is called.
20   *
21   * @author Juraj 'Loque' Simlovic
22   */
23  public class Requests extends SensomotoricModule<UDKBot>
24  {
25  	/**
26  	 * Sends PING message to the server, use {@link Requests#getLastPong()} to see the results.
27  	 * @return flag that contains {@link System#currentTimeMillis()} readings of the last {@link Pong} message received.
28  	 */
29  	public Flag<Long> ping() {
30  		act.act(new Ping());
31  		return lastPong;
32  	}
33  
34  	/**
35  	 * Returns flag that holds the value of 'last pong receive time'.
36  	 * @returnflag that contains {@link System#currentTimeMillis()} readings of the last {@link Pong} message received.
37  	 */
38  	public Flag<Long> getLastPong() {
39  		return lastPong;
40  	}
41  
42  	
43  	/*========================================================================*/
44  
45  	/**
46  	 * Pong listener.
47  	 */
48  	private class PongListener implements IWorldEventListener<Pong>
49  	{
50  		@Override
51  		public void notify(Pong event)
52  		{
53  			lastPong.setFlag(System.currentTimeMillis());
54  		}
55  
56  		/**
57  		 * Constructor. Registers itself on the given WorldView object.
58  		 * @param worldView WorldView object to listent to.
59  		 */
60  		public PongListener(IWorldView worldView)
61  		{
62  			worldView.addEventListener(Pong.class, this);
63  		}
64  	}
65  
66  	/** Pong listener */
67  	PongListener pongListener;
68  	
69  	Flag<Long> lastPong = new Flag<Long>((long)-1);
70  
71  	/*========================================================================*/
72  
73  	
74  	/**
75  	 * Constructor. Setups the memory module based on given WorldView.
76  	 * @param bot worldView WorldView object to read the info from.
77  	 */
78  	public Requests(UDKBot bot)
79  	{
80  		this(bot, null);
81  	}
82  	
83  	/**
84  	 * Constructor. Setups the memory module based on given WorldView.
85  	 * @param bot worldView WorldView object to read the info from.
86  	 * @param log Logger to be used for logging runtime/debug info. If <i>null</i>, module creates its own logger.
87  	 */
88  	public Requests(UDKBot bot, Logger log)
89  	{
90  		super(bot, log);
91  
92  		// create listeners
93  		pongListener = new PongListener(worldView);
94  	}
95  	
96  	/**
97  	 * Provides initialization of the module (clearing internal data structures). Called automatically
98  	 * during the agent starting sequence.
99  	 */
100 	@Override
101 	protected void start(boolean startPaused) {
102 		super.start(startPaused);
103 		lastPong.setFlag((long)-1);
104 	}
105 }