IBR-DTNSuite  0.8
ibrdtn/ibrdtn/data/PayloadBlock.cpp
Go to the documentation of this file.
00001 /*
00002  * PayloadBlock.cpp
00003  *
00004  *  Created on: 29.05.2009
00005  *      Author: morgenro
00006  */
00007 
00008 #include "ibrdtn/data/PayloadBlock.h"
00009 #include "ibrdtn/data/Exceptions.h"
00010 #include <ibrcommon/thread/MutexLock.h>
00011 #include <ibrcommon/Logger.h>
00012 
00013 namespace dtn
00014 {
00015         namespace data
00016         {
00017                 PayloadBlock::PayloadBlock()
00018                  : Block(PayloadBlock::BLOCK_TYPE), _blobref(ibrcommon::BLOB::create())
00019                 {
00020                 }
00021 
00022                 PayloadBlock::PayloadBlock(ibrcommon::BLOB::Reference ref)
00023                  : Block(PayloadBlock::BLOCK_TYPE), _blobref(ref)
00024                 {
00025                 }
00026 
00027                 PayloadBlock::~PayloadBlock()
00028                 {
00029                 }
00030 
00031                 ibrcommon::BLOB::Reference PayloadBlock::getBLOB() const
00032                 {
00033                         return _blobref;
00034                 }
00035 
00036                 size_t PayloadBlock::getLength() const
00037                 {
00038                         ibrcommon::BLOB::Reference blobref = _blobref;
00039                         ibrcommon::BLOB::iostream io = blobref.iostream();
00040                         return io.size();
00041                 }
00042 
00043                 std::ostream& PayloadBlock::serialize(std::ostream &stream, size_t &length) const
00044                 {
00045                         ibrcommon::BLOB::Reference blobref = _blobref;
00046                         ibrcommon::BLOB::iostream io = blobref.iostream();
00047 
00048                         try {
00049                                 ibrcommon::BLOB::copy(stream, *io, io.size());
00050                                 length += io.size();
00051                         } catch (const ibrcommon::IOException &ex) {
00052                                 throw dtn::SerializationFailedException(ex.what());
00053                         }
00054 
00055                         return stream;
00056                 }
00057 
00058                 std::ostream& PayloadBlock::serialize(std::ostream &stream, size_t clip_offset, size_t clip_length) const
00059                 {
00060                         ibrcommon::BLOB::Reference blobref = _blobref;
00061                         ibrcommon::BLOB::iostream io = blobref.iostream();
00062 
00063                         try {
00064                                 (*io).seekg(clip_offset, std::ios::beg);
00065                                 ibrcommon::BLOB::copy(stream, *io, clip_length);
00066                         } catch (const ibrcommon::IOException &ex) {
00067                                 throw dtn::SerializationFailedException(ex.what());
00068                         }
00069 
00070                         return stream;
00071                 }
00072 
00073                 std::istream& PayloadBlock::deserialize(std::istream &stream, const size_t length)
00074                 {
00075                         // lock the BLOB
00076                         ibrcommon::BLOB::iostream io = _blobref.iostream();
00077 
00078                         // clear the blob
00079                         io.clear();
00080 
00081                         // check if the blob is ready
00082                         if (!(*io).good()) throw dtn::SerializationFailedException("could not open BLOB for payload");
00083 
00084                         // set block not processed bit to false
00085                         set(dtn::data::Block::FORWARDED_WITHOUT_PROCESSED, false);
00086 
00087                         try {
00088                                 ibrcommon::BLOB::copy(*io, stream, length);
00089                         } catch (const ibrcommon::IOException &ex) {
00090                                 throw dtn::PayloadReceptionInterrupted(length, ex.what());
00091                         }
00092 
00093                         return stream;
00094                 }
00095         }
00096 }