Registration.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include "Registration.h"
00040 #include "bundling/Bundle.h"
00041 #include "bundling/BundleDaemon.h"
00042 #include "bundling/BundleList.h"
00043 #include "storage/GlobalStore.h"
00044 
00045 namespace dtn {
00046 
00047 //----------------------------------------------------------------------
00048 const char*
00049 Registration::failure_action_toa(failure_action_t action)
00050 {
00051     switch(action) {
00052     case DROP:  return "DROP";
00053     case DEFER: return "DEFER";
00054     case EXEC:  return "EXEC";
00055     }
00056 
00057     return "__INVALID__";
00058 }
00059 
00060 //----------------------------------------------------------------------
00061 Registration::Registration(u_int32_t regid,
00062                            const EndpointIDPattern& endpoint,
00063                            failure_action_t action,
00064                            u_int32_t expiration,
00065                            const std::string& script)
00066     
00067     : Logger("Registration", "/dtn/registration/%d", regid),
00068       regid_(regid),
00069       endpoint_(endpoint),
00070       failure_action_(action),
00071       script_(script),
00072       expiration_(expiration),
00073       expiration_timer_(NULL),
00074       active_(false),
00075       expired_(false)
00076 {
00077     struct timeval now;
00078     ::gettimeofday(&now, 0);
00079     creation_time_ = now.tv_sec;
00080     
00081     init_expiration_timer();
00082 }
00083 
00084 //----------------------------------------------------------------------
00085 Registration::Registration(const oasys::Builder&)
00086     : Logger("Registration", "/dtn/registration"),
00087       regid_(0),
00088       endpoint_(),
00089       failure_action_(DEFER),
00090       script_(),
00091       expiration_(0),
00092       creation_time_(0),
00093       expiration_timer_(NULL),
00094       active_(false),
00095       expired_(false)
00096 {
00097 }
00098 
00099 //----------------------------------------------------------------------
00100 Registration::~Registration()
00101 {
00102     cleanup_expiration_timer();
00103 }
00104 
00105 //----------------------------------------------------------------------
00106 void
00107 Registration::force_expire()
00108 {
00109     ASSERT(active_);
00110     
00111     cleanup_expiration_timer();
00112     set_expired(true);
00113 }
00114 
00115 //----------------------------------------------------------------------
00116 void
00117 Registration::cleanup_expiration_timer()
00118 {
00119     if (expiration_timer_) {
00120         // try to cancel the expiration timer. if it is still pending,
00121         // then the timer will clean itself up when it eventually
00122         // fires. otherwise, assert that we have actually expired and
00123         // delete the timer itself.
00124         bool pending = expiration_timer_->cancel();
00125         
00126         if (! pending) {
00127             ASSERT(expired_);
00128             delete expiration_timer_;
00129         }
00130         
00131         expiration_timer_ = NULL;
00132     }
00133 }
00134 
00135 //----------------------------------------------------------------------
00136 void
00137 Registration::serialize(oasys::SerializeAction* a)
00138 {
00139     a->process("endpoint", &endpoint_);
00140     a->process("regid", &regid_);
00141     a->process("action", (u_int32_t*)&failure_action_);
00142     a->process("script", &script_);
00143     a->process("creation_time", &creation_time_);
00144     a->process("expiration", &expiration_);
00145 
00146     // finish constructing the object after unserialization
00147     if (a->action_code() == oasys::Serialize::UNMARSHAL) {
00148         init_expiration_timer();
00149     }
00150 
00151     logpathf("/dtn/registration/%d", regid_);
00152 }
00153 
00154 //----------------------------------------------------------------------
00155 void
00156 Registration::init_expiration_timer()
00157 {
00158     if (expiration_ != 0) {
00159         struct timeval when, now;
00160         when.tv_sec  = creation_time_ + expiration_;
00161         when.tv_usec = 0;
00162 
00163         ::gettimeofday(&now, 0);
00164 
00165         long int in_how_long = TIMEVAL_DIFF_MSEC(when, now);
00166         if (in_how_long < 0) {
00167             log_warn("scheduling IMMEDIATE expiration for registration id %d: "
00168                      "[creation_time %u, expiration %u, now %u]", 
00169                      regid_, creation_time_, expiration_, (u_int)now.tv_sec);
00170         } else {
00171             log_debug("scheduling expiration for registration id %d at %u.%u "
00172                       "(in %ld seconds): ", regid_,
00173                       (u_int)when.tv_sec, (u_int)when.tv_usec,
00174                       in_how_long / 1000);
00175         }
00176         
00177         expiration_timer_ = new ExpirationTimer(this);
00178         expiration_timer_->schedule_at(&when);
00179 
00180     } else {
00181         set_expired(true);
00182     }
00183 }
00184 
00185 //----------------------------------------------------------------------
00186 void
00187 Registration::ExpirationTimer::timeout(const struct timeval& now)
00188 {
00189     (void)now;
00190     
00191     reg_->set_expired(true);
00192                       
00193     if (! reg_->active()) {
00194         BundleDaemon::post(new RegistrationExpiredEvent(reg_->regid()));
00195     } 
00196 }
00197 
00198 } // namespace dtn

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1