forked from BassP97/NetworksP2P
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
142 lines (128 loc) · 3.55 KB
/
main.cpp
File metadata and controls
142 lines (128 loc) · 3.55 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
142
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <iostream>
#include <pthread.h>
#include <sys/select.h>
#include <vector>
#include <netdb.h>
#include <algorithm>
#include <fcntl.h>
#include "client.h"
#include "server.h"
using namespace std;
int main(int argc,char *argv[]) {
pthread_t threads[4]; // 4 threads plus this one
int rc;
// initialize lock for list of FDs to read in select()
if (pthread_mutex_init(&server_readfd_lock, NULL) == -1)
{
perror("pthread_mutex_init");
return 1;
}
// initialize lock for client FD
if (pthread_mutex_init(&client_fd_lock, NULL) == -1)
{
perror("pthread_mutex_init");
pthread_mutex_destroy(&server_readfd_lock);
return 1;
}
// initialize stop lock
if (pthread_mutex_init(&stop_lock, NULL) == -1)
{
perror("pthread_mutex_init");
pthread_mutex_destroy(&server_readfd_lock);
pthread_mutex_destroy(&client_fd_lock);
return 1;
}
// ----------------------------------------------------------------------
// ensure that the readfds fd_set is zeroed out
if (pthread_mutex_lock(&server_readfd_lock) == -1)
{
perror("pthread_mutex_lock");
pthread_mutex_destroy(&server_readfd_lock);
pthread_mutex_destroy(&client_fd_lock);
pthread_mutex_destroy(&stop_lock);
return 1;
}
FD_ZERO(&server_readfds);
if (pthread_mutex_unlock(&server_readfd_lock) == -1)
{
perror("pthread_mutex_unlock");
pthread_mutex_destroy(&server_readfd_lock);
pthread_mutex_destroy(&client_fd_lock);
pthread_mutex_destroy(&stop_lock);
return 1;
}
// ----------------------------------------------------------------------
// create client requester thread
rc = pthread_create(&threads[0], NULL, start_client_requester, NULL);
if (rc)
{
perror("pthread_create");
pthread_mutex_destroy(&server_readfd_lock);
pthread_mutex_destroy(&client_fd_lock);
pthread_mutex_destroy(&stop_lock);
return 1;
}
// create client connector thread
rc = pthread_create(&threads[1], NULL, start_client_connector, NULL);
if (rc)
{
perror("pthread_create");
pthread_mutex_destroy(&server_readfd_lock);
pthread_mutex_destroy(&client_fd_lock);
pthread_mutex_destroy(&stop_lock);
return 1;
}
// create server listener thread
rc = pthread_create(&threads[2], NULL, start_server_listen, NULL);
if (rc)
{
perror("pthread_create");
pthread_mutex_destroy(&server_readfd_lock);
pthread_mutex_destroy(&client_fd_lock);
pthread_mutex_destroy(&stop_lock);
return 1;
}
// create server reader thread
rc = pthread_create(&threads[3], NULL, start_server_read, NULL);
if (rc)
{
perror("pthread_create");
pthread_mutex_destroy(&server_readfd_lock);
pthread_mutex_destroy(&client_fd_lock);
pthread_mutex_destroy(&stop_lock);
return 1;
}
// join on the threads to wait for them to finish
for (int i = 0; i < 4; i++)
{
pthread_join(threads[i], NULL);
}
// destroy the locks and exit once all threads have finished
if (pthread_mutex_destroy(&server_readfd_lock) == -1)
{
perror("pthread_mutex_destroy");
pthread_mutex_destroy(&client_fd_lock);
pthread_mutex_destroy(&stop_lock);
return 1;
}
if (pthread_mutex_destroy(&client_fd_lock) == -1)
{
perror("pthread_mutex_destroy");
pthread_mutex_destroy(&stop_lock);
return 1;
}
if (pthread_mutex_destroy(&stop_lock) == -1)
{
perror("pthread_mutex_destroy");
return 1;
}
return 0;
}