环境:RedHat9上安装的P2Psim0.3
目的:在P2Psim使用Vivaldi协议仿真
现状:主程序代码中关于vivaldi协议的部分注释掉了。
思路:从主函数分析代码,找到原因。
vivaldi协议主函数是vivalditest.C
#ifndef __VIVALDITEST_H
#define __VIVALDITEST_H
#include "p2psim/p2protocol.h"
#include "vivaldinode.h"
class VivaldiTest : public VivaldiNode {
public:
VivaldiTest(IPAddress i, Args &args);
~VivaldiTest();
string proto_name() { return "VivaldiTest"; }
virtual void join(Args*);
virtual void leave(Args*) { }
virtual void crash(Args*) { }
virtual void insert(Args*) { }
virtual void lookup(Args*) { }
virtual void nodeevent (Args *);
virtual void initstate ();
void tick(void *);
void status();
double error();
vector<IPAddress> best_n (unsigned int n);
void total_error(double &x05, double &x50, double &x95);
void node_errors(double &x05, double &x50, double &x95);
vector<IPAddress> _nip_best;
vector<IPAddress> _nip;
vector<IPAddress> _nip_far;
int _neighbors_far;
private:
int _ticks;
int _grid_config;
int _ring_config;
int _landmark_config;
int _near_config;
bool _aux_added;
//some neighbor adding strategies
// need to run after _all is filled
// in. This flag lets us run them once
Vivaldi *_vivaldi;
static vector<VivaldiTest*> _all;
int _next_neighbor;
int _neighbors; // if > 0, fix the number of neighbors
uint _total_nodes;
int _vis;
double _last_error;
bool _joined;
int _far_percent;
uint _old_all_size;
void handler(void *, void *);
void addRingNeighbors ();
void addGridNeighbors ();
void addRandNeighbors ();
void addLandmarkNeighbors ();
void addNearNeighbors ();
void print_all_loc();
};
#endif // __VIVALDITEST_H
vivalditest继承于vivaldinode类
void VivaldiNode::update_error (vector<Sample> samples)
{
double expect = dist (_c, samples[0]._c);
double actual = samples[0]._latency;
if (actual < 0) return;
double rel_error = fabs(expect - actual)/actual;
if (_pred_err < 0)
_pred_err = rel_error;
else if (_samples[0]._error < 0)
// _pred_err = _pred_err; ;
else {
double ce = _pred_err*_pred_err;
double he = samples[0]._error*samples[0]._error;
double new_pred_err = rel_error*(ce/(he + ce)) + _pred_err*(he/(he + ce));
_pred_err = (19*_pred_err + new_pred_err)/20.0;
if (_pred_err > 1.0) _pred_err = 1.0;
}
}
vivaldinode又继承于p2protocol
主要参数
#define MODEL_EUCLIDEAN 3 //嵌入欧式空间,其他有sphere空间、toroidal曲面空间
#define ALG_VIVALDI 1 //最基本的vivaldi算法
#define ALG_SIMPLEX 2
#define ALG_PERFECT 3
adaptive //调整的时间间隔
_timestep //最小的timestep
_pred_err //running average of prediction error
_window_size //历史窗口大小
_model_type //空间类型
_model
_algorithm_type //算法类型
_algorithm
_c //current estimated coordinates
_samples //RTT的历史记录(每个元素:_c, _latency, _whoIP, _error)
_init_samples //RTT初始记录
主要函数
Sample lowest_latency(vector<Sample> v); //从历史记录中找出最小的时延
Coord net_force(Coord c, vector<Sample> v); //多个节点的合力共同更新本节点坐标
vector<double> get_weights (vector<Sample> v); //多个参考节点的所对应的误差
void update_error (vector<Sample> v); //更新本地节点误差
double dist(VivaldiNode::Coord a, VivaldiNode::Coord b); //计算2个Vivaldi节点的距离
template<class BT, class AT, class RT> bool doRPC(IPAddress dst, void (BT::* fn)(AT *, RT *), AT *args, RT *ret) //发送消息函数,dynamic_cast,
【
VivaldiNode * t = dynamic_cast<VivaldiNode *>(getpeer(dst)); //peer包含dst自己吗?
Topology *topo = Network::Instance ()->gettopology ();
】
P2protocol继承于Node
class P2Protocol : public Node {
public:
P2Protocol(IPAddress);
virtual ~P2Protocol();
typedef void (P2Protocol::*event_f)(Args*);
virtual void join(Args*) = 0;
virtual void crash(Args*) {}
virtual void lookup(Args*) = 0;
virtual void nodeevent(Args*) = 0;
};
Node类继承于Observed
主要参数
_ip
static string _protocol;
static Args _args;
static Time _collect_stat_time;
static bool _collect_stat;
IPAddress _first_ip;
IPAddress _prev_ip;
主要函数
ip()
heaer()
alive()
static void Receive(void*);
Node *getpeer(IPAddress); // find peer protocol of my sub-type on a distant node.
//统计性函数
// statistic collection
typedef uint stat_type;
const static stat_type STAT_LOOKUP = 0;
unsigned long get_out_bw_stat() { return node_live_outbytes;}
void record_bw_stat(stat_type type, uint num_ids, uint num_else);
static void record_inout_bw_stat(IPAddress src, IPAddress dst, uint num_ids, uint num_else);
void record_in_bytes(uint b);
void record_out_bytes(uint b);
static void record_lookup_stat(IPAddress src, IPAddress dst, Time interval,
bool complete, bool correct,
uint num_hops = 0, uint num_timeouts = 0,
Time time_timeouts = 0);
void record_join();
void record_crash();
static void print_stats();
static void print_dist_stats(vector<double> v);
Observed类记录网络仿真日志
class Observed {
public:
void registerObserver(Observer *);
void unregisterObserver(Observer *);
void notifyObservers(ObserverInfo* = 0);
protected:
Observed();
virtual ~Observed() { }
private:
set<Observer*> _observers;
bool _hasObservers;
virtual void dummy() { } // make dynamic_cast<Kelips*>(Observed*) work
};
p2psim中主要指标参数有