00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
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
00122 {
00123
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
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
00145 app = new LogFilter(argc, argv);
00146 app->run();
00147
00148 return 0;
00149 }
00150 #endif // !USE_SINGLE_ADDRESS_SPACE