⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swarm.cc

📁 采用蚂蚁算法实现ad hoc网络路由协议,平台为ns2
💻 CC
字号:
#include <swarm/swarm.h>#include <swarm/swarm_rtable.h>#include <swarm/swarm_packet.h>#include <cmu-trace.h>#define min(a, b)  (((a) < (b)) ? (a) : (b))#define max(a, b)  (((a) > (b)) ? (a) : (b))SWARM::SWARM(nsaddr_t id):Agent(PT_SWARM){    env.networkId = id;    env.agent = this;    logtarget = 0;}static voidswarm_rt_failed_callback(Packet * p, void *arg){    ((SWARM *) arg)->rt_ll_failed(p);}voidSWARM::rt_ll_failed(Packet * p){    hdr_swarm *swarmPacket = HDR_SWARM(p);    struct hdr_cmn *ch = HDR_CMN(p);    sprintf(logtarget->pt_->buffer(),            "SWARM %.9f _%d_ MAC send of %d to %d failed due to %s (stability %f, god hops = %d)",            CURRENT_TIME,            env.networkId,            ch->uid(),            ch->next_hop_,            ch->xmit_reason_ == XMIT_REASON_RTS ? "RTS" : "ACK",            env.neighbours[ch->next_hop_].getProbabilitySuccessfulUnicast(),            God::instance()->hops(env.networkId, ch->next_hop_));    logtarget->pt_->dump();    swarmPacket->onFailedUnicast(env, p);}voidSWARM::log(string & s){    sprintf(logtarget->pt_->buffer(),            "SWARM %.9f _%d_ %s", CURRENT_TIME, env.networkId, s.data());    logtarget->pt_->dump();}voidSWARM::tap(const Packet * p){    struct hdr_cmn *ch = HDR_CMN(p);    if (ch->ptype() == PT_SWARM)    {        hdr_swarm *swarmPacket = HDR_SWARM(p);        swarmPacket->onOverheard(env, p);    }}voidSWARM::recv(Packet * p, Handler *){    assert(initialized());    struct hdr_cmn *ch = HDR_CMN(p);    struct hdr_swarm *swarmPacket = HDR_SWARM(p);    if (ch->ptype() == PT_SWARM)    {        struct hdr_ip *ih = HDR_IP(p);        assert(ih->sport() == RT_PORT);        assert(ih->dport() == RT_PORT);	if(ch->next_hop_ == env.networkId)	{	    swarmPacket->onReceivedUnicast(env, p);	}	else	{	    swarmPacket->onReceivedBroadcast(env, p);	}    }    else    {        swarmPacket->onReceiveFromHigherLevel(env, p);    }}voidSWARM::sendPacket(Packet * p, double delay){    struct hdr_cmn *ch = HDR_CMN(p);    ch->xmit_failure_ = swarm_rt_failed_callback;    ch->xmit_failure_data_ = (void *) this;    Scheduler::instance().schedule(target_, p, delay);}voidSWARM::receivePacketLocally(Packet * p){    target_->recv(p, (Handler *) 0);}/*  TCL Hooks*/int    hdr_swarm::offset_;static    class    SWARMHeaderClass:    public    PacketHeaderClass{  public:    SWARMHeaderClass():    PacketHeaderClass("PacketHeader/SWARM", sizeof(hdr_all_swarm))    {        bind_offset(&hdr_swarm::offset_);    }}class_rtProtoSWARM_hdr;double SWARM::COST_MAX = -1000;double SWARM::DECAY_PER_SECOND = 1.01;double SWARM::BROADCAST_RETRANSMIT_JITTER = 0.001;double SWARM::UNICAST_RETRANSMIT_JITTER = 0;double SWARM::PROBABILITY_HISTORY=20;int SWARM::PROBABILITY_SAMPLES=50;double SWARM::CONFIDENCE_FROM_RECEIVE=0.5;double SWARM::RECEIVE_SIGNIFICANCE=0.2;double SWARM::MAX_PACKET_RECEIVE_RATE=1000;double SWARM::CONGESTION_PROBABILITY_EXPONENTIAL=0.4;double SWARM::CONGESTION_PROBABILITY_WEIGHT=0.4;double SWARM::IMPROVEMENT_FOR_RESEND = 1.0;double SWARM::UNICAST_SUCCESS_REWARD = -1;double SWARM::UNICAST_FAIL_REWARD = -3;double SWARM::BROADCAST_REWARD = -4;double SWARM::MINIMUM_REWARD = 0.5;double SWARM::TEMPERATURE = 1;unsigned int SWARM::NUMBER_OF_SEQNOS_TO_STORE = 20;unsigned int SWARM::MAXIMUM_RECEIVES_WITHOUT_SEND = 2;double SWARM::HOLDOFF_FOR_PROACTIVE_SEND = 0.01;double SWARM::ROUTE_TIMEOUT = 10;bool SWARM::DEBUG = true;static    class    SWARMclass:    public    TclClass{  public:    SWARMclass():    TclClass("Agent/SWARM")    {    }    TclObject *    create(int argc, const char *const *argv)    {        assert(argc == 5);        //return (new SWARM((nsaddr_t) atoi(argv[4])));        return (new SWARM((nsaddr_t) Address::instance().str2addr(argv[4])));    }    void    bind()    {        TclClass::bind();        add_method("dump");        add_method("costMax");	add_method("decayPerSecond");	add_method("unicastRetransmitJitter");	add_method("broadcastRetransmitJitter");	add_method("probabilityHistory");	add_method("probabilitySamples");	add_method("confidenceFromReceive");	add_method("receiveSignificance");	add_method("maxPacketReceiveRate");	add_method("congestionProbabilityExponential");	add_method("congestionProbabilityWeight");	add_method("improvementForResend");	add_method("unicastSuccessReward");	add_method("unicastFailReward");	add_method("minimumReward");	add_method("broadcastReward");	add_method("temperature");	add_method("seqnoMemory");	add_method("maxReceivesWithoutSend");	add_method("holdoffForProactiveSend");	add_method("routeTimeout");    }    int    method(int ac, const char *const *av)    {        Tcl & tcl = Tcl::instance();        int            argc =            ac -            2;        const char *const *            argv =            av +            2;        if (argc == 2)        {            if (strncasecmp(argv[1], "dump", 2) == 0)            {                stringstream                    o;                o << "Agent/SWARM version \"" << swarmversion << "\"" << endl;                o << "Agent/SWARM costMax \"" << SWARM::COST_MAX << "\"" << endl;                o << "Agent/SWARM decayPerSecond " << SWARM::DECAY_PER_SECOND << endl;                o << "Agent/SWARM unicastRetransmitJitter " << SWARM::UNICAST_RETRANSMIT_JITTER << endl;                o << "Agent/SWARM broadcastRetransmitJitter " << SWARM::BROADCAST_RETRANSMIT_JITTER << endl;                o << "Agent/SWARM probabilityHistory " << SWARM::PROBABILITY_HISTORY << endl;                o << "Agent/SWARM probabilitySamples " << SWARM::PROBABILITY_SAMPLES << endl;                o << "Agent/SWARM confidenceFromReceive " << SWARM::CONFIDENCE_FROM_RECEIVE << endl;                o << "Agent/SWARM receiveSignificance " << SWARM::RECEIVE_SIGNIFICANCE << endl;                o << "Agent/SWARM maxPacketReceiveRate " << SWARM::MAX_PACKET_RECEIVE_RATE << endl;                o << "Agent/SWARM congestionProbabilityExponential " << SWARM::CONGESTION_PROBABILITY_EXPONENTIAL << endl;                o << "Agent/SWARM congestionProbabilityWeight " << SWARM::CONGESTION_PROBABILITY_WEIGHT << endl;                o << "Agent/SWARM unicastSuccessReward " << SWARM::UNICAST_SUCCESS_REWARD << endl;                o << "Agent/SWARM unicastFailReward " << SWARM::UNICAST_FAIL_REWARD << endl;                o << "Agent/SWARM minimumReward " << SWARM::MINIMUM_REWARD << endl;                o << "Agent/SWARM broadcastReward " << SWARM::BROADCAST_REWARD << endl;                o << "Agent/SWARM temperature " << SWARM::TEMPERATURE << endl;                o << "Agent/SWARM seqnoMemory " << SWARM::NUMBER_OF_SEQNOS_TO_STORE <<  endl;                o << "Agent/SWARM maxReceivesWithoutSend " << SWARM::MAXIMUM_RECEIVES_WITHOUT_SEND <<  endl;                o << "Agent/SWARM holdoffForProactiveSend " << SWARM::HOLDOFF_FOR_PROACTIVE_SEND <<  endl;                o << "Agent/SWARM routeTimeout " << SWARM::ROUTE_TIMEOUT <<  endl;                tcl.resultf("%s", o.str().data());                return TCL_OK;            }        }        if (argc == 3)        {            if (strcmp(argv[1], "costMax") == 0)            {		SWARM::COST_MAX = atof(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "decayPerSecond") == 0)            {		SWARM::DECAY_PER_SECOND = atof(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "broadcastRetransmitJitter") == 0)            {		SWARM::BROADCAST_RETRANSMIT_JITTER = atof(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "unicastRetransmitJitter") == 0)            {		SWARM::UNICAST_RETRANSMIT_JITTER = atof(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "probabilityHistory") == 0)            {		SWARM::PROBABILITY_HISTORY = atof(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "probabilitySamples") == 0)            {		SWARM::PROBABILITY_SAMPLES = atoi(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "confidenceFromReceive") == 0)            {		SWARM::CONFIDENCE_FROM_RECEIVE = atof(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "receiveSignificance") == 0)            {		SWARM::RECEIVE_SIGNIFICANCE = atof(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "maxPacketReceiveRate") == 0)            {		SWARM::MAX_PACKET_RECEIVE_RATE = atof(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "congestionProbabilityExponential") == 0)            {		SWARM::CONGESTION_PROBABILITY_EXPONENTIAL = atof(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "congestionProbabilityWeight") == 0)            {		SWARM::CONGESTION_PROBABILITY_WEIGHT = atof(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "improvementForResend") == 0)            {		SWARM::IMPROVEMENT_FOR_RESEND = atof(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "unicastSuccessReward") == 0)            {		SWARM::UNICAST_SUCCESS_REWARD = atof(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "unicastFailReward") == 0)            {		SWARM::UNICAST_FAIL_REWARD = atof(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "temperature") == 0)            {		SWARM::TEMPERATURE = atof(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "minimumReward") == 0)            {		SWARM::MINIMUM_REWARD = atof(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "broadcastReward") == 0)            {		SWARM::BROADCAST_REWARD = atof(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "seqnoMemory") == 0)            {		SWARM::NUMBER_OF_SEQNOS_TO_STORE = atoi(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "maxReceivesWithoutSend") == 0)            {		SWARM::MAXIMUM_RECEIVES_WITHOUT_SEND = atoi(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "holdoffForProactiveSend") == 0)            {		SWARM::HOLDOFF_FOR_PROACTIVE_SEND = atof(argv[2]);                return (TCL_OK);            }            if (strcmp(argv[1], "routeTimeout") == 0)            {		SWARM::ROUTE_TIMEOUT = atof(argv[2]);                return (TCL_OK);            }        }        return TclClass::method(ac, av);    }}class_rtProtoSWARM;intSWARM::command(int argc, const char *const *argv){    if (argc == 2)    {        Tcl & tcl = Tcl::instance();        if (strncasecmp(argv[1], "id", 2) == 0)        {            tcl.resultf("%d", index);            return TCL_OK;        }        if (strncasecmp(argv[1], "start", 2) == 0)        {            return TCL_OK;        }    }    else if (argc == 3)    {        if (strcmp(argv[1], "index") == 0)        {            env.networkId = atoi(argv[2]);            return TCL_OK;        }        else if (strcmp(argv[1], "log-target") == 0                 || strcmp(argv[1], "tracetarget") == 0)        {            logtarget = (Trace *) TclObject::lookup(argv[2]);            if (logtarget == 0)                return TCL_ERROR;            return TCL_OK;        }        else if (strcmp(argv[1], "if-queue") == 0)        {            ifqueue = (PriQueue *) TclObject::lookup(argv[2]);            if (ifqueue == 0)                return TCL_ERROR;            return TCL_OK;        }        else if (strcasecmp(argv[1], "install-tap") == 0)        {            mac_ = (Mac *) TclObject::lookup(argv[2]);            mac_->installTap(this);            return TCL_OK;        }	else if (strncasecmp(argv[1], "dump", 2) == 0)        {	    nsaddr_t dest = (nsaddr_t) atoi(argv[2]);            env.dump(dest);            return TCL_OK;        }    }    return Agent::command(argc, argv);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -