00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _BUNDLE_PAYLOAD_H_
00018 #define _BUNDLE_PAYLOAD_H_
00019
00020 #include <string>
00021 #include <oasys/serialize/Serialize.h>
00022 #include <oasys/debug/DebugUtils.h>
00023 #include <oasys/io/FileIOClient.h>
00024
00025 namespace dtn {
00026
00027 class BundleStore;
00028
00040 class BundlePayload : public oasys::SerializableObject, public oasys::Logger {
00041 public:
00042 BundlePayload(oasys::SpinLock* lock);
00043 virtual ~BundlePayload();
00044
00048 typedef enum {
00049 MEMORY = 1,
00050 DISK = 2,
00051 UNDETERMINED = 3,
00052 NODATA = 4,
00053 } location_t;
00054
00058 void init(int bundleid, location_t location = UNDETERMINED);
00059
00063 void init_from_store(int bundleid);
00064
00070 void set_length(size_t len, location_t location = UNDETERMINED);
00071
00075 void truncate(size_t len);
00076
00080 size_t length() const { return length_; }
00081
00085 location_t location() const { return location_; }
00086
00090 void set_data(const u_char* bp, size_t len);
00091
00095 void set_data(const std::string& data)
00096 {
00097 set_data((u_char*)data.data(), data.length());
00098 }
00099
00104 void append_data(const u_char* bp, size_t len);
00105
00109 void write_data(const u_char* bp, size_t offset, size_t len);
00110
00115 void write_data(BundlePayload* src, size_t src_offset,
00116 size_t len, size_t dst_offset);
00117
00121 void copy_file(oasys::FileIOClient* dst);
00122
00127 bool replace_with_file(const char* path);
00128
00132 const u_char* memory_data()
00133 {
00134 ASSERT(location_ == MEMORY);
00135 return (const u_char*)data_.c_str();
00136 }
00137
00141 typedef enum {
00142 FORCE_COPY = 0x2,
00143
00144 } read_data_flags_t;
00145
00154 const u_char* read_data(size_t offset, size_t len, u_char* buf,
00155 int flags = 0);
00156
00163 const u_char* read_data(size_t offset, size_t len, u_char* buf,
00164 int flags = 0) const
00165 {
00166 return const_cast<BundlePayload*>(this)->
00167 read_data(offset, len, buf, flags);
00168 }
00169
00173 virtual void serialize(oasys::SerializeAction* a);
00174
00175
00176
00177
00178
00179 static size_t mem_threshold_;
00180 static bool test_no_remove_;
00181
00182 protected:
00183 void pin_file();
00184 void unpin_file();
00185 void internal_write(const u_char* bp, size_t offset, size_t len);
00186
00187 location_t location_;
00188 std::string data_;
00189 size_t length_;
00190 size_t rcvd_length_;
00191 oasys::FileIOClient file_;
00192 size_t cur_offset_;
00193 size_t base_offset_;
00194 oasys::SpinLock* lock_;
00195 };
00196
00197 }
00198
00199 #endif