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

Commit de4ace6

Browse files
committed
intialized all multiplexer and cli target + setup base request sending and replying
1 parent 511bea8 commit de4ace6

File tree

11 files changed

+97
-19
lines changed

11 files changed

+97
-19
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ run-client: client
4444
- $(CLEAR)
4545
- ./bin/client ${SERVER_IP} ${SERVER_PORT}
4646

47-
server:
47+
server: clean
4848
- $(CLEAR)
4949
- $(MKDIR) ./bin
5050
- $(RM) ./bin/server

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ When adding/modifying different protocols , this is the main library that needs
118118

119119
In the following paragraphs, I will explain the workflow for adding a new protocol.
120120

121-
1- Adding the protocol type to `MessageType` enum in `handlers.h` file . assuming you would like to use characters to define a protocol , you can find their corresponding hex value in the following [`page`](https://www.ibm.com/support/knowledgecenter/en/SSVSD8_8.4.1/com.ibm.websphere.dtx.dsgnstud.doc/references/r_design_studio_intro_Hex_Decimal_and_Symbol_Values.htm). You must define a unique hex value for request and a unique hex value for reply . I use uppercase character hex representation for reques and its lowercase for for reply
121+
1- Adding the protocol type to `MessageType` enum in `handlers.h` file . assuming you would like to use characters to define a protocol , you can find their corresponding hex value in the following [`page`](https://www.ibm.com/support/knowledgecenter/en/SSVSD8_8.4.1/com.ibm.websphere.dtx.dsgnstud.doc/references/r_design_studio_intro_Hex_Decimal_and_Symbol_Values.htm). You must define a unique hex value for request and a unique hex value for reply . I use uppercase character hex representation for request and its lowercase for for reply
122122
2- define prototype for client and server methods in `handlers.h` file. as a general rule, you must define the prototypes with the following signature :
123123

124124
- `{PROTOCOL NAME}HandleServerReply(int socket)` : this method is invoked on the client side . It is supposed to read the server reply and have the client binary perform the unique action it is supposed to perform on based on what was recieved from server.

bin/client

-32.2 KB
Binary file not shown.

pkg/client/client.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,30 @@ void Loop(int socket) {
105105
printf("Your choice is Echo Protocol\n");
106106
EchoProtocolSendRequestToServer(socket);
107107
}
108-
// Echo-----------------------------------------------------------------------------------------
108+
// Download-----------------------------------------------------------------------------------------
109109
if (!bcmp(choice, "2", 1)) {
110110
printf("Your choice is Download Protocol\n");
111111
DownloadProtocolSendRequestToServer(socket);
112112
}
113+
// Upload-----------------------------------------------------------------------------------------
114+
if (!bcmp(choice, "3", 1)) {
115+
printf("Your choice is Upload Protocol\n");
116+
UploadProtocolSendRequestToServer(socket);
117+
}
118+
// Change
119+
// Directory-----------------------------------------------------------------------------------------
120+
121+
if (!bcmp(choice, "4", 1)) {
122+
printf("Your choice is ChangeDirectory Protocol\n");
123+
ChangeDirectoryProtocolSendRequestToServer(socket);
124+
}
125+
// List
126+
// Directory-----------------------------------------------------------------------------------------
127+
if (!bcmp(choice, "5", 1)) {
128+
printf("Your choice is List Directory Protocol\n");
129+
ListDirectoryProtocolSendRequestToServer(socket);
130+
}
113131
}
114-
printf("MSG WAS SENT SUCCEFULLY\n");
115132
}
116133
continue;
117134
}

