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
00044 #include "1pp_ping_sender.hh"
00045 #include <unistd.h>
00046
00047 #ifdef NS_DIFFUSION
00048 static class OPPPingSenderAppClass : public TclClass {
00049 public:
00050 OPPPingSenderAppClass() : TclClass("Application/DiffApp/PingSender/OPP") {}
00051 TclObject* create(int , const char*const*) {
00052 return(new OPPPingSenderApp());
00053 }
00054 } class_opp_ping_sender;
00055
00056 void OPPPingSendDataTimer::expire(Event *e) {
00057 a_->send();
00058 }
00059
00060 void OPPPingSenderApp::send()
00061 {
00062 struct timeval tmv;
00063 int retval;
00064
00065
00066 if (num_subscriptions_ > 0){
00067
00068 GetTime(&tmv);
00069 lastEventTime_->seconds_ = tmv.tv_sec;
00070 lastEventTime_->useconds_ = tmv.tv_usec;
00071
00072
00073 DiffPrint(DEBUG_ALWAYS, "Node%d: Sending Data %d\n", ((DiffusionRouting *)dr_)->getNodeId(), last_seq_sent_);
00074 retval = dr_->send(pubHandle_, &data_attr_);
00075
00076
00077 last_seq_sent_++;
00078 counterAttr_->setVal(last_seq_sent_);
00079 }
00080
00081
00082 sdt_.resched(SEND_DATA_INTERVAL);
00083 }
00084
00085 int OPPPingSenderApp::command(int argc, const char*const* argv) {
00086 if (argc == 2) {
00087 if (strcmp(argv[1], "publish") == 0) {
00088 run();
00089 return TCL_OK;
00090 }
00091 }
00092 return DiffApp::command(argc, argv);
00093 }
00094 #endif // NS_DIFFUSION
00095
00096 void OPPPingSenderReceive::recv(NRAttrVec *data, NR::handle my_handle)
00097 {
00098 app_->recv(data, my_handle);
00099 }
00100
00101 void OPPPingSenderApp::recv(NRAttrVec *data, NR::handle my_handle)
00102 {
00103 NRSimpleAttribute<int> *nrclass = NULL;
00104
00105 nrclass = NRClassAttr.find(data);
00106
00107 if (!nrclass){
00108 DiffPrint(DEBUG_ALWAYS, "Received a BAD packet !\n");
00109 return;
00110 }
00111
00112 switch (nrclass->getVal()){
00113
00114 case NRAttribute::INTEREST_CLASS:
00115
00116 DiffPrint(DEBUG_ALWAYS, "Received an Interest message !\n");
00117 num_subscriptions_++;
00118 break;
00119
00120 case NRAttribute::DISINTEREST_CLASS:
00121
00122 DiffPrint(DEBUG_ALWAYS, "Received a Disinterest message !\n");
00123 num_subscriptions_--;
00124 break;
00125
00126 default:
00127
00128 DiffPrint(DEBUG_ALWAYS, "Received an unknown message (%d)!\n", nrclass->getVal());
00129 break;
00130
00131 }
00132 }
00133
00134 handle OPPPingSenderApp::setupSubscription()
00135 {
00136 NRAttrVec attrs;
00137
00138 attrs.push_back(NRClassAttr.make(NRAttribute::NE, NRAttribute::DATA_CLASS));
00139 attrs.push_back(NRAlgorithmAttr.make(NRAttribute::IS, NRAttribute::ONE_PHASE_PULL_ALGORITHM));
00140 attrs.push_back(NRScopeAttr.make(NRAttribute::IS, NRAttribute::NODE_LOCAL_SCOPE));
00141 attrs.push_back(TargetAttr.make(NRAttribute::EQ, "F117A"));
00142 attrs.push_back(LatitudeAttr.make(NRAttribute::IS, 60.00));
00143 attrs.push_back(LongitudeAttr.make(NRAttribute::IS, 54.00));
00144
00145 handle h = dr_->subscribe(&attrs, mr_);
00146
00147 ClearAttrs(&attrs);
00148
00149 return h;
00150 }
00151
00152 handle OPPPingSenderApp::setupPublication()
00153 {
00154 NRAttrVec attrs;
00155
00156 attrs.push_back(NRClassAttr.make(NRAttribute::IS, NRAttribute::DATA_CLASS));
00157 attrs.push_back(NRAlgorithmAttr.make(NRAttribute::IS, NRAttribute::ONE_PHASE_PULL_ALGORITHM));
00158 attrs.push_back(LatitudeAttr.make(NRAttribute::IS, 60.00));
00159 attrs.push_back(LongitudeAttr.make(NRAttribute::IS, 54.00));
00160 attrs.push_back(TargetAttr.make(NRAttribute::IS, "F117A"));
00161
00162 handle h = dr_->publish(&attrs);
00163
00164 ClearAttrs(&attrs);
00165
00166 return h;
00167 }
00168
00169 void OPPPingSenderApp::run()
00170 {
00171 struct timeval tmv;
00172 #ifndef NS_DIFFUSION
00173 int retval;
00174 #endif // !NS_DIFFUSION
00175
00176 #ifdef INTERACTIVE
00177 char input;
00178 fd_set FDS;
00179 #endif // INTERATIVE
00180
00181
00182 subHandle_ = setupSubscription();
00183 pubHandle_ = setupPublication();
00184
00185
00186 GetTime(&tmv);
00187 lastEventTime_ = new EventTime;
00188 lastEventTime_->seconds_ = tmv.tv_sec;
00189 lastEventTime_->useconds_ = tmv.tv_usec;
00190 timeAttr_ = TimeAttr.make(NRAttribute::IS, (void *) &lastEventTime_,
00191 sizeof(EventTime));
00192 data_attr_.push_back(timeAttr_);
00193
00194
00195 delete lastEventTime_;
00196 lastEventTime_ = (EventTime *) timeAttr_->getVal();
00197
00198
00199 counterAttr_ = AppCounterAttr.make(NRAttribute::IS, last_seq_sent_);
00200 data_attr_.push_back(counterAttr_);
00201
00202 #ifndef NS_DIFFUSION
00203
00204 while(1){
00205 #ifdef INTERACTIVE
00206 FD_SET(0, &FDS);
00207 fprintf(stdout, "Press <Enter> to send a ping probe...");
00208 fflush(NULL);
00209 select(1, &FDS, NULL, NULL, NULL);
00210 input = getc(stdin);
00211 #else
00212 sleep(SEND_DATA_INTERVAL);
00213 #endif // INTERACTIVE
00214
00215
00216 if (num_subscriptions_ > 0){
00217
00218 GetTime(&tmv);
00219 lastEventTime_->seconds_ = tmv.tv_sec;
00220 lastEventTime_->useconds_ = tmv.tv_usec;
00221
00222
00223 DiffPrint(DEBUG_ALWAYS, "Node%d: Sending Data %d\n", ((DiffusionRouting *)dr_)->getNodeId(), last_seq_sent_);
00224 retval = dr_->send(pubHandle_, &data_attr_);
00225
00226
00227 last_seq_sent_++;
00228 counterAttr_->setVal(last_seq_sent_);
00229 }
00230 }
00231 #else
00232 send();
00233 #endif // !NS_DIFFUSION
00234 }
00235
00236 #ifdef NS_DIFFUSION
00237 OPPPingSenderApp::OPPPingSenderApp() : sdt_(this)
00238 #else
00239 OPPPingSenderApp::OPPPingSenderApp(int argc, char **argv)
00240 #endif
00241 {
00242 last_seq_sent_ = 0;
00243 num_subscriptions_ = 0;
00244
00245 mr_ = new OPPPingSenderReceive(this);
00246
00247 #ifndef NS_DIFFUSION
00248 parseCommandLine(argc, argv);
00249 dr_ = NR::createNR(diffusion_port_);
00250 #endif // NS_DIFFUSION
00251 }
00252
00253 #ifndef NS_DIFFUSION
00254 int main(int argc, char **argv)
00255 {
00256 OPPPingSenderApp *app;
00257
00258 app = new OPPPingSenderApp(argc, argv);
00259 app->run();
00260
00261 return 0;
00262 }
00263 #endif // NS_DIFFUSION