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) 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::istream& PayloadBlock::deserialize(std::istream &stream) 00058 { 00059 // lock the BLOB 00060 ibrcommon::BLOB::iostream io = _blobref.iostream(); 00061 00062 // clear the blob 00063 io.clear(); 00064 00065 // check if the blob is ready 00066 if (!(*io).good()) throw dtn::SerializationFailedException("could not open BLOB for payload"); 00067 00068 try { 00069 ibrcommon::BLOB::copy(*io, stream, _blocksize); 00070 } catch (const ibrcommon::IOException &ex) { 00071 throw dtn::SerializationFailedException(ex.what()); 00072 } 00073 00074 // set block not processed bit to false 00075 set(dtn::data::Block::FORWARDED_WITHOUT_PROCESSED, false); 00076 00077 return stream; 00078 } 00079 } 00080 }
1.7.1