|
IBR-DTNSuite 0.6
|
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::TmpFileBLOB::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 } catch (const ibrcommon::IOException &ex) { 00051 throw dtn::SerializationFailedException(ex.what()); 00052 } 00053 00054 return stream; 00055 } 00056 00057 std::ostream& PayloadBlock::serialize(std::ostream &stream, size_t clip_offset, size_t clip_length) const 00058 { 00059 ibrcommon::BLOB::Reference blobref = _blobref; 00060 ibrcommon::BLOB::iostream io = blobref.iostream(); 00061 00062 try { 00063 (*io).seekg(clip_offset, std::ios::beg); 00064 ibrcommon::BLOB::copy(stream, *io, clip_length); 00065 } catch (const ibrcommon::IOException &ex) { 00066 throw dtn::SerializationFailedException(ex.what()); 00067 } 00068 00069 return stream; 00070 } 00071 00072 std::istream& PayloadBlock::deserialize(std::istream &stream, const size_t length) 00073 { 00074 // lock the BLOB 00075 ibrcommon::BLOB::iostream io = _blobref.iostream(); 00076 00077 // clear the blob 00078 io.clear(); 00079 00080 // check if the blob is ready 00081 if (!(*io).good()) throw dtn::SerializationFailedException("could not open BLOB for payload"); 00082 00083 try { 00084 ibrcommon::BLOB::copy(*io, stream, length); 00085 } catch (const ibrcommon::IOException &ex) { 00086 throw dtn::SerializationFailedException(ex.what()); 00087 } 00088 00089 // set block not processed bit to false 00090 set(dtn::data::Block::FORWARDED_WITHOUT_PROCESSED, false); 00091 00092 return stream; 00093 } 00094 } 00095 }