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

classifier-virtual.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) 1996-1997 Regents of the University of California.
00004  * All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *      This product includes software developed by the MASH Research
00017  *      Group at the University of California Berkeley.
00018  * 4. Neither the name of the University nor of the Research Group may be
00019  *    used to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  * 
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  */
00034 
00035 #ifndef lint
00036 static const char rcsid[] =
00037     "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/classifier/classifier-virtual.cc,v 1.12 2001/12/20 00:15:33 haldar Exp $";
00038 #endif
00039 
00040 extern "C" {
00041 #include <tcl.h>
00042 }
00043 #include "config.h"
00044 #include "packet.h"
00045 #include "ip.h"
00046 #include "classifier.h"
00047 #include "route.h"
00048 #include "object.h"
00049 #include "address.h"
00050 
00051 class VirtualClassifier : public Classifier {
00052 public:
00053         VirtualClassifier() : routelogic_(0) {
00054                 Tcl_InitHashTable(&ht_, TCL_ONE_WORD_KEYS);
00055         }
00056         ~VirtualClassifier() {
00057                 Tcl_DeleteHashTable(&ht_);
00058         }
00059         virtual void do_install(char *dst, NsObject *target) { }
00060 protected:
00061         NsObject* next_;
00062         Tcl_HashTable ht_;
00063         RouteLogic *routelogic_;
00064         NsObject* target_;
00065         bool enableHrouting_;
00066         char nodeaddr_[SMALL_LEN];
00067 
00068         int classify(Packet *const p) {
00069                 hdr_ip* iph = hdr_ip::access(p);
00070                 return mshift(iph->daddr());
00071         }
00072         
00073         void recv(Packet* p, Handler* h) {
00074                 if (!routelogic_) {
00075                         Tcl &tcl = Tcl::instance();
00076                         tcl.evalc("[Simulator instance] get-routelogic");
00077                         routelogic_= (RouteLogic*) TclObject::lookup(tcl.result());
00078                         //tcl.evalf("%s info class", tcl.result());
00079                 }
00080                 /* first we find the next hop by asking routelogic
00081                  * then we use a hash next_hop -> target_object
00082                  * thus, the size of the table is at most N-1
00083                  */
00084                 Tcl &tcl = Tcl::instance();
00085                 hdr_ip* iph = hdr_ip::access(p);
00086                 char* adst= Address::instance().print_nodeaddr(iph->daddr());
00087                 //adst[strlen(adst)-1]= 0;
00088                 target_= 0;
00089 
00090                 int next_hopIP;
00091                 routelogic_->lookup_flat(nodeaddr_, adst, next_hopIP);
00092                 delete [] adst;
00093 
00094                 int newEntry;
00095                 Tcl_HashEntry *ep= Tcl_CreateHashEntry(&ht_, (const char*)next_hopIP, 
00096                                                        &newEntry); 
00097                 if (newEntry) {
00098                         tcl.evalf("%s find %d", name(), next_hopIP);
00099                         Tcl_SetHashValue(ep, target_= (NsObject*)tcl.lookup(tcl.result()));
00100                 } else {
00101                         target_= (NsObject*)Tcl_GetHashValue(ep);
00102                 }
00103                 
00104                 if (!target_) {
00105                         /*
00106                          * XXX this should be "dropped" somehow.  Right now,
00107                          * these events aren't traced.
00108                          */
00109                         Packet::free(p);
00110                         return;
00111                 }
00112                 target_->recv(p,h);
00113         }
00114         int command(int argc, const char*const* argv) {
00115                 if (argc == 3) {
00116                         if (strcmp(argv[1], "nodeaddr") == 0) {
00117                                 strcpy(nodeaddr_, argv[2]);
00118                                 return(TCL_OK);
00119                         }
00120                 }
00121                 return (NsObject::command(argc, argv));
00122         }
00123 };
00124 
00125 static class VirtualClassifierClass : public TclClass {
00126 public:
00127         VirtualClassifierClass() : TclClass("Classifier/Virtual") {}
00128         TclObject* create(int, const char*const*) {
00129                 return (new VirtualClassifier());
00130         }
00131 } class_virtual_classifier;
00132 

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