pkg/handlers/change_directory.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ void ChangeDirectoryProtocolSendRequestToServer(int socket) {
1212
if (send(socket, request, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
1313
perror("write failed: ");
1414
fprintf(stderr,
15-
"[DEBUG] client : sending download reques for file %s to server\n",
15+
"[DEBUG] client : sending change directory request for file %s to "
16+
"server\n",
1617
message.body);
1718
}
1819
void ChangeDirectoryProtocolServerHandler(int socket, Message message) {
@@ -22,5 +23,6 @@ void ChangeDirectoryProtocolServerHandler(int socket, Message message) {
2223
int mesg_length = MarshallMessage(reply, 0xC0DE, message.protocol, arr_ptr);
2324
if (send(socket, reply, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
2425
perror("write failed: ");
25-
fprintf(stderr, "[DEBUG] Echo Handler Server : Replying bac .... \n");
26+
fprintf(stderr,
27+
"[DEBUG] Change Directory Handler Server : Replying back .... \n");
2628
}

pkg/handlers/download.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void DownloadProtocolSendRequestToServer(int socket) {
1414
if (send(socket, request, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
1515
perror("write failed: ");
1616
fprintf(stderr,
17-
"[DEBUG] client : sending download reques for file %s to server\n",
17+
"[DEBUG] client : sending download request for file %s to server\n",
1818
message.body);
1919
}
2020
void DownloadProtocolServerHandler(int socket, Message message) {
@@ -25,5 +25,5 @@ void DownloadProtocolServerHandler(int socket, Message message) {
2525
int mesg_length = MarshallMessage(reply, 0xC0DE, FILE_REPLY, arr_ptr);
2626
if (send(socket, reply, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
2727
perror("write failed: ");
28-
fprintf(stderr, "[DEBUG] Echo Handler Server : Replying bac .... \n");
28+
fprintf(stderr, "[DEBUG] Download Handler Server : Replying back .... \n");
2929
}

pkg/handlers/echo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ void EchoProtocolServerHandler(int socket, Message message) {
2121
int mesg_length = MarshallMessage(reply, 0xC0DE, ECHO_REPLY, arr_ptr);
2222
if (send(socket, reply, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
2323
perror("write failed: ");
24-
fprintf(stderr, "[DEBUG] Echo Handler Server : Replying bac .... \n");
24+
fprintf(stderr, "[DEBUG] Echo Handler Server : Replying back .... \n");
2525
}

pkg/handlers/handlers.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,31 @@ void *ServerRequestHandler(void *arg) {
2424
}
2525
break;
2626
}
27+
case DOWNLOAD_REQUEST: {
28+
printf("[DEBUG] Server Recieved Download Request\n");
29+
// DownloadProtocolServerHandler(socket, message);
30+
break;
31+
}
32+
case UPLOAD_REQUEST: {
33+
printf("[DEBUG] Server Recieved Upload Request\n");
34+
// UploadProtocolServerHandler(socket, message);
35+
break;
36+
}
37+
case CHANGE_DIR_REQUEST: {
38+
printf("[DEBUG] Server Recieved Change Directory Request\n");
39+
// ChangeDirectoryProtocolServerHandler(socket, message);
40+
break;
41+
}
42+
case LIST_DIR_REQUEST: {
43+
printf("[DEBUG] Server Recieved List Directory Request\n");
44+
// ListDirectoryProtocolServerHandler(socket, message);
45+
break;
46+
}
2747
default: {
2848
break;
2949
}
3050
}
3151
}
3252
}
3353
}
34-
}
54+
}

pkg/handlers/handlers.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ typedef enum {
3838
// 'P' in hex
3939
CHANGE_DIR_REQUEST = 0x0050,
4040
// 'L' in hex
41-
LIST_DIR_REQUEST = 0x0050,
41+
LIST_DIR_REQUEST = 0x004C,
4242
// 'l' in hex
4343
LIST_DIR_REPLY = 0x006C,
44+
// 'E' in hex
45+
ERROR_MESSAGE = 0x0045,
4446
UNKNOWN_TYPE = 0xFFFF
4547
} MessageType;
4648
// RequestHandler - this is the main method

pkg/handlers/list_directory.c

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#include "handlers.h"
2+
3+
#include <dirent.h>
4+
#include <stdint.h>
5+
26
void ListDirectoryProtocolSendRequestToServer(int socket) {
37
printf("Enter Directory name For server list\n");
48
char input[MAX_BUFFER];
@@ -11,17 +15,50 @@ void ListDirectoryProtocolSendRequestToServer(int socket) {
1115

1216
if (send(socket, request, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
1317
perror("write failed: ");
14-
fprintf(stderr,
15-
"[DEBUG] client : sending download reques for file %s to server\n",
16-
message.body);
18+
fprintf(
19+
stderr,
20+
"[DEBUG] client : sending list directory request for file %s to server\n",
21+
message.body);
1722
}
1823
void ListDirectoryProtocolServerHandler(int socket, Message message) {
19-
char *arr_ptr = &message.body[0];
24+
25+
// dir (pointer) - used for keeping track of the current directory name.
26+
DIR *dir;
27+
char payload[MAX_BUFFER];
28+
uint16_t protocol;
29+
// If the directory exists.
30+
if ((dir = opendir(message.body)) != NULL) {
31+
struct dirent *ent;
32+
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.
38+
while ((ent = readdir(dir)) != NULL) {
39+
// Prints all of the data to the console.
40+
printf("[DEBUG] '%s\n", ent->d_name);
41+
fprintf(payload, "%s\n", ent->d_name);
42+
}
43+
// Closes file ponter & directory pointer, free's the data and shows where
44+
// the log was saved to.
45+
closedir(dir);
46+
}
47+
// If the directory does not exist.
48+
else if (dir == NULL) {
49+
fprintf(payload,
50+
"You either typed the path incorrectly or the directory does not "
51+
"existn");
52+
53+
closedir(dir);
54+
}
55+
char *arr_ptr = &payload[0];
2056
int payload_length = strlen(arr_ptr);
2157

2258
char *reply = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
23-
int mesg_length = MarshallMessage(reply, 0xC0DE, message.protocol, arr_ptr);
59+
int mesg_length = MarshallMessage(reply, 0xC0DE, protocol, arr_ptr);
2460
if (send(socket, reply, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
2561
perror("write failed: ");
26-
fprintf(stderr, "[DEBUG] Echo Handler Server : Replying bac .... \n");
62+
fprintf(stderr,
63+
"[DEBUG] List Directory Handler Server : Replying kbac .... \n");
2764
}

0 commit comments

Comments
 (0)