servlib/reg/APIRegistration.cc
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2005-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 "APIRegistration.h"
       
    22 #include "bundling/Bundle.h"
       
    23 #include "bundling/BundleDaemon.h"
       
    24 #include "bundling/BundleList.h"
       
    25 #include "session/Session.h"
       
    26 
       
    27 namespace dtn {
       
    28 
       
    29 //----------------------------------------------------------------------
       
    30 APIRegistration::APIRegistration(const oasys::Builder& builder)
       
    31     : Registration(builder)
       
    32 {
       
    33     bundle_list_ = new BlockingBundleList(logpath_);
       
    34     session_notify_list_ = NULL;
       
    35 }
       
    36     
       
    37 //----------------------------------------------------------------------
       
    38 APIRegistration::APIRegistration(u_int32_t regid,
       
    39                                  const EndpointIDPattern& endpoint,
       
    40                                  failure_action_t action,
       
    41                                  u_int32_t session_flags,
       
    42                                  u_int32_t expiration,
       
    43                                  const std::string& script)
       
    44     : Registration(regid, endpoint, action, session_flags, expiration, script)
       
    45 {
       
    46     bundle_list_ = new BlockingBundleList(logpath_);
       
    47     if (session_flags & Session::CUSTODY) {
       
    48         session_notify_list_ = new BlockingBundleList(logpath_);
       
    49         session_notify_list_->logpath_appendf("/session_notify");
       
    50     } else {
       
    51         session_notify_list_ = NULL;
       
    52     }
       
    53 }
       
    54 
       
    55 //----------------------------------------------------------------------
       
    56 void
       
    57 APIRegistration::serialize(oasys::SerializeAction* a)
       
    58 {
       
    59     Registration::serialize(a);
       
    60 
       
    61     if (a->action_code() == oasys::Serialize::UNMARSHAL &&
       
    62         (session_flags_ & Session::CUSTODY))
       
    63     {
       
    64         session_notify_list_ = new BlockingBundleList(logpath_);
       
    65         session_notify_list_->logpath_appendf("/session_notify");
       
    66     }
       
    67 }
       
    68 
       
    69 //----------------------------------------------------------------------
       
    70 APIRegistration::~APIRegistration()
       
    71 {
       
    72     delete bundle_list_;
       
    73     if (session_notify_list_) {
       
    74         delete session_notify_list_;
       
    75     }
       
    76 }
       
    77 
       
    78 //----------------------------------------------------------------------
       
    79 void
       
    80 APIRegistration::deliver_bundle(Bundle* bundle)
       
    81 {
       
    82     if (!active() && (failure_action_ == DROP)) {
       
    83         log_info("deliver_bundle: "
       
    84                  "dropping bundle id %d for passive registration %d (%s)",
       
    85                  bundle->bundleid(), regid_, endpoint_.c_str());
       
    86         
       
    87         // post an event saying we "delivered" it
       
    88         BundleDaemon::post(new BundleDeliveredEvent(bundle, this));
       
    89         return;
       
    90     }
       
    91     
       
    92     if (!active() && (failure_action_ == EXEC)) {
       
    93         // this sure seems like a security hole, but what can you
       
    94         // do -- it's in the spec
       
    95         log_info("deliver_bundle: "
       
    96                  "running script '%s' for registration %d (%s)",
       
    97                  script_.c_str(), regid_, endpoint_.c_str());
       
    98         
       
    99         system(script_.c_str());
       
   100         // fall through
       
   101     }
       
   102 
       
   103     log_info("deliver_bundle: queuing bundle id %d for %s delivery to %s",
       
   104              bundle->bundleid(),
       
   105              active() ? "active" : "deferred",
       
   106              endpoint_.c_str());
       
   107 
       
   108     if (BundleDaemon::instance()->params_.test_permuted_delivery_) {
       
   109         bundle_list_->insert_random(bundle);
       
   110     } else {
       
   111         bundle_list_->push_back(bundle);
       
   112     }
       
   113 }
       
   114 
       
   115 //----------------------------------------------------------------------
       
   116 void
       
   117 APIRegistration::session_notify(Bundle* bundle)
       
   118 {
       
   119     log_debug("session_notify *%p", bundle);
       
   120     session_notify_list_->push_back(bundle);
       
   121 }
       
   122 
       
   123 } // namespace dtn