servlib/bundling/BPQBlock.cc
changeset 30 bdf6aee9aa70
parent 6 d1f220643814
child 31 2e5913d44299
equal deleted inserted replaced
29:00dfdf113d87 30:bdf6aee9aa70
    55 
    55 
    56 BPQBlock::BPQBlock(BlockInfo* block)
    56 BPQBlock::BPQBlock(BlockInfo* block)
    57 {
    57 {
    58     log_info_p(LOG, "BPQBlock::constructor()");
    58     log_info_p(LOG, "BPQBlock::constructor()");
    59 
    59 
       
    60     ASSERT( block->type() == BundleProtocol::QUERY_EXTENSION_BLOCK);
       
    61 
    60     initialise(block);
    62     initialise(block);
    61 
    63 
    62     log_info_p(LOG, "leaving constructor");
    64     log_info_p(LOG, "leaving constructor");
    63 }
    65 }
    64 
    66 
   159 }
   161 }
   160 
   162 
   161 bool
   163 bool
   162 BPQBlock::match(const BPQBlock* other) const
   164 BPQBlock::match(const BPQBlock* other) const
   163 {
   165 {
   164 /*
       
   165     log_debug_p(LOG, "_BPQ_ Match: this(%s) other(%s)",
       
   166             (char*)query_val_,
       
   167             (char*)other->query_val());
       
   168 */
       
   169     return query_len_ == other->query_len() &&
   166     return query_len_ == other->query_len() &&
   170            strncmp( (char*)query_val_, (char*)other->query_val(),
   167            strncmp( (char*)query_val_, (char*)other->query_val(),
   171                      query_len_ ) == 0;
   168                      query_len_ ) == 0;
   172 }
   169 }
   173 
   170 
   178 
   175 
   179     BlockInfo* block = NULL;
   176     BlockInfo* block = NULL;
   180 
   177 
   181     if ( b->source() != NULL ) {
   178     if ( b->source() != NULL ) {
   182         block = const_cast<BlockInfo*>(b->source());
   179         block = const_cast<BlockInfo*>(b->source());
   183     } else {
   180         log_debug_p(LOG, "BPQBlock::initialise: b->source() != NULL");
       
   181     } else {
       
   182         log_debug_p(LOG, "BPQBlock::initialise: b->source() == NULL");
   184         block = b;
   183         block = b;
   185     }
   184     }
   186     
   185     
   187     int decoding_len=0; 
   186     int decoding_len=0; 
   188     u_int i=0, j=0;
   187     u_int i=0, j=0;
   189     u_int len = block->data_length();
   188     u_int len = block->data_length();
   190     u_int num_frags;
   189     u_int num_frags;
   191     u_char* buf = block->data();
   190     u_char* buf = block->data();
   192 
   191 
   193     // BPQ-kind             1-byte
   192     // BPQ-kind             1-byte
   194     if ( i < len )
   193     if ( i < len ) {
   195         kind_ = (kind_t) buf[i++];
   194         kind_ = (kind_t) buf[i++];
       
   195         log_debug_p(LOG, "BPQBlock::initialise: kind = %d", kind_);
       
   196     }
   196 
   197 
   197     // matching rule type   1-byte
   198     // matching rule type   1-byte
   198     if ( i < len )
   199     if ( i < len ) {
   199         matching_rule_ = (u_int) buf[i++];
   200         matching_rule_ = (u_int) buf[i++];
       
   201         log_debug_p(LOG, "BPQBlock::initialise: matching rule = %u", matching_rule_);
       
   202     }
   200 
   203 
   201     // Decode the SDNV-encoded query length. Note that we need to know the length of the
   204     // Decode the SDNV-encoded query length. Note that we need to know the length of the
   202     // of the encoded value and provide some pointers to the encoded value along with
   205     // of the encoded value and provide some pointers to the encoded value along with
   203     // where we want the decoded value (in this case, query_len_).
   206     // where we want the decoded value (in this case, query_len_).
   204     if ( i < len &&
   207     if ( i < len &&
   205         (decoding_len = SDNV::decode(&(buf[i]), len - i, &query_len_)) >= 0 )
   208         (decoding_len = SDNV::decode(&(buf[i]), len - i, &query_len_)) >= 0 ) {
   206         i += decoding_len;
   209         i += decoding_len;
       
   210         log_debug_p(LOG, "BPQBlock::initialise: query len = %u", query_len_);
       
   211     }
   207     else
   212     else
   208         log_err_p(LOG, "Error decoding BPQ query length");
   213         log_err_p(LOG, "Error decoding BPQ query length");
   209 
   214 
   210     // query-value           n-bytes
   215     // query-value           n-bytes
   211     if ( (i+query_len_) < len ) {
   216     if ( (i+query_len_) < len ) {
   212         query_val_ = (u_char*) malloc ( sizeof(u_char) * query_len_ );
   217         query_val_ = (u_char*) malloc ( sizeof(u_char) * query_len_ );
   213 
   218 
   214         for (j=0; query_val_ != NULL && i < len && j < query_len_; i++, j++)
   219         for (j=0; query_val_ != NULL && i < len && j < query_len_; i++, j++)
   215             query_val_[j] = buf[i];
   220             query_val_[j] = buf[i];
   216 
   221 
       
   222         log_debug_p(LOG, "BPQBlock::initialise: query val = %s", query_val_);
       
   223 
   217     } else {
   224     } else {
   218         query_val_ = NULL;
   225         query_val_ = NULL;
   219     }
   226     }
   220 
   227 
   221     if ( i < len &&
   228     if ( i < len &&
   222         (decoding_len = SDNV::decode(&(buf[i]), len - i, &num_frags)) >= 0 )
   229         (decoding_len = SDNV::decode(&(buf[i]), len - i, &num_frags)) >= 0 ) {
   223         i += decoding_len;
   230         i += decoding_len;
       
   231         log_debug_p(LOG, "BPQBlock::initialise: num frags = %u", num_frags);
       
   232     }
   224     else
   233     else
   225         log_err_p(LOG, "Error decoding BPQ fragment length");
   234         log_err_p(LOG, "Error decoding BPQ fragment length");
   226 
   235 
   227     // todo: Still need to handle fragments
   236     // todo: Still need to handle fragments
   228     // test assert - to be removed once we start handling fragments
   237     // test assert - to be removed once we start handling fragments
   233     return BP_SUCCESS;
   242     return BP_SUCCESS;
   234 }
   243 }
   235 
   244 
   236 } // namespace dtn
   245 } // namespace dtn
   237 
   246 
   238 
       
   239 
       
   240 
       
   241 
       
   242 
       
   243 
       
   244 
       
   245 
       
   246 
       
   247 
       
   248 
       
   249 
       
   250 
       
   251 
       
   252