00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifdef HAVE_CONFIG_H
00018 # include <dtn-config.h>
00019 #endif
00020
00021 #include <math.h>
00022
00023 #include <oasys/util/Options.h>
00024 #include <oasys/util/OptParser.h>
00025
00026 #include "TrAgent.h"
00027 #include "Simulator.h"
00028 #include "Node.h"
00029 #include "SimEvent.h"
00030 #include "SimLog.h"
00031 #include "bundling/Bundle.h"
00032 #include "bundling/BundleTimestamp.h"
00033
00034 namespace dtnsim {
00035
00036
00037 TrAgent::TrAgent(const EndpointID& src, const EndpointID& dst)
00038 : Logger("TrAgent", "/sim/tragent/%s", Node::active_node()->name()),
00039 src_(src), dst_(dst),
00040 size_(0), expiration_(30), reps_(0), batch_(1), interval_(0)
00041 {
00042 }
00043
00044
00045 TrAgent*
00046 TrAgent::init(const EndpointID& src, const EndpointID& dst,
00047 int argc, const char** argv)
00048 {
00049 TrAgent* a = new TrAgent(src, dst);
00050
00051 oasys::OptParser p;
00052 p.addopt(new oasys::SizeOpt("size", &a->size_));
00053 p.addopt(new oasys::UIntOpt("expiration", &a->expiration_));
00054 p.addopt(new oasys::UIntOpt("reps", &a->reps_));
00055 p.addopt(new oasys::UIntOpt("batch", &a->batch_));
00056 p.addopt(new oasys::DoubleOpt("interval", &a->interval_));
00057
00058 const char* invalid;
00059 if (! p.parse(argc, argv, &invalid)) {
00060 a->logf(oasys::LOG_ERR, "invalid option: %s", invalid);
00061 return NULL;
00062 }
00063
00064 if (a->size_ == 0) {
00065 a->logf(oasys::LOG_ERR, "size must be set in configuration");
00066 return NULL;
00067 }
00068
00069 if (a->reps_ == 0) {
00070 a->logf(oasys::LOG_ERR, "reps must be set in configuration");
00071 return NULL;
00072 }
00073
00074 if (a->reps_ != 1 && a->interval_ == 0) {
00075 a->logf(oasys::LOG_ERR, "interval must be set in configuration");
00076 return NULL;
00077 }
00078
00079 a->schedule_immediate();
00080 return a;
00081 }
00082
00083
00084 void
00085 TrAgent::timeout(const struct timeval& )
00086 {
00087 for (u_int i = 0; i < batch_; i++) {
00088 send_bundle();
00089 }
00090
00091 if (--reps_ > 0) {
00092 log_debug("scheduling timer in %u ms", (u_int)(interval_ * 1000));
00093 schedule_in((int)(interval_ * 1000));
00094 } else {
00095 log_debug("all batches finished");
00096 }
00097 }
00098
00099
00100 void
00101 TrAgent::send_bundle()
00102 {
00103 Bundle* b = new Bundle(BundlePayload::NODATA);
00104
00105
00106
00107
00108
00109 b->mutable_source()->assign(src_);
00110 b->mutable_replyto()->assign(src_);
00111 b->mutable_custodian()->assign(EndpointID::NULL_EID());
00112 b->mutable_dest()->assign(dst_);
00113 b->mutable_payload()->set_length(size_);
00114
00115 b->set_priority(0);
00116 b->set_custody_requested(false);
00117 b->set_local_custody(false);
00118 b->set_singleton_dest(false);
00119 b->set_receive_rcpt(false);
00120 b->set_custody_rcpt(false);
00121 b->set_forward_rcpt(false);
00122 b->set_delivery_rcpt(false);
00123 b->set_deletion_rcpt(false);
00124 b->set_app_acked_rcpt(false);
00125 b->set_creation_ts(BundleTimestamp(BundleTimestamp::get_current_time(),
00126 b->bundleid()));
00127 b->set_expiration(expiration_);
00128 b->set_is_fragment(false);
00129 b->set_is_admin(false);
00130 b->set_do_not_fragment(false);
00131 b->set_in_datastore(false);
00132
00133
00134
00135 log_info("N[%s]: GEN id:%d %s -> %s size:%llu",
00136 Node::active_node()->name(), b->bundleid(),
00137 src_.c_str(), dst_.c_str(), U64FMT(size_));
00138
00139 SimLog::instance()->log_gen(Node::active_node(), b);
00140
00141 BundleDaemon::post(new BundleReceivedEvent(b, EVENTSRC_APP,
00142 NULL ));
00143 }
00144
00145
00146 }