TrAgent.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 <oasys/util/Options.h>
00040 #include <oasys/util/OptParser.h>
00041 
00042 #include "TrAgent.h"
00043 #include "Simulator.h"
00044 #include "Node.h"
00045 #include "SimEvent.h"
00046 #include "bundling/Bundle.h"
00047 
00048 namespace dtnsim {
00049 
00050 TrAgent::TrAgent(Node* node, const EndpointID& src, const EndpointID& dst)
00051     : Logger("TrAgent", "/sim/tragent/%s", node->name()),
00052       node_(node), src_(src), dst_(dst),
00053       size_(0), reps_(0), batch_(1), interval_(0)
00054 {
00055 }
00056 
00057 TrAgent*
00058 TrAgent::init(Node* node, double start_time, 
00059               const EndpointID& src, const EndpointID& dst,
00060               int argc, const char** argv)
00061 {
00062     TrAgent* a = new TrAgent(node, src, dst);
00063 
00064     oasys::OptParser p;
00065     p.addopt(new oasys::IntOpt("size", &a->size_));
00066     p.addopt(new oasys::IntOpt("reps", &a->reps_));
00067     p.addopt(new oasys::IntOpt("batch", &a->batch_));
00068     p.addopt(new oasys::DoubleOpt("interval", &a->interval_));
00069 
00070     const char* invalid;
00071     if (! p.parse(argc, argv, &invalid)) {
00072         a->logf(oasys::LOG_ERR, "invalid option: %s", invalid);
00073         return NULL;
00074     }
00075 
00076     if (a->size_ == 0) {
00077         a->logf(oasys::LOG_ERR, "size must be set in configuration");
00078         return NULL;
00079     }
00080 
00081     if (a->reps_ == 0) {
00082         a->logf(oasys::LOG_ERR, "reps must be set in configuration");
00083         return NULL;
00084     }
00085 
00086     if (a->interval_ == 0) {
00087         a->logf(oasys::LOG_ERR, "interval must be set in configuration");
00088         return NULL;
00089     }
00090 
00091     Simulator::post(new SimEvent(SIM_NEXT_SENDTIME, start_time, a));
00092     return a;
00093 }
00094 
00095 void
00096 TrAgent::process(SimEvent* e)
00097 {
00098     if (e->type() == SIM_NEXT_SENDTIME) {
00099         for (int i = 0; i < batch_; i++) {
00100             send_bundle();
00101         }
00102         
00103         if (--reps_ > 0) {
00104             double sendtime = Simulator::time() + interval_;
00105             Simulator::post(new SimEvent(SIM_NEXT_SENDTIME, sendtime, this));
00106         }
00107         else {
00108             log_debug("all batches finished");
00109         }
00110 
00111     } else {
00112         PANIC("unhandlable event %s", e->type_str());
00113     }
00114 }
00115 
00116 void
00117 TrAgent::send_bundle()
00118 {
00119     Bundle* b = new Bundle(oasys::Builder::builder());
00120     b->bundleid_ = node_->next_bundleid();
00121     b->payload_.init(b->bundleid_, BundlePayload::NODATA);
00122 
00123     b->source_.assign(src_);
00124     b->replyto_.assign(src_);
00125     b->custodian_.assign(EndpointID::NULL_EID());
00126     b->dest_.assign(dst_);
00127     b->payload_.set_length(size_);
00128 
00129     log_info("N[%s]: GEN id:%d %s -> %s size:%d",
00130              node_->name(), b->bundleid_, src_.c_str(), dst_.c_str(), size_);
00131 
00132     BundleReceivedEvent* e = new BundleReceivedEvent(b, EVENTSRC_APP, size_);
00133     Simulator::post(new SimRouterEvent(Simulator::time(), node_, e));
00134 }
00135 
00136 
00137 } // namespace dtnsim

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