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 #ifndef lint
00036 static const char rcsid[] =
00037 "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/classifier/classifier-virtual.cc,v 1.13 2005/09/18 23:33:31 tomh 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
00079 }
00080
00081
00082
00083
00084 Tcl &tcl = Tcl::instance();
00085 hdr_ip* iph = hdr_ip::access(p);
00086 char* adst= Address::instance().print_nodeaddr(iph->daddr());
00087
00088 target_= 0;
00089
00090 int next_hopIP;
00091 routelogic_->lookup_flat(nodeaddr_, adst, next_hopIP);
00092 delete [] adst;
00093
00094 int newEntry;
00095 long key = next_hopIP;
00096 Tcl_HashEntry *ep= Tcl_CreateHashEntry(&ht_, (const char*)key,
00097 &newEntry);
00098 if (newEntry) {
00099 tcl.evalf("%s find %d", name(), next_hopIP);
00100 Tcl_SetHashValue(ep, target_= (NsObject*)tcl.lookup(tcl.result()));
00101 } else {
00102 target_= (NsObject*)Tcl_GetHashValue(ep);
00103 }
00104
00105 if (!target_) {
00106
00107
00108
00109
00110 Packet::free(p);
00111 return;
00112 }
00113 target_->recv(p,h);
00114 }
00115 int command(int argc, const char*const* argv) {
00116 if (argc == 3) {
00117 if (strcmp(argv[1], "nodeaddr") == 0) {
00118 strcpy(nodeaddr_, argv[2]);
00119 return(TCL_OK);
00120 }
00121 }
00122 return (NsObject::command(argc, argv));
00123 }
00124 };
00125
00126 static class VirtualClassifierClass : public TclClass {
00127 public:
00128 VirtualClassifierClass() : TclClass("Classifier/Virtual") {}
00129 TclObject* create(int, const char*const*) {
00130 return (new VirtualClassifier());
00131 }
00132 } class_virtual_classifier;
00133