-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAirfareServer.cpp
More file actions
326 lines (276 loc) · 8.04 KB
/
Copy pathAirfareServer.cpp
File metadata and controls
326 lines (276 loc) · 8.04 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
Author: Al Timofeyev
Date: 2/12/2019
Desc: The server end of this client/server program Used
to assign seats to the clients.
You can either start it with a row and column of
seats available as command line parameters, or no
parameters at all.
Please read the ReadMe.txt file.
*/
#include <iostream>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
using namespace std;
// Function Prototypes
void printSeats();
void deleteSeats();
bool allSeatsTaken();
void * connectToClient(void * connfd);
bool assignSeat(int row, int column);
void sendSeatMap(int connection);
#define DEFAULT_ROWS 10
#define DEFAULT_COLUMNS 10
#define PORT 8465
int** seats;
int rows = DEFAULT_ROWS; // Height
int columns = DEFAULT_COLUMNS; // Width
int listenfd; // This will be the server socket.
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
int main(int argc, char* argv[])
{
// If the number of Rows x Columns was provided.
if(argc > 2)
{
rows = stoi(argv[1]);
columns = stoi(argv[2]);
seats = new int*[rows];
for(int height = 0; height < rows; height++)
{
seats[height] = new int[columns];
for(int width = 0; width < columns; width++)
seats[height][width] = 0;
}
}
// Else, if only the program name is provided,
// assign the default seating arrangement.
else
{
seats = new int*[rows];
for(int height = 0; height < rows; height++)
{
seats[height] = new int[columns];
for(int width = 0; width < columns; width++)
seats[height][width] = 0;
}
}
// ********** Connect Socket **********
// This will hold the socket information.
struct sockaddr_in server_addr;
listenfd = 0; // This will be the server socket.
int connectfd = 0; // This will be the connected client.
int opt = 1; // Used for setsocket.
// Initialize (create) the socket.
listenfd = socket(AF_INET, SOCK_STREAM, 0);
// Forcefully attaching socket to the PORT.
// This is so that the server can be stopped and restarted without error.
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)))
{
perror("\nError : setsockopt");
return 1;
}
// Set the socket information.
memset(&server_addr, '0', sizeof(server_addr)); // Set memory of server_addr.
server_addr.sin_family = AF_INET; // Communication domain: IPv4 Protocol.
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // IP address (since it's local-use INADDR_ANY).
server_addr.sin_port = htons(PORT); // Port number.
// Attach/bind socket to PORT.
if(bind(listenfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0)
{
perror("\nError : Socket binding failed");
return 1;
}
// Place socket into listening (passive) mode and set a
// queue size of 20 where clients can wait.
listen(listenfd, 20);
// Print the initial status of the seat map.
cout << "The Current Seating Map:\n";
printSeats();
// Constantly wait for a connection to the socket.
while(!(allSeatsTaken()))
{
connectfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
if(allSeatsTaken())
{
// Notify client of successful connection to server.
char *msg = (char*) "Server Connection Successful!";
write(connectfd, msg, 100);
// Send client the current seating map.
sendSeatMap(connectfd);
break;
}
pthread_t thread;
pthread_create(&thread, NULL, connectToClient, (void*)connectfd);
}
cout << "\n\n****************************************\n";
cout << "******** All Seats Are Now TAKEN *******";
cout << "\n****************************************\n";
// Delete seating map.
deleteSeats();
return 0;
}
/* *****************************************************************
****************** All Function Definitions Below ******************
***************************************************************** */
void printSeats()
{
if(seats == nullptr)
return;
cout << "\t";
for(int i = 0; i < columns; i++)
cout << i << " ";
cout << "\n\n";
for(int height = 0; height < rows; height++)
{
cout << height << "\t";
for(int width = 0; width < columns; width++)
{
if(seats[height][width] == 0)
cout << "o ";
else
cout << "X ";
}
cout << endl;
}
cout << endl;
}
// *****************************************************************
void deleteSeats()
{
if(seats != nullptr)
{
for(int height = 0; height < rows; height++)
delete[] seats[height];
delete[] seats;
seats = nullptr;
}
}
// *****************************************************************
bool allSeatsTaken()
{
bool taken = true;
for(int height = 0; height < rows; height++)
{
for(int width = 0; width < columns; width++)
{
if(seats[height][width] == 0)
{
taken = false;
return taken;
}
}
}
return taken;
}
// *****************************************************************
/*
Assigns the client a seat on the plane.
@param connfd The connection to the client.
*/
void * connectToClient(void * connfd)
{
//cout << "dood\n\n";/////DEBUGGING
// Setup connectedfd for read/write to client.
int connectfd = (long) connfd;
// Notify client of successful connection to server.
char *msg = (char*) "Server Connection Successful!";
write(connectfd, msg, 100);
// Send client the current seating map.
sendSeatMap(connectfd);
// Recieve the mode from client.
char mode[100];
read(connectfd, mode, 100);
// If client connected in manual mode.
if(strcmp(mode, "manual") == 0)
{
// Get seating request from client and try to assign it.
bool assigned = false;
while(!(assigned))
{
int dim[2];
read(connectfd, dim, sizeof(int)*2);
pthread_mutex_lock(&mut);
assigned = assignSeat(dim[0], dim[1]);
pthread_mutex_unlock(&mut);
if(assigned)
{
msg = (char*) "Seat successfully assigned.";
// Print the New status of the seat map.
cout << "\n\n\n****************************************\n";
cout << "The Current Seating Map:\n";
printSeats();
}
else
msg = (char*) "Seat is already taken.";
write(connectfd, msg, 100);
sendSeatMap(connectfd);
}
}
// Else if client connected in automatic mode.
else
{
// Get seating request from client and try to assign it.
bool assigned;
while(!(allSeatsTaken()))
{
int dim[2];
read(connectfd, dim, sizeof(int)*2);
assigned = assignSeat(dim[0], dim[1]);
if(assigned)
{
msg = (char*) "Seat successfully assigned.";
// Print the New status of the seat map.
cout << "\n\n\n****************************************\n";
cout << "The Current Seating Map:\n";
printSeats();
}
else
msg = (char*) "Seat is already taken.";
write(connectfd, msg, 100);
sendSeatMap(connectfd);
}
}
//write(connectfd, msg, 100);
//sendSeatMap(connectfd);
// Close connection to client.
close(connectfd);
pthread_exit(NULL);
}
// *****************************************************************
/*
Assigns a seat on the plane based on row and column.
Returns true if seat was successfully assigned, else
returns false.
*/
bool assignSeat(int row, int column)
{
// If can't assign the seat.
if(seats[row][column] == 1)
return false;
else
seats[row][column] = 1;
return true;
}
// *****************************************************************
/*
Sends the seat map to the client.
@param connection The connection to the client.
*/
void sendSeatMap(int connection)
{
// Send the dimensions of seat map first.
int dimensions[] = {rows, columns};
write(connection, dimensions, sizeof(int)*2);
// Send the seat map.
for(int i = 0; i < rows; i++)
write(connection, seats[i], sizeof(int)*columns);
}