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
00040
00041
00042
00043 #include "1pp_ping_receiver.hh"
00044
00045 #ifdef NS_DIFFUSION
00046 static class OPPPingReceiverAppClass : public TclClass {
00047 public:
00048 OPPPingReceiverAppClass() : TclClass("Application/DiffApp/PingReceiver/OPP") {}
00049 TclObject* create(int , const char*const* ) {
00050 return(new OPPPingReceiverApp());
00051 }
00052 } class_ping_receiver;
00053
00054 int OPPPingReceiverApp::command(int argc, const char*const* argv) {
00055 if (argc == 2) {
00056 if (strcmp(argv[1], "subscribe") == 0) {
00057 run();
00058 return TCL_OK;
00059 }
00060 }
00061 return DiffApp::command(argc, argv);
00062 }
00063
00064 #endif // NS_DIFFUSION
00065
00066 void OPPPingReceiverReceive::recv(NRAttrVec *data, NR::handle my_handle)
00067 {
00068 app_->recv(data, my_handle);
00069 }
00070
00071 void OPPPingReceiverApp::recv(NRAttrVec *data, NR::handle my_handle)
00072 {
00073 NRSimpleAttribute<int> *counterAttr = NULL;
00074 NRSimpleAttribute<void *> *timeAttr = NULL;
00075 EventTime *probe_event;
00076 long delay_seconds;
00077 long delay_useconds;
00078 float total_delay;
00079 struct timeval tmv;
00080
00081 GetTime(&tmv);
00082
00083 counterAttr = AppCounterAttr.find(data);
00084 timeAttr = TimeAttr.find(data);
00085
00086 if (!counterAttr || !timeAttr){
00087 DiffPrint(DEBUG_ALWAYS, "Received a BAD packet !\n");
00088 PrintAttrs(data);
00089 return;
00090 }
00091
00092
00093 probe_event = (EventTime *) timeAttr->getVal();
00094 delay_seconds = tmv.tv_sec;
00095 delay_useconds = tmv.tv_usec;
00096
00097 if ((delay_seconds < probe_event->seconds_) ||
00098 ((delay_seconds == probe_event->seconds_) &&
00099 (delay_useconds < probe_event->useconds_))){
00100
00101 delay_seconds = -1;
00102 delay_useconds = 0;
00103 DiffPrint(DEBUG_ALWAYS, "Error calculating delay !\n");
00104 }
00105 else{
00106 delay_seconds = delay_seconds - probe_event->seconds_;
00107 if (delay_useconds < probe_event->useconds_){
00108 delay_seconds--;
00109 delay_useconds = delay_useconds + 1000000;
00110 }
00111 delay_useconds = delay_useconds - probe_event->useconds_;
00112 }
00113 total_delay = (float) (1.0 * delay_seconds) + ((float) delay_useconds / 1000000.0);
00114
00115
00116 if (first_msg_recv_ < 0){
00117 first_msg_recv_ = counterAttr->getVal();
00118 }
00119
00120
00121 if (last_seq_recv_ >= 0){
00122 if (counterAttr->getVal() < last_seq_recv_){
00123
00124 last_seq_recv_ = -1;
00125 DiffPrint(DEBUG_ALWAYS, "Node%d: Received data %d, total latency = %f!\n",
00126 ((DiffusionRouting *)dr_)->getNodeId(),
00127 counterAttr->getVal(), total_delay);
00128 }
00129 else{
00130 last_seq_recv_ = counterAttr->getVal();
00131 num_msg_recv_++;
00132 DiffPrint(DEBUG_ALWAYS, "Node%d: Received data: %d, total latency = %f, %% messages received: %f !\n",
00133 ((DiffusionRouting *)dr_)->getNodeId(),
00134 last_seq_recv_, total_delay,
00135 (float) ((num_msg_recv_ * 100.00) /
00136 ((last_seq_recv_ - first_msg_recv_) + 1)));
00137 }
00138 }
00139 else{
00140 DiffPrint(DEBUG_ALWAYS, "Node%d: Received data %d, total latency = %f !\n",
00141 ((DiffusionRouting *)dr_)->getNodeId(),
00142 counterAttr->getVal(), total_delay);
00143 }
00144 }
00145
00146 handle OPPPingReceiverApp::setupSubscription()
00147 {
00148 NRAttrVec attrs;
00149
00150 attrs.push_back(NRClassAttr.make(NRAttribute::IS, NRAttribute::INTEREST_CLASS));
00151 attrs.push_back(NRAlgorithmAttr.make(NRAttribute::IS, NRAttribute::ONE_PHASE_PULL_ALGORITHM));
00152 attrs.push_back(LatitudeAttr.make(NRAttribute::GT, 54.78));
00153 attrs.push_back(LongitudeAttr.make(NRAttribute::LE, 87.32));
00154 attrs.push_back(TargetAttr.make(NRAttribute::IS, "F117A"));
00155
00156 handle h = dr_->subscribe(&attrs, mr_);
00157
00158 ClearAttrs(&attrs);
00159
00160 return h;
00161 }
00162
00163 void OPPPingReceiverApp::run()
00164 {
00165 subHandle_ = setupSubscription();
00166
00167 #ifndef NS_DIFFUSION
00168
00169 while (1){
00170 sleep(1000);
00171 }
00172 #endif // !NS_DIFFUSION
00173 }
00174
00175 #ifdef NS_DIFFUSION
00176 OPPPingReceiverApp::OPPPingReceiverApp()
00177 #else
00178 OPPPingReceiverApp::OPPPingReceiverApp(int argc, char **argv)
00179 #endif // NS_DIFFUSION
00180 {
00181 last_seq_recv_ = 0;
00182 num_msg_recv_ = 0;
00183 first_msg_recv_ = -1;
00184
00185 mr_ = new OPPPingReceiverReceive(this);
00186
00187 #ifndef NS_DIFFUSION
00188 parseCommandLine(argc, argv);
00189 dr_ = NR::createNR(diffusion_port_);
00190 #endif // !NS_DIFFUSION
00191 }
00192
00193 #ifndef NS_DIFFUSION
00194 int main(int argc, char **argv)
00195 {
00196 OPPPingReceiverApp *app;
00197
00198 app = new OPPPingReceiverApp(argc, argv);
00199 app->run();
00200
00201 return 0;
00202 }
00203 #endif // !NS_DIFFUSION