IBR-DTNSuite  0.8
daemon/src/CapsuleWorker.cpp
Go to the documentation of this file.
00001 /*
00002  * CapsuleWorker.cpp
00003  *
00004  *  Created on: 26.04.2011
00005  *      Author: morgenro
00006  */
00007 
00008 #include "CapsuleWorker.h"
00009 #include "net/BundleReceivedEvent.h"
00010 #include "core/BundleCore.h"
00011 #include <ibrdtn/data/PayloadBlock.h>
00012 #include <ibrdtn/data/ScopeControlHopLimitBlock.h>
00013 #include <ibrdtn/utils/Clock.h>
00014 #include <ibrcommon/Logger.h>
00015 
00016 namespace dtn
00017 {
00018         namespace daemon
00019         {
00020                 CapsuleWorker::CapsuleWorker()
00021                 {
00022                         AbstractWorker::initialize("/bundle-in-bundle", 2, true);
00023                 }
00024 
00025                 CapsuleWorker::~CapsuleWorker()
00026                 {
00027                 }
00028 
00029                 void CapsuleWorker::callbackBundleReceived(const dtn::data::Bundle &capsule)
00030                 {
00031                         try {
00032                                 const PayloadBlock &payload = capsule.getBlock<PayloadBlock>();
00033                                 ibrcommon::BLOB::iostream stream = payload.getBLOB().iostream();
00034 
00035                                 // read the number of bundles
00036                                 SDNV nob; (*stream) >> nob;
00037 
00038                                 // read all offsets
00039                                 for (size_t i = 0; i < (nob.getValue() - 1); i++)
00040                                 {
00041                                         SDNV offset; (*stream) >> offset;
00042                                 }
00043 
00044                                 // create a deserializer for all bundles
00045                                 dtn::data::DefaultDeserializer deserializer(*stream);
00046                                 dtn::data::Bundle b;
00047 
00048                                 try {
00049                                         // read all bundles
00050                                         for (size_t i = 0; i < nob.getValue(); i++)
00051                                         {
00052                                                 // deserialize the next bundle
00053                                                 deserializer >> b;
00054 
00055                                                 // validate the bundle
00056                                                 dtn::core::BundleCore::getInstance().validate(b);
00057 
00058                                                 // increment value in the scope control hop limit block
00059                                                 try {
00060                                                         dtn::data::ScopeControlHopLimitBlock &schl = b.getBlock<dtn::data::ScopeControlHopLimitBlock>();
00061                                                         schl.increment();
00062                                                 } catch (const dtn::data::Bundle::NoSuchBlockFoundException&) { };
00063 
00064                                                 // raise default bundle received event
00065                                                 dtn::net::BundleReceivedEvent::raise(capsule._source, b, false, true);
00066                                         }
00067                                 }
00068                                 catch (const dtn::InvalidDataException &ex) {
00069                                         // display the rejection
00070                                         IBRCOMMON_LOGGER(warning) << "invalid bundle-data received: " << ex.what() << IBRCOMMON_LOGGER_ENDL;
00071                                 }
00072                         } catch (const dtn::data::Bundle::NoSuchBlockFoundException&) { };
00073                 }
00074         }
00075 }