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

srcrt.cc

Go to the documentation of this file.
00001 //
00002 // srcrt.cc       : Source Route Filter
00003 // author         : Fabio Silva
00004 //
00005 // Copyright (C) 2000-2002 by the Unversity of Southern California
00006 // $Id: srcrt.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 "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   // Check if we are the last hop
00090   if (len == 0)
00091     return msg;
00092 
00093   // Get the next hop
00094   next_hop = atoi(original_route);
00095 
00096   // Remove last hop from source route
00097   p = strstr(original_route, ":");
00098   if (!p){
00099     // There's just one more hop
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   // Free memory
00115   delete [] new_route;
00116 
00117   // Send the packet to the next hop
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   // Match all packets with a SourceRoute Attribute
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   // Doesn't do anything
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   // Create Diffusion Routing class
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   // Set up the filter
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   // Initialize and run the Source Route Filter
00188   app = new SrcRtFilter(argc, argv);
00189   app->run();
00190 
00191   return 0;
00192 }
00193 #endif // !USE_SINGLE_ADDRESS_SPACE

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