Exemplo n.º 1
0
  public void keyPressed() { // NOPMD
    if ((phase != 2) || (changingLevel)) {
      return;
    }

    if ((key == 'b') || (key == 'B')) {
      final int whoAmI = BLUE;
      counterValue[whoAmI]++;
      final boolean check = thisInteractome.terra(whoAmI, INC_SIMPLE + (counterValue[whoAmI] / 5));
      fixCounterBoxWidth(whoAmI);
      splayer.playSample(whoAmI);
      if (check) {
        Logger.getInstance().debug("Found winner");
      }

    } else if ((key == 'g') || (key == 'G')) {
      final int whoAmI = GREEN;
      counterValue[whoAmI]++;
      final boolean check = thisInteractome.terra(whoAmI, INC_SIMPLE + (counterValue[whoAmI] / 5));
      fixCounterBoxWidth(whoAmI);
      splayer.playSample(whoAmI);
      if (check) {
        Logger.getInstance().debug("Found winner");
      }
    }
  }
Exemplo n.º 2
0
  public boolean sendEventSync(final Event event) {
    boolean succesfull = false;
    if (EchoProtocolManager.getInstance().getMyStation().isActive()) {
      try {
        // Creates a broadcast Datagram Connection
        dgConnection =
            (DatagramConnection)
                Connector.open(
                    "radiogram://"
                        + EchoProtocolManager.getInstance().getMyStation().getAddress().longValue()
                        + ":"
                        + DTSPORT);

        // Creates a Datagram using the above Connection
        final Datagram datagram = dgConnection.newDatagram(dgConnection.getMaximumLength());

        // Clean the Datagram
        datagram.reset();

        // Convert event to byte Array
        final byte[] eventArray = event.toByteArray();

        // Send Class Type
        datagram.writeUTF(event.getClass().getName());

        // Send length
        datagram.writeInt(eventArray.length);

        // Send array
        datagram.write(eventArray, 0, eventArray.length);

        // Send the datagram
        dgConnection.send(datagram);

        dgConnection.close();

        Logger.getInstance().debug("Event sent succefully");
        succesfull = true;

      } catch (Exception ex) {
        Logger.getInstance().debug("Unable to send event", ex);

        // Close the open connection
        try {
          dgConnection.close();
        } catch (Exception e) {
          // eat it
        }

        succesfull = false;
      }
    }
    return succesfull;
  }
Exemplo n.º 3
0
  /**
   * Check if the event is a legal continuation of the current situation.
   *
   * @param event The event to be checked
   * @return The value 0 if the event is valid; the value -1 if the event has already been received;
   *     the value 1 if the event is not yet valid
   */
  private int checkEvent(final Event event) {
    // Check if Guardian ID is known
    if (!guardianMap.containsKey(event.getGuardianID())) {
      Logger.getInstance().debug("\t\tNo such eu.funinnumbers.guardian");
      return -1;
    }

    /* TODO check this Super by-passing!
            // Check if this is an Event generated by the Station
            if (event.getGuardianCounter() == MOTIONEVENT_ID) {
                return 0;
            }

            // Locate current counter for particular eu.funinnumbers.guardian
            final int curCount = getCounter(event.getGuardianID());

            // Check if event is old
            if (event.getGuardianCounter() <= curCount) {
                Logger.getInstance().debug("\t\tOld event");
                return -1;
            }

            // Check if event is in the future
            if (event.getGuardianCounter() > curCount + 1) {
                return 1;
            }
    */

    return 0;
  }
