forked from mooman/Raymond-Tree-Simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messenger.cpp
46 lines (39 loc) · 1.2 KB
/
messenger.cpp
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
#include "main.h"
#include "queue.h"
#include "event.h"
#include "messenger.h"
#include "RaymondTree.h"
#include "site.h"
#include "simulator.h"
Messenger::Messenger (Simulator * s):
wd_size(8)
{
//hook in with the current simulator
this->s = s;
//weighted time delays, the index corresponds to the delay
int wd[8] = {0, 300, 300, 200, 100, 50, 25, 25};
weighted_delays = new int[8];
for (int i = 0; i < wd_size; i++) {
weighted_delays[i] = wd[i];
}
}
void Messenger::send (int to, int from, int action, Request* req) {
// this shouldn't happen
if (to == from) {
cout << "you're sending a message to yourself. ignoring reuqest" << endl;
return;
}
int time = s->get_current_time() + compute_delivery_delay();
s->new_event(time, to, from, action, req);
cout << "------ message is scheduled to deliver at time " << time << endl;
}
int Messenger::compute_delivery_delay () {
int i, sum, r = rand() % 1000 + 1;
for (i = sum = 0; i < wd_size; i++) {
sum += weighted_delays[i];
//does our random number fall within the distribution?
if (r <= sum) return i;
}
//program should die if this happens
exit(1);
}