-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.cpp
205 lines (164 loc) · 4.87 KB
/
Server.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <arpa/inet.h>
#include <errno.h>
#include <iostream>
#include <pthread.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define MAX_CLI 5
#define R_BUF_SIZE 2000
#define W_BUF_SIZE 2000
using namespace std;
struct packet {
int cli_desc;
int prior;
};
// Function Headers
void *transmission_handler(void *packet1);
int recognize(int cli_desc);
//-----Function Headers
// Global Variables
int curPrior = 0;
int clients = 0;
pthread_t thread1;
pthread_mutex_t authorization_lock, thr_ending_waiter, mutex1;
pthread_cond_t thr_end_ctrler;
//-----Global Variables
int main(int argc, char *argv[]) {
int serv_sock_desc, cli_desc, port = 5001;
struct sockaddr_in server, client;
if (argc == 2) {
port = atoi(argv[1]);
}
serv_sock_desc = socket(AF_INET, SOCK_STREAM, 0);
if (serv_sock_desc == -1) {
puts("could'nt create socket.");
exit(0);
}
puts("Socket created");
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);
if (bind(serv_sock_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
puts("bind faild.");
exit(0);
}
puts("bind done.");
listen(serv_sock_desc, 5);
pthread_t thread1;
int c = sizeof(struct sockaddr_in);
puts("Waiting for connection ...");
pthread_mutex_init(&authorization_lock, NULL);
pthread_mutex_init(&thr_ending_waiter, NULL);
pthread_mutex_init(&mutex1, NULL);
pthread_cond_init(&thr_end_ctrler, NULL);
while (1) {
while (clients < MAX_CLI) {
if (cli_desc = accept(serv_sock_desc, (struct sockaddr *)&client,
(socklen_t *)&c)) {
if (pthread_create(&thread1, NULL, transmission_handler,
(void *)&cli_desc) < 0) {
perror("could not create thread");
return 1;
}
}
}
}
}
void *transmission_handler(void *cli_desc1) {
int cli_desc = *(int *)cli_desc1;
char *message;
int newPrior = recognize(cli_desc);
if (newPrior < 0) {
cout << "Client didn't specified it's priority.";
close(cli_desc);
pthread_exit(NULL);
}
// Decide whether to connect client or not
// mutex vase in mikhad ke age masalan 2 , 3 , 4 baham kanect bedan
// momkene aval 2 bere bad 3 ham bere bad 4 beran tu ghesmate 1 va 3 , 4 bahan
// kanect beshan
pthread_mutex_lock(
&authorization_lock); //-----------------------------------------------------------------
if (newPrior > curPrior || clients == 0) {
curPrior = newPrior;
if (clients != 0) {
pthread_cond_wait(&thr_end_ctrler,
&thr_ending_waiter); //-------------------------
// pthread_cond_broadcast(&thr_end_ctrler);//it let threads do the ending
// process pthread_mutex_lock(&thr_ending_waiter);
}
clients++;
}
else if (newPrior == curPrior) {
clients++;
}
else if (newPrior < curPrior) {
message = "You don't have enough priority.";
write(cli_desc, message, strlen(message));
close(cli_desc);
pthread_mutex_unlock(&authorization_lock); //--------------------
pthread_exit(NULL);
}
pthread_mutex_unlock(
&authorization_lock); //--------------------------------------------------------------
int prior = newPrior;
char cliMsg[R_BUF_SIZE];
int cliMsgSize = 0;
memset(cliMsg, 0, R_BUF_SIZE);
cout << "client " << cli_desc << "is connected successuly." << endl;
message = "Greetings! you are connected to server successuly.\nNow type "
"something and i shall repeat what you type.";
write(cli_desc, message, strlen(message));
while (prior == curPrior) {
while ((cliMsgSize = recv(cli_desc, cliMsg, R_BUF_SIZE, MSG_DONTWAIT)) >
0) {
cliMsg[cliMsgSize] = '\0';
cout << "client " << cli_desc << ": " << cliMsg << endl;
write(cli_desc, cliMsg, strlen(cliMsg));
memset(cliMsg, 0, R_BUF_SIZE);
}
if (cliMsgSize == 0) {
cout << "client: " << cli_desc << " disconnected" << endl;
close(cli_desc);
clients--;
fflush(stdout);
return 0;
} else if (errno != EAGAIN) {
perror("recv failed");
close(cli_desc);
clients--;
return 0;
}
}
// Ending Process--------------------------
pthread_mutex_lock(&mutex1); //----------------
close(cli_desc);
clients--;
if (clients == 0) {
pthread_cond_signal(&thr_end_ctrler); //------------------------
}
pthread_mutex_unlock(&mutex1); //--------------
return 0;
}
int recognize(int cli_desc) {
char cliMsg[10];
bzero(cliMsg, 10);
int prior = 0, cliMsgSize;
if ((cliMsgSize = recv(cli_desc, cliMsg, 10, 0)) > 0) {
cliMsg[cliMsgSize] = '\0';
prior = atoi(cliMsg);
return prior;
}
if (cliMsgSize == 0) {
puts("Client disconnected");
fflush(stdout);
return -1;
} else if (cliMsgSize == -1) {
perror("recv failed");
return -1;
}
}