sim/TrAgent.cc
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2004-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 <math.h>
       
    22 
       
    23 #include <oasys/util/Options.h>
       
    24 #include <oasys/util/OptParser.h>
       
    25 
       
    26 #include "TrAgent.h"
       
    27 #include "Simulator.h"
       
    28 #include "Node.h"
       
    29 #include "SimEvent.h"
       
    30 #include "SimLog.h"
       
    31 #include "bundling/Bundle.h"
       
    32 #include "bundling/BundleTimestamp.h"
       
    33 
       
    34 namespace dtnsim {
       
    35 
       
    36 //----------------------------------------------------------------------
       
    37 TrAgent::TrAgent(const EndpointID& src, const EndpointID& dst)
       
    38     : Logger("TrAgent", "/sim/tragent/%s", Node::active_node()->name()),
       
    39       src_(src), dst_(dst),
       
    40       size_(0), expiration_(30), reps_(0), batch_(1), interval_(0)
       
    41 {
       
    42 }
       
    43 
       
    44 //----------------------------------------------------------------------
       
    45 TrAgent*
       
    46 TrAgent::init(const EndpointID& src, const EndpointID& dst,
       
    47               int argc, const char** argv)
       
    48 {
       
    49     TrAgent* a = new TrAgent(src, dst);
       
    50 
       
    51     oasys::OptParser p;
       
    52     p.addopt(new oasys::SizeOpt("size", &a->size_));
       
    53     p.addopt(new oasys::UIntOpt("expiration", &a->expiration_));
       
    54     p.addopt(new oasys::UIntOpt("reps", &a->reps_));
       
    55     p.addopt(new oasys::UIntOpt("batch", &a->batch_));
       
    56     p.addopt(new oasys::DoubleOpt("interval", &a->interval_));
       
    57 
       
    58     const char* invalid;
       
    59     if (! p.parse(argc, argv, &invalid)) {
       
    60         a->logf(oasys::LOG_ERR, "invalid option: %s", invalid);
       
    61         return NULL;
       
    62     }
       
    63 
       
    64     if (a->size_ == 0) {
       
    65         a->logf(oasys::LOG_ERR, "size must be set in configuration");
       
    66         return NULL;
       
    67     }
       
    68 
       
    69     if (a->reps_ == 0) {
       
    70         a->logf(oasys::LOG_ERR, "reps must be set in configuration");
       
    71         return NULL;
       
    72     }
       
    73 
       
    74     if (a->reps_ != 1 && a->interval_ == 0) {
       
    75         a->logf(oasys::LOG_ERR, "interval must be set in configuration");
       
    76         return NULL;
       
    77     }
       
    78 
       
    79     a->schedule_immediate();
       
    80     return a;
       
    81 }
       
    82 
       
    83 //----------------------------------------------------------------------
       
    84 void
       
    85 TrAgent::timeout(const struct timeval& /* now */)
       
    86 {
       
    87     for (u_int i = 0; i < batch_; i++) {
       
    88         send_bundle();
       
    89     }
       
    90         
       
    91     if (--reps_ > 0) {
       
    92         log_debug("scheduling timer in %u ms", (u_int)(interval_ * 1000));
       
    93         schedule_in((int)(interval_ * 1000));
       
    94     } else {
       
    95         log_debug("all batches finished");
       
    96     }
       
    97 }
       
    98 
       
    99 //----------------------------------------------------------------------
       
   100 void
       
   101 TrAgent::send_bundle()
       
   102 {
       
   103     Bundle* b = new Bundle(BundlePayload::NODATA);
       
   104         
       
   105     //oasys::StaticStringBuffer<1024> buf;
       
   106     //b->format_verbose(&buf);
       
   107     //log_multiline(oasys::LOG_DEBUG, buf.c_str());
       
   108         
       
   109     b->mutable_source()->assign(src_);
       
   110     b->mutable_replyto()->assign(src_);
       
   111     b->mutable_custodian()->assign(EndpointID::NULL_EID());
       
   112     b->mutable_dest()->assign(dst_);
       
   113     b->mutable_payload()->set_length(size_);
       
   114         
       
   115     b->set_priority(0);
       
   116     b->set_custody_requested(false);
       
   117     b->set_local_custody(false);
       
   118     b->set_singleton_dest(false);
       
   119     b->set_receive_rcpt(false);
       
   120     b->set_custody_rcpt(false);
       
   121     b->set_forward_rcpt(false);
       
   122     b->set_delivery_rcpt(false);
       
   123     b->set_deletion_rcpt(false);
       
   124     b->set_app_acked_rcpt(false);
       
   125     b->set_creation_ts(BundleTimestamp(BundleTimestamp::get_current_time(),
       
   126                                        b->bundleid()));
       
   127     b->set_expiration(expiration_);
       
   128     b->set_is_fragment(false);
       
   129     b->set_is_admin(false);
       
   130     b->set_do_not_fragment(false);
       
   131     b->set_in_datastore(false);
       
   132     //b->orig_length_   = 0;
       
   133     //b->frag_offset_   = 0;    
       
   134     
       
   135     log_info("N[%s]: GEN id:%d %s -> %s size:%llu",
       
   136              Node::active_node()->name(), b->bundleid(),
       
   137              src_.c_str(), dst_.c_str(), U64FMT(size_));
       
   138 
       
   139     SimLog::instance()->log_gen(Node::active_node(), b);
       
   140 		
       
   141     BundleDaemon::post(new BundleReceivedEvent(b, EVENTSRC_APP,
       
   142                                                NULL /* registration? */));
       
   143 }
       
   144 
       
   145 
       
   146 } // namespace dtnsim