Ejemplo n.º 1
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;
  }
Ejemplo n.º 2
0
 /** Creates a new instance of DTSManager. */
 private DTSManager() {
   // Observer EchoProtocol events etc.
   EchoProtocolManager.getInstance().addObserver(this);
   eventSender = new EventSender();
   eventSender.start();
   failedUpdate = false;
 }
Ejemplo n.º 3
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);
   }
 }
Ejemplo n.º 4
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());
      }
    }
  }
Ejemplo n.º 5
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!!!");
    }
  }