IBR-DTNSuite 0.6

ibrdtn/ibrdtn/data/BundleMerger.cpp

Go to the documentation of this file.
00001 /*
00002  * BundleMerger.cpp
00003  *
00004  *  Created on: 25.01.2010
00005  *      Author: morgenro
00006  */
00007 
00008 #include "ibrdtn/config.h"
00009 #include "ibrdtn/data/BundleMerger.h"
00010 #include "ibrdtn/data/Block.h"
00011 #include "ibrdtn/data/PayloadBlock.h"
00012 #include "ibrdtn/data/Exceptions.h"
00013 #include <ibrcommon/thread/MutexLock.h>
00014 
00015 namespace dtn
00016 {
00017         namespace data
00018         {
00019                 BundleMerger::Container::Container(dtn::data::Bundle &b, ibrcommon::BLOB::Reference &ref)
00020                  : _bundle(b), _blob(ref)
00021                 {
00022                         // check if the given bundle is a fragment
00023                         if (!(_bundle.get(dtn::data::Bundle::FRAGMENT)))
00024                         {
00025                                 throw ibrcommon::Exception("This bundle is not a fragment!");
00026                         }
00027 
00028                         // remove all block of the copy
00029                         _bundle.clearBlocks();
00030 
00031                         // mark the copy as non-fragment
00032                         _bundle.set(dtn::data::Bundle::FRAGMENT, false);
00033 
00034                         // add a new payloadblock
00035                         _bundle.push_back(_blob);
00036                 }
00037 
00038                 BundleMerger::Container::~Container()
00039                 {
00040 
00041                 }
00042 
00043                 bool BundleMerger::Container::isComplete()
00044                 {
00045                         return false;
00046                 }
00047 
00048                 Bundle BundleMerger::Container::getBundle()
00049                 {
00050                         return _bundle;
00051                 }
00052 
00053                 BundleMerger::Container &operator<<(BundleMerger::Container &c, dtn::data::Bundle &obj)
00054                 {
00055                         if (    (c._bundle._timestamp != obj._timestamp) ||
00056                                         (c._bundle._sequencenumber != obj._sequencenumber) ||
00057                                         (c._bundle._source != obj._source) )
00058                                 throw ibrcommon::Exception("This fragment does not belongs to the others.");
00059 
00060                         ibrcommon::BLOB::iostream stream = c._blob.iostream();
00061                         (*stream).seekp(obj._fragmentoffset);
00062 
00063                         dtn::data::PayloadBlock &p = obj.getBlock<dtn::data::PayloadBlock>();
00064 
00065                         ibrcommon::BLOB::Reference ref = p.getBLOB();
00066                         ibrcommon::BLOB::iostream s = ref.iostream();
00067 
00068                         size_t ret = 0;
00069                         s->seekg(0);
00070 
00071                         while (!s->eof())
00072                         {
00073                                 char buf;
00074                                 s->get(buf);
00075                                 stream->put(buf);
00076                                 ret++;
00077                         }
00078 
00079                         return c;
00080                 }
00081 
00082                 BundleMerger::Container BundleMerger::getContainer(dtn::data::Bundle &b)
00083                 {
00084                         ibrcommon::BLOB::Reference ref = ibrcommon::TmpFileBLOB::create();
00085                         return Container(b, ref);
00086                 }
00087 
00088                 BundleMerger::Container BundleMerger::getContainer(dtn::data::Bundle &b, ibrcommon::BLOB::Reference &ref)
00089                 {
00090                         return Container(b, ref);
00091                 }
00092         }
00093 }