📄 mc-agent.cc
字号:
#ifdef LOG_APP_MESH_RT printf ("[mesh-rt-recv] at %8.4f < ag %d nd %d > got-updates-from < ag %d nd %d > begin\n", Scheduler::Clock(), id, n->id, mm->ainfo.agent_id, mm->ainfo.node_id); #endif // LOG_APP_MESH_RT /* for (int i = 0; i < MAXMESHMEMBERS; i++) if ((ap->u.meshrt_p.valid[i] == true) && (ap->u.meshrt_p.pathlen[i] > 1)) free(ap->u.meshrt_p.path[i]); delete[] ap->u.meshrt_p.path; delete[] ap->u.meshrt_p.valid; delete[] ap->u.meshrt_p.pathlen; delete[] ap->u.meshrt_p.cost; */ return; } else if (mm->is_neighbor == false) { #ifdef LOG_APP_MESH_RT printf ("[mesh-rt-recv] at %8.4f < ag %d nd %d > got-updates-from < ag %d nd %d > begin\n", Scheduler::Clock(), id, n->id, mm->ainfo.agent_id, mm->ainfo.node_id); #endif // LOG_APP_MESH_RT /* for (int i = 0; i < MAXMESHMEMBERS; i++) if ((ap->u.meshrt_p.valid[i] == true) && (ap->u.meshrt_p.pathlen[i] > 1)) free(ap->u.meshrt_p.path[i]); delete[] ap->u.meshrt_p.path; delete[] ap->u.meshrt_p.valid; delete[] ap->u.meshrt_p.pathlen; delete[] ap->u.meshrt_p.cost; */ return; } #ifdef LOG_APP_MESH_RT { printf ("[mesh-rt-recv] at %8.4f < ag %d nd %d > got-updates-from < ag %d nd %d > begin\n", Scheduler::Clock(), id, n->id, mm->ainfo.agent_id, mm->ainfo.node_id); for (int i = 0; i < MAXMESHMEMBERS; i++) { if (ap->u.meshrt_p.valid[i] == true) { printf("[route-pkt] to < ag %d > of len %d and cost %8.2f\n",i,ap->u.meshrt_p.pathlen[i],ap->u.meshrt_p.cost[i]); } } } #endif // LOG_APP_MESH_RT double neighborcost = meshrt.cost[ap->src_agent]; bool throughme; for (int i = 0; i < MAXMESHMEMBERS; i++) { if ((meshrt.valid[i]) && (meshrt.pathlen[i] > 1) && (meshrt.path[i][0].agent_id == ap->src_agent)) { free(meshrt.path[i]); meshrt.path[i] = NULL; meshrt.valid[i] = false; if (ap->u.meshrt_p.valid[i]) { throughme = false; for (int j = 0; j < ap->u.meshrt_p.pathlen[i] - 1; j++) { if (ap->u.meshrt_p.path[i][j].agent_id == id) { throughme = true; break; } } if (!throughme) { meshrt.valid[i] = true; meshrt.cost[i] = ap->u.meshrt_p.cost[i] + neighborcost; meshrt.pathlen[i] = ap->u.meshrt_p.pathlen[i] + 1; meshrt.path[i] = (struct AgentInfo *)safe_malloc((meshrt.pathlen[i]-1)*sizeof(struct AgentInfo)); meshrt.path[i][0].agent_id = ap->src_agent; meshrt.path[i][0].node_id = ap->src; for (int j = 1; j < meshrt.pathlen[i] - 1; j++) { meshrt.path[i][j].agent_id = ap->u.meshrt_p.path[i][j-1].agent_id; meshrt.path[i][j].node_id = ap->u.meshrt_p.path[i][j-1].node_id; } } } } else { if (ap->u.meshrt_p.valid[i]) { if ((meshrt.valid[i] == false) || (meshrt.cost[i] > ap->u.meshrt_p.cost[i] + neighborcost)) { throughme = false; for (int j = 0; j < ap->u.meshrt_p.pathlen[i] - 1; j++) { if (ap->u.meshrt_p.path[i][j].agent_id == id) { throughme = true; break; } } if (!throughme) { if ((meshrt.valid[i]) && (meshrt.pathlen[i] > 1)) { free(meshrt.path[i]); meshrt.path[i] = NULL; } meshrt.valid[i] = true; meshrt.cost[i] = ap->u.meshrt_p.cost[i] + neighborcost; meshrt.pathlen[i] = ap->u.meshrt_p.pathlen[i] + 1; assert(meshrt.path[i] == NULL); meshrt.path[i] = (struct AgentInfo *)safe_malloc((meshrt.pathlen[i]-1)*sizeof(struct AgentInfo)); meshrt.path[i][0].agent_id = ap->src_agent; meshrt.path[i][0].node_id = ap->src; for (int j = 1; j < meshrt.pathlen[i] - 1; j++) { meshrt.path[i][j].agent_id = ap->u.meshrt_p.path[i][j-1].agent_id; meshrt.path[i][j].node_id = ap->u.meshrt_p.path[i][j-1].node_id; } } } } } } mm->gotrt = true; int thrume = 0, thruneighbor = 0; for (int i = 0; i < MAXMESHMEMBERS; i++) { MeshMember *src_mm = find_mesh_member_info(i); if (meshrt.valid[i]) if (meshrt.pathlen[i] > 1) if (meshrt.path[i][0].agent_id == mm->ainfo.agent_id) thruneighbor++; if ((ap->u.meshrt_p.pathlen[i] == INVALID) && (ap->u.meshrt_p.valid[i] == false) && (meshrt.valid[i])) { thrume++; if (src_mm != NULL) { src_mm->source[ap->src_agent] = true; } } else { if (src_mm != NULL) src_mm->source[ap->src_agent] = false; } } if (thrume > thruneighbor) mm->consensuscost = thrume; else mm->consensuscost = thruneighbor; /* for (int i = 0; i < MAXMESHMEMBERS; i++) if ((ap->u.meshrt_p.valid[i] == true) && (ap->u.meshrt_p.pathlen[i] > 1)) free(ap->u.meshrt_p.path[i]); delete[] ap->u.meshrt_p.path; delete[] ap->u.meshrt_p.valid; delete[] ap->u.meshrt_p.pathlen; delete[] ap->u.meshrt_p.cost; */ #ifdef LOG_APP_MESH_RT { printf ("[rt] at %8.4f < ag %d nd %d > after-update-from < ag %d nd %d > begin \n",Scheduler::Clock(),id,n->id,ap->src_agent,ap->src); for (int i = 0; i < MAXMESHMEMBERS; i++) { if (meshrt.valid[i] == true) { printf("[routing-table] to %d of len %d and cost %8.2f via %d\n",i,meshrt.pathlen[i],meshrt.cost[i],((meshrt.pathlen[i] > 1) ? meshrt.path[i][0].agent_id : -1)); } } printf ("[consensus-cost] to %d is %d\n",mm->ainfo.agent_id,mm->consensuscost); printf ("[mesh-rt-recv] at %8.4f < ag %d nd %d > got-updates-from < ag %d nd %d > end\n", Scheduler::Clock(), id, n->id, mm->ainfo.agent_id, mm->ainfo.node_id); } #endif // LOG_APP_MESH_RT return; }void mcAgent::tx_mesh_link_add(void) { if (all_agent_list.GetSize() == mesh_neighbor_list.GetSize()) { #ifdef LOG_LINK_ADD printf("[link-add] at %8.4f < ag %d nd %d > has no non-neighbors \n",Scheduler::Clock(),id,n->id); #endif // LOG_LINK_ADD return; } int rand = get_rand(all_agent_list.GetSize() - mesh_neighbor_list.GetSize()); void *pos = all_agent_list.GetHeadPosition(); int i = 0; MeshMember *mm = all_agent_list.GetAt(pos); while (mm->is_neighbor == true) { all_agent_list.GetNext(pos); mm = all_agent_list.GetAt(pos); } while (i < rand) { i++; all_agent_list.GetNext(pos); mm = all_agent_list.GetAt(pos); while (mm->is_neighbor == true) { all_agent_list.GetNext(pos); mm = all_agent_list.GetAt(pos); } } if ((mm->is_neighbor == false) && (mm->neighbor_setup_pending == false)) { AppPacket *ap = new AppPacket(MESH_LINK_ADD); #ifdef LOG_LINK_ADD printf("[link-addreq] at %8.4f < ag %d nd %d > to < ag %d nd %d >\n",Scheduler::Clock(),id,n->id,mm->ainfo.agent_id,mm->ainfo.node_id); #endif // LOG_LINK_ADD send_pkt(ap,mm->ainfo.agent_id,mm->ainfo.node_id); } return; }void mcAgent::handle_mesh_link_add (AppPacket *ap) { AppPacket *app = new AppPacket(MESH_LINK_ADD_RESPONSE); app->u.meshrt_p.valid = new (bool[MAXMESHMEMBERS]); app->u.meshrt_p.pathlen = new (int[MAXMESHMEMBERS]); app->u.meshrt_p.cost = new (double[MAXMESHMEMBERS]); app->u.meshrt_p.path = new (struct AgentInfo *[MAXMESHMEMBERS]); for (int i = 0; i < MAXMESHMEMBERS; i++) { if ((i != id) && (meshrt.valid[i])) { app->u.meshrt_p.valid[i] = meshrt.valid[i]; app->u.meshrt_p.pathlen[i] = meshrt.pathlen[i]; app->u.meshrt_p.cost[i] = meshrt.cost[i]; if (meshrt.pathlen[i] > 1) app->u.meshrt_p.path[i] = (struct AgentInfo *)safe_malloc((meshrt.pathlen[i]-1)*sizeof(struct AgentInfo)); for (int j = 0; j < meshrt.pathlen[i] - 1; j++) { app->u.meshrt_p.path[i][j].agent_id = meshrt.path[i][j].agent_id; app->u.meshrt_p.path[i][j].node_id = meshrt.path[i][j].node_id; } } else { app->u.meshrt_p.valid[i] = false; app->u.meshrt_p.pathlen[i] = INVALID; app->u.meshrt_p.cost[i] = INFINITELINKCOST; app->u.meshrt_p.path[i] = NULL; } } #ifdef LOG_LINK_ADD printf("[link-addresp] at %8.4f < ag %d nd %d > to < ag %d nd %d >\n",Scheduler::Clock(),id,n->id,ap->src_agent,ap->src); #endif // LOG_LINK_ADD send_pkt(app,ap->src_agent,ap->src); return; } void mcAgent::handle_mesh_link_add_response(AppPacket *ap) { #ifdef LOG_LINK_ADD printf("[link-addresp] at %8.4f < ag %d nd %d > from < ag %d nd %d >\n",Scheduler::Clock(),id,n->id,ap->src_agent,ap->src); #endif // LOG_LINK_ADD double utility=0.0,current,next; double neighborcost = n->rt->get_path_cost(ap->src); for (int i = 0; i < MAXMESHMEMBERS; i++) { if (i == id) continue; if ((ap->u.meshrt_p.valid[i]) && (meshrt.valid[i])) { current = meshrt.cost[i]; next = ap->u.meshrt_p.cost[i] + neighborcost; if (next < current) { utility += (current - next) / current; #ifdef LOG_LINK_ADD printf("[link-addresp-details] for %d current cost is %8.2f and next is %8.2f\n",i,current,next); #endif // LOG_LINK_ADD } } if ((ap->u.meshrt_p.valid[i]) && (meshrt.valid[i] == false)) { utility += 1.0; #ifdef LOG_LINK_ADD printf("[link-addresp-details] for %d new route discovered\n",i); #endif // LOG_LINK_ADD } } #ifdef LOG_LINK_ADD printf("[link-addresp-details] util is %8.2f\n",utility); #endif // LOG_LINK_ADD if (utility > LINKADDTHRESHOLD) { MeshMember *mm = find_mesh_member_info(ap->src_agent); if (mm != NULL) { assert (mm->neighbor_setup_pending == false); if (mm->is_neighbor == true) { #ifdef LOG_LINK_ADD printf ("[link-add] at %8.4f < ag %d nd %d > not-needed-to < ag %d nd %d > \n", Scheduler::Clock(),id,n->id, mm->ainfo.agent_id,mm->ainfo.node_id); #endif // LOG_LINK_ADD } else { #ifdef LOG_LINK_ADD printf("[link-add] at %8.4f < ag %d nd %d > to < ag %d nd %d >\n",Scheduler::Clock(),id,n->id,mm->ainfo.agent_id,mm->ainfo.node_id); #endif // LOG_LINK_ADD add_to_setup_neighbor_list(mm); MeshSetupNeighborTimer *msnt = new MeshSetupNeighborTimer(this,mm); msnt->SetTimer(CONST_MESH_SETUP_NEIGHBOR_TIMEOUT);/* Suman's correction */ // printf ("[timer-debug] About to add timer at %8.3f for %d at %d\n", Scheduler::Clock(), mm->ainfo.agent_id, id); mesh_setup_neighbor_timer_list.Add(msnt, mm->ainfo.agent_id); tx_mesh_neighbor_setup_req(mm); } } } /* for (int i = 0; i < MAXMESHMEMBERS; i++) if ((ap->u.meshrt_p.valid[i] == true) && (ap->u.meshrt_p.pathlen[i] > 1)) free(ap->u.meshrt_p.path[i]); delete[] ap->u.meshrt_p.path; delete[] ap->u.meshrt_p.valid; delete[] ap->u.meshrt_p.pathlen; delete[] ap->u.meshrt_p.cost; */ return; }void mcAgent::tx_mesh_link_drop () { MeshMember *badneighbor = NULL; int badconsensuscost = MAXMESHMEMBERS; for (void *pos = mesh_neighbor_list.GetHeadPosition(); pos != NULL; mesh_neighbor_list.GetNext(pos) ) { MeshMember *mm = mesh_neighbor_list.GetAt(pos); if (mm->consensuscost < badconsensuscost) { badconsensuscost = mm->consensuscost; badneighbor = mm; } } #ifdef LOG_LINK_DROP if (badneighbor != NULL) printf("[link-drop-details] at %8.4f < ag %d nd %d > thinks < ag %d nd %d > has bad consensus cost of %d\n",Scheduler::Clock(),id,n->id,badneighbor->ainfo.agent_id,badneighbor->ainfo.node_id,badconsensuscost); #endif // LOG_LINK_DROP if (badconsensuscost < LINKDROPTHRESHOLD) { AppPacket *ap = new AppPacket(MESH_LINK_DROP); #ifdef LOG_LINK_DROP printf("[link-drop] at %8.4f < ag %d nd %d > to < ag %d nd %d >\n",Scheduler::Clock(),id,n->id,badneighbor->ainfo.agent_id,badneighbor->ainfo.node_id); #endif // LOG_LINK_DROP send_pkt(ap,badneighbor->ainfo.agent_id, badneighbor->ainfo.node_id); purge_from_neighbor_list(badneighbor); cleanup_rt(badneighbor->ainfo.agent_id); } return; } void mcAgent::handle_mesh_link_drop(AppPacket *ap) { MeshMember *mm = find_mesh_member_info(ap->src_agent); if ((mm != NULL) && (mm->is_neighbor)) { #ifdef LOG_LINK_DROP printf("[link-drop-recv] at %8.4f < ag %d nd %d > from < ag %d nd %d >\n",Scheduler::Clock(),id,n->id,mm->ainfo.agent_id,mm->ainfo.node_id); #endif // LOG_LINK_DROP purge_from_neighbor_list(mm); cleanup_rt(mm->ainfo.agent_id); } return; } void mcAgent::specific_send_data_pkt (void) { assert (started == true); #ifdef LOG_DATA printf ("[data] at %8.4f < ag %d nd %d > source-data-pkt < %ld > ssn\n", Scheduler::Clock(), id, n->id, data_seq_no); #endif // LOG_DATA tx_data(); data_seq_no++; return;}void mcAgent::tx_data() { for (void *pos = mesh_neighbor_list.GetHeadPosition(); pos != NULL; mesh_neighbor_list.GetNext(pos) ) { AppPacket *ap = new AppPacket(DATA); ap->u.data_p.src_agent_id = id; ap->u.data_p.src_node_id = n->id; ap->u.data_p.seq_no = data_seq_no; MeshMember *mm = mesh_neighbor_list.GetAt(pos); #ifdef LOG_DATA printf("[data-send] at %8.4f < ag %d nd %d > sent-data-to < ag %d nd %d > < %ld > ssn \n", Scheduler::Clock(), id, n->id, mm->ainfo.agent_id, mm->ainfo.node_id, data_seq_no); #endif // LOG_DATA send_pkt(ap,mm->ainfo.agent_id,mm->ainfo.node_id); }}void mcAgent::handle_data(AppPacket *ap) { MeshMember *mm = find_mesh_member_info(ap->src_agent); long diff; if ((mm != NULL) && (mm->is_neighbor)) { MeshMember *src_mm = find_mesh_member_info(ap->u.data_p.src_agent_id); if (src_mm != NULL) { diff = ap->u.data_p.seq_no - src_mm->last_data_seq_no; if (diff > 0) { src_mm->last_data_seq_no = ap->u.data_p.seq_no; #ifdef LOG_DATA { printf("[data-recv] at %8.4f < ag %d nd %d > from < ag %d nd %d > originating-at < ag %d nd %d > < %ld > ssn\n",Scheduler::Clock(), id, n->id, ap->src_agent, ap->src, src_mm->ainfo.agent_id, src_mm->ainfo.node_id, src_mm->last_data_seq_no); } #endif // LOG_DATA #ifdef LOG_DATA_LOSS { if (diff > 1) printf("[data-loss] at %8.4f < ag %d nd %d > detects < %ld > lost-pkts\n",Scheduler::Clock(), id, n->id, diff-1); } #endif // LOG_DATA_LOSS for (int i = 0; i < MAXMESHMEMBERS; i++) { if (src_mm->source[i] == true) { MeshMember *dest = find_mesh_member_info(i); if (dest != NULL) { if (dest->is_neighbor == true) { AppPacket *app = new AppPacket(DATA); app->u.data_p.src_agent_id = ap->u.data_p.src_agent_id; app->u.data_p.src_node_id = ap->u.data_p.src_node_id; app->u.data_p.seq_no = ap->u.data_p.seq_no; #ifdef LOG_DATA printf("[data-forward] at %8.4f < ag %d nd %d > is-src < ag %d nd %d > is-dest < ag %d nd %d > is-origin < %ld > is ssn\n", Scheduler::Clock(), id, n->id, dest->ainfo.agent_id, dest->ainfo.node_id, ap->u.data_p.src_agent_id, ap->u.data_p.src_node_id, ap->u.data_p.seq_no); #endif // LOG_DATA send_pkt(app,dest->ainfo.agent_id,dest->ainfo.node_id); } } } } } } } }void mcAgent::handle_mesh_rt_exchange_timeout() { for (void *pos = mesh_neighbor_list.GetHeadPosition(); pos != NULL; mesh_neighbor_list.GetNext(pos) ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -