servlib/bundling/BPQBlock.cc
changeset 66 e1101c5d54a1
parent 65 333724f2f7cf
child 68 17b33ba237f2
equal deleted inserted replaced
65:333724f2f7cf 66:e1101c5d54a1
    17 #ifdef HAVE_CONFIG_H
    17 #ifdef HAVE_CONFIG_H
    18 #  include <dtn-config.h>
    18 #  include <dtn-config.h>
    19 #endif
    19 #endif
    20 
    20 
    21 #include "BPQBlock.h"
    21 #include "BPQBlock.h"
       
    22 #include "BPQFragmentList.h"
    22 #include "Bundle.h"
    23 #include "Bundle.h"
    23 #include "BundleProtocol.h"
    24 #include "BundleProtocol.h"
    24 #include "SDNV.h"
    25 #include "SDNV.h"
    25 
    26 
    26 namespace dtn {
    27 namespace dtn {
    27 
    28 
    28 BPQBlock::BPQBlock(const Bundle* bundle)
    29 BPQBlock::BPQBlock(const Bundle* bundle)
    29 		: Logger("BPQBlock", "/dtn/bundle/bpq")
    30 		: Logger("BPQBlock", "/dtn/bundle/bpq"),
       
    31 		  fragments_("fragments")
    30 {
    32 {
    31     log_info("constructor()");
    33     log_info("constructor()");
       
    34 
       
    35     //TODO: Handle an initialisation failure
    32 
    36 
    33     if( bundle->recv_blocks().
    37     if( bundle->recv_blocks().
    34         has_block(BundleProtocol::QUERY_EXTENSION_BLOCK) ) {
    38         has_block(BundleProtocol::QUERY_EXTENSION_BLOCK) ) {
    35 
    39 
    36         log_debug("BPQBlock found in Recv Block Vec => created remotely");
    40         log_debug("BPQBlock found in Recv Block Vec => created remotely");
    59     log_info("BPQBlock: destructor");
    63     log_info("BPQBlock: destructor");
    60     if ( query_val_ != NULL ){
    64     if ( query_val_ != NULL ){
    61         free(query_val_);
    65         free(query_val_);
    62         query_val_ = NULL;
    66         query_val_ = NULL;
    63     }
    67     }
       
    68 
       
    69     BPQFragmentList::iterator iter;
       
    70     for (iter  = fragments_.begin();
       
    71     	 iter != fragments_.end();
       
    72     	 ++iter) {
       
    73 
       
    74     	delete *iter;
       
    75     }
    64 }
    76 }
    65 
    77 
    66 //----------------------------------------------------------------------
    78 //----------------------------------------------------------------------
    67 int
    79 int
    68 BPQBlock::write_to_buffer(u_char* buf, size_t len)
    80 BPQBlock::write_to_buffer(u_char* buf, size_t len)
   135         log_err("Error encoding BPQ fragment length");
   147         log_err("Error encoding BPQ fragment length");
   136         return -1;
   148         return -1;
   137     }
   149     }
   138 
   150 
   139     // fragment-values		SDNV
   151     // fragment-values		SDNV
   140     BPQFragmentVec::const_iterator iter;
   152     BPQFragmentList::const_iterator iter;
   141     for (iter  = fragments_.begin();
   153     for (iter  = fragments_.begin();
   142     	 iter != fragments_.end();
   154     	 iter != fragments_.end();
   143     	 ++iter) {
   155     	 ++iter) {
   144 
   156 
       
   157     	BPQFragment* fragment = *iter;
       
   158 
   145         if ( i < len &&
   159         if ( i < len &&
   146             (encoding_len = SDNV::encode (iter->offset(), &(buf[i]), len -i)) >= 0 ) {
   160             (encoding_len = SDNV::encode (fragment->offset(), &(buf[i]), len -i)) >= 0 ) {
   147             i += encoding_len;
   161             i += encoding_len;
   148         } else {
   162         } else {
   149             log_err("Error encoding BPQ individual fragment offset");
   163             log_err("Error encoding BPQ individual fragment offset");
   150             return -1;
   164             return -1;
   151         }
   165         }
   152 
   166 
   153         if ( i < len &&
   167         if ( i < len &&
   154             (encoding_len = SDNV::encode (iter->length(), &(buf[i]), len -i)) >= 0 ) {
   168             (encoding_len = SDNV::encode (fragment->length(), &(buf[i]), len -i)) >= 0 ) {
   155             i += encoding_len;
   169             i += encoding_len;
   156         } else {
   170         } else {
   157             log_err("Error encoding BPQ individual fragment length");
   171             log_err("Error encoding BPQ individual fragment length");
   158             return -1;
   172             return -1;
   159         }
   173         }
   178 
   192 
   179     len += SDNV::encoding_len(query_len_);
   193     len += SDNV::encoding_len(query_len_);
   180     len += query_len_;
   194     len += query_len_;
   181 
   195 
   182     len += SDNV::encoding_len(frag_len());
   196     len += SDNV::encoding_len(frag_len());
   183     BPQFragmentVec::const_iterator iter;
   197     BPQFragmentList::const_iterator iter;
   184 	for (iter  = fragments_.begin();
   198 	for (iter  = fragments_.begin();
   185 		 iter != fragments_.end();
   199 		 iter != fragments_.end();
   186 		 ++iter) {
   200 		 ++iter) {
   187 
   201 
   188 		len += SDNV::encoding_len(iter->offset());
   202 		BPQFragment* fragment = *iter;
   189 		len += SDNV::encoding_len(iter->length());
   203 
       
   204 		len += SDNV::encoding_len(fragment->offset());
       
   205 		len += SDNV::encoding_len(fragment->length());
   190 	}
   206 	}
   191 
   207 
   192     return len;
   208     return len;
   193 }
   209 }
   194 
   210 
   198 {
   214 {
   199     return matching_rule_ == other->matching_rule() &&
   215     return matching_rule_ == other->matching_rule() &&
   200     	   query_len_ == other->query_len() 		&&
   216     	   query_len_ == other->query_len() 		&&
   201            strncmp( (char*)query_val_, (char*)other->query_val(),
   217            strncmp( (char*)query_val_, (char*)other->query_val(),
   202                      query_len_ ) == 0;
   218                      query_len_ ) == 0;
       
   219 }
       
   220 
       
   221 //----------------------------------------------------------------------
       
   222 void
       
   223 BPQBlock::add_fragment(BPQFragment* new_fragment)
       
   224 {
       
   225     fragments_.insert_sorted(new_fragment);
   203 }
   226 }
   204 
   227 
   205 //----------------------------------------------------------------------
   228 //----------------------------------------------------------------------
   206 int
   229 int
   207 BPQBlock::initialise(BlockInfo* block, bool created_locally, const Bundle* bundle)
   230 BPQBlock::initialise(BlockInfo* block, bool created_locally, const Bundle* bundle)
   521 	    } else {
   544 	    } else {
   522 	        log_err("Error decoding fragment [%u] length", i);
   545 	        log_err("Error decoding fragment [%u] length", i);
   523 	        return BP_FAIL;
   546 	        return BP_FAIL;
   524 		}
   547 		}
   525 
   548 
   526 		BPQFragment frag(frag_off, frag_len);
   549 
   527 		this->add_fragment(frag);
   550 		add_fragment(new BPQFragment(frag_off, frag_len));
   528 	}
   551 	}
   529 
   552 
   530 	return BP_SUCCESS;
   553 	return BP_SUCCESS;
   531 }
   554 }
   532 
   555