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

Commit 120fa5e

Browse files
committed
file upload completed
1 parent 33b6330 commit 120fa5e

File tree

10 files changed

+222
-63
lines changed

10 files changed

+222
-63
lines changed

bin/client

4 KB
Binary file not shown.

bin/server

0 Bytes
Binary file not shown.

fixture/client/recieved

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
./bin/*
2-
core.**
1+
this
2+
is a multiline
3+
file

fixture/server/recieved

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
./bin/*
2+
core.**

pkg/client/client.c

+116-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
void Loop(int socket)
44
{
55
int file_count = 1;
6+
char *upload_name = malloc(MAX_BUFFER);
7+
68
fd_set clientFds;
79
// char choice[MAX_BUFFER];
810
int show_menu = 1;
911
int waiting_for_choice = 1;
1012
int waiting_for_reply = 0;
13+
int upload_initiated = 0;
1114
while (1)
1215
{
13-
1416
if (show_menu)
1517
{
1618
printf("\n--------------------------------------------------\n");
@@ -93,6 +95,99 @@ void Loop(int socket)
9395
fclose(fp);
9496
break;
9597
}
98+
case READY_REPLY:
99+
{
100+
101+
if (upload_initiated)
102+
{
103+
char *payload = malloc(MAX_BUFFER);
104+
uint16_t protocol;
105+
106+
FILE *fp = fopen(upload_name, "r");
107+
if (fp == NULL)
108+
{
109+
memset(payload, 0, sizeof(payload));
110+
111+
strcpy(payload, "file not found");
112+
protocol = ERROR_MESSAGE;
113+
}
114+
else
115+
{
116+
/* Go to the end of the file. */
117+
if (fseek(fp, 0L, SEEK_END) == 0)
118+
{
119+
/* Get the size of the file. */
120+
long bufsize = ftell(fp);
121+
if (bufsize == -1)
122+
{
123+
memset(payload, 0, sizeof(payload));
124+
125+
strcpy(payload, "could not get file size");
126+
protocol = ERROR_MESSAGE;
127+
}
128+
else
129+
{
130+
131+
/* Allocate our buffer to that size. */
132+
payload = malloc(sizeof(char) * (bufsize + 1));
133+
134+
/* Go back to the start of the file. */
135+
if (fseek(fp, 0L, SEEK_SET) != 0)
136+
{
137+
memset(payload, 0, sizeof(payload));
138+
139+
strcpy(payload, "could not go to file start");
140+
protocol = ERROR_MESSAGE;
141+
}
142+
else
143+
{
144+
/* Read the entire file into memory. */
145+
size_t newLen = fread(payload, sizeof(char), bufsize, fp);
146+
if (ferror(fp) != 0)
147+
{
148+
memset(payload, 0, sizeof(payload));
149+
150+
strcpy(payload, "Error reading file");
151+
protocol = ERROR_MESSAGE;
152+
}
153+
else
154+
{
155+
payload[newLen++] = '\0'; /* Just to be safe. */
156+
}
157+
}
158+
}
159+
}
160+
fclose(fp);
161+
}
162+
char *arr_ptr = &payload[0];
163+
int payload_length = strlen(arr_ptr);
164+
// fprintf(stderr, "file Content %s\n", arr_ptr);
165+
if (payload_length <= MAX_BUFFER)
166+
{
167+
char *reply = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
168+
int mesg_length = MarshallMessage(reply, 0xC0DE, FILE_REPLY, arr_ptr);
169+
if (send(socket, reply, payload_length + PROTOCOL_HEADER_LEN, 0) == -1)
170+
perror("write failed: ");
171+
fprintf(stderr, "[DEBUG] Download Handler Server : Replying back .... \n");
172+
}
173+
else
174+
{
175+
while (payload_length > 0)
176+
{
177+
char *reply = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
178+
int mesg_length = MarshallMessage(reply, 0xC0DE, FILE_REPLY, arr_ptr);
179+
int sent = send(socket, reply, payload_length + PROTOCOL_HEADER_LEN, 0);
180+
if (sent == -1)
181+
perror("write failed: ");
182+
fprintf(stderr, "[DEBUG] Download Handler Server : Replying back .... \n");
183+
payload_length = payload_length - sent;
184+
}
185+
}
186+
}
187+
upload_initiated = 0;
188+
break;
189+
}
190+
96191
default:
97192
{
98193
break;
@@ -165,7 +260,26 @@ void Loop(int socket)
165260
if (!bcmp(choice, "3", 1))
166261
{
167262
printf("Your choice is Upload Protocol\n");
168-
UploadProtocolSendRequestToServer(socket);
263+
printf("Enter File Name for upload\n");
264+
fgets(upload_name, MAX_BUFFER - 1, stdin);
265+
char *arr_ptr = &upload_name[0];
266+
arr_ptr[strlen(arr_ptr) - 1] = '\0';
267+
268+
char *request = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
269+
int mesg_length = MarshallMessage(request, 0xC0DE, UPLOAD_REQUEST, arr_ptr);
270+
Message message;
271+
message.body = (char *)(request + PROTOCOL_HEADER_LEN);
272+
printf("value cli [%s]\n", arr_ptr);
273+
if (send(socket, request, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
274+
perror("write failed: ");
275+
fprintf(
276+
stderr,
277+
"[DEBUG] client : sending upload request for file %s to server\n",
278+
message.body);
279+
280+
upload_initiated = 1;
281+
continue;
282+
// UploadProtocolSendRequestToServer(socket);
169283
}
170284
// Change
171285
// Directory-----------------------------------------------------------------------------------------

pkg/handlers/handlers.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void *ServerRequestHandler(void *arg) {
2929
DownloadProtocolServerHandler(socket, message);
3030
break;
3131
}
32-
case UPLOAD_REQUEST: {
32+
case FILE_REPLY: {
3333
printf("[DEBUG] Server Recieved Upload Request\n");
3434
UploadProtocolServerHandler(socket, message);
3535
break;
@@ -44,6 +44,7 @@ void *ServerRequestHandler(void *arg) {
4444
ListDirectoryProtocolServerHandler(socket, message);
4545
break;
4646
}
47+
4748
default: {
4849
break;
4950
}

pkg/handlers/handlers.h

-23
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,6 @@ static char* ROOT_DIR = "./";
2525

2626
// 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
2727

28-
typedef enum {
29-
// 'A' in hex
30-
ECHO_REQUEST = 0x0042,
31-
// 'a' in hex
32-
ECHO_REPLY = 0x0062,
33-
// 'D' in hex
34-
DOWNLOAD_REQUEST = 0x0044,
35-
// 'U' in hex
36-
UPLOAD_REQUEST = 0x0055,
37-
// 'R' in hex
38-
READY_REPLY = 0x0052,
39-
// 'F' in hex
40-
FILE_REPLY = 0x0046,
41-
// 'P' in hex
42-
CHANGE_DIR_REQUEST = 0x0050,
43-
// 'L' in hex
44-
LIST_DIR_REQUEST = 0x004C,
45-
// 'l' in hex
46-
LIST_DIR_REPLY = 0x006C,
47-
// 'E' in hex
48-
ERROR_MESSAGE = 0x0045,
49-
UNKNOWN_TYPE = 0xFFFF
50-
} MessageType;
5128
// RequestHandler - this is the main method
5229
// that reads messages from queue and based on
5330
// their protocol, it would redirect them to the approporiate

pkg/handlers/upload.c

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
#include "handlers.h"
2-
void UploadProtocolSendRequestToServer(int socket) {
3-
printf("Enter File name (path) to upload\n");
2+
void UploadProtocolSendRequestToServer(int socket)
3+
{
4+
printf("Enter File Name for upload\n");
45
char input[MAX_BUFFER];
56
fgets(input, MAX_BUFFER - 1, stdin);
67
char *arr_ptr = &input[0];
8+
arr_ptr[strlen(arr_ptr) - 1] = '\0';
79
char *request = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
8-
int mesg_length = MarshallMessage(request, 0xC0DE, DOWNLOAD_REQUEST, input);
10+
int mesg_length = MarshallMessage(request, 0xC0DE, UPLOAD_REQUEST, arr_ptr);
911
Message message;
1012
message.body = (char *)(request + PROTOCOL_HEADER_LEN);
11-
12-
if (send(socket, request, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
13-
perror("write failed: ");
14-
fprintf(stderr,
15-
"[DEBUG] client : sending Upload request for file %s to server\n",
16-
message.body);
1713
}
1814

19-
void UploadProtocolServerHandler(int socket, Message message) {
20-
char *arr_ptr = &message.body[0];
21-
int payload_length = strlen(arr_ptr);
22-
char *reply = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
23-
int mesg_length = MarshallMessage(reply, 0xC0DE, LIST_DIR_REPLY, arr_ptr);
24-
if (send(socket, reply, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
25-
perror("write failed: ");
26-
fprintf(stderr, "[DEBUG] Upload Handler Server : Replying back .... \n");
15+
void UploadProtocolServerHandler(int socket, Message message)
16+
{
17+
fprintf(stderr, "[ File Upload ] : [ %s ]", message.body);
18+
// the following would store the file ...
19+
FILE *fp;
20+
// sscanf(file_count, "./fixture/client/recieved", buf);
21+
fp = fopen("./fixture/server/recieved", "w+");
22+
fprintf(fp, "%s\n", message.body);
23+
fclose(fp);
2724
}

pkg/multiplexer/multiplexer.c

+62-18
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@
22

33
// Adds client's fd to list of client fds and
44
// spawns a new ClientHandler thread for it
5-
void *Multiplex(void *arg) {
5+
void *Multiplex(void *arg)
6+
{
7+
68
Multiplexer *mux = (Multiplexer *)arg;
7-
while (1) {
9+
while (1)
10+
{
811
int clientSocketFd = accept((mux->conn)->socketFd, NULL, NULL);
9-
if (clientSocketFd > 0) {
12+
if (clientSocketFd > 0)
13+
{
1014
fprintf(stderr, " accepted new client. Socket: %d\n", clientSocketFd);
1115
// Obtain lock on clients list and add new client in
1216
pthread_mutex_lock(mux->clientListMutex);
13-
if ((mux->conn)->numClients < MAX_BUFFER) {
17+
if ((mux->conn)->numClients < MAX_BUFFER)
18+
{
1419
// Add new client to list
15-
for (int i = 0; i < MAX_BUFFER; i++) {
16-
if (!FD_ISSET((mux->conn)->clientSockets[i], &(mux->readFds))) {
20+
for (int i = 0; i < MAX_BUFFER; i++)
21+
{
22+
if (!FD_ISSET((mux->conn)->clientSockets[i], &(mux->readFds)))
23+
{
1724
(mux->conn)->clientSockets[i] = clientSocketFd;
1825
i = MAX_BUFFER;
1926
}
@@ -24,13 +31,15 @@ void *Multiplex(void *arg) {
2431

2532
pthread_t clientThread;
2633
if ((pthread_create(&clientThread, NULL, (void *)&ClientHandler,
27-
(void *)mux)) == 0) {
34+
(void *)mux)) == 0)
35+
{
2836
(mux->conn)->numClients++;
2937
fprintf(stderr,
3038
"Client connection to server has been successfully "
3139
"multiplexed on socket: %d\n",
3240
clientSocketFd);
33-
} else
41+
}
42+
else
3443
close(clientSocketFd);
3544
}
3645
pthread_mutex_unlock(mux->clientListMutex);
@@ -39,28 +48,35 @@ void *Multiplex(void *arg) {
3948
}
4049

4150
// ClientHandler - Listens for payloads from client to add to queue
42-
void *ClientHandler(void *arg) {
51+
void *ClientHandler(void *arg)
52+
{
4353
Multiplexer *mux = (Multiplexer *)arg;
4454

4555
Queue *q = mux->Queue;
4656
int clientSocketFd = mux->clientSocketFd;
4757
char *header = malloc(PROTOCOL_HEADER_LEN + 1);
4858
int n;
49-
while ((n = read(clientSocketFd, header, PROTOCOL_HEADER_LEN)) > 1) {
59+
while ((n = read(clientSocketFd, header, PROTOCOL_HEADER_LEN)) > 1)
60+
{
5061
uint16_t magic = ExtractMessageMagic(header);
51-
if (magic == 0xC0DE) {
62+
if (magic == 0xC0DE)
63+
{
5264
uint16_t protocol = ExtractMessageProtocol(header);
5365
uint32_t payload_size = ExtractMessageBodySize(header);
5466
char *recv_buffer = malloc(payload_size);
5567
read(clientSocketFd, recv_buffer, payload_size);
56-
if (strcmp(recv_buffer, "/exit\n") == 0) {
68+
if (strcmp(recv_buffer, "/exit\n") == 0)
69+
{
5770
fprintf(stderr, "Client on socket %d has disconnected.\n",
5871
clientSocketFd);
5972
Disconnect(mux, clientSocketFd);
6073
return NULL;
61-
} else {
74+
}
75+
else
76+
{
6277
// Wait for Queue to not be full before pushing message
63-
while (q->full) {
78+
while (q->full)
79+
{
6480
pthread_cond_wait(q->notFull, q->mutex);
6581
}
6682
pthread_mutex_lock(q->mutex);
@@ -71,7 +87,32 @@ void *ClientHandler(void *arg) {
7187
message.protocol = protocol;
7288
message.size = payload_size;
7389
message.body = recv_buffer;
74-
Push(q, clientSocketFd, message);
90+
if (message.protocol == ERROR_MESSAGE)
91+
{
92+
fprintf(stderr, "[DEBUG] Client on Socket [%d] send server error message [%s] \n", message.message_sender, message.body);
93+
}
94+
else
95+
{
96+
// change dir
97+
if (message.protocol == CHANGE_DIR_REQUEST || message.protocol == UPLOAD_REQUEST)
98+
{
99+
fprintf(stderr, "[DEBUG] srv upload msg protocol[%s] \n", message.body);
100+
101+
char payload[MAX_BUFFER];
102+
char *arr_ptr = &payload[0];
103+
int payload_length = strlen(arr_ptr);
104+
char *reply = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
105+
int mesg_length = MarshallMessage(reply, 0xC0DE, READY_REPLY, arr_ptr);
106+
if (send(message.message_sender, reply, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
107+
perror("write failed: ");
108+
fprintf(stderr, "[DEBUG] Upload Handler Server : Replying back .... \n");
109+
}
110+
111+
else
112+
{
113+
Push(q, clientSocketFd, message);
114+
}
115+
}
75116
}
76117
pthread_mutex_unlock(q->mutex);
77118
pthread_cond_signal(q->notEmpty);
@@ -80,10 +121,13 @@ void *ClientHandler(void *arg) {
80121
}
81122

82123
// Removes the socket from the list of active client sockets and closes it
83-
void Disconnect(Multiplexer *data, int clientSocketFd) {
124+
void Disconnect(Multiplexer *data, int clientSocketFd)
125+
{
84126
pthread_mutex_lock(data->clientListMutex);
85-
for (int i = 0; i < MAX_BUFFER; i++) {
86-
if ((data->conn)->clientSockets[i] == clientSocketFd) {
127+
for (int i = 0; i < MAX_BUFFER; i++)
128+
{
129+
if ((data->conn)->clientSockets[i] == clientSocketFd)
130+
{
87131
(data->conn)->clientSockets[i] = 0;
88132
close(clientSocketFd);
89133
(data->conn)->numClients--;

0 commit comments

Comments
 (0)