-
Notifications
You must be signed in to change notification settings - Fork 0
/
urb.cpp
103 lines (91 loc) · 2.45 KB
/
urb.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
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
//
// urb.cpp
// project1
//
// Created by Henrik Åslund on 2018-10-14.
// Copyright © 2018 Henrik Åslund. All rights reserved.
//
#include "structures.h"
#include "beb.h"
#include "urb.hpp"
void urb::init(deliver_callback* callback){
bbb.init(this);
frb_callback = callback;
}
void urb::urbBroadcast(Message message) {
// add itself as the immediate sender and put into the pending set
message.sender = my_process_id;
// pen_m.lock();
pending.push_back(message);
// pen_m.unlock();
bbb.bebBroadcast(message);
}
void urb::urb_deliver(Message message, int from) {
// add process to acknowledgement array
// here, we identify messages by their initial sender
// ack_m.lock();
ack[message.initial_sender][message.seq_no].insert(from);
// ack_m.unlock();
// check if pending
bool notInPending = true;
// it_m.lock();
it = pending.begin();
while(it != pending.end()) {
if((((Message)*it).initial_sender == message.initial_sender) && (((Message)*it).seq_no == message.seq_no)) {
notInPending = false;
break;
}
else {
it++;
}
}
// it_m.unlock();
if(notInPending) {
// pen_m.lock();
pending.push_back(message);
// pen_m.unlock();
bbb.bebBroadcast(message);
}
}
void urb::deliver(Message message) {
if(message.seq_no == 0)
return;
urb_deliver(message, message.sender);
// upon exists
// it_m.lock();
it = pending.begin();
while(it != pending.end()) {
if(candeliver(*it) && not_in_deliver(*it)) {
// del_m.lock();
delivered.push_back(*it);
// printf("Process %d deliver %d %d \n", my_process_id, (*it).initial_sender, (*it).seq_no);
// del_m.unlock();
if (frb_callback != NULL){
frb_callback -> deliver(*it);
}
else
bbb.beb_deliver(*it);
pen_m.lock();
it = pending.erase(it);
pen_m.unlock();
}
else
it++;
}
// it_m.unlock();
}
bool urb::candeliver(Message message) {
// calculate the number of acks for this message
// ack_m.lock();
int nAcks = (ack[message.initial_sender][message.seq_no]).size();
// ack_m.unlock();
// return statement whether majority or not
return nAcks > (nb_of_processes/2);
}
bool urb::not_in_deliver(Message message){
for(Message m: delivered){
if(m.initial_sender == message.initial_sender && m.seq_no == message.seq_no)
return false;
}
return true;
}