00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
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 }