private void doPing(SingletonEndpoint destination) { // create a new bundle Bundle b = new Bundle(); // set the destination of the bundle b.setDestination(destination); // limit the lifetime of the bundle to 60 seconds b.setLifetime(60L); // set status report requests for bundle reception b.set(ProcFlags.REQUEST_REPORT_OF_BUNDLE_RECEPTION, true); // set destination for status reports b.setReportto(SingletonEndpoint.ME); // generate some payload String payload = "Hello World"; try { // store the current time mStart = System.nanoTime(); // send the bundle BundleID ret = mSession.send(b, payload.getBytes()); if (ret == null) { Log.e(TAG, "could not send the message"); } else { Log.d(TAG, "Bundle sent, BundleID: " + ret.toString()); } } catch (SessionDestroyedException e) { Log.e(TAG, "could not send the message", e); } }
@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(); }
@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; } }