Skip to content
This repository was archived by the owner on Sep 24, 2020. It is now read-only.

Commit 162b36b

Browse files
committed
list directory protocol done
1 parent de4ace6 commit 162b36b

File tree

6 files changed

+63
-44
lines changed

6 files changed

+63
-44
lines changed

bin/server

36.8 KB
Binary file not shown.

pkg/client/client.c

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,41 +33,52 @@ void Loop(int socket) {
3333
char *header = malloc(PROTOCOL_HEADER_LEN);
3434
int n = read(socket, header, PROTOCOL_HEADER_LEN);
3535
printf("size read from socket [%d] \n", n);
36-
uint16_t magic = ExtractMessageMagic(header);
37-
printf("server reply magic [0x%04hX] \n", magic);
38-
if (magic == 0xC0DE) {
39-
uint16_t protocol = ExtractMessageProtocol(header);
40-
uint32_t payload_size = ExtractMessageBodySize(header);
41-
char *recv_buffer = malloc(payload_size);
42-
read(socket, recv_buffer, payload_size);
43-
Message reply;
44-
reply.message_sender = socket;
45-
reply.magic = magic;
46-
reply.protocol = protocol;
47-
reply.size = payload_size;
48-
reply.body = recv_buffer;
36+
if (n > 1) {
37+
uint16_t magic = ExtractMessageMagic(header);
38+
if (magic == 0xC0DE) {
39+
uint16_t protocol = ExtractMessageProtocol(header);
40+
uint32_t payload_size = ExtractMessageBodySize(header);
41+
char *recv_buffer = malloc(payload_size);
42+
read(socket, recv_buffer, payload_size);
43+
Message reply;
44+
reply.message_sender = socket;
45+
reply.magic = magic;
46+
reply.protocol = protocol;
47+
reply.size = payload_size;
48+
reply.body = recv_buffer;
4949

50-
switch (reply.protocol) {
51-
case ECHO_REPLY: {
52-
fprintf(stderr,
53-
"MAGIC "
54-
"[0x%04hX] | PROTOCOL "
55-
"[0x%04hX] = [%C] | data : %s\n",
56-
reply.magic, reply.protocol, reply.protocol,
57-
reply.body);
58-
fprintf(stderr, "[ ECHO FROM SERVER ] ");
59-
break;
60-
}
61-
default: {
50+
switch (reply.protocol) {
51+
case ERROR_MESSAGE: {
52+
fprintf(stderr, "[ ERROR MESSAGE ] : [ %s ]", reply.body);
53+
break;
54+
}
55+
case ECHO_REPLY: {
56+
fprintf(stderr,
57+
"MAGIC "
58+
"[0x%04hX] | PROTOCOL "
59+
"[0x%04hX] = [%C] | data : %s\n",
60+
reply.magic, reply.protocol, reply.protocol,
61+
reply.body);
62+
fprintf(stderr, "[ ECHO FROM SERVER ] ");
63+
break;
64+
}
65+
case LIST_DIR_REPLY: {
66+
fprintf(stderr, "[ List Dir Result ] : [ %s ]", reply.body);
67+
break;
68+
}
69+
default: {
70+
break;
71+
}
72+
}
73+
} else {
6274
break;
6375
}
64-
}
65-
} else {
66-
break;
76+
77+
show_menu = 1;
6778
}
6879
}
80+
6981
waiting_for_choice = 1;
70-
show_menu = 1;
7182
// break;
7283
}
7384

pkg/handlers/download.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ void DownloadProtocolSendRequestToServer(int socket) {
66
char input[MAX_BUFFER];
77
fgets(input, MAX_BUFFER - 1, stdin);
88
char *arr_ptr = &input[0];
9+
arr_ptr = Trim(arr_ptr);
910
char *request = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
1011
int mesg_length = MarshallMessage(request, 0xC0DE, DOWNLOAD_REQUEST, input);
1112
Message message;

pkg/handlers/handlers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void *ServerRequestHandler(void *arg) {
4141
}
4242
case LIST_DIR_REQUEST: {
4343
printf("[DEBUG] Server Recieved List Directory Request\n");
44-
// ListDirectoryProtocolServerHandler(socket, message);
44+
ListDirectoryProtocolServerHandler(socket, message);
4545
break;
4646
}
4747
default: {

pkg/handlers/handlers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "../multiplexer/multiplexer.h"
55
// #include "../queue/queue.h"
66
#include "../shared/consts.h"
7+
#include "../shared/utils.h"
78

89
#include <stdio.h>
910

pkg/handlers/list_directory.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
#include <dirent.h>
44
#include <stdint.h>
5+
#include <string.h>
56

67
void ListDirectoryProtocolSendRequestToServer(int socket) {
78
printf("Enter Directory name For server list\n");
89
char input[MAX_BUFFER];
910
fgets(input, MAX_BUFFER - 1, stdin);
1011
char *arr_ptr = &input[0];
1112
char *request = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
12-
int mesg_length = MarshallMessage(request, 0xC0DE, LIST_DIR_REQUEST, input);
13+
int mesg_length = MarshallMessage(request, 0xC0DE, LIST_DIR_REQUEST, arr_ptr);
1314
Message message;
1415
message.body = (char *)(request + PROTOCOL_HEADER_LEN);
1516

@@ -26,32 +27,37 @@ void ListDirectoryProtocolServerHandler(int socket, Message message) {
2627
DIR *dir;
2728
char payload[MAX_BUFFER];
2829
uint16_t protocol;
30+
char buf[256];
31+
sscanf(message.body, "%s", buf); // Trimming on both sides occurs here
32+
dir = opendir(buf);
2933
// If the directory exists.
30-
if ((dir = opendir(message.body)) != NULL) {
34+
if (dir != NULL) {
3135
struct dirent *ent;
3236
protocol = LIST_DIR_REPLY;
33-
/* Prepearing the File pointer for use in the while loop.
34-
Pointer needed for file access and output. */
35-
FILE *fp;
36-
37-
// While we are in a directory and there are other directories present.
37+
// While we are in a directory and there are other directories
38+
// present.
3839
while ((ent = readdir(dir)) != NULL) {
40+
char temp[256];
41+
3942
// Prints all of the data to the console.
40-
printf("[DEBUG] '%s\n", ent->d_name);
41-
fprintf(payload, "%s\n", ent->d_name);
43+
sscanf(ent->d_name, "%s\n",
44+
temp); // Trimming on both sides occurs here
45+
strcat(payload, temp);
46+
strcat(payload, " | ");
4247
}
43-
// Closes file ponter & directory pointer, free's the data and shows where
44-
// the log was saved to.
4548
closedir(dir);
4649
}
4750
// If the directory does not exist.
4851
else if (dir == NULL) {
49-
fprintf(payload,
50-
"You either typed the path incorrectly or the directory does not "
51-
"existn");
52+
memset(payload, 0, sizeof(payload));
5253

54+
strcpy(payload, "You either typed the path incorrectly or the directory "
55+
"does not existn");
56+
protocol = ERROR_MESSAGE;
5357
closedir(dir);
5458
}
59+
printf("%s\n", payload);
60+
5561
char *arr_ptr = &payload[0];
5662
int payload_length = strlen(arr_ptr);
5763

0 commit comments

Comments
 (0)