コード例 #1
0
 @Override
 public void register() {
   List<Class<? extends IEvent>> events = new ArrayList<Class<? extends IEvent>>();
   events.add(MessageReceivedEvent.class);
   events.add(WiFiInfoCollectedEvent.class);
   events.add(SendBroadcastMessageEvent.class);
   events.add(SendUnicastMessageEvent.class);
   events.add(UpdateStatusEvent.class);
   EventDispatcher.getInstance().registerComponent(this, events);
 }
コード例 #2
0
  /**
   * Setter method for the map. If current device is the GroupOwner, it sends back to all the
   * devices requestConnectionSent to his group the complete map of all the peers
   *
   * @param map
   */
  public synchronized void setPeersIdIPmap(Map<String, String> map) {

    // If setup was already completed, just do nothing
    if (setupCompleted) {
      return;
    }

    if (peerIdIpMap == null) {
      peerIdIpMap = map;
    } else {
      peerIdIpMap.putAll(map);
    }
    if (peerIdIpMap.containsKey(MAC_ADDRESS)) peerIdIpMap.remove(MAC_ADDRESS);
    // If I'm the group owner and I haven't sent the map to all yet and the map is complete (all
    // peers sent me their ID)
    // Broadcast the map to all, after adding my <ID,IP> to it!
    Map<String, String> mapToBroadcast = null;
    if (isGroupOwner) {
      if (!mapSent && peerIdIpMap.keySet().size() >= peers.size()) {

        setupCompleted = true;
        EventDispatcher.getInstance()
            .triggerEvent(new SetupProviderEvent(0, peerIdIpMap.size() + 1));
        EventDispatcher.getInstance().triggerEvent(new UpdateLocationEvent());
        mapToBroadcast = new HashMap<String, String>(peerIdIpMap);
        mapToBroadcast.put(MAC_ADDRESS, groupOwnerAddress);
        IMessage message = createMapMessage(mapToBroadcast, IPaketSender.BROADCAST_ADDRESS);
        logger.d("Invio la mappa a tutti : ");
        sendBroadcast(message);
        mapSent = true;
        // Now start simulation
        EventDispatcher.getInstance().triggerEvent(new SetupCompletedEvent());
        EventDispatcher.getInstance()
            .triggerEvent(new UpdateStatusEvent("Group owner setup completed"));
      }
    } else {
      // If I'm not the group owner and I'm here, I received the map,
      // so I can start simulation
      setupCompleted = true;
      EventDispatcher.getInstance().triggerEvent(new UpdateLocationEvent());
      EventDispatcher.getInstance().triggerEvent(new SetupCompletedEvent());
      EventDispatcher.getInstance().triggerEvent(new UpdateStatusEvent("Peer Setup completed"));
    }

    if (mapToBroadcast == null) mapToBroadcast = peerIdIpMap;
    String s = "Current MAP:\n";
    for (String k : mapToBroadcast.keySet()) {
      s += k + "  " + mapToBroadcast.get(k) + "\n";
    }
    logger.d("Message received\n" + s);
  }