Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

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.6 2002/11/26 22:45:38 haldar 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 //
00022 
00023 #include "log.hh"
00024 
00025 char *msg_types[] = {"INTEREST", "POSITIVE REINFORCEMENT",
00026                      "NEGATIVE REINFORCEMENT", "DATA",
00027                      "EXPLORATORY DATA", "PUSH EXPLORATORY DATA",
00028                      "CONTROL", "REDIRECT"};
00029 
00030 #ifdef NS_DIFFUSION
00031 static class LogFilterClass : public TclClass {
00032 public:
00033   LogFilterClass() : TclClass("Application/DiffApp/LogFilter") {}
00034   TclObject * create(int argc, const char*const* argv) {
00035     return(new LogFilter());
00036   }
00037 } class_log_filter;
00038 
00039 int LogFilter::command(int argc, const char*const* argv) {
00040   if (argc == 2) {
00041     if (strcmp(argv[1], "start") == 0) {
00042       run();
00043       return TCL_OK;
00044     }
00045   }
00046   return DiffApp::command(argc, argv);
00047 }
00048 #endif // NS_DIFFUSION
00049 
00050 void LogFilterReceive::recv(Message *msg, handle h)
00051 {
00052   app_->recv(msg, h);
00053 }
00054 
00055 void LogFilter::recv(Message *msg, handle h)
00056 {
00057   if (h != filter_handle_){
00058     DiffPrint(DEBUG_ALWAYS,
00059               "Error: recv received message for handle %d when subscribing to handle %d !\n", h, filter_handle_);
00060     return;
00061   }
00062 
00063   ProcessMessage(msg);
00064 
00065   ((DiffusionRouting *)dr_)->sendMessage(msg, h);
00066 }
00067 
00068 void LogFilter::ProcessMessage(Message *msg)
00069 {
00070   DiffPrint(DEBUG_ALWAYS, "Received a");
00071 
00072   if (msg->new_message_)
00073     DiffPrint(DEBUG_ALWAYS, " new ");
00074   else
00075     DiffPrint(DEBUG_ALWAYS, "n old ");
00076 
00077   if (msg->last_hop_ != LOCALHOST_ADDR)
00078     DiffPrint(DEBUG_ALWAYS, "%s message from node %d, %d bytes\n",
00079               msg_types[msg->msg_type_], msg->last_hop_,
00080               CalculateSize(msg->msg_attr_vec_));
00081   else
00082     DiffPrint(DEBUG_ALWAYS, "%s message from agent %d, %d bytes\n",
00083               msg_types[msg->msg_type_], msg->source_port_,
00084               CalculateSize(msg->msg_attr_vec_));
00085 }
00086 
00087 handle LogFilter::setupFilter()
00088 {
00089   NRAttrVec attrs;
00090   handle h;
00091 
00092   // This is a dummy attribute for filtering that matches everything
00093   attrs.push_back(NRClassAttr.make(NRAttribute::IS, NRAttribute::INTEREST_CLASS));
00094 
00095   h = ((DiffusionRouting *)dr_)->addFilter(&attrs, LOG_FILTER_PRIORITY,
00096                                            filter_callback_);
00097 
00098   ClearAttrs(&attrs);
00099   return h;
00100 }
00101 
00102 void LogFilter::run()
00103 {
00104 #ifdef NS_DIFFUSION
00105   filter_handle_ = setupFilter();
00106   DiffPrint(DEBUG_ALWAYS, "Log filter subscribed to *, received handle %d\n",
00107             filter_handle_);
00108   DiffPrint(DEBUG_ALWAYS, "Log filter initialized !\n");
00109 #else
00110   // Doesn't do anything
00111   while (1){
00112     sleep(1000);
00113   }
00114 #endif // NS_DIFFUSION
00115 }
00116 
00117 #ifdef NS_DIFFUSION
00118 LogFilter::LogFilter()
00119 #else
00120 LogFilter::LogFilter(int argc, char **argv)
00121 #endif // NS_DIFFUSION
00122 {
00123   // Create Diffusion Routing class
00124 #ifndef NS_DIFFUSION
00125   parseCommandLine(argc, argv);
00126   dr_ = NR::createNR(diffusion_port_);
00127 #endif // !NS_DIFFUSION
00128 
00129   filter_callback_ = new LogFilterReceive(this);
00130 
00131 #ifndef NS_DIFFUSION
00132   // Set up the filter
00133   filter_handle_ = setupFilter();
00134   DiffPrint(DEBUG_ALWAYS, "Log filter subscribed to *, received handle %d\n", filter_handle_);
00135   DiffPrint(DEBUG_ALWAYS, "Log filter initialized !\n");
00136 #endif // !NS_DIFFUSION
00137 }
00138 
00139 #ifndef USE_SINGLE_ADDRESS_SPACE
00140 int main(int argc, char **argv)
00141 {
00142   LogFilter *app;
00143 
00144   // Initialize and run the Log Filter
00145   app = new LogFilter(argc, argv);
00146   app->run();
00147 
00148   return 0;
00149 }
00150 #endif // !USE_SINGLE_ADDRESS_SPACE

Generated on Tue Apr 20 12:14:22 2004 for NS2.26SourcesOriginal by doxygen 1.3.3