00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef _BUNDLE_PAYLOAD_H_
00039 #define _BUNDLE_PAYLOAD_H_
00040
00041 #include <string>
00042 #include <oasys/serialize/Serialize.h>
00043 #include <oasys/debug/DebugUtils.h>
00044 #include <oasys/io/FileIOClient.h>
00045
00046 namespace dtn {
00047
00048 class BundleStore;
00049
00061 class BundlePayload : public oasys::SerializableObject, public oasys::Logger {
00062 public:
00063 BundlePayload(oasys::SpinLock* lock);
00064 virtual ~BundlePayload();
00065
00069 typedef enum {
00070 MEMORY = 1,
00071 DISK = 2,
00072 UNDETERMINED = 3,
00073 NODATA = 4,
00074 } location_t;
00075
00079 void init(int bundleid, location_t location = UNDETERMINED);
00080
00084 void init_from_store(int bundleid);
00085
00091 void set_length(size_t len, location_t location = UNDETERMINED);
00092
00096 void truncate(size_t len);
00097
00101 size_t length() const { return length_; }
00102
00106 location_t location() const { return location_; }
00107
00112 void set_data(const u_char* bp, size_t len);
00113
00118 void set_data(const std::string& data)
00119 {
00120 set_data((u_char*)data.data(), data.length());
00121 }
00122
00127 void append_data(const u_char* bp, size_t len);
00128
00133 void write_data(const u_char* bp, size_t offset, size_t len);
00134
00140 void write_data(BundlePayload* src, size_t src_offset,
00141 size_t len, size_t dst_offset);
00142
00146 void reopen_file();
00147
00151 void close_file();
00152
00156 bool is_file_open() { return file_->is_open(); }
00157
00161 void copy_file(oasys::FileIOClient* dst);
00162
00166 const u_char* memory_data()
00167 {
00168 ASSERT(location_ == MEMORY);
00169 return (const u_char*)data_.c_str();
00170 }
00171
00175 typedef enum {
00176 KEEP_FILE_OPEN = 0x1,
00177 FORCE_COPY = 0x2,
00178
00179 } read_data_flags_t;
00180
00189 const u_char* read_data(size_t offset, size_t len, u_char* buf,
00190 int flags = 0);
00191
00198 const u_char* read_data(size_t offset, size_t len, u_char* buf,
00199 int flags = 0) const
00200 {
00201 return const_cast<BundlePayload*>(this)->
00202 read_data(offset, len, buf, flags);
00203 }
00204
00208 virtual void serialize(oasys::SerializeAction* a);
00209
00210
00211
00212
00213 static std::string payloaddir_;
00214 static size_t mem_threshold_;
00215 static bool test_no_remove_;
00216
00217 protected:
00218 void internal_write(const u_char* bp, size_t offset, size_t len);
00219
00220 location_t location_;
00221 std::string data_;
00222 size_t length_;
00223 size_t rcvd_length_;
00224 std::string fname_;
00225 oasys::FileIOClient* file_;
00226 size_t cur_offset_;
00227 size_t base_offset_;
00228 oasys::SpinLock* lock_;
00229 };
00230
00231 }
00232
00233 #endif