00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "Node.h"
00018 #include <time.h>
00019 #include <netinet/in.h>
00020 #include <math.h>
00021
00022 namespace prophet {
00023
00024 const double
00025 NodeParams::DEFAULT_P_ENCOUNTER = 0.75;
00026
00027 const double
00028 NodeParams::DEFAULT_BETA = 0.25;
00029
00030 const double
00031 NodeParams::DEFAULT_GAMMA = 0.99;
00032
00033 const u_int
00034 NodeParams::DEFAULT_KAPPA = 100;
00035
00036 Node::Node(const NodeParams* params)
00037 : p_value_(0.0), relay_(DEFAULT_RELAY), custody_(DEFAULT_CUSTODY),
00038 internet_gateway_(DEFAULT_INTERNET), dest_id_(""), heap_pos_(0)
00039 {
00040
00041 if (params != NULL)
00042 params_ = new NodeParams(*params);
00043 else
00044
00045 params_ = new NodeParams();
00046
00047
00048 age_ = time(0);
00049 }
00050
00051 Node::Node(const Node& n)
00052 : p_value_(n.p_value_), relay_(n.relay_),
00053 custody_(n.custody_), internet_gateway_(n.internet_gateway_),
00054 dest_id_(n.dest_id_), age_(n.age_), heap_pos_(n.heap_pos_)
00055 {
00056
00057 params_ = new NodeParams(*n.params_);
00058 }
00059
00060 Node::Node(const std::string& dest_id,
00061 bool relay, bool custody, bool internet,
00062 const NodeParams* params)
00063 : params_(NULL), p_value_(0.0), relay_(relay),
00064 custody_(custody), internet_gateway_(internet), dest_id_(dest_id),
00065 heap_pos_(0)
00066 {
00067
00068 if (params != NULL)
00069 params_ = new NodeParams(*params);
00070 else
00071
00072 params_ = new NodeParams();
00073
00074
00075 age_ = time(0);
00076 }
00077
00078 Node::~Node()
00079 {
00080 delete params_;
00081 }
00082
00083 void
00084 Node::update_pvalue()
00085 {
00086
00087 if (!(p_value_ >= 0.0 && p_value_ <= 1.0)) return;
00088 if (params_ == NULL) return;
00089
00090
00091
00092
00093
00094 p_value_ = p_value_ + (1.0 - p_value_) * params_->encounter_;
00095
00096
00097 age_ = time(0);
00098 }
00099
00100 void
00101 Node::update_transitive(double ab, double bc)
00102 {
00103
00104 if (!(p_value_ >= 0.0 && p_value_ <= 1.0)) return;
00105 if (!(ab >= 0.0 && ab <= 1.0)) return;
00106 if (!(bc >= 0.0 && bc <= 1.0)) return;
00107 if (p_value_ > bc) return;
00108 if (params_ == NULL) return;
00109
00110
00111
00112
00113
00114
00115 p_value_ = (p_value_ * params_->beta_) + (1.0 - params_->beta_) * ab * bc * params_->encounter_;
00116
00117
00118 age_ = time(0);
00119 }
00120
00121 void
00122 Node::update_age()
00123 {
00124
00125 if (!(p_value_ >= 0.0 && p_value_ <= 1.0)) return;
00126 if (params_ == NULL) return;
00127
00128
00129
00130
00131 u_int32_t now = time(0);
00132
00133 double agefactor = 1.0;
00134
00135 u_int timeunits = time_to_units(now - age_);
00136 if (timeunits > 0)
00137 agefactor = pow( params_->gamma_, timeunits );
00138
00139 p_value_ *= agefactor;
00140
00141
00142 age_ = time(0);
00143 }
00144
00145 u_int
00146 Node::time_to_units(u_int32_t timediff) const
00147 {
00148
00149 if (params_->kappa_ == 0.0) return 0;
00150
00151
00152 return (u_int) (timediff * 1000 / params_->kappa_);
00153 }
00154
00155 };