View Javadoc

1   package cz.cuni.amis.pogamut.usar2004.agent.module.master;
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.agent.module.state.MissionPackageState;
8   import cz.cuni.amis.pogamut.usar2004.communication.messages.usarinfomessages.MissionPackageMessage;
9   import java.util.HashMap;
10  import java.util.Map;
11  import java.util.Set;
12  
13  /**
14   * Master module for gathering every mission state message server sends. They
15   * are saved respectively by their name. There is a listener for MISSTA messages
16   * that updates data in local Map. All mastermodules are singletons.
17   *
18   * @author vejmanm
19   */
20  public class MissionPackageModule extends SensorModule<USAR2004Bot>
21  {
22      protected MissionPackageMessageListener packageListener;
23      protected static MissionPackageModule singleton = null;
24      protected Map<String, MissionPackageState> packageModule;
25  
26      /**
27       * Private ctor
28       *
29       * @param bot USAR2004Bot variable for creating instance of each new record
30       * in stateModules(due to inheritance)
31       */
32      private MissionPackageModule(USAR2004Bot bot)
33      {
34          super(bot);
35          packageModule = new HashMap<String, MissionPackageState>();
36          packageListener = new MissionPackageMessageListener(worldView);
37      }
38  
39      /**
40       * Returns singleton instance of this module if it already exists. If not it
41       * is created.
42       *
43       * @param bot Parameter for possible creating of new instance.
44       * @return Returns singleton instance of this module.
45       */
46      public static MissionPackageModule getModuleInstance(USAR2004Bot bot)
47      {
48          if(singleton == null)
49          {
50              singleton = new MissionPackageModule(bot);
51          }
52          return singleton;
53      }
54  
55      /**
56       * Check method for making sure the acces to this module is possible.Returns
57       * false if either local map is empty or null;
58       *
59       * @return Returns false if either local map is empty or null;
60       */
61      public Boolean isReady()
62      {
63          return (packageModule != null && !packageModule.isEmpty());
64      }
65  
66      /**
67       * Gets mission state message representative from local hashmap. Returns
68       * null if none matches.
69       *
70       * @param name String representing the type of state to return
71       * @return Returns mission package State according to the name.
72       */
73      public MissionPackageState getStatesByName(String name)
74      {
75          if(name == null)
76          {
77              return null;
78          }
79          return packageModule.get(name.toLowerCase());
80      }
81  
82      /**
83       * Returns list of availible mission state object types.
84       *
85       * @return Returns list of availible mission state object types.
86       */
87      public Set<String> getAvailibleTypes()
88      {
89          return packageModule.keySet();
90      }
91  
92      /**
93       * Returns a flag that indicates if udatePackage was successful.
94       *
95       * @param message new MissionPackageMessage object.
96       * @return Return false if this message type with this name does not exist
97       * yet.
98       */
99      protected boolean updatePackageCollection(MissionPackageMessage message)
100     {
101         if(!packageModule.containsKey(message.getName().toLowerCase()))
102         {
103             return false;
104         }
105         packageModule.get(message.getName().toLowerCase()).updateMessage(message);
106         return true;
107     }
108 
109     /**
110      * Updates previous MissionPackageMessage or creates a new Record.
111      *
112      * @param message This ought to be MissionPackageMessage caught by listener.
113      */
114     protected void filePackageMessage(MissionPackageMessage message)
115     {
116         if(updatePackageCollection(message))
117         {
118             return;
119         }
120 
121         MissionPackageState newPackage = new MissionPackageState();
122         String name = message.getName().toLowerCase();
123         newPackage.updateMessage(message);//fill the object
124         packageModule.put(name, newPackage);
125     }
126 
127     @Override
128     protected void cleanUp()
129     {
130         super.cleanUp();
131         packageListener = null;
132         packageModule = null;
133         singleton = null;
134     }
135 
136     private class MissionPackageMessageListener implements IWorldEventListener<MissionPackageMessage>
137     {
138         @Override
139         public void notify(MissionPackageMessage event)
140         {
141             filePackageMessage(event);
142         }
143 
144         public MissionPackageMessageListener(IWorldView worldView)
145         {
146             worldView.addEventListener(MissionPackageMessage.class, this);
147         }
148     }
149 }