00001
00002 #ifndef lint
00003 static const char rcsid[] =
00004 "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/link/hackloss.cc,v 1.6 2000/09/01 03:04:05 haoboy Exp $";
00005 #endif
00006
00007 #include "connector.h"
00008
00009 #include "packet.h"
00010 #include "queue.h"
00011
00012 class HackLossyLink : public Connector {
00013 public:
00014 HackLossyLink() : down_(0), src_(0), dst_(0), fid_(0), ctr_(0), nth_(0){
00015 }
00016 protected:
00017 int command(int argc, const char*const* argv);
00018 void recv(Packet* p, Handler* h);
00019 NsObject* down_;
00020 int src_, dst_, fid_;
00021 int ctr_, nth_;
00022 };
00023
00024 static class HackLossyLinkClass : public TclClass {
00025 public:
00026 HackLossyLinkClass() : TclClass("HackLossyLink") {}
00027 TclObject* create(int, const char*const*) {
00028 return (new HackLossyLink);
00029 }
00030 } class_dynamic_link;
00031
00032
00033 int HackLossyLink::command(int argc, const char*const* argv)
00034 {
00035 if (strcmp(argv[1], "down-target") == 0) {
00036 NsObject* p = (NsObject*)TclObject::lookup(argv[2]);
00037 if (p == 0) {
00038 Tcl::instance().resultf("no object %s", argv[2]);
00039 return TCL_ERROR;
00040 }
00041 down_ = p;
00042 return TCL_OK;
00043 }
00044 if (strcmp(argv[1], "show-params") == 0) {
00045 Tcl::instance().resultf("src_ = %d, dst_ = %d, fid_ = %d, nth_ = %d",
00046 src_, dst_, fid_, nth_);
00047 return TCL_OK;
00048 }
00049 if (strcmp(argv[1], "set-params") == 0) {
00050 src_ = atoi(argv[2]);
00051 dst_ = atoi(argv[3]);
00052 fid_ = atoi(argv[4]);
00053 return TCL_OK;
00054 }
00055 if (strcmp(argv[1], "nth") == 0) {
00056 nth_ = atoi(argv[2]);
00057 return TCL_OK;
00058 }
00059 return Connector::command(argc, argv);
00060 }
00061
00062 void HackLossyLink::recv(Packet* p, Handler* h)
00063 {
00064 hdr_ip* iph = hdr_ip::access(p);
00065 if (nth_ && (iph->flowid() == fid_) &&
00066 (iph->saddr() == src_) && (iph->daddr() == dst_) &&
00067 ((++ctr_ % nth_) == 0))
00068 down_->recv(p);
00069 else
00070 target_->recv(p, h);
00071 }