Exemplo n.º 1
0
  @Override
  public void onDestroy() {
    if (mDestroySessionOnExit) mSession.destroy();

    if (mStreamJob != null) {
      mStreamJob.cancel(false);
      mStreamJob = null;

      try {
        mTransmitter.close();
      } catch (InterruptedException e) {
        Log.d(TAG, "interrupted while closing", e);
      } catch (IOException e) {
        Log.d(TAG, "error while closing", e);
      }
    }

    // shutdown executor
    mExecutor.shutdown();

    super.onDestroy();
  }
Exemplo n.º 2
0
  @Override
  protected void onHandleIntent(Intent intent) {
    String action = intent.getAction();

    if (de.tubs.ibr.dtn.Intent.RECEIVE.equals(action)) {
      // Received bundles from the DTN service here
      try {
        // We loop here until no more bundles are available
        // (queryNext() returns false)
        while (mSession.queryNext()) ;
      } catch (SessionDestroyedException e) {
        Log.e(TAG, "Can not query for bundle", e);
      }
    } else if (MARK_DELIVERED_INTENT.equals(action)) {
      // retrieve the bundle ID of the intent
      BundleID bundleid = intent.getParcelableExtra("bundleid");

      try {
        // mark the bundle ID as delivered
        mSession.delivered(bundleid);
      } catch (Exception e) {
        Log.e(TAG, "Can not mark bundle as delivered.", e);
      }
    } else if (REPORT_DELIVERED_INTENT.equals(action)) {
      // retrieve the source of the status report
      SingletonEndpoint source = intent.getParcelableExtra("source");

      // retrieve the bundle ID of the intent
      BundleID bundleid = intent.getParcelableExtra("bundleid");

      Log.d(
          TAG, "Status report received for " + bundleid.toString() + " from " + source.toString());
    } else if (PING_INTENT.equals(action)) {
      // retrieve the ping destination
      SingletonEndpoint destination = new SingletonEndpoint(intent.getStringExtra("destination"));

      // send out the ping
      doPing(destination);
    } else if (STREAM_START_INTENT.equals(action)) {
      Log.d(TAG, "create stream");

      // create DTN stream
      mTransmitter = new DtnStreamTransmitter(PingService.this, mSession);
      mTransmitter.connect(mStreamGroup, MediaType.BINARY, null);
      mTransmitter.setLifetime(10);

      mStreamJob =
          mExecutor.scheduleWithFixedDelay(
              new Runnable() {
                @Override
                public void run() {
                  Log.d(TAG, "send stream packet");
                  try {
                    mTransmitter.write("Hello World!".getBytes());
                  } catch (InterruptedException e) {
                    Log.d(TAG, "interrupted while sending", e);
                  } catch (IOException e) {
                    Log.d(TAG, "error while sending", e);
                  }
                }
              },
              0,
              1,
              TimeUnit.SECONDS);
    } else if (STREAM_STOP_INTENT.equals(action)) {
      if (mStreamJob != null) {
        mStreamJob.cancel(false);
        mStreamJob = null;

        try {
          mTransmitter.close();
        } catch (InterruptedException e) {
          Log.d(TAG, "interrupted while closing", e);
        } catch (IOException e) {
          Log.d(TAG, "error while closing", e);
        }
      }
    } else if (STREAM_SWITCH_GROUP.equals(action)) {
      try {
        mSession.leave(mStreamGroup);

        if (STREAM_GROUP2_EID.equals(mStreamGroup)) {
          // set current stream group
          mStreamGroup = STREAM_GROUP1_EID;
        } else {
          // set current stream group
          mStreamGroup = STREAM_GROUP2_EID;
        }

        mSession.join(mStreamGroup);
      } catch (SessionDestroyedException e) {
        Log.e(TAG, "session destroyed", e);
      }
    } else if (UNREGISTER.equals(action)) {
      mDestroySessionOnExit = true;
    }
  }