1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
//K.5 Reputationsystem.cc
#include "reputationsystem.h"
ofstream reputationlog("reputationlog.txt");
void InactivityTimer::expire(Event * e)
{
a_->handleInactivityTimeout();
resched(INACTIVITY_TIMEOUT + INACTIVITY_TIMEOUT * Random::uniform(1.0));
}
void PublishingTimer::expire(Event * e)
{
a_->handlePublishingTimeout();
resched(PUBLISHING_TIMEOUT + PUBLISHING_TIMEOUT * Random::uniform(1.0));
}
/****************************************************
**** Public functions of ReputationSystem class ****
****************************************************/
ReputationSystem::ReputationSystem(DSRAgent* agent)
{
this->dsragent = agent;
this->inactivity_timer = new InactivityTimer(this);
inactivity_timer->sched(INACTIVITY_TIMEOUT + INACTIVITY_TIMEOUT * Random::uniform(1.0));
this->publishing_timer = new PublishingTimer(this);
publishing_timer->sched(PUBLISHING_TIMEOUT + PUBLISHING_TIMEOUT * Random::uniform(1.0));
}
void ReputationSystem::handleSecondHandInfo(nsaddr_t from, nsaddr_t address, double alpha1, double beta1)
{
map<nsaddr_t, Rating*>::iterator it;
it = reputation_t.find(address);
if (it == reputation_t.end())
{
reputation_t[address] = new Rating(1.0, 1.0);
}
double alpha2 = reputation_t[address]->getAlpha();
double beta2 = reputation_t[address]->getBeta();
bool isPass = deviationTest(address, alpha1, beta1, alpha2, beta2);
if ( ((USING_TRUST == 1) && isPass) || ((USING_TRUST == 1) && trust_manager.isTrustworthy(from)))
{
if(CONFIDANTDEBUG)
{
reputationlog << "Node " << net_id << " handle secondhand info from ";
reputationlog << from << ":" << address << " [" << alpha1 << "," << beta1 << "] ";
reputationlog << " [" << alpha2 << "," << beta2 << "] " << endl;
}
updateReputation(address, alpha1, beta1, 1, SECONDHAND_INFO_WEIGHT);
}
double alpha = isPass ? 0 : 1;
trust_manager.updateTrust(address, alpha, 1-alpha);
return;
}
void ReputationSystem::setNetID(nsaddr_t address)
{
net_id = address;
}
void ReputationSystem::setPathManager(RouteCache* pathmanager)
{
this->path_manager = pathmanager;
}
/*void ReputationSystem::Terminate()
{
map<nsaddr_t, Rating*>::iterator it;
for (it = reputation_t.begin(); it != reputation_t.end(); it ++)
{
reputationlog << "_" <<net_id << "_ Reputation values [" << it->first << ", alpha:";
reputationlog << it->second->getAlpha() << ", beta:" << it->second->getBeta() << "] ";
reputationlog << "mean: " << BayeMean(it->second->getAlpha(), it->second->getBeta()) << endl;
}
path_manager->Terminate(net_id);
}*/
/*****************************************************
**** Private functions of ReputationSystem class ****
*****************************************************/
Rating* ReputationSystem::initNewRating(double alpha, double beta, double fading)
{
Rating* rating = new Rating(1.0, 1.0);
rating->updateRating(alpha, beta, fading, 1);
return rating;
}
void ReputationSystem::updateReputation(nsaddr_t address, double alpha, double beta, double fading, double weight)
{
map<nsaddr_t, Rating*>::iterator it;
it = reputation_t.find(address);
if (it == reputation_t.end())
{
reputation_t[address] = initNewRating(alpha, beta, 1);
}
else
{
if (CONFIDANTDEBUG)
{
reputationlog << "Before updating: ";
reputationlog << "_" << net_id <<"_ node "<<address <<": [";
reputationlog << (it->second)->getAlpha() <<","<<(it->second)->getBeta()<<"]"<<endl;
}
(it->second)->updateRating(alpha, beta, fading, weight);
}
it = reputation_t.find(address);
if (CONFIDANTDEBUG)
{
reputationlog << "After updating: ";
reputationlog << "_" << net_id <<"_ node "<<address <<" reputation updated: [";
reputationlog << (it->second)->getAlpha() <<","<<(it->second)->getBeta()<<"]"<<endl;
}
if (isMisbehavedNode(address))
{
if (CONFIDANTDEBUG)
reputationlog << "Node " << address << " is put into _" << net_id << "_ misbehaved list" << endl;
//path_manager->addMisbehavedNode(address);
}
else
{
//path_manager->removeMisbehavedNode(address);
}
return;
}
void ReputationSystem::handleInactivityTimeout()
{
map<nsaddr_t, Rating*>::iterator it;
for (it = firsthandinfo_t.begin(); it != firsthandinfo_t.end(); it ++)
{
if ( (Scheduler::instance().clock() - (it->second)->getTime()) > INACTIVITY_TIMEOUT)
(it->second)->updateRating(0, 0, INACTIVITY_FADING, 1);
}
map<nsaddr_t, Rating*>::iterator it2;
for (it2 = reputation_t.begin(); it2 != reputation_t.end(); it2 ++)
{
if ( (Scheduler::instance().clock() - (it2->second)->getTime()) > INACTIVITY_TIMEOUT)
(it2->second)->updateRating(0, 0, INACTIVITY_FADING, 1);
}
trust_manager.handleInactivityTimeout();
}
void ReputationSystem::handlePublishingTimeout()
{
if (firsthandinfo_t.empty())
return;
if(CONFIDANTDEBUG)
{
map<nsaddr_t, Rating*>::iterator it;
it = firsthandinfo_t.begin();
reputationlog << "Node " << net_id << " at ";
reputationlog << Scheduler::instance().clock() << "publish time out ";
while (it != firsthandinfo_t.end())
{
double alpha = (it->second)->getAlpha();
double beta = (it->second)->getBeta();
reputationlog << "[" << it->first << ": " << alpha << "," << beta <<"]";
it ++;
}
reputationlog << endl;
}
dsragent->publishInfo(firsthandinfo_t);
}
}
|