servlib/conv_layers/NullConvergenceLayer.cc
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2006 Intel Corporation
       
     3  * 
       
     4  *    Licensed under the Apache License, Version 2.0 (the "License");
       
     5  *    you may not use this file except in compliance with the License.
       
     6  *    You may obtain a copy of the License at
       
     7  * 
       
     8  *        http://www.apache.org/licenses/LICENSE-2.0
       
     9  * 
       
    10  *    Unless required by applicable law or agreed to in writing, software
       
    11  *    distributed under the License is distributed on an "AS IS" BASIS,
       
    12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  *    See the License for the specific language governing permissions and
       
    14  *    limitations under the License.
       
    15  */
       
    16 
       
    17 #ifdef HAVE_CONFIG_H
       
    18 #  include <dtn-config.h>
       
    19 #endif
       
    20 
       
    21 #include <oasys/util/OptParser.h>
       
    22 #include "NullConvergenceLayer.h"
       
    23 #include "bundling/BundleDaemon.h"
       
    24 
       
    25 namespace dtn {
       
    26 
       
    27 struct NullConvergenceLayer::Params NullConvergenceLayer::defaults_;
       
    28 
       
    29 //----------------------------------------------------------------------
       
    30 void
       
    31 NullConvergenceLayer::Params::serialize(oasys::SerializeAction* a)
       
    32 {
       
    33     a->process("can_transmit", &can_transmit_);
       
    34 }
       
    35 
       
    36 //----------------------------------------------------------------------
       
    37 NullConvergenceLayer::NullConvergenceLayer()
       
    38   : ConvergenceLayer("NullConvergenceLayer", "null")
       
    39 {
       
    40     defaults_.can_transmit_ = true;
       
    41 }
       
    42 
       
    43 //----------------------------------------------------------------------
       
    44 bool
       
    45 NullConvergenceLayer::parse_link_params(Params* params,
       
    46                                         int argc, const char** argv,
       
    47                                         const char** invalidp)
       
    48 {
       
    49     oasys::OptParser p;
       
    50     p.addopt(new oasys::BoolOpt("can_transmit", &params->can_transmit_));
       
    51     return p.parse(argc, argv, invalidp);
       
    52 }
       
    53 
       
    54 //----------------------------------------------------------------------
       
    55 bool
       
    56 NullConvergenceLayer::init_link(const LinkRef& link,
       
    57                                 int argc, const char* argv[])
       
    58 {
       
    59     ASSERT(link != NULL);
       
    60     ASSERT(!link->isdeleted());
       
    61     ASSERT(link->cl_info() == NULL);
       
    62     
       
    63     log_debug("adding %s link %s", link->type_str(), link->nexthop());
       
    64 
       
    65     // Create a new parameters structure, parse the options, and store
       
    66     // them in the link's cl info slot
       
    67     Params* params = new Params(defaults_);
       
    68 
       
    69     const char* invalid;
       
    70     if (! parse_link_params(params, argc, argv, &invalid)) {
       
    71         log_err("error parsing link options: invalid option '%s'", invalid);
       
    72         delete params;
       
    73         return false;
       
    74     }
       
    75     link->set_cl_info(params);
       
    76     return true;
       
    77 }
       
    78 
       
    79 //----------------------------------------------------------------------
       
    80 bool
       
    81 NullConvergenceLayer::reconfigure_link(const LinkRef& link,
       
    82                                        int argc, const char* argv[])
       
    83 {
       
    84     ASSERT(link != NULL);
       
    85     ASSERT(!link->isdeleted());
       
    86     ASSERT(link->cl_info() != NULL);
       
    87     
       
    88     Params* params = dynamic_cast<Params*>(link->cl_info());
       
    89     ASSERT(params != NULL);
       
    90     
       
    91     const char* invalid;
       
    92     if (! parse_link_params(params, argc, argv, &invalid)) {
       
    93         log_err("reconfigure_link: invalid parameter %s", invalid);
       
    94         return false;
       
    95     }
       
    96 
       
    97     return true;
       
    98 }
       
    99 
       
   100 //----------------------------------------------------------------------
       
   101 void
       
   102 NullConvergenceLayer::delete_link(const LinkRef& link)
       
   103 {
       
   104     ASSERT(link != NULL);
       
   105     ASSERT(!link->isdeleted());
       
   106     ASSERT(link->cl_info() != NULL);
       
   107 
       
   108     log_debug("deleting link %s", link->name());
       
   109     
       
   110     delete link->cl_info();
       
   111     link->set_cl_info(NULL);
       
   112 }
       
   113 
       
   114 //----------------------------------------------------------------------
       
   115 bool
       
   116 NullConvergenceLayer::open_contact(const ContactRef& contact)
       
   117 {
       
   118     LinkRef link = contact->link();
       
   119     ASSERT(link != NULL);
       
   120     ASSERT(!link->isdeleted());
       
   121 
       
   122     BundleDaemon::post(new ContactUpEvent(contact));
       
   123     return true;
       
   124 }
       
   125 
       
   126 //----------------------------------------------------------------------
       
   127 void
       
   128 NullConvergenceLayer::bundle_queued(const LinkRef& link, const BundleRef& bundle)
       
   129 {
       
   130     ASSERT(link != NULL);
       
   131     ASSERT(!link->isdeleted());
       
   132 
       
   133     Params* params = (Params*)link->cl_info();
       
   134 
       
   135     if (! params->can_transmit_) {
       
   136         return;
       
   137     }
       
   138     
       
   139     const BlockInfoVec* blocks = bundle->xmit_blocks()->find_blocks(link);
       
   140     ASSERT(blocks != NULL);
       
   141     size_t total_len = BundleProtocol::total_length(blocks);
       
   142     
       
   143     log_debug("send_bundle *%p to *%p (total len %zu)",
       
   144               bundle.object(), link.object(), total_len);
       
   145     
       
   146     link->del_from_queue(bundle, total_len);
       
   147     link->add_to_inflight(bundle, total_len);
       
   148     
       
   149     BundleDaemon::post(
       
   150         new BundleTransmittedEvent(bundle.object(), link->contact(), link, total_len, 0));
       
   151 }
       
   152 
       
   153 //----------------------------------------------------------------------
       
   154 void
       
   155 NullConvergenceLayer::cancel_bundle(const LinkRef& link, const BundleRef& bundle)
       
   156 {
       
   157     Params* params = (Params*)link->cl_info();
       
   158     
       
   159     // if configured to not sent bundles, and if the bundle in
       
   160     // question is still on the link queue, then it can be cancelled
       
   161     if (! params->can_transmit_&& link->queue()->contains(bundle)) {
       
   162         log_debug("NullConvergenceLayer::cancel_bundle: "
       
   163                   "cancelling bundle *%p on *%p", bundle.object(), link.object());
       
   164         BundleDaemon::post(new BundleSendCancelledEvent(bundle.object(), link));
       
   165         return;
       
   166     } else {
       
   167         log_debug("NullConvergenceLayer::cancel_bundle: "
       
   168                   "not cancelling bundle *%p on *%p since !is_queued()",
       
   169                   bundle.object(), link.object());
       
   170     }
       
   171 }
       
   172 
       
   173 } // namespace dtn