1pp_ping_receiver.cc

Go to the documentation of this file.
00001 //
00002 // ping_receiver.cc : Ping Receiver Main File
00003 // author           : Fabio Silva
00004 //
00005 // Copyright (C) 2000-2002 by the University of Southern California
00006 // $Id: 1pp_ping_receiver.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 #include "1pp_ping_receiver.hh"
00044 
00045 #ifdef NS_DIFFUSION
00046 static class OPPPingReceiverAppClass : public TclClass {
00047 public:
00048     OPPPingReceiverAppClass() : TclClass("Application/DiffApp/PingReceiver/OPP") {}
00049     TclObject* create(int , const char*const* ) {
00050         return(new OPPPingReceiverApp());
00051     }
00052 } class_ping_receiver;
00053 
00054 int OPPPingReceiverApp::command(int argc, const char*const* argv) {
00055   if (argc == 2) {
00056     if (strcmp(argv[1], "subscribe") == 0) {
00057       run();
00058       return TCL_OK;
00059     }
00060    }
00061   return DiffApp::command(argc, argv);
00062 }
00063 
00064 #endif // NS_DIFFUSION
00065 
00066 void OPPPingReceiverReceive::recv(NRAttrVec *data, NR::handle my_handle)
00067 {
00068   app_->recv(data, my_handle);
00069 }
00070 
00071 void OPPPingReceiverApp::recv(NRAttrVec *data, NR::handle my_handle)
00072 {
00073   NRSimpleAttribute<int> *counterAttr = NULL;
00074   NRSimpleAttribute<void *> *timeAttr = NULL;
00075   EventTime *probe_event;
00076   long delay_seconds;
00077   long delay_useconds;
00078   float total_delay;
00079   struct timeval tmv;
00080 
00081   GetTime(&tmv);
00082 
00083   counterAttr = AppCounterAttr.find(data);
00084   timeAttr = TimeAttr.find(data);
00085 
00086   if (!counterAttr || !timeAttr){
00087     DiffPrint(DEBUG_ALWAYS, "Received a BAD packet !\n");
00088     PrintAttrs(data);
00089     return;
00090   }
00091 
00092   // Calculate latency
00093   probe_event = (EventTime *) timeAttr->getVal();
00094   delay_seconds = tmv.tv_sec;
00095   delay_useconds = tmv.tv_usec;
00096 
00097   if ((delay_seconds < probe_event->seconds_) ||
00098       ((delay_seconds == probe_event->seconds_) &&
00099        (delay_useconds < probe_event->useconds_))){
00100     // Time's not synchronized
00101     delay_seconds = -1;
00102     delay_useconds = 0;
00103     DiffPrint(DEBUG_ALWAYS, "Error calculating delay !\n");
00104   }
00105   else{
00106     delay_seconds = delay_seconds - probe_event->seconds_;
00107     if (delay_useconds < probe_event->useconds_){
00108       delay_seconds--;
00109       delay_useconds = delay_useconds + 1000000;
00110     }
00111     delay_useconds = delay_useconds - probe_event->useconds_;
00112   }
00113   total_delay = (float) (1.0 * delay_seconds) + ((float) delay_useconds / 1000000.0);
00114 
00115   // Check if this is the first message received
00116   if (first_msg_recv_ < 0){
00117     first_msg_recv_ = counterAttr->getVal();
00118   }
00119 
00120   // Print output message
00121   if (last_seq_recv_ >= 0){
00122     if (counterAttr->getVal() < last_seq_recv_){
00123       // Multiple sources detected, disabling statistics
00124       last_seq_recv_ = -1;
00125       DiffPrint(DEBUG_ALWAYS, "Node%d: Received data %d, total latency = %f!\n",
00126         ((DiffusionRouting *)dr_)->getNodeId(),
00127         counterAttr->getVal(), total_delay);
00128     }
00129     else{
00130       last_seq_recv_ = counterAttr->getVal();
00131       num_msg_recv_++;
00132       DiffPrint(DEBUG_ALWAYS, "Node%d: Received data: %d, total latency = %f, %% messages received: %f !\n",
00133         ((DiffusionRouting *)dr_)->getNodeId(),
00134         last_seq_recv_, total_delay,
00135         (float) ((num_msg_recv_ * 100.00) /
00136              ((last_seq_recv_ - first_msg_recv_) + 1)));
00137     }
00138   }
00139   else{
00140     DiffPrint(DEBUG_ALWAYS, "Node%d: Received data %d, total latency = %f !\n",
00141           ((DiffusionRouting *)dr_)->getNodeId(),
00142           counterAttr->getVal(), total_delay);
00143   }
00144 }
00145 
00146 handle OPPPingReceiverApp::setupSubscription()
00147 {
00148   NRAttrVec attrs;
00149 
00150   attrs.push_back(NRClassAttr.make(NRAttribute::IS, NRAttribute::INTEREST_CLASS));
00151   attrs.push_back(NRAlgorithmAttr.make(NRAttribute::IS, NRAttribute::ONE_PHASE_PULL_ALGORITHM));
00152   attrs.push_back(LatitudeAttr.make(NRAttribute::GT, 54.78));
00153   attrs.push_back(LongitudeAttr.make(NRAttribute::LE, 87.32));
00154   attrs.push_back(TargetAttr.make(NRAttribute::IS, "F117A"));
00155 
00156   handle h = dr_->subscribe(&attrs, mr_);
00157 
00158   ClearAttrs(&attrs);
00159 
00160   return h;
00161 }
00162 
00163 void OPPPingReceiverApp::run()
00164 {
00165   subHandle_ = setupSubscription();
00166 
00167 #ifndef NS_DIFFUSION
00168   // Do nothing
00169   while (1){
00170     sleep(1000);
00171   }
00172 #endif // !NS_DIFFUSION
00173 }
00174 
00175 #ifdef NS_DIFFUSION
00176 OPPPingReceiverApp::OPPPingReceiverApp()
00177 #else
00178 OPPPingReceiverApp::OPPPingReceiverApp(int argc, char **argv)
00179 #endif // NS_DIFFUSION
00180 {
00181   last_seq_recv_ = 0;
00182   num_msg_recv_ = 0;
00183   first_msg_recv_ = -1;
00184 
00185   mr_ = new OPPPingReceiverReceive(this);
00186 
00187 #ifndef NS_DIFFUSION
00188   parseCommandLine(argc, argv);
00189   dr_ = NR::createNR(diffusion_port_);
00190 #endif // !NS_DIFFUSION
00191 }
00192 
00193 #ifndef NS_DIFFUSION
00194 int main(int argc, char **argv)
00195 {
00196   OPPPingReceiverApp *app;
00197 
00198   app = new OPPPingReceiverApp(argc, argv);
00199   app->run();
00200 
00201   return 0;
00202 }
00203 #endif // !NS_DIFFUSION

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