2010-12-31 wcdj 第一步 :首先生成初始随机密钥。 #include <iostream> #include <fstream> #include <time.h> using namespace std; int main() { ofstream ofs("key_result.txt"); if (!ofs)// error { return 1; } // use system time to initialize seed srand((unsigned)time(NULL)); for (int nodes=0; nodes<100; ++nodes)// 100 nodes { ofs<<nodes<<"/t"; for (int keys=0; keys<50; ++keys)// one node has 50 random keys from 1000 keys pool { // get random number between 0 and 999 ofs<<rand()%1000<<" "; } ofs<<endl; } ofs.close(); system("pause"); return 0; } 第二步: 选择簇内成员节点密钥。 # select_node_key.awk # select some nodes form setup_key.txt BEGIN { lines="1 3 5";# node's ID begins zero len=split(lines, arr, " "); asort(arr);# if unsorted data, it will use idx=1; } { # NR stands for line number if ( len && (NR==arr[idx]+1) ) { print; len--; idx++; } # selet opposite nodes against lines variable #if (NR==arr[idx]+1) idx++; #else print; } 第三步: 计算具有相同密钥的节点数量。 # calc_node_key.awk # calculate the number of shareing one key in a cluster BEGIN { keys="11 13 12 14 15"; # keys of cluster header node len=split(keys, arr, " "); # store keys in an array #print len; # debug used asort(arr); # if unsorted data, it will use idx=1; # index of arr cnt=0; # record the number of sharing one key node with CH next_key=2; # begin at the second row total_key=5; # the total number of keys one node has bhaskey=0; # mark sharing key } { next_key=2;# reset to begin bhaskey=0;# initial mark while ( next_key <= total_key+1 )# visit node every key { #print $next_key;# debug used for (idx=1; idx<=len; ++idx)# compare $next_key to each key of cluster header node { if ( $next_key == arr[idx] ) { #print NR, $next_key, next_key, idx;# debug used ++cnt; bhaskey=1; break; } } if (bhaskey) { ++next_key; break; } ++next_key; }# end of while } END { print cnt " nodes sharing one key with CH"; }