-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitoring_subservice.cpp
90 lines (76 loc) · 2.52 KB
/
monitoring_subservice.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
#pragma once
#include <string>
#include <iostream>
#include <time.h>
#include <map>
#include "socket.cpp"
#include "machine.cpp"
#include "management_subservice.cpp"
#include "information_subservice.cpp"
#include "replication_subservice.cpp"
#define PORT 4000
#define EXIT_PORT 4001
#define MAX_MISSED_CALLS 4
#define MONITORING_PACKET_INTERVAL 1
using namespace std;
void monitorateParticipant(string IP) {
Socket mng_socket;
int send_res, recv_res, missed_calls = 0;
bool awake = true;
auto machine = MachinesManager::Instance().getMachine(IP);
string mac_addr = getMacAddress();
string hostname = getSelfHostname();
mng_socket.setSendAddr(IP, PORT);
while(machine->second.isParticipating()) {
// Send packet looking for participants
send_res = mng_socket.sendMessage(mac_addr + hostname);
if (send_res < 0)
cerr << ("[M] ERROR sendto") << endl;
recv_res = mng_socket.receiveMessage();
if (recv_res < 0) {
missed_calls++;
// cerr << "[M] Participant " << IP << " didn't answer. Missed calls: " << missed_calls << endl;
if ((awake == true) && (missed_calls >= MAX_MISSED_CALLS)) {
// cout << "[M] Participant " << IP << " set as ASLEEP" << endl;
awake = false;
machine->second.setAsleep();
MachinesManager::Instance().setMapChanged(true);
replicateAsleepStatus(IP);
}
}
else {
// cout << "[M] Participant " << IP << " answered: " << mng_socket.getBuffer() << endl;
missed_calls = 0;
if (awake == false) {
awake = true;
machine->second.setAwake();
MachinesManager::Instance().setMapChanged(true);
initialReplicationFor(IP);
replicateAwakeStatus(IP);
}
}
sleep(MONITORING_PACKET_INTERVAL);
}
}
void exitListener(){
Socket exit_socket;
int send_res, recv_res;
string participant_ip;
exit_socket.listenPort(EXIT_PORT);
while(true) {
if (!manager) {
continue;
}
// Listen for packets sent by the manager to PORT
recv_res = exit_socket.receiveMessage(true);
if (recv_res < 0)
cerr << "[M] ERROR recvfrom" << endl;
// cout << "[M] Participant (IP " << exit_socket.getSenderIP() << " ) asked: " << exit_socket.getBuffer() << endl;
participant_ip = exit_socket.getSenderIP();
MachinesManager::Instance().removeMachine(participant_ip);
replicateExitStatus(participant_ip);
// cout << participant_ip << " Machine exited the service" << endl;
MachinesManager::Instance().setMapChanged(true);
}
exit_socket.closeSocket();
}