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