2pp_ping_sender.cc

Go to the documentation of this file.
00001 //
00002 // ping_sender.cc : Ping Server Main File
00003 // author         : Fabio Silva
00004 //
00005 // Copyright (C) 2000-2002 by the University of Southern California
00006 // $Id: 2pp_ping_sender.cc,v 1.3 2005/09/13 04:53:46 tomh Exp $
00007 //
00008 // This program is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU General Public License,
00010 // version 2, as published by the Free Software Foundation.
00011 //
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License along
00018 // with this program; if not, write to the Free Software Foundation, Inc.,
00019 // 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00020 //
00021 // Linking this file statically or dynamically with other modules is making
00022 // a combined work based on this file.  Thus, the terms and conditions of
00023 // the GNU General Public License cover the whole combination.
00024 //
00025 // In addition, as a special exception, the copyright holders of this file
00026 // give you permission to combine this file with free software programs or
00027 // libraries that are released under the GNU LGPL and with code included in
00028 // the standard release of ns-2 under the Apache 2.0 license or under
00029 // otherwise-compatible licenses with advertising requirements (or modified
00030 // versions of such code, with unchanged license).  You may copy and
00031 // distribute such a system following the terms of the GNU GPL for this
00032 // file and the licenses of the other code concerned, provided that you
00033 // include the source code of that other code when and as the GNU GPL
00034 // requires distribution of source code.
00035 //
00036 // Note that people who make modified versions of this file are not
00037 // obligated to grant this special exception for their modified versions;
00038 // it is their choice whether to do so.  The GNU General Public License
00039 // gives permission to release a modified version without this exception;
00040 // this exception also makes it possible to release a modified version
00041 // which carries forward this exception.
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   // Send data if we have active subscriptions
00065   if (num_subscriptions_ > 0){
00066     // Update time in the packet
00067     GetTime(&tmv);
00068     lastEventTime_->seconds_ = tmv.tv_sec;
00069     lastEventTime_->useconds_ = tmv.tv_usec;
00070 
00071     // Send data probe
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     // Update counter
00076     last_seq_sent_++;
00077     counterAttr_->setVal(last_seq_sent_);
00078   }
00079 
00080   // re-schedule the timer 
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   // Setup publication and subscription
00181   subHandle_ = setupSubscription();
00182   pubHandle_ = setupPublication();
00183 
00184   // Create time attribute
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   // Change pointer to point to attribute's payload
00194   delete lastEventTime_;
00195   lastEventTime_ = (EventTime *) timeAttr_->getVal();
00196 
00197   // Create counter attribute
00198   counterAttr_ = AppCounterAttr.make(NRAttribute::IS, last_seq_sent_);
00199   data_attr_.push_back(counterAttr_);
00200 
00201 #ifndef NS_DIFFUSION
00202   // Main thread will send ping probes
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     // Send data packet if we have active subscriptions
00215     if (num_subscriptions_ > 0){
00216       // Update time in the packet
00217       GetTime(&tmv);
00218       lastEventTime_->seconds_ = tmv.tv_sec;
00219       lastEventTime_->useconds_ = tmv.tv_usec;
00220 
00221       // Send data probe
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       // Update counter
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 // NS_DIFFUSION
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

Generated on Tue Mar 6 16:47:43 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6