Exemplo n.º 4
0
  /**
   * Process a new event.
   *
   * @param event The event to be processed
   */
  private void processEvent(final Event event) { // NOPMD
    final int result = checkEvent(event);
    // Logger.getInstance().debug("result=" + result);
    // result = 0;
    if (result == 0) {
      /**
       * if the event is valid, remove it (in case it exists) from the buffer, update the values of
       * the counters and send it to the EngineRMIImpl. Also check the buffer for events that may
       * now be legal
       */
      removeFromBuffer(event);

      // Check if this is an Event generated by the Station -- if not increase the counter
      if (event.getGuardianCounter() != MOTIONEVENT_ID) {
        incCounter(event.getGuardianID());
      }

      /*    Logger.getInstance().debug("Coordinator: "
      + event.getClass().getName() + " submitted --> " + event.toString());*/
      submitEvent(event);
      checkBuffer();

    } else if (result == -1) {
      // if we have already received the event, do nothing
      Logger.getInstance()
          .debug("Coordinator: " + event.getClass().getName() + " dumped --> " + event.toString());

    } else if (result == 1) {
      // if the event cannot yet be sent, store it in the buffer
      addToBuffer(event);
    }
  }
Exemplo n.º 5
0
    public void run() { // NOPMD

      try {
        synchronized (team1gesA) {
          switch (stage) {
            case 0:
              team1gesA = team2gesA = anyFrames.get(curImageFrame);
              break;

            case 1:
              // Only check curBlueGes and curGreenGes
              team1gesA = imagesForGestures.get(curGesture[BLUE]).get(curImageFrame);
              team2gesA = imagesForGestures.get(curGesture[GREEN]).get(curImageFrame);
              break;

            default:
              break;
          }
        }

        if (curImageFrame >= 6) {
          curImageFrame = -1;
        }
        curImageFrame++;

      } catch (Exception e) {
        Logger.getInstance().debug("No gesture?");
      }
    }
Exemplo n.º 6
0
  private void drawGestures() {

    try {
      switch (stage) {
        case 0:
          // Draw gesture for team A
          image(team1gesA, stage1Team1width, stage1height);

          // Draw gesture for team B
          image(team2gesA, stage1Team2width, stage1height);
          break;

        case 1:
          // Draw gesture for team A
          image(team1gesA, stage1Team1width, stage1height);

          // Draw gesture for team B
          image(team2gesA, stage1Team2width, stage1height);
          break;

        default:
          break;
      }
    } catch (Exception e) {
      Logger.getInstance().debug("No imageIoann   Ioa?");
    }
  }
Exemplo n.º 7
0
  /**
   * The constuctor of the class.
   *
   * @param guardians collection with all guardians
   */
  public Coordinator(final Collection guardians) {
    Logger.getInstance().debug("Coordinator: Initializing... ");

    // Construct HashMap for Stations indexed by IP
    final Iterator iterGuardians = guardians.iterator();
    while (iterGuardians.hasNext()) {
      final Guardian guardian = (Guardian) iterGuardians.next();
      guardianMap.put(guardian.getID(), 0);
      Logger.getInstance()
          .debug(
              "Guardian ID="
                  + guardian.getID()
                  + ", MAC="
                  + guardian.getAddress()
                  + ", LEDID="
                  + guardian.getLedId());
    }
  }
Exemplo n.º 8
0
 /**
  * Add an event to the buffer, after checking first if the event already exists.
  *
  * @param event The event to be stored
  */
 private void addToBuffer(final Event event) {
   // if event is already in buffer don't add it
   if (!eventBuffer.contains(event)) {
     // Add to buffer
     eventBuffer.add(event);
     Logger.getInstance()
         .debug(
             "Added to buffer. Current size: " + eventBuffer.size() + " --> " + event.toString());
   }
 }
Exemplo n.º 9
0
 /** The connection to the Battle Station was lost. */
 private void setDisconnected() {
   // Close a possible connection to the Battle FGStationApp
   try {
     if (dgConnection != null && !EchoProtocolManager.getInstance().getMyStation().isActive()) {
       dgConnection.close();
     }
   } catch (Exception ex) {
     Logger.getInstance().debug("Unable to disconnect", ex);
   }
 }
Exemplo n.º 10
0
  /**
   * Connects to the Coordinator and sends the specified event.
   *
   * @param event The event to send to the coordinator
   */
  public void sendEvent(final Event event) {

    try {

      engineIF.addEvent(event);

    } catch (Exception e) {
      Logger.getInstance().debug("Unable to send event to Engine", e);
    }
  }
