• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

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._procflags & 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._procflags &= ~(dtn::data::Bundle::FRAGMENT);
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::MutexLock l(c._blob);
00061                         (*c._blob).seekp(obj._fragmentoffset);
00062 
00063                         dtn::data::PayloadBlock &p = obj.getBlock<dtn::data::PayloadBlock>();
00064 
00065                         ibrcommon::BLOB::Reference ref = p.getBLOB();
00066                         ibrcommon::MutexLock reflock(ref);
00067 
00068                         size_t ret = 0;
00069                         istream &s = (*ref);
00070                         s.seekg(0);
00071 
00072                         while (!s.eof())
00073                         {
00074                                 char buf;
00075                                 s.get(buf);
00076                                 (*c._blob).put(buf);
00077                                 ret++;
00078                         }
00079 
00080                         return c;
00081                 }
00082 
00083                 BundleMerger::Container BundleMerger::getContainer(dtn::data::Bundle &b)
00084                 {
00085                         ibrcommon::BLOB::Reference ref = ibrcommon::TmpFileBLOB::create();
00086                         return Container(b, ref);
00087                 }
00088 
00089                 BundleMerger::Container BundleMerger::getContainer(dtn::data::Bundle &b, ibrcommon::BLOB::Reference &ref)
00090                 {
00091                         return Container(b, ref);
00092                 }
00093         }
00094 }

Generated on Thu Nov 11 2010 09:49:47 for IBR-DTNSuite by  doxygen 1.7.1