servlib/bundling/BPQBlockProcessor.cc
changeset 5 1849bf57d910
parent 4 c02ca5a6ab82
child 34 8f9f167096ee
--- a/servlib/bundling/BPQBlockProcessor.cc	Wed May 04 15:44:40 2011 +0100
+++ b/servlib/bundling/BPQBlockProcessor.cc	Fri May 27 18:33:25 2011 +0100
@@ -19,17 +19,22 @@
 #endif
 
 #include "BPQBlockProcessor.h"
-#include "BPQBlock.h"
-
-#include "BlockInfo.h"
-#include "BundleProtocol.h"
 
 namespace dtn {
 
+// Setup our logging information
+static const char* LOG = "/dtn/bundle/extblock/bpq";
+
+template <> BPQBlockProcessor*
+oasys::Singleton<BPQBlockProcessor>::instance_ = NULL;
+
+
+
 //----------------------------------------------------------------------
 BPQBlockProcessor::BPQBlockProcessor() :
         BlockProcessor(BundleProtocol::QUERY_EXTENSION_BLOCK)
 {
+    log_info_p(LOG, "BPQBlockProcessor::BPQBlockProcessor()");
 }
 
 //----------------------------------------------------------------------
@@ -39,16 +44,69 @@
                            u_char* buf,
                            size_t len)
 {
+    log_info_p(LOG, "BPQBlockProcessor::consume() start");
+
     (void)bundle;
-    (void)block;
-    (void)buf;
-    (void)len;
+//    (void)block;
+//    (void)buf;
+//    (void)len;
+
+    int cc;
+
+    if ( (cc = BlockProcessor::consume(bundle, block, buf, len)) < 0) {
+        log_err_p(LOG, "BPQBlockProcessor::consume(): error handling block 0x%x",
+                        BundleProtocol::QUERY_EXTENSION_BLOCK);
+        return cc;
+    }
+
+    // If we don't finish processing the block, return the number of bytes
+    // consumed. (Error checking done in the calling function?)
+    if (! block->complete()) {
+        ASSERT(cc == (int)len);
+        return cc;
+    }
+
+    BPQBlock* bpq_block = new BPQBlock(block);
+    log_info_p(LOG, "     BPQBlock:");
+    log_info_p(LOG, "         kind: %d", bpq_block->kind());
+    log_info_p(LOG, "matching rule: %d", bpq_block->matching_rule());
+    log_info_p(LOG, "    query_len: %d", bpq_block->query_len());
+    log_info_p(LOG, "    query_val: %s", bpq_block->query_val());
+    delete bpq_block;
 
-    //static const char* log = "/home/aidan/Desktop/dtn_log";
-    static const char* log = "/dtn/bundle/protocol";
-    log_err_p(log, "BPQ: consume() returning -1");
+    log_info_p(LOG, "BPQBlockProcessor::consume() end");
+    
+    return cc;
+}
+
+//----------------------------------------------------------------------
+
+int 
+BPQBlockProcessor::prepare(const Bundle*    bundle,
+                           BlockInfoVec*    xmit_blocks,
+                           const BlockInfo* source,
+                           const LinkRef&   link,
+                           list_owner_t     list)
+{
+    log_info_p(LOG, "BPQBlockProcessor::prepare()");
 
-    return -1;
+    if ( (const_cast<Bundle*>(bundle))->api_blocks()->
+            has_block(BundleProtocol::QUERY_EXTENSION_BLOCK) ) {
+
+        log_info_p(LOG, "BPQBlock found in API Block Vec => created locally");
+        return BlockProcessor::prepare(bundle, xmit_blocks, source, link, list);
+
+    } else if ( (const_cast<Bundle*>(bundle))->recv_blocks().
+                has_block(BundleProtocol::QUERY_EXTENSION_BLOCK) ) {
+
+        log_info_p(LOG, "BPQBlock found in Recv Block Vec => created remotly");
+        return BlockProcessor::prepare(bundle, xmit_blocks, source, link, list);
+
+    } else {
+
+        log_info_p(LOG, "BPQBlock not found in bundle");
+        return BP_FAIL;
+    }
 }
 
 //----------------------------------------------------------------------