Exemplo n.º 11
0
    public void run() { // NOPMD
      counter++;
      switch (stage) {
        case 0:
          if (firstTimeInBonus) {
            counter = 0;
            firstTimeInBonus = false;
            stageDuration = 5;
            Logger.getInstance().debug("\t\t---Level 0---");

          } else if (counter > stageDuration) {
            Logger.getInstance().debug("\t\t--End of level 0--");

            // Random next stage
            stage = randomGenerator.nextInt(3);
            // give more changes to level 1
            if (stage > 1) {
              stage = 1;
            }

            firstTimeInBonus = true;
          }
          break;

        case 1:
          if (firstTimeInBonus) {
            counter = 0;
            firstTimeInBonus = false;
            stageDuration = 5;
            Logger.getInstance().debug("\t\t---Level 1---");

            // Generate gesture for blue team
            curGesture[BLUE] = gestMap.get(randomGenerator.nextInt(gestMap.size()));
            Logger.getInstance().debug("Blue Current Gesture: " + curGesture[BLUE]);

            // Generate gesture for green team
            curGesture[GREEN] = gestMap.get(randomGenerator.nextInt(gestMap.size()));
            Logger.getInstance().debug("Green Current Gesture: " + curGesture[GREEN]);

          } else if (counter > stageDuration) {
            Logger.getInstance().debug("\t\t--End of level 1--");

            // Random next stage
            stage = randomGenerator.nextInt(3);

            // give more changes to level 1
            if (stage > 1) {
              stage = 1;
            }
            firstTimeInBonus = true;
            curGesture[BLUE] = null;
            curGesture[GREEN] = null;
            resetCurrentFrames();
          }
          break;

        default:
          break;
      }
    }
Exemplo n.º 12
0
  /**
   * This method is called whenever the observed object is changed. An application calls an
   * <tt>Observable</tt> object's <code>notifyObservers</code> method to have all the object's
   * observers notified of the change.
   *
   * @param obs the observable object.
   * @param arg an argument passed to the <code>notifyObservers</code> method.
   */
  public void update(final Observable obs, final Object arg) { // NOPMD
    // Only interested to updates received from TOW Manager
    if (!(obs instanceof EventConsumer)) {
      return;
    }

    // Only interested for updates conveing an Event
    if (!(arg instanceof Event)) {
      return;
    }

    final Event event = (Event) arg;

    if (!event.getType().equals("TOW")) {
      // ignore it
      return;
    }

    // Extract team
    final int team = event.getGuardianID();
    if (team < 0 || team > 1) {
      Logger.getInstance().debug("Illegal");
      return;
    }

    Logger.getInstance().debug("Team " + event.getGuardianID() + event.getDescription());
    switch (stage) {
      case 0:
        if ((phase != 2) || (changingLevel)) {
          // Accept gesture only when we are playing a stage
          return;
        }
        Logger.getInstance().debug(" \n\n\t\tWELL DONE! team " + team);
        counterValue[team]++;
        fixCounterBoxWidth(team);
        thisInteractome.terra(team, INC_SIMPLE + (counterValue[team] / 10));
        // TODO play sample correct gesture
        splayer.playSample(team);
        break;

        // Gesture per team
      case 1:
        if ((phase != 2) || (changingLevel)) {
          // Accept gesture only when we are playing a stage
          return;
        }

        // check if gesture is correct
        if (event.getDescription().equals(curGesture[team])) {
          Logger.getInstance().debug(" \n\n\t\tWELL DONE! team " + team);
          thisInteractome.terra(team, INC_SIMPLE + (counterValue[team] / 10));
          counterValue[team]++;
          fixCounterBoxWidth(team);
          // TODO play sample correct gesture
          splayer.playSample(team);
        }

        break;

      default:
        break;
    }
  }
