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 ibrcommon::BLOB::Reference blobref = _blobref; 00038 ibrcommon::MutexLock l(blobref); 00039 return blobref.getSize(); 00040 } 00041 00042 std::ostream& ExtensionBlock::serialize(std::ostream &stream) const 00043 { 00044 ibrcommon::BLOB::Reference blobref = _blobref; 00045 ibrcommon::MutexLock l(blobref); 00046 00047 // write payload 00048 (*blobref).seekg(0); 00049 stream << (*blobref).rdbuf(); 00050 00051 return stream; 00052 } 00053 00054 std::istream& ExtensionBlock::deserialize(std::istream &stream) 00055 { 00056 // lock the BLOB 00057 ibrcommon::MutexLock l(_blobref); 00058 00059 // clear the blob 00060 _blobref.clear(); 00061 00062 // remember the old exceptions state 00063 std::ios::iostate oldstate = (*_blobref).exceptions(); 00064 00065 // activate exceptions for this method 00066 (*_blobref).exceptions(std::ios::badbit | std::ios::eofbit); 00067 00068 try { 00069 // read payload 00070 const int buffer_size = 0x1000; 00071 char buffer[buffer_size]; 00072 ssize_t remain = _blocksize; 00073 00074 while (remain > 0 && stream.good()) 00075 { 00076 if (remain > buffer_size) 00077 { 00078 stream.read(buffer, buffer_size); 00079 } 00080 else 00081 { 00082 stream.read(buffer, remain); 00083 } 00084 00085 (*_blobref).write(buffer, stream.gcount()); 00086 00087 remain -= stream.gcount(); 00088 } 00089 } catch (std::ios_base::failure ex) { 00090 throw dtn::SerializationFailedException(); 00091 } catch (...) { 00092 // restore the old state 00093 (*_blobref).exceptions(oldstate); 00094 00095 throw; 00096 } 00097 00098 // restore the old state 00099 (*_blobref).exceptions(oldstate); 00100 00101 // set block not processed bit 00102 set(dtn::data::Block::FORWARDED_WITHOUT_PROCESSED, true); 00103 00104 return stream; 00105 } 00106 } 00107 }
1.7.1