ProphetTLV.h

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Baylor University
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 #ifndef _DTN_PROPHET_TLV_
00018 #define _DTN_PROPHET_TLV_
00019 
00020 #include <oasys/debug/Log.h>
00021 #include "naming/EndpointID.h"
00022 #include <sys/types.h> 
00023 #include <netinet/in.h>
00024 
00025 #include <vector>
00026 
00027 #include "routing/Prophet.h"
00028 #include "routing/ProphetNode.h"
00029 #include "routing/ProphetLists.h"
00030 
00031 #include <oasys/util/ScratchBuffer.h>
00032 
00033 #define LOGPATH "/dtn/router/prophet/tlv"
00034 
00039 namespace dtn {
00040 
00041 template<class TLV>
00042 struct TLVFactory
00043 {
00044     static TLV* deserialize(u_char* bp,size_t len,const char* logpath)
00045     {
00046         TLV* t = new TLV(logpath);
00047         if (t->deserialize(bp,len))
00048             return t;
00049         delete t;
00050         return NULL;
00051     }
00052 }; // TLVFactory template
00053 
00054 class BaseTLV : public oasys::Logger
00055 {
00056 public:
00057     virtual ~BaseTLV() {}
00058 
00059     Prophet::prophet_tlv_t typecode() { return typecode_; }
00060     u_int8_t flags() { return flags_; }
00061     u_int16_t length() { return length_; }
00062 
00063     virtual size_t serialize(u_char* bp,size_t len) = 0;
00064     virtual void dump(oasys::StringBuffer* buf);
00065 
00066 protected:
00067     BaseTLV(Prophet::prophet_tlv_t typecode = Prophet::UNKNOWN_TLV,
00068             const char* logpath = LOGPATH) :
00069         oasys::Logger("TLVParser",logpath),
00070         typecode_(typecode),
00071         flags_(0),
00072         length_(0)
00073     {} 
00074 
00075     virtual bool deserialize(u_char* bp,size_t len) = 0;
00076 
00077     Prophet::prophet_tlv_t typecode_;
00078     u_int8_t flags_;
00079     u_int16_t length_;
00080 }; // BaseTLV template
00081 
00082 class HelloTLV : public BaseTLV
00083 {
00084 public:
00085     HelloTLV(Prophet::hello_hf_t hf,
00086              u_int8_t timer,
00087              const EndpointID& eid,
00088              const char* logpath) :
00089         BaseTLV(Prophet::HELLO_TLV,logpath),
00090         hf_(hf), timer_(timer), sender_(eid)
00091     {
00092         length_ = Prophet::HelloTLVHeaderSize + eid.length();
00093         length_ = FOUR_BYTE_ALIGN(length_);
00094     }
00095 
00096     virtual ~HelloTLV() {}
00097 
00098     Prophet::hello_hf_t hf() { return hf_; }
00099     u_int8_t timer() { return timer_; }
00100     const EndpointID& sender() { return sender_; }
00101 
00102     size_t serialize(u_char*,size_t);
00103     void dump(oasys::StringBuffer* buf);
00104 
00105 protected:
00106     friend class TLVFactory<HelloTLV>;
00107 
00108     HelloTLV(const char* logpath) :
00109         BaseTLV(Prophet::HELLO_TLV,logpath),
00110         hf_(Prophet::HF_UNKNOWN),
00111         timer_(0),
00112         sender_(EndpointID::NULL_EID())
00113     {}
00114 
00115     bool deserialize(u_char*,size_t);
00116 
00117     Prophet::hello_hf_t hf_;
00118     u_int8_t timer_;
00119     EndpointID sender_;
00120 }; // HelloTLV
00121 
00122 class RIBDTLV : public BaseTLV
00123 {
00124 public:
00125     RIBDTLV(const ProphetDictionary& ribd,
00126             const char* logpath) :
00127         BaseTLV(Prophet::RIBD_TLV,logpath),
00128         ribd_(ribd)
00129     {
00130         length_ = Prophet::RIBDTLVHeaderSize;
00131         length_ += ribd_.guess_ribd_size();
00132     }
00133     
00134     virtual ~RIBDTLV() {}
00135 
00136     const ProphetDictionary& ribd() const { return ribd_; }
00137 
00138     size_t serialize(u_char*,size_t);
00139     void dump(oasys::StringBuffer* buf);
00140 protected:
00145     size_t write_ras_entry(u_int16_t sid,
00146                            EndpointID eid,
00147                            u_char* buffer,
00148                            size_t len);
00149 
00154     size_t read_ras_entry(u_int16_t* sid,
00155                           EndpointID* eid,
00156                           u_char* buffer,
00157                           size_t len);
00158 
00159     friend class TLVFactory<RIBDTLV>;
00160 
00161     RIBDTLV(const char* logpath) :
00162         BaseTLV(Prophet::RIBD_TLV,logpath),
00163         ribd_(EndpointID::NULL_EID(),EndpointID::NULL_EID())
00164     {}
00165 
00166     bool deserialize(u_char*,size_t);
00167 
00168     ProphetDictionary ribd_;
00169 }; // RIBDTLV
00170 
00171 class RIBTLV : public BaseTLV
00172 {
00173 public:
00174     typedef PointerList<RIBNode> List;
00175     typedef List::iterator iterator;
00176 
00177     RIBTLV(const List& nodes,
00178            bool relay,
00179            bool custody,
00180            bool internet,
00181            const char* logpath) :
00182         BaseTLV(Prophet::RIB_TLV,logpath),
00183         nodes_(nodes),
00184         relay_(relay),
00185         custody_(custody),
00186         internet_(internet)
00187     {
00188         length_ = Prophet::RIBTLVHeaderSize;
00189         length_ += Prophet::RIBEntrySize * nodes_.size();
00190     }
00191 
00192     virtual ~RIBTLV() {}
00193 
00194     const List& nodes() { return nodes_; }
00195     bool relay_node() { return relay_; }
00196     bool custody_node() { return custody_; }
00197     bool internet_gateway() { return internet_ ;}
00198 
00199     size_t serialize(u_char*,size_t);
00200     void dump(oasys::StringBuffer* buf);
00201 protected:
00202     friend class TLVFactory<RIBTLV>;
00203 
00204     size_t write_rib_entry(u_int16_t sid,
00205                            double pvalue,
00206                            bool relay,
00207                            bool custody,
00208                            bool internet,
00209                            u_char* buffer,
00210                            size_t len);
00211 
00212     size_t read_rib_entry(u_int16_t* sid,
00213                           double* pvalue,
00214                           bool* relay,
00215                           bool* custody,
00216                           bool* internet,
00217                           u_char* buffer,
00218                           size_t len);
00219 
00220     RIBTLV(const char* logpath) :
00221         BaseTLV(Prophet::RIB_TLV,logpath),
00222         relay_(false),
00223         custody_(false),
00224         internet_(false)
00225     {}
00226 
00227     bool deserialize(u_char*,size_t);
00228 
00229     List nodes_;
00230     bool relay_;
00231     bool custody_;
00232     bool internet_;
00233 }; // RIBTLV
00234 
00235 class BundleTLV : public BaseTLV
00236 {
00237 public:
00238     BundleTLV(const BundleOfferList& list,
00239               const char* logpath) :
00240         BaseTLV(Prophet::BUNDLE_TLV,logpath),
00241         list_(list)
00242     {
00243         length_ = Prophet::BundleOfferTLVHeaderSize;
00244         length_ += list_.size() * Prophet::BundleOfferEntrySize;
00245     }
00246 
00247     virtual ~BundleTLV() { list_.clear(); }
00248 
00252     const BundleOfferList& list() { return list_; }
00253 
00254     BundleOffer::bundle_offer_t type() { return list_.type(); }
00255 
00256     size_t serialize(u_char*,size_t);
00257     void dump(oasys::StringBuffer* buf);
00258 protected:
00259     friend class TLVFactory<BundleTLV>;
00260 
00261     size_t read_bundle_offer(u_int32_t *cts, u_int16_t *sid, 
00262                              bool *custody, bool *accept, bool *ack,
00263                              BundleOffer::bundle_offer_t *type,
00264                              u_char* bp, size_t len);
00265 
00266     size_t write_bundle_offer(u_int32_t cts, u_int16_t sid,
00267                               bool custody, bool accept, bool ack,
00268                               BundleOffer::bundle_offer_t type,
00269                               u_char* bp, size_t len);
00270 
00271     BundleTLV(const char* logpath) :
00272         BaseTLV(Prophet::BUNDLE_TLV,logpath)
00273     {
00274         ASSERT(list_.type() == BundleOffer::UNDEFINED);
00275     }
00276 
00277     bool deserialize(u_char*,size_t);
00278 
00279     BundleOfferList list_;
00280 }; // BundleTLV
00281 
00286 class ProphetTLV : public oasys::Logger
00287 {
00288 public:
00289     typedef std::list<BaseTLV*> List;
00290     typedef List::iterator iterator; 
00291     typedef List::const_iterator const_iterator; 
00292 
00293     ProphetTLV(Prophet::header_result_t result,
00294                u_int16_t local_instance,
00295                u_int16_t remote_instance,
00296                u_int32_t tid,
00297                const char* logpath = LOGPATH);
00298 
00299     virtual ~ProphetTLV();
00300 
00301     // caller is responsible for cleaning up Bundle*
00302     bool create_bundle(Bundle* bundle,
00303                        const EndpointID& local,
00304                        const EndpointID& remote) const;
00305 
00306     // caller is responsible for cleaning up ProphetTLV*
00307     static ProphetTLV* deserialize(Bundle* b,
00308                                    EndpointID* local = NULL,
00309                                    EndpointID* remote = NULL,
00310                                    const char* logpath = LOGPATH);
00311 
00312     Prophet::header_result_t result() { return result_; }
00313     u_int16_t sender_instance()   { return sender_instance_; }
00314     u_int16_t receiver_instance() { return receiver_instance_; }
00315     u_int32_t transaction_id()    { return tid_; }
00316     u_int16_t length()            { return parsedlen_; }
00317     size_t num_tlv()              { return list_.size(); }
00318 
00319     // caller is responsible for memory cleanup on returned pointer
00320     BaseTLV* get_tlv(); 
00321 
00322     // ProphetTLV will clean up memory on submitted pointer
00323     void add_tlv(BaseTLV* tlv);
00324 
00325     // look but don't touch!
00326     const List& list() const { return list_; }
00327 
00328     void dump(oasys::StringBuffer* buf);
00329 
00330 #ifndef PROPHET_TLV_TEST
00331 protected:
00332 #endif
00333     ProphetTLV(const char *logpath = LOGPATH);
00334 
00335     bool deserialize(u_char* bp, size_t buflen);
00336     size_t serialize(u_char*,size_t) const;
00337 
00338     Prophet::header_result_t result_;
00339     u_int16_t sender_instance_;
00340     u_int16_t receiver_instance_;
00341     u_int32_t tid_;
00342     u_int16_t parsedlen_;
00343     List list_;
00344 
00345 }; // ProphetTLV
00346 
00347 }; // dtn
00348 
00349 #endif // _DTN_PROPHET_ENCOUNTER_

Generated on Sat Sep 8 08:43:32 2007 for DTN Reference Implementation by  doxygen 1.5.3