-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructures.h
More file actions
141 lines (122 loc) · 3.54 KB
/
structures.h
File metadata and controls
141 lines (122 loc) · 3.54 KB
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
#include <string>
#include <fstream>
#include <iostream>
#include <mutex>
#include <vector>
#ifndef STRUCTURES_H_
#define STRUCTURES_H_
using namespace std;
#define MAX_PROCESSES_NUM 10
#define MAX_MESSAGE_NUM 10000
#define MSG_LEN 64
#define MAX_LOG_FILE 1000 //let's say after each 100 message, I will write to a file
#define MAX_CONTAINER_NUM 50
struct Message {
int seq_no;
int sender; //last hop = immediate sender
int initial_sender; //the initial sender
// TODO: try to find a way to assign the size on creating the message (note: int* does not work
// since sending the message will send only the memory address without the content causing segmentation fault)
int vector_clock[MAX_PROCESSES_NUM];
bool operator <(const Message & m) const
{
return seq_no < m.seq_no;
}
bool operator ==(const Message & m) const
{
return ((seq_no == m.seq_no) && (initial_sender == m.initial_sender) && (sender == m.sender));// && (vector_clock == m.vector_clock));
}
};
struct m_container{
Message c[MAX_CONTAINER_NUM];
int num;
};
struct ack_message {
int acking_process; //process to send the ack
Message message;
bool operator ==(const ack_message & m) const
{
return ((acking_process == m.acking_process) && (message == m.message));
}
};
struct ack_container{
ack_message a[MAX_CONTAINER_NUM];
int num;
};
struct AckMessageComp
{
bool operator()(const ack_message& msg1, const ack_message& msg2) const
{
return true;
}
};
// Message comparator / Functor
// Compare objects with sequence number then with initial_sender id
struct MessageComp
{
bool operator()(const Message& msg1, const Message& msg2) const
{
return (msg1.seq_no != msg2.seq_no) || (msg1.initial_sender != msg2.initial_sender);
}
};
struct comp_m
{
bool operator()(const Message& msg1, const Message& msg2) const
{
return (msg1.seq_no < msg2.seq_no);
}
};
struct LogMessage {
char message_type;
int seq_nr;
int sender;
};
struct Process {
int id;
string ip;
int port;
};
// Process comparator / Functor
// Compare objects with id number.
// Should I compare wrt to something else?
struct ProcessComp
{
bool operator()(const Process& prcs1, const Process& prcs2) const
{
return (prcs1.id < prcs2.id) || ((!(prcs2.id < prcs1.id)));
}
};
extern int nb_of_processes;
extern Process processes[MAX_PROCESSES_NUM];
extern std::vector<Message> un_acked_messages[MAX_PROCESSES_NUM];
extern std::mutex un_acked_messages_m;
extern int my_process_id;
extern string my_ip;
extern int my_port;
extern int recv_sock;
extern int recvack_sock;
extern int* send_sock;
extern int send_sock_all; // TODO: added
extern std::mutex log_m;
extern LogMessage* messages_log;
extern int log_pointer;
extern ofstream out_file;
extern std::vector<int> processes_dependencies[MAX_PROCESSES_NUM];
extern std::vector<int> my_dependencies;
//This function writes the logs received to the log file
static void write_log(){
cout << "At process " << my_process_id << " writing output to file... number of lines in log: " << log_pointer << endl;
for(int i = 0; i < log_pointer; i++) {
if(messages_log[i].message_type == 'b')
out_file << "b " << messages_log[i].seq_nr << endl;
else
out_file << "d " << messages_log[i].sender << " " << messages_log[i].seq_nr << endl;
}
log_pointer = 0; //return the point to the beginning
}
class deliver_callback { // @suppress("Class has a virtual method and non-virtual destructor")
public:
virtual void deliver(Message) = 0;
};
// This is for the localized causal broadcast
#endif /* STRUCTURES_H_ */