00001 /* 00002 * ExtensionBlock.cpp 00003 * 00004 * Created on: 26.05.2010 00005 * Author: morgenro 00006 */ 00007 00008 #include "ibrdtn/data/ExtensionBlock.h" 00009 #include "ibrdtn/data/Exceptions.h" 00010 #include <ibrcommon/thread/MutexLock.h> 00011 00012 namespace dtn 00013 { 00014 namespace data 00015 { 00016 ExtensionBlock::ExtensionBlock() 00017 : Block(0), _blobref(ibrcommon::TmpFileBLOB::create()) 00018 { 00019 } 00020 00021 ExtensionBlock::ExtensionBlock(ibrcommon::BLOB::Reference ref) 00022 : Block(0), _blobref(ref) 00023 { 00024 } 00025 00026 ExtensionBlock::~ExtensionBlock() 00027 { 00028 } 00029 00030 ibrcommon::BLOB::Reference ExtensionBlock::getBLOB() 00031 { 00032 return _blobref; 00033 } 00034 00035 size_t ExtensionBlock::getLength() const 00036 { 00037 return _blobref.getSize(); 00038 } 00039 00040 std::ostream& ExtensionBlock::serialize(std::ostream &stream) const 00041 { 00042 ibrcommon::BLOB::Reference blobref = _blobref; 00043 ibrcommon::MutexLock l(blobref); 00044 00045 // write payload 00046 (*blobref).seekg(0); 00047 stream << (*blobref).rdbuf(); 00048 00049 return stream; 00050 } 00051 00052 std::istream& ExtensionBlock::deserialize(std::istream &stream) 00053 { 00054 // clear the blob 00055 _blobref.clear(); 00056 00057 // lock the BLOB 00058 ibrcommon::MutexLock l(_blobref); 00059 00060 // remember the old exceptions state 00061 std::ios::iostate oldstate = (*_blobref).exceptions(); 00062 00063 // activate exceptions for this method 00064 (*_blobref).exceptions(std::ios::badbit | std::ios::failbit | std::ios::eofbit); 00065 00066 try { 00067 // read payload 00068 const int buffer_size = 0x1000; 00069 char buffer[buffer_size]; 00070 ssize_t remain = _blocksize; 00071 00072 while (remain > 0 && stream.good()) 00073 { 00074 if (remain > buffer_size) 00075 { 00076 stream.read(buffer, buffer_size); 00077 } 00078 else 00079 { 00080 stream.read(buffer, remain); 00081 } 00082 00083 (*_blobref).write(buffer, stream.gcount()); 00084 00085 remain -= stream.gcount(); 00086 } 00087 } catch (std::ios_base::failure ex) { 00088 throw dtn::SerializationFailedException(); 00089 } catch (...) { 00090 // restore the old state 00091 (*_blobref).exceptions(oldstate); 00092 00093 throw; 00094 } 00095 00096 // restore the old state 00097 (*_blobref).exceptions(oldstate); 00098 00099 // set block not processed bit 00100 set(dtn::data::Block::FORWARDED_WITHOUT_PROCESSED, true); 00101 00102 return stream; 00103 } 00104 } 00105 }
1.6.3