push_sender.cc

Go to the documentation of this file.
00001 //
00002 // push_sender.cc : Ping Server Main File
00003 // author         : Fabio Silva
00004 //
00005 // Copyright (C) 2000-2002 by the University of Southern California
00006 // $Id: push_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 "push_sender.hh"
00044 #include <unistd.h>
00045 
00046 #ifdef NS_DIFFUSION
00047 static class PushSenderAppClass : public TclClass {
00048 public:
00049   PushSenderAppClass() : TclClass("Application/DiffApp/PushSender") {}
00050   TclObject* create(int , const char*const*) {
00051     return(new PushSenderApp());
00052   }
00053 } class_ping_sender;
00054 
00055 void PushSendDataTimer::expire(Event *e) {
00056   a_->send();
00057 }
00058 
00059 void PushSenderApp::send()
00060 {
00061   struct timeval tmv;
00062   int retval;
00063 
00064   // Update time in the packet
00065   GetTime(&tmv);
00066   lastEventTime_->seconds_ = tmv.tv_sec;
00067   lastEventTime_->useconds_ = tmv.tv_usec;
00068 
00069   // Send data probe
00070   DiffPrint(DEBUG_ALWAYS, "Node%d: Sending Data %d\n", ((DiffusionRouting *)dr_)->getNodeId(), last_seq_sent_);
00071   retval = dr_->send(pubHandle_, &data_attr_);
00072 
00073   // Update counter
00074   last_seq_sent_++;
00075   counterAttr_->setVal(last_seq_sent_);
00076 
00077   // re-schedule the timer 
00078   sdt_.resched(SEND_DATA_INTERVAL);
00079 }
00080 
00081 int PushSenderApp::command(int argc, const char*const* argv) {
00082   if (argc == 2) {
00083     if (strcmp(argv[1], "publish") == 0) {
00084       run();
00085       return TCL_OK;
00086     }
00087   }
00088   return DiffApp::command(argc, argv);
00089 }
00090 #endif // NS_DIFFUSION
00091 
00092 handle PushSenderApp::setupPublication()
00093 {
00094   NRAttrVec attrs;
00095 
00096   attrs.push_back(NRClassAttr.make(NRAttribute::IS, NRAttribute::DATA_CLASS));
00097   attrs.push_back(NRScopeAttr.make(NRAttribute::IS, NRAttribute::GLOBAL_SCOPE));
00098   attrs.push_back(LatitudeAttr.make(NRAttribute::LE, 70.00));
00099   attrs.push_back(LatitudeAttr.make(NRAttribute::GT, 50.00));
00100   attrs.push_back(LongitudeAttr.make(NRAttribute::LE, 85.00));
00101   attrs.push_back(LongitudeAttr.make(NRAttribute::GT, 50.00));
00102   attrs.push_back(TargetAttr.make(NRAttribute::IS, "F117A"));
00103 
00104   handle h = dr_->publish(&attrs);
00105 
00106   ClearAttrs(&attrs);
00107 
00108   return h;
00109 }
00110 
00111 void PushSenderApp::run()
00112 {
00113   struct timeval tmv;
00114 #ifndef NS_DIFFUSION
00115   int retval;
00116 #endif // !NS_DIFFUSION
00117 
00118 #ifdef INTERACTIVE
00119   char input;
00120   fd_set FDS;
00121 #endif // INTERATIVE
00122 
00123   // Setup publication and subscription
00124   pubHandle_ = setupPublication();
00125 
00126   // Create time attribute
00127   GetTime(&tmv);
00128   lastEventTime_ = new EventTime;
00129   lastEventTime_->seconds_ = tmv.tv_sec;
00130   lastEventTime_->useconds_ = tmv.tv_usec;
00131   timeAttr_ = TimeAttr.make(NRAttribute::IS, (void *) &lastEventTime_,
00132                 sizeof(EventTime));
00133   data_attr_.push_back(timeAttr_);
00134 
00135   // Change pointer to point to attribute's payload
00136   delete lastEventTime_;
00137   lastEventTime_ = (EventTime *) timeAttr_->getVal();
00138 
00139   // Create counter attribute
00140   counterAttr_ = AppCounterAttr.make(NRAttribute::IS, last_seq_sent_);
00141   data_attr_.push_back(counterAttr_);
00142 
00143 #ifndef NS_DIFFUSION
00144   // Main thread will send ping probes
00145   while(1){
00146 #ifdef INTERACTIVE
00147     FD_SET(0, &FDS);
00148     fprintf(stdout, "Press <Enter> to send a ping probe...");
00149     fflush(NULL);
00150     select(1, &FDS, NULL, NULL, NULL);
00151     input = getc(stdin);
00152 #else
00153     sleep(5);
00154 #endif // INTERACTIVE
00155 
00156     // Update time in the packet
00157     GetTime(&tmv);
00158     lastEventTime_->seconds_ = tmv.tv_sec;
00159     lastEventTime_->useconds_ = tmv.tv_usec;
00160 
00161     // Send data probe
00162     DiffPrint(DEBUG_ALWAYS, "Sending Data %d\n", last_seq_sent_);
00163     retval = dr_->send(pubHandle_, &data_attr_);
00164 
00165     // Update counter
00166     last_seq_sent_++;
00167     counterAttr_->setVal(last_seq_sent_);
00168   }
00169 #else
00170   send();
00171 #endif // NS_DIFFUSION
00172 }
00173 
00174 #ifdef NS_DIFFUSION
00175 PushSenderApp::PushSenderApp() : sdt_(this)
00176 #else
00177 PushSenderApp::PushSenderApp(int argc, char **argv)
00178 #endif // NS_DIFFUSION
00179 {
00180   last_seq_sent_ = 0;
00181 
00182 #ifndef NS_DIFFUSION
00183   parseCommandLine(argc, argv);
00184   dr_ = NR::createNR(diffusion_port_);
00185 #endif // NS_DIFFUSION
00186 }
00187 
00188 #ifndef NS_DIFFUSION
00189 int main(int argc, char **argv)
00190 {
00191   PushSenderApp *app;
00192 
00193   app = new PushSenderApp(argc, argv);
00194   app->run();
00195 
00196   return 0;
00197 }
00198 #endif // NS_DIFFUSION

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