Example #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;
  }
Example #2
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!!!");
    }
  }