00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "srcrt.hh"
00024
00025 #ifdef NS_DIFFUSION
00026 class DiffAppAgent;
00027 #endif // NS_DIFFUSION
00028
00029 #ifdef NS_DIFFUSION
00030 static class SourceRouteFilterClass : public TclClass {
00031 public:
00032 SourceRouteFilterClass() : TclClass("Application/DiffApp/SourceRouteFilter") {}
00033 TclObject* create(int argc, const char*const* argv) {
00034 return(new SrcRtFilter());
00035 }
00036 } class_source_route_filter;
00037
00038 int SrcRtFilter::command(int argc, const char*const* argv) {
00039 Tcl& tcl = Tcl::instance();
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 SrcRtFilterReceive::recv(Message *msg, handle h)
00051 {
00052 app_->recv(msg, h);
00053 }
00054
00055 void SrcRtFilter::recv(Message *msg, handle h)
00056 {
00057 Message *return_msg = NULL;
00058
00059 if (h != filter_handle_){
00060 DiffPrint(DEBUG_ALWAYS, "Error: Received a message for handle %ld when subscribing to handle %ld !\n", h, filter_handle_);
00061 return;
00062 }
00063
00064 return_msg = ProcessMessage(msg);
00065
00066 if (return_msg){
00067 ((DiffusionRouting *)dr_)->sendMessage(msg, h);
00068
00069 delete msg;
00070 }
00071 }
00072
00073 Message * SrcRtFilter::ProcessMessage(Message *msg)
00074 {
00075 char *original_route, *new_route, *p;
00076 int len;
00077 int32_t next_hop;
00078 NRSimpleAttribute<char *> *route = NULL;
00079
00080 route = SourceRouteAttr.find(msg->msg_attr_vec_);
00081 if (!route){
00082 DiffPrint(DEBUG_ALWAYS, "Error: Can't find the route attribute !\n");
00083 return msg;
00084 }
00085
00086 original_route = route->getVal();
00087 len = strlen(original_route);
00088
00089
00090 if (len == 0)
00091 return msg;
00092
00093
00094 next_hop = atoi(original_route);
00095
00096
00097 p = strstr(original_route, ":");
00098 if (!p){
00099
00100 new_route = new char[1];
00101 new_route[0] = '\0';
00102 }
00103 else{
00104 p++;
00105 len = strlen(p);
00106 new_route = new char[(len + 1)];
00107 strncpy(new_route, p, (len + 1));
00108 if (new_route[len] != '\0')
00109 DiffPrint(DEBUG_ALWAYS, "Warning: String must end with NULL !\n");
00110 }
00111
00112 route->setVal(new_route);
00113
00114
00115 delete [] new_route;
00116
00117
00118 msg->next_hop_ = next_hop;
00119 ((DiffusionRouting *)dr_)->sendMessage(msg, filter_handle_);
00120
00121 delete msg;
00122
00123 return NULL;
00124 }
00125
00126 handle SrcRtFilter::setupFilter()
00127 {
00128 NRAttrVec attrs;
00129 handle h;
00130
00131
00132 attrs.push_back(SourceRouteAttr.make(NRAttribute::EQ_ANY, ""));
00133
00134 h = ((DiffusionRouting *)dr_)->addFilter(&attrs, SRCRT_FILTER_PRIORITY,
00135 filter_callback_);
00136
00137 ClearAttrs(&attrs);
00138 return h;
00139 }
00140
00141 void SrcRtFilter::run()
00142 {
00143 #ifdef NS_DIFFUSION
00144 filter_handle_ = setupFilter();
00145 DiffPrint(DEBUG_ALWAYS, "SrcRtFilter filter received handle %ld\n",
00146 filter_handle_);
00147 DiffPrint(DEBUG_ALWAYS, "SrcRtFilter filter initialized !\n");
00148 #else
00149
00150 while (1){
00151 sleep(1000);
00152 }
00153 #endif // NS_DIFFUSION
00154 }
00155
00156 #ifdef NS_DIFFUSION
00157 SrcRtFilter::SrcRtFilter()
00158 {
00159 DiffAppAgent *agent;
00160 #else
00161 SrcRtFilter::SrcRtFilter(int argc, char **argv)
00162 {
00163 #endif // NS_DIFFUSION
00164
00165
00166 #ifndef NS_DIFFUSION
00167 parseCommandLine(argc, argv);
00168 dr_ = NR::createNR(diffusion_port_);
00169 #endif // !NS_DIFFUSION
00170
00171 filter_callback_ = new SrcRtFilterReceive(this);
00172
00173 #ifndef NS_DIFFUSION
00174
00175 filter_handle_ = setupFilter();
00176 DiffPrint(DEBUG_ALWAYS, "SrcRtFilter filter received handle %ld\n",
00177 filter_handle_);
00178 DiffPrint(DEBUG_ALWAYS, "SrcRtFilter filter initialized !\n");
00179 #endif // !NS_DIFFUSION
00180 }
00181
00182 #ifndef USE_SINGLE_ADDRESS_SPACE
00183 int main(int argc, char **argv)
00184 {
00185 SrcRtFilter *app;
00186
00187
00188 app = new SrcRtFilter(argc, argv);
00189 app->run();
00190
00191 return 0;
00192 }
00193 #endif // !USE_SINGLE_ADDRESS_SPACE