1pp_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: 1pp_ping_sender.cc,v 1.2 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 
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   // Send data if we have active subscriptions
00066   if (num_subscriptions_ > 0){
00067     // Update time in the packet
00068     GetTime(&tmv);
00069     lastEventTime_->seconds_ = tmv.tv_sec;
00070     lastEventTime_->useconds_ = tmv.tv_usec;
00071 
00072     // Send data probe
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     // Update counter
00077     last_seq_sent_++;
00078     counterAttr_->setVal(last_seq_sent_);
00079   }
00080 
00081   // re-schedule the timer 
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   // Setup publication and subscription
00182   subHandle_ = setupSubscription();
00183   pubHandle_ = setupPublication();
00184 
00185   // Create time attribute
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   // Change pointer to point to attribute's payload
00195   delete lastEventTime_;
00196   lastEventTime_ = (EventTime *) timeAttr_->getVal();
00197 
00198   // Create counter attribute
00199   counterAttr_ = AppCounterAttr.make(NRAttribute::IS, last_seq_sent_);
00200   data_attr_.push_back(counterAttr_);
00201 
00202 #ifndef NS_DIFFUSION
00203   // Main thread will send ping probes
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     // Send data packet if we have active subscriptions
00216     if (num_subscriptions_ > 0){
00217       // Update time in the packet
00218       GetTime(&tmv);
00219       lastEventTime_->seconds_ = tmv.tv_sec;
00220       lastEventTime_->useconds_ = tmv.tv_usec;
00221 
00222       // Send data probe
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       // Update counter
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 // NS_DIFFUSION
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

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