View Javadoc

1   package cz.cuni.amis.pogamut.usar2004.agent.module.nfo;
2   
3   import cz.cuni.amis.pogamut.base.agent.module.SensorModule;
4   import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
5   import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
6   import cz.cuni.amis.pogamut.usar2004.agent.USAR2004Bot;
7   import cz.cuni.amis.pogamut.usar2004.communication.messages.usarinfomessages.NfoMessage;
8   
9   /**
10   * Parent class for all possible nfo subjects. It covers the basic and common
11   * properties for all the nfo subjects.
12   *
13   * @author vejmanm
14   */
15  public abstract class SuperNfo extends SensorModule<USAR2004Bot>
16  {
17      protected NfoMessageListener nfoListener;
18      protected NfoMessage lastMessage;
19  
20      /**
21       * Ctor.
22       *
23       * @param bot USAR2004Bot variable for filling base class.
24       * @param type Nfo type describes particular subject about which
25       * we want to know about. It is used to distinguish incoming message from
26       * the server.
27       */
28      public SuperNfo(USAR2004Bot bot, String type)
29      {
30          super(bot);
31          nfoListener = new NfoMessageListener(worldView, type);
32      }
33  
34      @Override
35      protected void cleanUp()
36      {
37          super.cleanUp();
38          lastMessage = null;
39      }
40  
41      /**
42       * Used to make sure the object is filled.
43       *
44       * @return Returns true if the object is filled with Nfo message.
45       */
46      public Boolean isReady()
47      {
48          return (lastMessage != null);
49      }
50  
51      private class NfoMessageListener implements IWorldEventListener<NfoMessage>
52      {
53          private String type = null;
54  
55          @Override
56          public void notify(NfoMessage event)
57          {
58              if(isStartPoseMessage(event))
59              {
60                  if(type.equalsIgnoreCase("StartPoses"))
61                  {
62                      lastMessage = event;
63                  }
64              }
65              else
66              {
67                  if(type.equalsIgnoreCase("MapInfo"))
68                  {
69                      lastMessage = event;
70                  }
71              }
72  
73  //            switch(type)
74  //            {
75  //                case "MapInfo":
76  //                    if(!isStartPoseMessage(event))
77  //                        lastMessage = event;
78  //                    break;
79  //                case "StartPoses":
80  //                    if(isStartPoseMessage(event))
81  //                        lastMessage = event;
82  //                    break;
83  //            }
84          }
85  
86          public NfoMessageListener(IWorldView worldView, String type)
87          {
88              this.type = type;
89              worldView.addEventListener(NfoMessage.class, this);
90          }
91  
92          /**
93           * Check method for determination of the Nfo origin.
94           *
95           * @param new NfoMessage message.
96           * @return Returns true if GameType is null or empty - Start Pose does
97           * not have this property. False otherwise.
98           */
99          private boolean isStartPoseMessage(NfoMessage message)
100         {
101             if(message.getGameType() == null || "".equals(message.getGameType()))
102             {
103                 return true;
104             }
105             return false;
106         }
107     }
108 }