@@ -59,73 +117,92 @@
                             const LinkRef& link,
                             bool           last)
 {
+    log_info_p(LOG, "BPQBlockProcessor::generate() starting");
 
-    //static const char* log = "/home/aidan/Desktop/dtn_log";
-    static const char* log = "/dtn/bundle/protocol";
-    log_err_p(log, "BPQ: generate() returning %d", BP_SUCCESS);
+    (void)xmit_blocks;    
+    (void)link;
+
+    ASSERT (block->type() == BundleProtocol::QUERY_EXTENSION_BLOCK);
 
-    BPQBlock* bpq_block = new BPQBlock(block);
+    // set flags
+    u_int8_t flags = BundleProtocol::BLOCK_FLAG_REPLICATE |
+                     (last ? BundleProtocol::BLOCK_FLAG_LAST_BLOCK : 0);
+                     //BundleProtocol::BLOCK_FLAG_DISCARD_BUNDLE_ONERROR |
+
+    BlockInfo* bpq_info;
 
-/*
-    for (BlockInfoVec::iterator iter = bundle->recv_blocks().begin();
-         iter != bundle->recv_blocks().end();
-         ++iter)
-    {
-        log_err_p(log,"\n type: 0x%02x ", iter->type());
-        if (iter->data_offset() == 0)
-            log_err_p(log,"(runt)");
-        else {
-            if (!iter->complete())
-                log_err_p(log,"(incomplete) ");
-            log_err_p(log,"data length: %d", iter->full_length());
-        }
+    if ( (const_cast<Bundle*>(bundle))->api_blocks()->
+            has_block(BundleProtocol::QUERY_EXTENSION_BLOCK) ) {
+
+        bpq_info = const_cast<BlockInfo*>((const_cast<Bundle*>(bundle))->
+                   api_blocks()->find_block(BundleProtocol::QUERY_EXTENSION_BLOCK));
+        log_info_p(LOG, "BPQBlock found in API Block Vec => created locally");
+        
+    } else if ( (const_cast<Bundle*>(bundle))->recv_blocks().
+                has_block(BundleProtocol::QUERY_EXTENSION_BLOCK) ) {
+
+
+        bpq_info = const_cast<BlockInfo*>((const_cast<Bundle*>(bundle))->
+                   recv_blocks().find_block(BundleProtocol::QUERY_EXTENSION_BLOCK));
+        log_info_p(LOG, "BPQBlock found in Recv Block Vec => created remotly");
+
+    } else {
+        log_err_p(LOG, "Cannot find BPQ block");
+        return BP_FAIL;
     }
 
 
-
-
-*/
-/*    oasys::StaticStringBuffer<1024> buf;
-    bundle->format_verbose(&buf);
-    log_multiline(oasys::LOG_NOTICE, buf.c_str());
+    BPQBlock* bpq_block = new BPQBlock(bpq_info);
 
-*/
-    /*oasys::StringBuffer *sb = new oasys::StringBuffer();
-    //char c[10000];
-    bundle->format_verbose(sb);
-    log_err_p(log, "%s", sb->data());
-    */
-
-
-
-    u_int8_t flags = BundleProtocol::BLOCK_FLAG_DISCARD_BUNDLE_ONERROR |
-                     (last ? BundleProtocol::BLOCK_FLAG_LAST_BLOCK : 0);
-
+    //int length = bpq_block->length();
+    int length = bpq_info->data_length();
+ 
     generate_preamble(xmit_blocks,
                       block,
                       BundleProtocol::QUERY_EXTENSION_BLOCK,
                       flags,
-                      1 ); 
+                      length ); 
 
 
-    // source block must include at least a block header, if not actual data
-//    ASSERT(source->contents().len() != 0);
-//    ASSERT(source->data_offset() != 0);
+    // The process of storing the value into the block. We'll create a
+    // `DataBuffer` object and `reserve` the length of our BPQ data and
+    // update the length of the `DataBuffer`.
+
+    BlockInfo::DataBuffer* contents = block->writable_contents();
+    contents->reserve(block->data_offset() + length);
+    contents->set_len(block->data_offset() + length);
 
-//    generate_preamble(xmit_blocks, block, source->type(), flags, source->data_length());
-//    ASSERT(block->data_offset() == source->data_offset());
-//    ASSERT(block->data_length() == source->data_length());
-/*
-    BlockInfo::DataBuffer* contents = block->writable_contents();
-    contents->reserve(block->full_length());
-    memcpy(contents->buf()          + block->data_offset(),
-           source->contents().buf() + block->data_offset(),
-           block->data_length());
-    contents->set_len(block->full_length());
-*/
+    // Set our pointer to the right offset.
+    u_char* buf = contents->buf() + block->data_offset();
+    
+    // now write contents of BPQ block into the block
+    if ( bpq_block->write_to_buffer(buf, length) == -1 ) {
+        log_err_p(LOG, "Error writing BPQ block to buffer");
+        return BP_FAIL;
+    }
 
+    delete bpq_block;
+    log_info_p(LOG, "BPQBlockProcessor::generate() ending");
     return BP_SUCCESS;
 }
 
-} // namespace dtn
+//----------------------------------------------------------------------
+/*
+int
+BPQBlockProcessor::finalize(const Bundle*  bundle,
+                            BlockInfoVec*  xmit_blocks,
+                            BlockInfo*     block,
+                            const LinkRef& link)
+{
+    log_info_p(LOG, "BPQBlockProcessor::finalize()");
 
+    (void)bundle;
+    (void)xmit_blocks;
+    (void)block;
+    (void)link;
+
+    return 0;
+}
+*/
+
+} // namespace dtn_FOO__   i