log.cc

Go to the documentation of this file.
00001 //
00002 // log.cc         : Log Filter
00003 // author         : Fabio Silva
00004 //
00005 // Copyright (C) 2000-2002 by the University of Southern California
00006 // $Id: log.cc,v 1.2 2005/09/13 04:53:48 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 "log.hh"
00044 
00045 char *msg_types[] = {"INTEREST", "POSITIVE REINFORCEMENT",
00046              "NEGATIVE REINFORCEMENT", "DATA",
00047              "EXPLORATORY DATA", "PUSH EXPLORATORY DATA",
00048              "CONTROL", "REDIRECT"};
00049 
00050 #ifdef NS_DIFFUSION
00051 static class LogFilterClass : public TclClass {
00052 public:
00053   LogFilterClass() : TclClass("Application/DiffApp/LogFilter") {}
00054   TclObject * create(int argc, const char*const* argv) {
00055     return(new LogFilter());
00056   }
00057 } class_log_filter;
00058 
00059 int LogFilter::command(int argc, const char*const* argv) {
00060   if (argc == 2) {
00061     if (strcmp(argv[1], "start") == 0) {
00062       run();
00063       return TCL_OK;
00064     }
00065   }
00066   return DiffApp::command(argc, argv);
00067 }
00068 #endif // NS_DIFFUSION
00069 
00070 void LogFilterReceive::recv(Message *msg, handle h)
00071 {
00072   app_->recv(msg, h);
00073 }
00074 
00075 void LogFilter::recv(Message *msg, handle h)
00076 {
00077   if (h != filter_handle_){
00078     DiffPrint(DEBUG_ALWAYS,
00079           "Error: recv received message for handle %d when subscribing to handle %d !\n", h, filter_handle_);
00080     return;
00081   }
00082 
00083   ProcessMessage(msg);
00084 
00085   ((DiffusionRouting *)dr_)->sendMessage(msg, h);
00086 }
00087 
00088 void LogFilter::ProcessMessage(Message *msg)
00089 {
00090   DiffPrint(DEBUG_ALWAYS, "Received a");
00091 
00092   if (msg->new_message_)
00093     DiffPrint(DEBUG_ALWAYS, " new ");
00094   else
00095     DiffPrint(DEBUG_ALWAYS, "n old ");
00096 
00097   if (msg->last_hop_ != LOCALHOST_ADDR)
00098     DiffPrint(DEBUG_ALWAYS, "%s message from node %d, %d bytes\n",
00099           msg_types[msg->msg_type_], msg->last_hop_,
00100           CalculateSize(msg->msg_attr_vec_));
00101   else
00102     DiffPrint(DEBUG_ALWAYS, "%s message from agent %d, %d bytes\n",
00103           msg_types[msg->msg_type_], msg->source_port_,
00104           CalculateSize(msg->msg_attr_vec_));
00105 }
00106 
00107 handle LogFilter::setupFilter()
00108 {
00109   NRAttrVec attrs;
00110   handle h;
00111 
00112   // This is a dummy attribute for filtering that matches everything
00113   attrs.push_back(NRClassAttr.make(NRAttribute::IS, NRAttribute::INTEREST_CLASS));
00114 
00115   h = ((DiffusionRouting *)dr_)->addFilter(&attrs, LOG_FILTER_PRIORITY,
00116                        filter_callback_);
00117 
00118   ClearAttrs(&attrs);
00119   return h;
00120 }
00121 
00122 void LogFilter::run()
00123 {
00124 #ifdef NS_DIFFUSION
00125   filter_handle_ = setupFilter();
00126   DiffPrint(DEBUG_ALWAYS, "Log filter subscribed to *, received handle %d\n",
00127         filter_handle_);
00128   DiffPrint(DEBUG_ALWAYS, "Log filter initialized !\n");
00129 #else
00130   // Doesn't do anything
00131   while (1){
00132     sleep(1000);
00133   }
00134 #endif // NS_DIFFUSION
00135 }
00136 
00137 #ifdef NS_DIFFUSION
00138 LogFilter::LogFilter()
00139 #else
00140 LogFilter::LogFilter(int argc, char **argv)
00141 #endif // NS_DIFFUSION
00142 {
00143   // Create Diffusion Routing class
00144 #ifndef NS_DIFFUSION
00145   parseCommandLine(argc, argv);
00146   dr_ = NR::createNR(diffusion_port_);
00147 #endif // !NS_DIFFUSION
00148 
00149   filter_callback_ = new LogFilterReceive(this);
00150 
00151 #ifndef NS_DIFFUSION
00152   // Set up the filter
00153   filter_handle_ = setupFilter();
00154   DiffPrint(DEBUG_ALWAYS, "Log filter subscribed to *, received handle %d\n", filter_handle_);
00155   DiffPrint(DEBUG_ALWAYS, "Log filter initialized !\n");
00156 #endif // !NS_DIFFUSION
00157 }
00158 
00159 #ifndef USE_SINGLE_ADDRESS_SPACE
00160 int main(int argc, char **argv)
00161 {
00162   LogFilter *app;
00163 
00164   // Initialize and run the Log Filter
00165   app = new LogFilter(argc, argv);
00166   app->run();
00167 
00168   return 0;
00169 }
00170 #endif // !USE_SINGLE_ADDRESS_SPACE

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