📄 macedon-utils.cc
字号:
//Copyright (c) 2004, Charles Killian, Adolfo Rodriguez, Dejan Kostic, Sooraj Bhat, and Amin Vahdat//All rights reserved.////Redistribution and use in source and binary forms, with or without//modification, are permitted provided that the following conditions are met://// * Redistributions of source code must retain the above copyright// notice, this list of conditions and the following disclaimer.// * Redistributions in binary form must reproduce the above copyright// notice, this list of conditions and the following disclaimer in// the documentation and/or other materials provided with the// distribution.// * Neither the names of Duke University nor The University of// California, San Diego, nor the names of its contributors// may be used to endorse or promote products derived from// this software without specific prior written permission.////THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"//AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE//IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE//FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL//DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,//OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE//USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE./* Adolfo Rodriguez macedon-utils.cc 2000801*/#include <stdio.h>#include "agent.h"#include "ip.h"#include "nsport.h"#include "macedon.h"#include "macedon_swp.h"#include "random.h"#include <math.h>#include <openssl/sha.h>#define USE_SHAint global_exit;// assuming value is greater than 0int round_double (double value){ double valueadded = value + 0.5; int lower =(int) floor (value); int higher =(int) ceil (value); int higheradded =(int) ceil (valueadded); if (higher == higheradded) { return lower; } else { return higher; }}void dump_hex(void *addr, int size) { int *where = (int *)addr; int num = size/sizeof(int); printf("Dumping %d bytes from %.8x:\n", size, addr); for (int i=0; i<num; i++) { if (i%8 == 0) printf("\n"); printf ("%.8x ", where[i]); } printf("\n");}double wall_clock () { struct timezone tz_; timeval clock_; gettimeofday(&clock_, &tz_); return (clock_.tv_sec+(double)clock_.tv_usec/1000000.0);}int macedon_hash(int in) { int result; unsigned char sha_hash1[SHA_DIGEST_LENGTH];#ifdef USE_SHA SHA1((const unsigned char *)&in, sizeof(int), sha_hash1); result = *((int *)&sha_hash1[SHA_DIGEST_LENGTH-4]); // printf("SHA result %x\n", result);#else result = in*in*in + in%13;#endif return result;}int isinrange(int look, int low, int high){ // be default, left inclusive if (low == high) return 1; // low is lower than high, no cycle if (low < high) if (look >= low && look < high) return 1; else return 0; else if (look >= low || look < high) return 1; else return 0;}int isinrangeright(int look, int low, int high){ // right inclusize if (low == high) return 1; // low is lower than high, no cycle if (low < high) if (look > low && look <= high) return 1; else return 0; else if (look > low || look <= high) return 1; else return 0;}int better_or_equal (double a, double b){ if (b*0.9 <= a ) return 1; else return 0;}int random_integer( int maximum){ // returns a random int in the range {0, maximum-1} int retval; double value = drand48(); value*= maximum; retval = (int) floor(value); if (retval == maximum) retval--; return retval;}macedon_packet_qent::macedon_packet_qent(){ qe_pkt = 0; qe_next = 0; qe_time_sent = 0; qe_retransmitted = 0;}macedon_neighbor_entry::macedon_neighbor_entry(int addr){ ne_addr = addr; ne_send_seqno = 1; // next pkt to send will have this ne_ack_seqno = 0; // last pkt acked had this ne_recv_seqno = 1; // expect to see this pkt next // ne_last_sent = 0; ne_next_entry = 0; ne_pktq = 0; ne_sent = 0; ne_rtt_average = MACEDON_RETRANS_TIMEOUT ; ne_retransmit_timeout = MACEDON_RETRANS_TIMEOUT ; ne_rtt_deviation = 0;}voidmacedon_Agent::macedon_exit(){ global_exit = 1; sprintf(trace_buf_, "killing deprecated read thread\n"); trace_print(); pthread_cancel(((Agent *)this)->childtid); sprintf(trace_buf_, "killing transports threads\n"); trace_print(); ((MACEDON_Agent *)this)->exit_agent(); if (globalsched->main_thread) { sprintf(trace_buf_, "killing main timer thread\n"); trace_print(); pthread_cancel(globalsched->main_thread); } sprintf(trace_buf_, "joining killer thread\n"); trace_print(); pthread_join(globalsched->killer, NULL);}voidmacedon_Agent::macedon_wait(double time){ if (time) sure_sleep(time); else pthread_join(((Agent *)this)->childtid, NULL);}doubleMACEDON_Agent::oracle_get_overlay_cost(int from, int to){ if (to != oracle[0].to) { printf("Exception in getting oracle cost, tos don't match %x %x\n", to, oracle[0].to); exit(32); } int i=0; while (i<oracle_nodes) { if (oracle[i].from == from) { // printf("oracle is reporting %x %x %lf\n", from, to, oracle[i].cost); return oracle[i].cost; } i++; } printf("Exception: node unknown to oracle %x\n", to); exit(31);}voidMACEDON_Agent::read_oracle_file(){ char whole_line[100]; char dir_name[150]; char file_name[150]; int myfrom, myto; double mycost; FILE *myfile; char my_hostname[80]; char * mn_mdb = getenv("MN_MDB"); if (!mn_mdb) { printf("Exception: MN_MDB undefined in oracle\n"); exit(30); } if (gethostname(my_hostname, sizeof(my_hostname)) == -1) { printf("Bad host name.\n"); exit(132); } strcpy(dir_name, mn_mdb); strcat(dir_name,"/oracle/"); strcpy(file_name, dir_name); strcat(file_name, my_hostname); myfile = (FILE *)fopen(file_name, "r"); if (!myfile) { printf("Exception: nonexistent oracle file %s\n", file_name); exit(54); } while (fgets(whole_line, 512, myfile)!=NULL) oracle_nodes++; fclose(myfile); oracle = (struct oracle_ent *) malloc (oracle_nodes * (sizeof(struct oracle_ent)+1)); myfile = (FILE *)fopen(file_name, "r"); int i=0; while (fgets(whole_line, 512, myfile)!=NULL) { sscanf(whole_line,"%x %x %lf", &(oracle[i].from), &(oracle[i].to), &(oracle[i].cost)); printf("My oracle cost: %x %lf\n", oracle[i].from, oracle[i].cost); i++; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -