00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
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
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
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
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
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
00165 app = new LogFilter(argc, argv);
00166 app->run();
00167
00168 return 0;
00169 }
00170 #endif // !USE_SINGLE_ADDRESS_SPACE