tag.cc

Go to the documentation of this file.
00001 //
00002 // tag.cc         : Tag Filter
00003 // author         : Fabio Silva
00004 //
00005 // Copyright (C) 2000-2002 by the Unversity of Southern California
00006 // $Id: tag.cc,v 1.2 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 "tag.hh"
00044 
00045 //TagFilter *app;
00046 
00047 #ifdef NS_DIFFUSION
00048 static class TagFilterClass : public TclClass {
00049 public:
00050   TagFilterClass() : TclClass("Application/DiffApp/TagFilter") {}
00051   TclObject * create(int argc, const char*const* argv) {
00052     return(new TagFilter());
00053   }
00054 } class_tag_filter;
00055 
00056 int TagFilter::command(int argc, const char*const* argv) {
00057   if (argc == 2) {
00058     if (strcmp(argv[1], "start") == 0) {
00059       run();
00060       return (TCL_OK);
00061     }
00062   }
00063   return (DiffApp::command(argc, argv));
00064 }
00065 #endif // NS_DIFFUSION
00066 
00067 void TagFilterReceive::recv(Message *msg, handle h)
00068 {
00069   app_->recv(msg, h);
00070 }
00071 
00072 void TagFilter::recv(Message *msg, handle h)
00073 {
00074   if (h != filter_handle_){
00075     DiffPrint(DEBUG_ALWAYS, "Error: TagFilter::recv received message for handle %ld when subscribing to handle %ld !\n", h, filter_handle_);
00076     return;
00077   }
00078 
00079   ProcessMessage(msg);
00080 
00081   ((DiffusionRouting *)dr_)->sendMessage(msg, h);
00082 }
00083 
00084 void TagFilter::ProcessMessage(Message *msg)
00085 {
00086   char *original_route;
00087   char *new_route;
00088   int len, total_len;
00089   NRSimpleAttribute<char *> *route = NULL;
00090 
00091   // Can't do anything if node id unknown
00092   if (!id_)
00093     return;
00094 
00095   route = RouteAttr.find(msg->msg_attr_vec_);
00096   if (!route){
00097     DiffPrint(DEBUG_ALWAYS, "Error: Can't find the route attribute !\n");
00098     return;
00099   }
00100 
00101   original_route = route->getVal();
00102   len = strlen(original_route);
00103 
00104   if (len == 0){
00105     total_len = strlen(id_);
00106     // Route is empty, need to allocate memory
00107     // for our id and the terminating '\0'
00108     new_route = new char[(total_len + 1)];
00109     strcpy(new_route, id_);
00110     if (new_route[total_len] != '\0')
00111       DiffPrint(DEBUG_ALWAYS, "Warning: String must end with NULL !\n");
00112   }
00113   else{
00114     // Route already exists. We need to allocate
00115     // memory for the current route + ':' + our
00116     // id + the terminating '\0'
00117     total_len = len + strlen(id_) + 1;
00118     new_route = new char[(total_len + 1)];
00119     strcpy(new_route, original_route);
00120     new_route[len] = ':';
00121     strcpy(&new_route[len+1], id_);
00122     if (new_route[total_len] != '\0'){
00123       DiffPrint(DEBUG_ALWAYS, "Warning: String must end with NULL !\n");
00124     }
00125   }
00126 
00127   // Debug
00128   DiffPrint(DEBUG_ALWAYS, "Tag Filter: Original route : %s\n", original_route);
00129   DiffPrint(DEBUG_ALWAYS, "Tag Filter: New route : %s\n", new_route);
00130 
00131   route->setVal(new_route);
00132 
00133   // Free memory
00134   delete [] new_route;
00135 }
00136 
00137 handle TagFilter::setupFilter()
00138 {
00139   NRAttrVec attrs;
00140   handle h;
00141 
00142   // Match all packets with a Route Attribute
00143   attrs.push_back(RouteAttr.make(NRAttribute::EQ_ANY, ""));
00144 
00145   h = ((DiffusionRouting *)dr_)->addFilter(&attrs, TAG_FILTER_PRIORITY,
00146                        filter_callback_);
00147 
00148   ClearAttrs(&attrs);
00149   return h;
00150 }
00151 
00152 void TagFilter::run()
00153 {
00154 #ifdef NS_DIFFUSION
00155   // Set up the filter
00156   filter_handle_ = setupFilter();
00157   DiffPrint(DEBUG_ALWAYS, "Tag Filter subscribed to *, received handle %d\n",
00158         (int) filter_handle_);
00159   DiffPrint(DEBUG_ALWAYS, "Tag Filter initialized !\n");
00160 #else
00161   // Doesn't do anything
00162   while (1){
00163     sleep(1000);
00164   }
00165 #endif // NS_DIFFUSION
00166 }
00167 
00168 void TagFilter::getNodeId()
00169 {
00170   DiffPrint(DEBUG_ALWAYS, "Tag Filter: getNodeID function not yet implemented !\n");
00171   DiffPrint(DEBUG_ALWAYS, "Tag Filter: Please set scadds_addr to the node id !\n");
00172   exit(-1);
00173   // Future implementation for the inter-module API
00174 }
00175 
00176 #ifdef NS_DIFFUSION
00177 TagFilter::TagFilter()
00178 #else
00179 TagFilter::TagFilter(int argc, char **argv)
00180 #endif // NS_DIFFUSION
00181 {
00182   char *id_env = NULL;
00183   char buffer[BUFFER_SIZE];
00184   int flag;
00185   int node_id;
00186 
00187   id_ = NULL;
00188   node_id = 0;
00189 
00190   // Create Diffusion Routing class
00191 #ifndef NS_DIFFUSION
00192   parseCommandLine(argc, argv);
00193   dr_ = NR::createNR(diffusion_port_);
00194 #endif // !NS_DIFFUSION
00195 
00196   filter_callback_ = new TagFilterReceive(this);
00197 
00198   // Try to figure out the node ID
00199   id_env = getenv("scadds_addr");
00200   if (id_env){
00201     node_id = atoi(id_env);
00202     flag = snprintf(&buffer[0], BUFFER_SIZE, "%d", node_id);
00203     if (flag == -1 || flag == BUFFER_SIZE){
00204       DiffPrint(DEBUG_ALWAYS, "Error: Buffer too small !\n");
00205       exit(-1);
00206     }
00207     id_ = strdup(&buffer[0]);
00208   }
00209   else{
00210     getNodeId();
00211   }
00212 #ifndef NS_DIFFUSION
00213   // Set up the filter
00214   filter_handle_ = setupFilter();
00215   DiffPrint(DEBUG_ALWAYS, "Tag Filter subscribed to *, received handle %ld\n",
00216       filter_handle_);
00217   DiffPrint(DEBUG_ALWAYS, "Tag Filter initialized !\n");
00218 #endif // !NS_DIFFUSION
00219 }
00220 
00221 #ifndef USE_SINGLE_ADDRESS_SPACE
00222 int main(int argc, char **argv)
00223 {
00224   TagFilter *app;
00225 
00226   // Initialize and run the Tag Filter
00227   app = new TagFilter(argc, argv);
00228   app->run();
00229 
00230   return 0;
00231 }
00232 #endif // !USE_SINGLE_ADDRESS_SPACE

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