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.4 2005/09/13 04:53:48 tomh 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 // Linking this file statically or dynamically with other modules is making
00022 // a combined work based on this file.  Thus, the terms and conditions of
00023 // the GNU General Public License cover the whole combination.
00024 //
00025 // In addition, as a special exception, the copyright holders of this file
00026 // give you permission to combine this file with free software programs or
00027 // libraries that are released under the GNU LGPL and with code included in
00028 // the standard release of ns-2 under the Apache 2.0 license or under
00029 // otherwise-compatible licenses with advertising requirements (or modified
00030 // versions of such code, with unchanged license).  You may copy and
00031 // distribute such a system following the terms of the GNU GPL for this
00032 // file and the licenses of the other code concerned, provided that you
00033 // include the source code of that other code when and as the GNU GPL
00034 // requires distribution of source code.
00035 //
00036 // Note that people who make modified versions of this file are not
00037 // obligated to grant this special exception for their modified versions;
00038 // it is their choice whether to do so.  The GNU General Public License
00039 // gives permission to release a modified version without this exception;
00040 // this exception also makes it possible to release a modified version
00041 // which carries forward this exception.
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   // Check if we are the last hop
00109   if (len == 0)
00110     return msg;
00111 
00112   // Get the next hop
00113   next_hop = atoi(original_route);
00114 
00115   // Remove last hop from source route
00116   p = strstr(original_route, ":");
00117   if (!p){
00118     // There's just one more hop
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   // Free memory
00134   delete [] new_route;
00135 
00136   // Send the packet to the next hop
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   // Match all packets with a SourceRoute Attribute
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   // Doesn't do anything
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   // Create Diffusion Routing class
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   // Set up the filter
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   // Initialize and run the Source Route Filter
00207   app = new SrcRtFilter(argc, argv);
00208   app->run();
00209 
00210   return 0;
00211 }
00212 #endif // !USE_SINGLE_ADDRESS_SPACE
00213 #endif // !NS_DIFFUSION

Generated on Tue Mar 6 16:47:51 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6