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 "srcrt.hh"
00044
00045 #ifdef NS_DIFFUSION
00046 class DiffAppAgent;
00047 #endif // NS_DIFFUSION
00048
00049 #ifdef NS_DIFFUSION
00050 static class SourceRouteFilterClass : public TclClass {
00051 public:
00052 SourceRouteFilterClass() : TclClass("Application/DiffApp/SourceRouteFilter") {}
00053 TclObject* create(int argc, const char*const* argv) {
00054 return(new SrcRtFilter());
00055 }
00056 } class_source_route_filter;
00057
00058 int SrcRtFilter::command(int argc, const char*const* argv) {
00059 if (argc == 2) {
00060 if (strcmp(argv[1], "start") == 0) {
00061 run();
00062 return (TCL_OK);
00063 }
00064 }
00065 return (DiffApp::command(argc, argv));
00066 }
00067 #endif // NS_DIFFUSION
00068
00069 void SrcRtFilterReceive::recv(Message *msg, handle h)
00070 {
00071 app_->recv(msg, h);
00072 }
00073
00074 void SrcRtFilter::recv(Message *msg, handle h)
00075 {
00076 Message *return_msg = NULL;
00077
00078 if (h != filter_handle_){
00079 DiffPrint(DEBUG_ALWAYS, "Error: Received a message for handle %ld when subscribing to handle %ld !\n", h, filter_handle_);
00080 return;
00081 }
00082
00083 return_msg = ProcessMessage(msg);
00084
00085 if (return_msg){
00086 ((DiffusionRouting *)dr_)->sendMessage(msg, h);
00087
00088 delete msg;
00089 }
00090 }
00091
00092 Message * SrcRtFilter::ProcessMessage(Message *msg)
00093 {
00094 char *original_route, *new_route, *p;
00095 int len;
00096 int32_t next_hop;
00097 NRSimpleAttribute<char *> *route = NULL;
00098
00099 route = SourceRouteAttr.find(msg->msg_attr_vec_);
00100 if (!route){
00101 DiffPrint(DEBUG_ALWAYS, "Error: Can't find the route attribute !\n");
00102 return msg;
00103 }
00104
00105 original_route = route->getVal();
00106 len = strlen(original_route);
00107
00108
00109 if (len == 0)
00110 return msg;
00111
00112
00113 next_hop = atoi(original_route);
00114
00115
00116 p = strstr(original_route, ":");
00117 if (!p){
00118
00119 new_route = new char[1];
00120 new_route[0] = '\0';
00121 }
00122 else{
00123 p++;
00124 len = strlen(p);
00125 new_route = new char[(len + 1)];
00126 strncpy(new_route, p, (len + 1));
00127 if (new_route[len] != '\0')
00128 DiffPrint(DEBUG_ALWAYS, "Warning: String must end with NULL !\n");
00129 }
00130
00131 route->setVal(new_route);
00132
00133
00134 delete [] new_route;
00135
00136
00137 msg->next_hop_ = next_hop;
00138 ((DiffusionRouting *)dr_)->sendMessage(msg, filter_handle_);
00139
00140 delete msg;
00141
00142 return NULL;
00143 }
00144
00145 handle SrcRtFilter::setupFilter()
00146 {
00147 NRAttrVec attrs;
00148 handle h;
00149
00150
00151 attrs.push_back(SourceRouteAttr.make(NRAttribute::EQ_ANY, ""));
00152
00153 h = ((DiffusionRouting *)dr_)->addFilter(&attrs, SRCRT_FILTER_PRIORITY,
00154 filter_callback_);
00155
00156 ClearAttrs(&attrs);
00157 return h;
00158 }
00159
00160 void SrcRtFilter::run()
00161 {
00162 #ifdef NS_DIFFUSION
00163 filter_handle_ = setupFilter();
00164 DiffPrint(DEBUG_ALWAYS, "SrcRtFilter filter received handle %ld\n",
00165 filter_handle_);
00166 DiffPrint(DEBUG_ALWAYS, "SrcRtFilter filter initialized !\n");
00167 #else
00168
00169 while (1){
00170 sleep(1000);
00171 }
00172 #endif // NS_DIFFUSION
00173 }
00174
00175 #ifdef NS_DIFFUSION
00176 SrcRtFilter::SrcRtFilter()
00177 {
00178 #else
00179 SrcRtFilter::SrcRtFilter(int argc, char **argv)
00180 {
00181 #endif // NS_DIFFUSION
00182
00183
00184 #ifndef NS_DIFFUSION
00185 parseCommandLine(argc, argv);
00186 dr_ = NR::createNR(diffusion_port_);
00187 #endif // !NS_DIFFUSION
00188
00189 filter_callback_ = new SrcRtFilterReceive(this);
00190
00191 #ifndef NS_DIFFUSION
00192
00193 filter_handle_ = setupFilter();
00194 DiffPrint(DEBUG_ALWAYS, "SrcRtFilter filter received handle %ld\n",
00195 filter_handle_);
00196 DiffPrint(DEBUG_ALWAYS, "SrcRtFilter filter initialized !\n");
00197 #endif // !NS_DIFFUSION
00198 }
00199
00200 #ifndef NS_DIFFUSION
00201 #ifndef USE_SINGLE_ADDRESS_SPACE
00202 int main(int argc, char **argv)
00203 {
00204 SrcRtFilter *app;
00205
00206
00207 app = new SrcRtFilter(argc, argv);
00208 app->run();
00209
00210 return 0;
00211 }
00212 #endif // !USE_SINGLE_ADDRESS_SPACE
00213 #endif // !NS_DIFFUSION