Exemplo n.º 13
0
  public void draw() { // NOPMD

    switch (phase) {
      case 0: // Show Starting SlidingBar.
        Logger.getInstance().debug("Case 0");
        if (starting.fadeIn(100, 0, 100, 0, 5, 0, 0)) {
          phase = 1;
        }
        textFont(fontCounter, 255);
        textSize(48);
        break;

      case 1: // Show Transition Effect.
        splayer.playSample(SamplePlayer.START_GAME + 2);

        if (createTransition(kare)) {
          phase = 2;
          changingLevel = false;
          kare = 0;
          trans = 250;
        }

        kare += 1;
        textFont(fontCounter, 255);
        textSize(48);
        break;

      case 2: // Show Actual Game Environment.
        if (redrawMorphome()) {
          counterValue[BLUE] = 0;
          counterValue[GREEN] = 0;
          phase = 3;
          changingLevel = true;
        }

        if (!changingLevel) {
          // draw gestures for both teams
          drawGestures();

          // draw gesture counter
          drawCounters();
        }

        break;

      case 3: // Make Everything White/Serene/Tranquil/Peaceful.
        eraser(255, 20);
        glow();
        unlockControls = false;
        if (thisInteractome.serene()) {
          phase = 4;
        }

        break;

      case 4: // Erase Everything for once.
        eraser(255, 255);
        phase = 5;
        break;

      case 5: // Show Ending SlidingBar. For Instance "http://finn.cti.gr"
        phase = 6;
        ending.fadeIn(50, 0, 10, (SCREENS - 1) * 50, 10, 10, 2);
        ending.reInitialize();
        break;

      case 6: // Uber Black Box.
        phase = 7;
        ending.fadeOut(50, 0, 0, 300, 30, teamWidth, 0, 0, 0, color(0, 0, 0), 1);
        ending.reInitialize();
        break;

      case 7: // Enwhiten.
        phase = 1;
        ending.fadeOut(25, 0, 0, 200, 10, teamWidth, 0, 0, 0, color(255, 255, 255), 1);
        createRandomness();
        thisInteractome = new Interactome(this, MAX_SIZE, MIN_SIZE, CIRCLE_COUNT, SCREENS, splayer);
        // initializeTitles(); //starting, ending
        kare = 0;
        trans = 0;
        break;

      default:
        break;
    }
  }
Exemplo n.º 14
0
  /** Loops through the eventBuffer and sends every entry to Battle Station. */
  protected void emptyEventBuffer() { // NOPMD
    /*TODO find an efficient way to empty send multiple *EVENTENTITIES*/

    //        time1 = System.currentTimeMillis();
    // Send EVENTENTITIES
    // Read all events from the corresponding recordstore
    Vector eventBuffer = StorageService.getInstance().listEntities(StorableEntity.EVENTENTITY);

    // Clear the event recordstore
    StorageService.getInstance().clear(StorableEntity.EVENTENTITY);
    //        time2 = System.currentTimeMillis();
    //       Logger.getInstance().debug("\t\t***Events: "+ eventBuffer.size());
    //        time2 = time2 - time1;
    //       Logger.getInstance().debug("\t\t***Storage time: " + time2);

    // Send each event to the Battle FGStationApp
    Enumeration eve;
    for (eve = eventBuffer.elements(); eve.hasMoreElements(); ) {

      final Event event = (Event) eve.nextElement();
      // Previous attempt sending the event failed
      // don't try to send next event, add it to storage
      if (failedUpdate) {
        Logger.getInstance().debug("Failed.Adding event back to storage.");
        StorageService.getInstance().add(event);
        // FinnLogger.getInstance().increaseStoredEvents();
        // Send the event normally
      } else {
        sendEvent(event);
        // FinnLogger.getInstance().decreaseStoredEvents();
        final int tmpVar = 5;
        Utils.sleep(tmpVar * (1 + EchoProtocolManager.getInstance().getNeighbours().size()));
        // Logger.getInstance().debug("Updating eu.funinnumbers.station event: " + i + "/" +
        // eventBuffer.size());
      }
    }

    // Sending ACTIONEVENTENTITIES

    //        time1 = System.currentTimeMillis();
    // Read all events from the corresponding recordstore
    eventBuffer = StorageService.getInstance().listEntities(StorableEntity.ACTIONEVENTENTITY);

    // Clear the event recordstore
    StorageService.getInstance().clear(StorableEntity.ACTIONEVENTENTITY);

    //        time2 = System.currentTimeMillis();
    //      Logger.getInstance().debug("\t\t***Events: "+ eventBuffer.size());
    //        time2 = time2 - time1;
    //       Logger.getInstance().debug("\t\t***Storage time: " + time2);

    // Send each event to the Battle FGStationApp
    Enumeration act;
    for (act = eventBuffer.elements(); act.hasMoreElements(); ) {

      final Event event = (Event) act.nextElement();

      // Previous attempt sending the event failed
      // don't try to send next event, add it to storage
      if (failedUpdate) {
        Logger.getInstance().debug("Failed.Adding event back to storage.");
        StorageService.getInstance().add(event);
        // FinnLogger.getInstance().increaseStoredEvents();
        // Send the event normally
      } else {

        sendEvent(event);
        // FinnLogger.getInstance().decreaseStoredEvents();
        // Variable rate
        final int tmpVar = 5;
        Utils.sleep(tmpVar * (1 + EchoProtocolManager.getInstance().getNeighbours().size()));
        // Logger.getInstance().debug("Updating eu.funinnumbers.station event: " + i + "/" +
        // eventBuffer.size());
      }
    }
  }
