-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
497 lines (444 loc) · 14.3 KB
/
Copy pathclient.c
File metadata and controls
497 lines (444 loc) · 14.3 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/* client.c Kyrylo Bakumenko 29 May, 2023
*
* This file provides the client side for the Nuggets project in CS50.
* The client is launched by the user providing hostname and port to connect to the server (see DESIGN.md).
* After connecting to the server the user declares themselves with a PLAY message. From this point, responses from
* the server are displayed using the ncurses library and the user interacts with the display using keystrokes.
*
* Exit codes: 1 -> invalid number of arguments
* 2 -> null argument passed
* 3 -> failed data struct initialization
* 4 -> failure to initialize messages module
* 5 -> bad hostname or port
* 6 -> failed screen verification
* 7 -> failed to initalize player (malformed OK message)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ncurses.h>
#include <time.h>
#include "message.h"
/**************** global types ****************/
typedef struct data {
// size of board, initialized to initial window size
int NROWS;
int NCOLS;
char player; //if non-zero, prepresents player's letter inbetween display frames
} data_t;
// internal function prototypes
static bool handleMessage(void* arg, const addr_t from, const char* message);
static bool handleInput(void* arg);
static void initialize_curses(); // CURSES
static void init_map();
static void display_map(char* display);
static void display_temp_message(const char* temp);
static void clear_temp_message();
// handleMessage helpers
static bool handleOK(const char* message);
static bool handleGRID(const char* message);
static bool handleGOLD(const char* message);
static void handleDISPLAY(const char* message);
// game helper function
static data_t* data_new();
static data_t* data;
/* ***************************
* main function
* Accepts either 2 or 3 arguments from command-line: hostname port [playername]
* client interacts with server to play the nugget game by sending keystrokes.
* Usage described in DESIGN.md
*
*/
int main(int argc, char *argv[])
{
/* parse the command line, validate parameters */
const char* program = argv[0];
// check num parameters
if (argc != 3 && argc != 4) {
fprintf(stderr, "ERROR: Expected either 2 or 3 arguments but recieved %d\n", argc-1);
fprintf(stderr, "USAGE: %s hostname port [playername]\n", program);
exit(1); // bad num arguments
}
// Defensive programming, check for null arguments
for (int i = 1; i < argc; i++) {
if (argv[i] == NULL) {
fprintf(stderr, "ERROR: argmument %d is NULL\n", i);
exit(2); // bad argument
}
}
// create data struct
data = data_new();
/* initialize messages module */
// (without logging)
if (message_init(NULL) == 0) {
free(data);
exit(3); // failure to initialize message module
}
/* assign command-line arguments to variables */
// commandline provides address for server
const char* serverHost = argv[1];
const char* serverPort = argv[2];
addr_t server; // address of the server
if (!message_setAddr(serverHost, serverPort, &server)) {
fprintf(stderr, "ERROR: Failure forming address from %s %s\n", serverHost, serverPort);
free(data);
exit(4); // bad hostname/port
}
// connect to server
const char* playername = argv[3];
if (playername != NULL) {
// send playername to server
char line[message_MaxBytes];
strcpy(line, "PLAY ");
strncat(line, playername, message_MaxBytes-strlen("PLAY "));
// connect as player
message_send(server, line);
} else {
// connect as spectator
message_send(server, "SPECTATE");
// no ok message is sent, auto-initialize
if(!handleOK(NULL)) {
// send quit message
message_send(server, "KEY Q");
message_done();
free(data);
exit(6);
// failed to initialize player name
}
}
// Loop, waiting for input or for messages; provide callback functions.
// We use the 'arg' parameter to carry a pointer to 'server'.
bool ok = message_loop(&server, 0, NULL, handleInput, handleMessage);
// clear data
free(data);
// close curses
endwin(); // CURSES
// shut down the message module
message_done();
return ok ? 0 : 1;
}
/**************** handleInput ****************/
/* stdin has input ready; read a char and send it to the server.
* Return true if the message loop should exit, otherwise false.
* i.e., return true if EOF was encountered on stdin, or fatal error.
*/
static bool
handleInput(void* arg)
{
// We use 'arg' to receive an addr_t referring to the 'server' correspondent.
// Defensive checks ensure it is not NULL pointer or non-address value.
addr_t* serverp = arg;
if (serverp == NULL) {
fprintf(stderr, "handleInput called with arg=NULL");
return true;
}
if (!message_isAddr(*serverp)) {
fprintf(stderr, "handleInput called without a correspondent.");
return true;
}
// gather key input
char c = getch();
char message[strlen("KEY ") + 2];
strcpy(message, "KEY ");
strcat(message, &c);
message[strlen("KEY ") + 1] = '\0';
// clear previous temp message (if it exists)
clear_temp_message();
// send keystroke
message_send(*serverp, message);
// keep looping
return false;
}
/**************** handleMessage ****************/
/* Datagram received; print it.
* We ignore 'arg' here.
* Return true if any fatal error.
*/
static bool
handleMessage(void* arg, const addr_t from, const char* message)
{
// defensive programming
if (arg == NULL || message == NULL) {
return false;
}
// Find the position of the header within the message
if (strncmp(message, "OK ", strlen("OK ")) == 0){
if(!handleOK(message)) {
// send quit message
message_send(from, "KEY Q");
message_done();
free(data);
exit(6);
}
} else if (strncmp(message, "GRID ", strlen("GRID ")) == 0) {
if(!handleGRID(message)) {
// send quit message
message_send(from, "KEY Q");
message_done();
// failed to initialize grid
free(data);
exit(5);
}
return false;
} else if (strncmp(message, "GOLD ", strlen("GOLD ")) == 0) {
handleGOLD(message);
return false;
} else if (strncmp(message, "DISPLAY\n", strlen("DISPLAY\n")) == 0) {
handleDISPLAY(message);
return false;
} else if (strncmp(message, "QUIT ", strlen("QUIT ")) == 0) {
// close curses
endwin(); // CURSES
fflush(stdout);
fprintf(stdout, "\n%s\n", message);
return true;
} else if (strncmp(message, "ERROR ", strlen("ERROR ")) == 0) {
// log error
fprintf(stderr, "%s\n", message);
// display to player
const char* temp = strchr(message, ':') + 1;
display_temp_message(temp);
return false;
} else {
// MALFORMED MESSAGE -- IGNORE
fprintf(stderr, "ERROR: Malformed message '%s'\n", message);
return false;
}
return false;
}
/**************** handleOK ****************/
/* fills player arg in data with char sent by server or 0 otherwise */
static bool
handleOK(const char* message)
{
data->player = 0;
if (message != NULL) {
if (sscanf(message, "OK %c", &data->player) != 1) {
return false;
}
}
/* initialize curses library */
initialize_curses(); // CURSES
init_map(); // initialize with blank map
return true;
}
/**************** handleGRID ****************/
/* takes a char* as an argument, of the GRID message type. */
/* GRID message must be in *exact* syntax as described in requirments. */
/* verifies that CURSES window is at least the required size (specified by message) */
/* "The display shall consist of NR+1 rows and NC columns" */
static bool
handleGRID(const char* message)
{
int nrows;
int ncols;
// assign values and check for null
if (sscanf(message, "GRID %d %d", &nrows, &ncols) != 2) {
fprintf(stderr, "ERROR: Malformed GRID message '%s'", message);
return false;
}
// if not null, verify screen size
while (nrows+1 > data->NROWS || ncols > data->NCOLS) {
endwin(); // CURSES
fprintf(stderr, "ERROR: incompatible screen of size [%d, %d] for [%d, %d]\n", data->NROWS, data->NCOLS, nrows+1, ncols);
mvprintw(0,0, "ERROR: incompatible screen of size [%d, %d] for [%d, %d]\nRESIZE and press ENTER to continue", data->NROWS, data->NCOLS, nrows+1, ncols);
char ch = getch();
if (ch == '\n') {
endwin();
initialize_curses();
}
}
// update with screen output dimensions
data->NROWS = nrows+1;
data->NCOLS = ncols;
return true;
}
/**************** handleGOLD ****************/
/* takes a char* as an argument, of the GOLD message type. */
/* GOLD message must be in *exact* syntax as described in requirments. */
/* displays gold values in the topline of CURSES window */
/* if the display line is longer than the window, it is cropped. */
static bool
handleGOLD(const char* message)
{
// ints to store gold information to display in curse
int gold_collected;
int gold_purse;
int gold_left;
if (sscanf(message, "GOLD %d %d %d", &gold_collected, &gold_purse, &gold_left) != 3) {
// INVALID GOLD MESSAGE SENT
fprintf(stderr, "ERROR: Malformed GOLD message '%s'", message);
return false;
} else {
// display info message
int temp = 0; // dummy
int ncols = 0;
getmaxyx(stdscr, temp, ncols);
(void)temp; // keep compiler from complaining
for (int col = 0; col < ncols; col++) {
move(0, col);
addch(' ');
}
char buffer[ncols]; //buffer string of max length
if (data->player == 0) {
snprintf(buffer, ncols, "Spectator: %d nuggets unclaimed.", gold_left);
} else {
int len_msg = snprintf(buffer, ncols, "Player %c has %d nuggets (%d nuggets unclaimed).", data->player, gold_purse, gold_left);
char gold_left_buffer[ncols];
int len_gold_left_msg = 0;
if (gold_collected > 0) {
if (ncols - len_msg > 0) { // len to buffer cannot be negative
len_gold_left_msg = snprintf(gold_left_buffer, ncols - len_msg, " GOLD recieved: %d", gold_collected);
}
}
// copy extra message into buffer
for (int i = len_msg; i < len_msg + len_gold_left_msg; i++) {
buffer[i] = gold_left_buffer[i-len_msg];
}
buffer[len_msg+len_gold_left_msg] = '\0';
}
mvprintw(0,0, "%.*s", ncols, buffer);
refresh(); // CURSES
return true;
}
}
/**************** handleDISPLAY ****************/
/* takes a char* as an argument, of the DISPLAY message type. */
/* DISPLAY message must be in *exact* syntax as described in requirments. */
/* displays the chars following '\n' character in CURSES. */
static void
handleDISPLAY(const char* message)
{
/* get display contents */
size_t headerLength = strlen("DISPLAY\n");
size_t displayLength = strlen(message) - headerLength + 1;
// display buffer
char display[displayLength];
strncpy(display, message + headerLength, displayLength);
display[displayLength] = '\0'; // in-case length is such that last terminating character is not copied
// print display
display_map(display);
}
/* ************ initialize_curses *********************** */
/* initialize curses // CURSES everywhere in this function */
static void
initialize_curses()
{
// initialize the screen - which defines LINES and COLS
initscr();
// cache the size of the window in our global variables
// (this is a macro, which is why we don't need & before the variables.)
getmaxyx(stdscr, data->NROWS, data->NCOLS);
cbreak(); // actually, this is the default
noecho(); // don't show the characters users type
curs_set(0); // set cursor to invisible
// I like yellow on a black background
start_color();
init_pair(1, COLOR_YELLOW, COLOR_BLACK);
attron(COLOR_PAIR(1));
}
/* ************ display_map *********************** */
/* Display the map (char* display) into CURSES screen */
static void
display_map(char* display)
{
// move chars from display to ncurses
for (int row = 0; row < data->NROWS; row++) {
for (int col = 0; col < data->NCOLS; col++) {
move(row+1,col); // CURSES, +1 account for info line
int idx = row * (data->NCOLS+1) + col;
if (idx < strlen(display)) {
addch(display[idx]); // CURSES
} else {
addch(' '); // fill with blank
}
}
}
refresh(); // CURSES
}
/* ************ clear_temp_message ************* */
/* clears temp string after gold status message */
static void
clear_temp_message()
{
/* clear previous temp message */
int max_nrows = 0; //dummy
int max_ncols = 0;
int loc = 0;
getmaxyx(stdscr, max_nrows, max_ncols);
// clear dummy variable
(void)max_nrows;
// clear temp status messages
for (loc = 0; loc < max_ncols; loc++) {
char c = mvinch(0, loc) & A_CHARTEXT;
if (c == '.') {
loc++;
break;
}
}
if (loc != 0) {
while(loc < max_ncols) {
move(0, loc++);
addch(' ');
}
}
refresh();
}
/* ************ display_temp_message ************* */
/* display temp string after gold status message */
static void
display_temp_message(const char* temp)
{
int max_nrows = 0; //dummy
int max_ncols = 0;
int loc = 0;
getmaxyx(stdscr, max_nrows, max_ncols);
// clear dummy variable
(void)max_nrows;
// clear temp status messages
for (loc = 0; loc < max_ncols; loc++) {
char c = mvinch(0, loc) & A_CHARTEXT;
if (c == '.') {
loc++;
break;
}
}
// if new temp message is needed
if (temp != NULL) {
mvprintw(0,loc+1, "%.*s", max_ncols-(loc+2), temp);
}
refresh();
}
/* ************ init_map *********************** */
/* fills ncurses screen with bank character ' ' */
static void
init_map()
{
// blank map
int max_nrows = 0;
int max_ncols = 0;
getmaxyx(stdscr, max_nrows, max_ncols);
for (int row = 1; row < max_nrows; row++) {
for (int col = 0; col < max_ncols; col++) {
move(row, col);
addch(' '); // fill with blank
}
}
refresh();
}
/* ************ data_new *********** */
/* allocates memory for data struct */
static data_t*
data_new()
{
// allocate memory for array of pointers
data_t* data = malloc(sizeof(data_t));
if (data == NULL) {
exit(3);
}
data->NROWS = -1;
data->NCOLS = -1;
data->player = 0;
return data;
}