TrAgent.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #include <oasys/util/Options.h>
00019 #include <oasys/util/OptParser.h>
00020 
00021 #include "TrAgent.h"
00022 #include "Simulator.h"
00023 #include "Node.h"
00024 #include "SimEvent.h"
00025 #include "bundling/Bundle.h"
00026 #include "bundling/BundleTimestamp.h"
00027 
00028 namespace dtnsim {
00029 
00030 TrAgent::TrAgent(Node* node, const EndpointID& src, const EndpointID& dst)
00031     : Logger("TrAgent", "/sim/tragent/%s", node->name()),
00032       node_(node), src_(src), dst_(dst),
00033       size_(0), reps_(0), batch_(1), interval_(0)
00034 {
00035 }
00036 
00037 TrAgent*
00038 TrAgent::init(Node* node, double start_time, 
00039               const EndpointID& src, const EndpointID& dst,
00040               int argc, const char** argv)
00041 {
00042     TrAgent* a = new TrAgent(node, src, dst);
00043 
00044     oasys::OptParser p;
00045     p.addopt(new oasys::IntOpt("size", &a->size_));
00046     p.addopt(new oasys::IntOpt("reps", &a->reps_));
00047     p.addopt(new oasys::IntOpt("batch", &a->batch_));
00048     p.addopt(new oasys::DoubleOpt("interval", &a->interval_));
00049 
00050     const char* invalid;
00051     if (! p.parse(argc, argv, &invalid)) {
00052         a->logf(oasys::LOG_ERR, "invalid option: %s", invalid);
00053         return NULL;
00054     }
00055 
00056     if (a->size_ == 0) {
00057         a->logf(oasys::LOG_ERR, "size must be set in configuration");
00058         return NULL;
00059     }
00060 
00061     if (a->reps_ == 0) {
00062         a->logf(oasys::LOG_ERR, "reps must be set in configuration");
00063         return NULL;
00064     }
00065 
00066     if (a->interval_ == 0) {
00067         a->logf(oasys::LOG_ERR, "interval must be set in configuration");
00068         return NULL;
00069     }
00070 
00071     Simulator::post(new SimEvent(SIM_NEXT_SENDTIME, start_time, a));
00072     return a;
00073 }
00074 
00075 void
00076 TrAgent::process(SimEvent* e)
00077 {
00078     if (e->type() == SIM_NEXT_SENDTIME) {
00079         for (int i = 0; i < batch_; i++) {
00080             send_bundle();
00081         }
00082         
00083         if (--reps_ > 0) {
00084             double sendtime = Simulator::time() + interval_;
00085             Simulator::post(new SimEvent(SIM_NEXT_SENDTIME, sendtime, this));
00086         } else {
00087             log_debug("all batches finished");
00088         }
00089 
00090     } else {
00091         PANIC("unhandlable event %s", e->type_str());
00092     }
00093 }
00094 
00095 void
00096 TrAgent::send_bundle()
00097 {
00098     Bundle* b = new Bundle(BundlePayload::NODATA);
00099         
00100     //oasys::StaticStringBuffer<1024> buf;
00101     //b->format_verbose(&buf);
00102     //log_multiline(oasys::LOG_DEBUG, buf.c_str());
00103         
00104     b->source_.assign(src_);
00105     b->replyto_.assign(src_);
00106     b->custodian_.assign(EndpointID::NULL_EID());
00107     b->dest_.assign(dst_);
00108     b->payload_.set_length(size_);
00109         
00110     b->priority_ = 0;
00111     b->custody_requested_ = false;
00112     b->local_custody_ = false;
00113     b->singleton_dest_ = false;
00114     b->receive_rcpt_ = false;
00115     b->custody_rcpt_ = false;
00116     b->forward_rcpt_ = false;
00117     b->delivery_rcpt_ = false;
00118     b->deletion_rcpt_ = false;
00119     b->app_acked_rcpt_ = false;
00120     b->creation_ts_.seconds_ = BundleTimestamp::get_current_time();
00121     b->creation_ts_.seqno_ = b->bundleid_;
00122     b->expiration_ = 30;        
00123     b->is_fragment_     = false;
00124     b->is_admin_ = false;
00125     b->do_not_fragment_ = false;
00126     b->in_datastore_ = false;
00127     //b->orig_length_   = 0;
00128     //b->frag_offset_   = 0;
00129         
00130         
00131     log_info("N[%s]: GEN id:%d %s -> %s size:%d",
00132              node_->name(), b->bundleid_, src_.c_str(), dst_.c_str(), size_);   
00133                 
00134     log_debug("Posting(new SimRouterEvent(%f,%s,BundleReceivedEvent)",
00135               Simulator::time(), node_->name());        
00136     
00137     BundleReceivedEvent* e = new BundleReceivedEvent(b, EVENTSRC_APP, size_);
00138     Simulator::post(new SimRouterEvent(Simulator::time(), node_, e));
00139 }
00140 
00141 
00142 } // namespace dtnsim

Generated on Sat Sep 8 08:36:18 2007 for DTN Reference Implementation by  doxygen 1.5.3