Exemplo n.º 15
0
  /**
   * Tries to send an event to it's recipient. If there is no connection to some
   * eu.funinnumbers.station event is saved in the event buffer.
   *
   * @param event the event to be sent
   */
  public synchronized void sendEvent(final Event event) { // NOPMD
    // Check if event should be sent to a Battle FGStationApp (checking type? id?)

    if (EchoProtocolManager.getInstance().getMyStation().isActive()) {
      try {
        // Creates a broadcast Datagram Connection
        dgConnection =
            (DatagramConnection)
                Connector.open(
                    "radiogram://"
                        + EchoProtocolManager.getInstance().getMyStation().getAddress().longValue()
                        + ":"
                        + DTSPORT);

        // Creates a Datagram using the above Connection
        final Datagram datagram = dgConnection.newDatagram(dgConnection.getMaximumLength());

        // Clean the Datagram
        datagram.reset();

        // Convert event to byte Array
        final byte[] eventArray = event.toByteArray();

        // Send Class Type
        datagram.writeUTF(event.getClass().getName());

        // Send length
        datagram.writeInt(eventArray.length);

        // Send array
        datagram.write(eventArray, 0, eventArray.length);

        // Send the datagram
        dgConnection.send(datagram);

        dgConnection.close();

        // There was a succefull update attempt after a failed one.
        if (failedUpdate) {
          Logger.getInstance().debug("Connection to eu.funinnumbers.station now seems to be OK");
          failedUpdate = false;

          // Try to empty events buffer again
          emptyEventBuffer();
        }
        Logger.getInstance().debug("Event sent succefully");

      } catch (Exception ex) {
        Logger.getInstance().debug("Unable to send event", ex);
        Logger.getInstance().debug("Adding event to storage: " + event.getDescription());
        // Close the open connection
        setDisconnected();

        // The attempt has failed
        failedUpdate = true;

        // Add event back to storage
        StorageService.getInstance().add(event);
        // FinnLogger.getInstance().increaseStoredEvents();

      }

    } else if (storageEnabled) {

      Logger.getInstance().debug("The connection seems to be inactive. Saving to buffer");
      // Add event to the corresponding recordstore
      StorageService.getInstance().add(event);
      // FinnLogger.getInstance().increaseStoredEvents();
    } else {
      Logger.getInstance().debug("Event lost!!!");
    }
  }