-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAirfareClient.cpp
More file actions
353 lines (302 loc) · 9.02 KB
/
AirfareClient.cpp
File metadata and controls
353 lines (302 loc) · 9.02 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
Author: Al Timofeyev
Date: 2/12/2019
Desc: The client end of this client/server program Used
to assign seats to the clients.
It expects up to 2 command line parameters.
First the mode followed by the ini file.
If those two aren't provided, the defaults will be used.
Please read the ReadMe.txt file.
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <iterator>
#include <vector>
#include <ctime>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
using namespace std;
// Function Prototypes.
void printSeats();
void deleteSeats();
bool allSeatsTaken();
void assignSeatManual(int sockfd, char *msg);
void assignSeatAutomatic(int sockfd, char *msg);
#define DEFAULT_ADDR "127.0.0.1"
#define DEFAULT_PORT 8465
#define DEFAULT_TIMEOUT 20
int** seats;
int dimensions[2]; // Dimensions of seats[].
int rows; // Height
int columns; // Width
int main(int argc, char *argv[])
{
char *sockAddr = (char*) DEFAULT_ADDR;
int sockPort = DEFAULT_PORT;
int timeout = DEFAULT_TIMEOUT;
string mode = "automatic";
string filename;
// If there was a file provided.
if(argc == 3)
{
mode = argv[1];
filename = argv[2];
// Open the file an make sure it exists.
ifstream inputFile(filename);
if(inputFile.fail())
{
cout << "Failed to open file: " << filename << endl;
return 0;
}
// Read the file and parse the integers into numList.
string line;
for(int i = 1; !inputFile.eof(); i++)
{
getline(inputFile, line);
istringstream stream(line);
if(line.length() == 0)
break;
vector<string> words((istream_iterator<string>(stream)), istream_iterator<string>());
if(i == 1)
{
sockAddr = new char[words[2].length() + 1];
memcpy(sockAddr, words[2].c_str(), words[2].length() + 1);
}
else if(i == 2)
sockPort = stoi(words[2]);
else if(i == 3)
timeout = stoi(words[2]);
}
inputFile.close();
}
// Else if only the mode was provided.
else if (argc == 2)
mode = argv[1];
// ********** Connect Socket **********
// This will hold the socket information.
struct sockaddr_in server_addr;
int sockfd = 0; // This will connect to server socket.
int valRead = 0; // This will hold number of values read from server.
char msg[1024];
// Initialize (create) the socket.
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
cout << "\nError : Could not create socket\n";
return 1;
}
// Allocate memory.
memset(&server_addr, '0', sizeof(server_addr)); // Set memory of server_addr.
memset(msg, '0', sizeof(msg)); // Set memory of msg buffer.
// Set socket information.
server_addr.sin_family = AF_INET; // Communication domain: IPv4 Protocol.
server_addr.sin_port = htons(sockPort); // Port number.
// Try connecting the address.
if(inet_pton(AF_INET, sockAddr, &server_addr.sin_addr)<=0)
{
cout << "\nError : Invalid address / Address not supported\n";
return 1;
}
// Try connecting to the server socket.
for(int i = 0; i <= timeout; i++)
{
if(connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
// If connection fails by timeout, return FAIL.
if(i == timeout)
{
cout << "\nError : Connection To Server Failed\n";
return 1;
}
else
{
sleep(1);
continue;
}
}
// Else, if the connection was finally successful, exit loop.
else
break;
}
// Notify client of successful server connection.
valRead = read(sockfd, msg, 100);
cout << "\n***** From Server : " << msg << " *****\n\n";
// Send the mode to server.
memset(msg, '0', sizeof(msg)); // Reset memory of msg buffer.
memcpy(msg, mode.c_str(), mode.length() + 1);
write(sockfd, msg, 100);
// Assign a seat for the client.
if(mode.compare("manual") == 0)
assignSeatManual(sockfd, msg);
else
assignSeatAutomatic(sockfd, msg);
// If all the seats are now taken.
if(allSeatsTaken())
{
cout << "\n\n****************************************\n";
cout << "******** All Seats Are Now TAKEN *******";
cout << "\n****************************************\n";
}
// Close connection to server.
close(sockfd);
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 seats based on client request.
*/
void assignSeatManual(int sockfd, char *msg)
{
// Get the seating map from the server.
read(sockfd, dimensions, sizeof(int)*2);
rows = dimensions[0];
columns = dimensions[1];
seats = new int*[rows];
for(int height = 0; height < rows; height++)
{
seats[height] = new int[columns];
read(sockfd, seats[height], sizeof(int)*columns);
}
// Ask client to request a seat.
while(!(strcmp(msg, "Seat successfully assigned.") == 0 || allSeatsTaken()))
{
// Print the seating map to the client.
cout << "The Current Seating Map:\n";
printSeats();
// Ask client what seat seat to assign thm and send to server.
cout << "Please enter another row and column to assign: ";
int r, c;
while(1)
{
cin >> r;
cin >> c;
if(!(r >= rows || c >= columns || r < 0 || c < 0))
break;
cout << "\nOne of both of the values you entered is out of range.\n";
cout << "Please enter a row between 0 and " << rows-1;
cout << "\n and a column between 0 and " << columns-1 << ": ";
}
// Send the row and column to server to be assigned.
int dim[] = {r, c};
write(sockfd, dim, sizeof(int)*2);
// See if the seating assignment was successful.
read(sockfd, msg, 100);
cout << "\n\n\n****************************************\n";
cout << msg << endl;
// Get the seating map from the server.
read(sockfd, dimensions, sizeof(int)*2);
for(int height = 0; height < rows; height++)
read(sockfd, seats[height], sizeof(int)*columns);
}
// Print the seating map to the client.
cout << "The New Seating Map:\n";
printSeats();
}
// *****************************************************************
/*
Assigns seats automatically based on seat availability.
*/
void assignSeatAutomatic(int sockfd, char *msg)
{
// Get the seating map from the server.
read(sockfd, dimensions, sizeof(int)*2);
rows = dimensions[0];
columns = dimensions[1];
seats = new int*[rows];
for(int height = 0; height < rows; height++)
{
seats[height] = new int[columns];
read(sockfd, seats[height], sizeof(int)*columns);
}
// Print the seating map to the client.
cout << "The Current Seating Map:\n";
printSeats();
// Randomly generate a seat request until no seats are available.
srand(time(0)); // So each client has a different random generator.
while(!(allSeatsTaken()))
{
// Ask client what seat seat to assign thm and send to server.
int r = rand() %rows, c = rand() % columns;
cout << "Chosen random row and column to assign: " << r << " " << c << endl;
sleep(2);
// Send the row and column to server to be assigned.
int dim[] = {r, c};
write(sockfd, dim, sizeof(int)*2);
// See if the seating assignment was successful.
read(sockfd, msg, 100);
cout << "\n\n\n****************************************\n";
cout << msg << endl;
// Get the seating map from the server.
read(sockfd, dimensions, sizeof(int)*2);
for(int height = 0; height < rows; height++)
read(sockfd, seats[height], sizeof(int)*columns);
// Print the seating map to the client.
cout << "The Current Seating Map:\n";
printSeats();
}
// Print the seating map to the client.
cout << "The New Seating Map:\n";
printSeats();
}