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

Commit 33b6330

Browse files
committed
file download completed
1 parent 162b36b commit 33b6330

File tree

10 files changed

+185
-51
lines changed

10 files changed

+185
-51
lines changed

1.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
this
2+
is a multiline
3+
file

bin/client

37.9 KB
Binary file not shown.

bin/server

944 Bytes
Binary file not shown.

fixture/client/recieved

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
./bin/*
2+
core.**

pkg/client/client.c

Lines changed: 80 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
#include "client.h"
22
// Main loop to take in input and display output result from server
3-
void Loop(int socket) {
3+
void Loop(int socket)
4+
{
5+
int file_count = 1;
46
fd_set clientFds;
57
// char choice[MAX_BUFFER];
68
int show_menu = 1;
79
int waiting_for_choice = 1;
810
int waiting_for_reply = 0;
9-
while (1) {
11+
while (1)
12+
{
1013

11-
if (show_menu) {
14+
if (show_menu)
15+
{
1216
printf("\n--------------------------------------------------\n");
1317
puts("Please select your prefer service:\n 1. Echo\n 2. "
1418
"Download\n 3. Upload\n 4. Change Directory\n 5. List "
@@ -23,19 +27,25 @@ void Loop(int socket) {
2327
FD_SET(socket, &clientFds);
2428
FD_SET(0, &clientFds);
2529
// wait for an available socket
26-
if (select(FD_SETSIZE, &clientFds, NULL, NULL, NULL) != -1) {
30+
if (select(FD_SETSIZE, &clientFds, NULL, NULL, NULL) != -1)
31+
{
2732
for (int connection_file_descriptor_socket = 0;
2833
connection_file_descriptor_socket < FD_SETSIZE;
29-
connection_file_descriptor_socket++) {
30-
if (FD_ISSET(connection_file_descriptor_socket, &clientFds)) {
31-
if (connection_file_descriptor_socket == socket) {
34+
connection_file_descriptor_socket++)
35+
{
36+
if (FD_ISSET(connection_file_descriptor_socket, &clientFds))
37+
{
38+
if (connection_file_descriptor_socket == socket)
39+
{
3240
printf("SERVER SOCKET CONNECTED\n");
3341
char *header = malloc(PROTOCOL_HEADER_LEN);
3442
int n = read(socket, header, PROTOCOL_HEADER_LEN);
35-
printf("size read from socket [%d] \n", n);
36-
if (n > 1) {
43+
printf("size read [%d] \n", n);
44+
if (n > 1)
45+
{
3746
uint16_t magic = ExtractMessageMagic(header);
38-
if (magic == 0xC0DE) {
47+
if (magic == 0xC0DE)
48+
{
3949
uint16_t protocol = ExtractMessageProtocol(header);
4050
uint32_t payload_size = ExtractMessageBodySize(header);
4151
char *recv_buffer = malloc(payload_size);
@@ -47,12 +57,15 @@ void Loop(int socket) {
4757
reply.size = payload_size;
4858
reply.body = recv_buffer;
4959

50-
switch (reply.protocol) {
51-
case ERROR_MESSAGE: {
60+
switch (reply.protocol)
61+
{
62+
case ERROR_MESSAGE:
63+
{
5264
fprintf(stderr, "[ ERROR MESSAGE ] : [ %s ]", reply.body);
5365
break;
5466
}
55-
case ECHO_REPLY: {
67+
case ECHO_REPLY:
68+
{
5669
fprintf(stderr,
5770
"MAGIC "
5871
"[0x%04hX] | PROTOCOL "
@@ -62,15 +75,32 @@ void Loop(int socket) {
6275
fprintf(stderr, "[ ECHO FROM SERVER ] ");
6376
break;
6477
}
65-
case LIST_DIR_REPLY: {
78+
case LIST_DIR_REPLY:
79+
{
6680
fprintf(stderr, "[ List Dir Result ] : [ %s ]", reply.body);
6781
break;
6882
}
69-
default: {
83+
case FILE_REPLY:
84+
{
85+
// this can be piped to a file
86+
fprintf(stderr, "[ File Download Reply ] : [ %s ]", reply.body);
87+
// the following would store the file ...
88+
FILE *fp;
89+
char buf[256];
90+
// sscanf(file_count, "./fixture/client/recieved", buf);
91+
fp = fopen("./fixture/client/recieved", "w+");
92+
fprintf(fp, "%s\n", reply.body);
93+
fclose(fp);
94+
break;
95+
}
96+
default:
97+
{
7098
break;
7199
}
72100
}
73-
} else {
101+
}
102+
else
103+
{
74104
break;
75105
}
76106

@@ -83,59 +113,72 @@ void Loop(int socket) {
83113
}
84114

85115
// continue;
86-
if (connection_file_descriptor_socket == 0) {
87-
if (waiting_for_choice) {
116+
if (connection_file_descriptor_socket == 0)
117+
{
118+
if (waiting_for_choice)
119+
{
88120
char choice[MAX_BUFFER];
89-
if (fgets(choice, MAX_BUFFER - 1, stdin) == NULL) {
90-
if (errno == EINTR) {
121+
if (fgets(choice, MAX_BUFFER - 1, stdin) == NULL)
122+
{
123+
if (errno == EINTR)
124+
{
91125
perror("fgets error");
92126
printf("restart...");
93127
continue;
94-
} else {
128+
}
129+
else
130+
{
95131
perror("fgets else error");
96132
break;
97133
}
98134
}
99135
if (bcmp(choice, "1", 1) && bcmp(choice, "2", 1) &&
100136
bcmp(choice, "3", 1) && bcmp(choice, "4", 1) &&
101-
bcmp(choice, "5", 1) && bcmp(choice, "6", 1)) {
102-
printf("Please enter a valid number from 1 to 3\n");
137+
bcmp(choice, "5", 1) && bcmp(choice, "6", 1))
138+
{
139+
printf("Please enter a valid number from 1 to 6\n");
103140
continue;
104141
}
105142
system("clear");
106143

107144
waiting_for_choice = 0;
108145
// Quit-----------------------------------------------------------------------------------------
109-
if (!bcmp(choice, "6", 1)) {
146+
if (!bcmp(choice, "6", 1))
147+
{
110148
printf("Your choice is to Quit the program\n");
111149
leave_request(socket);
112150
exit(0);
113151
}
114152
// Echo-----------------------------------------------------------------------------------------
115-
if (!bcmp(choice, "1", 1)) {
153+
if (!bcmp(choice, "1", 1))
154+
{
116155
printf("Your choice is Echo Protocol\n");
117156
EchoProtocolSendRequestToServer(socket);
118157
}
119158
// Download-----------------------------------------------------------------------------------------
120-
if (!bcmp(choice, "2", 1)) {
159+
if (!bcmp(choice, "2", 1))
160+
{
121161
printf("Your choice is Download Protocol\n");
122162
DownloadProtocolSendRequestToServer(socket);
123163
}
124164
// Upload-----------------------------------------------------------------------------------------
125-
if (!bcmp(choice, "3", 1)) {
165+
if (!bcmp(choice, "3", 1))
166+
{
126167
printf("Your choice is Upload Protocol\n");
127168
UploadProtocolSendRequestToServer(socket);
128169
}
129170
// Change
130171
// Directory-----------------------------------------------------------------------------------------
131172

132-
if (!bcmp(choice, "4", 1)) {
173+
if (!bcmp(choice, "4", 1))
174+
{
133175
printf("Your choice is ChangeDirectory Protocol\n");
134176
ChangeDirectoryProtocolSendRequestToServer(socket);
135177
}
136178
// List
137179
// Directory-----------------------------------------------------------------------------------------
138-
if (!bcmp(choice, "5", 1)) {
180+
if (!bcmp(choice, "5", 1))
181+
{
139182
printf("Your choice is List Directory Protocol\n");
140183
ListDirectoryProtocolSendRequestToServer(socket);
141184
}
@@ -148,27 +191,31 @@ void Loop(int socket) {
148191
}
149192
void establish_connection_with_server(struct sockaddr_in *serverAddr,
150193
struct hostent *host,
151-
int connection_socket, long port) {
194+
int connection_socket, long port)
195+
{
152196
memset(serverAddr, 0, sizeof(serverAddr));
153197
serverAddr->sin_family = AF_INET;
154198
serverAddr->sin_addr = *((struct in_addr *)host->h_addr_list[0]);
155199
serverAddr->sin_port = htons(port);
156200
if (connect(connection_socket, (struct sockaddr *)serverAddr,
157-
sizeof(struct sockaddr)) < 0) {
201+
sizeof(struct sockaddr)) < 0)
202+
{
158203
perror("Couldn't connect to server");
159204
exit(1);
160205
}
161206
}
162207

163-
void set_non_blocking(int file_descriptor) {
208+
void set_non_blocking(int file_descriptor)
209+
{
164210
int flags = fcntl(file_descriptor, F_GETFL);
165211
if (flags < 0)
166212
perror("fcntl failed");
167213

168214
fcntl(file_descriptor, F_SETFL, flags);
169215
}
170216

171-
void leave_request(int socket) {
217+
void leave_request(int socket)
218+
{
172219
if (write(socket, "/exit\n", MAX_BUFFER - 1) == -1)
173220
perror("write failed: ");
174221

pkg/handlers/change_directory.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@ void ChangeDirectoryProtocolSendRequestToServer(int socket) {
44
char input[MAX_BUFFER];
55
fgets(input, MAX_BUFFER - 1, stdin);
66
char *arr_ptr = &input[0];
7+
arr_ptr[strlen(arr_ptr)-1] ='\0' ;
78
char *request = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
8-
int mesg_length = MarshallMessage(request, 0xC0DE, CHANGE_DIR_REQUEST, input);
9+
int mesg_length = MarshallMessage(request, 0xC0DE, CHANGE_DIR_REQUEST, arr_ptr);
910
Message message;
1011
message.body = (char *)(request + PROTOCOL_HEADER_LEN);
11-
1212
if (send(socket, request, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
1313
perror("write failed: ");
1414
fprintf(stderr,
1515
"[DEBUG] client : sending change directory request for file %s to "
1616
"server\n",
1717
message.body);
1818
}
19+
20+
// ROOT_DIR
1921
void ChangeDirectoryProtocolServerHandler(int socket, Message message) {
22+
message.body = ROOT_DIR;
2023
char *arr_ptr = &message.body[0];
2124
int payload_length = strlen(arr_ptr);
2225
char *reply = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
23-
int mesg_length = MarshallMessage(reply, 0xC0DE, message.protocol, arr_ptr);
26+
int mesg_length = MarshallMessage(reply, 0xC0DE, READY_REPLY, arr_ptr);
2427
if (send(socket, reply, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
2528
perror("write failed: ");
2629
fprintf(stderr,

pkg/handlers/download.c

Lines changed: 88 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11

22
#include "handlers.h"
33

4-
void DownloadProtocolSendRequestToServer(int socket) {
4+
void DownloadProtocolSendRequestToServer(int socket)
5+
{
56
printf("Enter File Name for download\n");
67
char input[MAX_BUFFER];
78
fgets(input, MAX_BUFFER - 1, stdin);
89
char *arr_ptr = &input[0];
9-
arr_ptr = Trim(arr_ptr);
10+
arr_ptr[strlen(arr_ptr) - 1] = '\0';
1011
char *request = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
11-
int mesg_length = MarshallMessage(request, 0xC0DE, DOWNLOAD_REQUEST, input);
12+
int mesg_length = MarshallMessage(request, 0xC0DE, DOWNLOAD_REQUEST, arr_ptr);
1213
Message message;
1314
message.body = (char *)(request + PROTOCOL_HEADER_LEN);
1415

@@ -18,13 +19,89 @@ void DownloadProtocolSendRequestToServer(int socket) {
1819
"[DEBUG] client : sending download request for file %s to server\n",
1920
message.body);
2021
}
21-
void DownloadProtocolServerHandler(int socket, Message message) {
22-
char *arr_ptr = &message.body[0];
23-
int payload_length = strlen(arr_ptr);
22+
void DownloadProtocolServerHandler(int socket, Message message)
23+
{
24+
char *payload = malloc(MAX_BUFFER);
25+
uint16_t protocol;
2426

25-
char *reply = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
26-
int mesg_length = MarshallMessage(reply, 0xC0DE, FILE_REPLY, arr_ptr);
27-
if (send(socket, reply, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
28-
perror("write failed: ");
29-
fprintf(stderr, "[DEBUG] Download Handler Server : Replying back .... \n");
27+
FILE *fp = fopen(message.body, "r");
28+
if (fp == NULL)
29+
{
30+
memset(payload, 0, sizeof(payload));
31+
32+
strcpy(payload, "file not found");
33+
protocol = ERROR_MESSAGE;
34+
}
35+
else
36+
{
37+
/* Go to the end of the file. */
38+
if (fseek(fp, 0L, SEEK_END) == 0)
39+
{
40+
/* Get the size of the file. */
41+
long bufsize = ftell(fp);
42+
if (bufsize == -1)
43+
{
44+
memset(payload, 0, sizeof(payload));
45+
46+
strcpy(payload, "could not get file size");
47+
protocol = ERROR_MESSAGE;
48+
}
49+
else
50+
{
51+
52+
/* Allocate our buffer to that size. */
53+
payload = malloc(sizeof(char) * (bufsize + 1));
54+
55+
/* Go back to the start of the file. */
56+
if (fseek(fp, 0L, SEEK_SET) != 0)
57+
{
58+
memset(payload, 0, sizeof(payload));
59+
60+
strcpy(payload, "could not go to file start");
61+
protocol = ERROR_MESSAGE;
62+
}
63+
else
64+
{
65+
/* Read the entire file into memory. */
66+
size_t newLen = fread(payload, sizeof(char), bufsize, fp);
67+
if (ferror(fp) != 0)
68+
{
69+
memset(payload, 0, sizeof(payload));
70+
71+
strcpy(payload, "Error reading file");
72+
protocol = ERROR_MESSAGE;
73+
}
74+
else
75+
{
76+
payload[newLen++] = '\0'; /* Just to be safe. */
77+
}
78+
}
79+
}
80+
}
81+
fclose(fp);
82+
}
83+
char *arr_ptr = &payload[0];
84+
int payload_length = strlen(arr_ptr);
85+
// fprintf(stderr, "file Content %s\n", arr_ptr);
86+
if (payload_length <= MAX_BUFFER)
87+
{
88+
char *reply = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
89+
int mesg_length = MarshallMessage(reply, 0xC0DE, FILE_REPLY, arr_ptr);
90+
if (send(socket, reply, payload_length + PROTOCOL_HEADER_LEN, 0) == -1)
91+
perror("write failed: ");
92+
fprintf(stderr, "[DEBUG] Download Handler Server : Replying back .... \n");
93+
}
94+
else
95+
{
96+
while (payload_length > 0)
97+
{
98+
char *reply = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
99+
int mesg_length = MarshallMessage(reply, 0xC0DE, FILE_REPLY, arr_ptr);
100+
int sent = send(socket, reply, payload_length + PROTOCOL_HEADER_LEN, 0);
101+
if (sent == -1)
102+
perror("write failed: ");
103+
fprintf(stderr, "[DEBUG] Download Handler Server : Replying back .... \n");
104+
payload_length = payload_length - sent;
105+
}
106+
}
30107
}

0 commit comments

Comments
 (0)