BundlePayload.h

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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      * Tunable parameters
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 } // namespace dtn
00232 
00233 #endif /* _BUNDLE_PAYLOAD_H_ */

Generated on Fri Dec 22 14:47:58 2006 for DTN Reference Implementation by  doxygen 1.5.1