mvirusextended.nc

来自「tinyos最新版」· NC 代码 · 共 545 行 · 第 1/2 页

NC
545
字号
	return FAIL;      }      else {#ifdef PLATFORM_PC	char timeVal[128];	printTime(timeVal, 128);	dbg(DBG_USR3, "Sending capsule %i @ %s\n", (int)idx, timeVal);#endif	    	return SUCCESS;      }    }  }  result_t sendBCastCapsule(uint8_t idx) {    MateCapsule* capsule = capsules[idx];    MateCapsuleMsg* msg = (MateCapsuleMsg*)sendPtr->data;    if (sendBusy) {return FAIL;}    else {      // Fill in random bits to packet      sendBusy = TRUE;      // Fill in capsule      nmemcpy(&msg->capsule, capsule, sizeof(MateCapsule));            if (!call BCastSend.send(TOS_BCAST_ADDR, sizeof(MateCapsuleMsg), sendPtr)) {	sendBusy = FALSE;	return FAIL;      }      else {#ifdef PLATFORM_PC	char timeVal[128];	printTime(timeVal, 128);	printf("%i: Broadcasting capsule %i @ %s\n", (int)TOS_LOCAL_ADDRESS, (int)idx, timeVal);#else	dbg(DBG_USR3, "Sending capsule %i\n", (int)idx);#endif	    	return SUCCESS;      }    }  }    result_t sendVersionPacket() {    int i;    MateVersionMsg* msg = (MateVersionMsg*)sendPtr->data;    dbg(DBG_USR3, "Sending version vector packet:\n  ");    if (sendBusy) {return FAIL;}    sendBusy = TRUE;    msg->type = MATE_VERSION_VECTOR;    for (i = 0; i < MATE_CAPSULE_NUM; i++) {      if (capsules[i] != NULL &&	  (capsules[i]->type & MATE_OPTION_FORWARD)) {	msg->versions[i] = capsules[i]->version;      }      else {	msg->versions[i] = 0;      }      dbg_clear(DBG_USR3, "%08x ", msg->versions[i]);    }    dbg_clear(DBG_USR3, "\n");    if (!call VersionSend.send(TOS_BCAST_ADDR, sizeof(MateVersionMsg), sendPtr)) {      dbg(DBG_USR3|DBG_ERROR, "MVirus: Version vector send failed\n");      sendBusy = FALSE;      return FAIL;    }    else {      return SUCCESS;    }  }  task void versionTimerTask() {    versionCounter++;    if (versionCounter >= tau) {      tau *= 2;      if (tau > TAU_MAX) {	tau = TAU_MAX;      }      newVersionCounter();    }    else if (versionCounter == versionThreshold) {#ifdef PLATFORM_PC      char timeBuf[128];      printTime(timeBuf, 128);      dbg(DBG_USR3, "MVirus: Version timer counter expired (hrd: %i, thr: %i, cancel: %i): %s\n",(int)versionHeard, (int)versionThreshold, (int)versionCancelled, timeBuf);#endif      if (!versionCancelled) {	dbg(DBG_USR3, "MVirus: Sending version packet\n");	sendVersionPacket();	versionCancelled = 1;      }    }    else {      // do nothing    }  }  event result_t VersionTimer.fired() {    if (state == BVIRUS_PULLING) {      post versionTimerTask();    }    return SUCCESS;  }  TOS_MsgPtr receiveProgram(TOS_MsgPtr msg) {    dbg(DBG_USR3, "MVirus: Received program vector.\n");    return msg;  }    TOS_MsgPtr receiveVector(TOS_MsgPtr msg) {    uint8_t i;    bool same = TRUE; // Is this vector the same as mine?    MateVersionMsg* versions = (MateVersionMsg*)msg->data;    //dbg(DBG_USR3, "MVirus: Received version vector.\n");        for (i = 0; i < MATE_CAPSULE_NUM; i++) {      if (capsules[i] != NULL) {	if(versions->versions[i] > capsules[i]->version) {	  // I have something older, send my version vector	  // ASAP so he'll send capsules	  dbg(DBG_USR3, "MVirus: heard newer version vector\n");	  tau = TAU_INIT;	  newVersionCounter();	  same = FALSE;	  break;	}	else if (versions->versions[i] < capsules[i]->version) {	  // I have something newer, mark capsule to send	  // if not already doing so.	  dbg(DBG_USR3, "MVirus: heard older version vector, send out capsule.\n");	  capsuleTimerThresholds[i] = BVIRUS_CAPSULE_INIT;	  capsuleTimerCounters[i] = 0;	  if (state != BVIRUS_PUSHING) {	    call CapsuleTimer.start(TIMER_REPEAT, BVIRUS_TIMER_CAPSULE);	    state = BVIRUS_PUSHING;	  }	  same = FALSE;	  break;	}      }    }    if (same == TRUE) {      dbg(DBG_USR3, "MVirus: Heard same version vector as mine.\n");      versionHeard++;      if (versionHeard >= BVIRUS_VERSION_HEARD_THRESHOLD) {	cancelVersionCounter();      }    }    return msg;  }  event TOS_MsgPtr VersionReceive.receive(TOS_MsgPtr msg) {    MateVersionMsg* versions = (MateVersionMsg*)msg->data;    dbg(DBG_USR3, "Received version packet, type %i\n", versions->type);    if (versions->type == MATE_VERSION_VECTOR) {      return receiveVector(msg);    }    else if (versions->type == MATE_VERSION_PROGRAM) {      return receiveProgram(msg);    }    else {      return msg;    }  }  // Increment all of the timers. Try to send (only) the first capsule  // with an expired timer. If the send request succeeds, decay the  // timer.  task void capsuleTimerTask() {    uint8_t i;    bool halt = TRUE;    dbg(DBG_USR3, "MVirus: Capsule task running.\n");    for (i = 0; i < MATE_CAPSULE_NUM; i++) {      capsuleTimerCounters[i]++;    }    for (i = 0; i < MATE_CAPSULE_NUM; i++) {      if (capsuleTimerThresholds[i] <= BVIRUS_CAPSULE_MAX) {	halt = FALSE;	if (capsuleTimerCounters[i] >= capsuleTimerThresholds[i]) {	  if (sendCapsule(i)) {	    capsuleTimerThresholds[i] <<= 2;	    capsuleTimerCounters[i] = 0;	  }	  break;	}      }    }    if (halt) {      call CapsuleTimer.stop();      state = BVIRUS_PULLING;    }    return;  }    event result_t CapsuleTimer.fired() {    post capsuleTimerTask();    return SUCCESS;  }  event result_t InterceptRouted.intercept(TOS_MsgPtr msg,					   void* payload,					   uint16_t payloadLen) {    MateCapsuleMsg* cMsg = (MateCapsuleMsg*)payload;    MateCapsule* capsule = &(cMsg->capsule);    if (msg->type != 66) {return FAIL;}    //dbg(DBG_TEMP, "Received intercepted capsule.\n");    receiveCapsule(capsule, payloadLen, "intercepted", 3);    return SUCCESS;  }    event TOS_MsgPtr ReceiveRouted.receive(TOS_MsgPtr msg,					 void* payload,					 uint16_t payloadLen) {    MateCapsuleMsg* cMsg = (MateCapsuleMsg*)payload;    MateCapsule* capsule = &(cMsg->capsule);    //dbg(DBG_TEMP, "Received routed capsule.\n");    receiveCapsule(capsule, payloadLen, "routed", 1);    return msg;  }    event TOS_MsgPtr CapsuleReceive.receive(TOS_MsgPtr msg) {    MateCapsuleMsg* cMsg = (MateCapsuleMsg*)msg->data;    MateCapsule* capsule = &(cMsg->capsule);    //dbg(DBG_TEMP, "Received normal capsule.\n");    receiveCapsule(capsule, sizeof(MateCapsule), "trickle", 1);    return msg;  }  event TOS_MsgPtr BCastReceive.receive(TOS_MsgPtr msg) {    MateCapsuleMsg* cMsg = (MateCapsuleMsg*)msg->data;    MateCapsule* capsule = &(cMsg->capsule);    //    dbg(DBG_TEMP, "Received broadcast capsule.\n");    if (receiveCapsule(capsule, sizeof(MateCapsule), "bcast", 1) == SUCCESS) {      amBCasting = TRUE;      bCastIdx = typeToIndex(capsule->type);      call BCastTimer.start(TIMER_ONE_SHOT, 10 + call Random.rand() % 90);    }    else if (amBCasting) {      call BCastTimer.stop();      call BCastTimer.start(TIMER_ONE_SHOT, 10 + call Random.rand() % 90);    }    return msg;  }  event result_t BCastTimer.fired() {    sendBCastCapsule(bCastIdx);    amBCasting = FALSE;    return SUCCESS;  }    event result_t CapsuleSend.sendDone(TOS_MsgPtr msg, result_t success) {    if (msg == sendPtr) {      sendBusy = FALSE;      //dbg(DBG_USR3, "Capsule send done event handled.\n");    }    return SUCCESS;  }  event result_t VersionSend.sendDone(TOS_MsgPtr msg, result_t success) {    if (msg == sendPtr) {      sendBusy = FALSE;      //dbg(DBG_USR3, "Version send done event handled.\n");    }    return SUCCESS;  }  event result_t BCastSend.sendDone(TOS_MsgPtr msg, result_t success) {    if (msg == sendPtr) {      sendBusy = FALSE;    }    return SUCCESS;  }}

⌨️ 快捷键说明

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