1
1
#include "client.h"
2
2
// 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 ;
4
6
fd_set clientFds ;
5
7
// char choice[MAX_BUFFER];
6
8
int show_menu = 1 ;
7
9
int waiting_for_choice = 1 ;
8
10
int waiting_for_reply = 0 ;
9
- while (1 ) {
11
+ while (1 )
12
+ {
10
13
11
- if (show_menu ) {
14
+ if (show_menu )
15
+ {
12
16
printf ("\n--------------------------------------------------\n" );
13
17
puts ("Please select your prefer service:\n 1. Echo\n 2. "
14
18
"Download\n 3. Upload\n 4. Change Directory\n 5. List "
@@ -23,19 +27,25 @@ void Loop(int socket) {
23
27
FD_SET (socket , & clientFds );
24
28
FD_SET (0 , & clientFds );
25
29
// 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
+ {
27
32
for (int connection_file_descriptor_socket = 0 ;
28
33
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
+ {
32
40
printf ("SERVER SOCKET CONNECTED\n" );
33
41
char * header = malloc (PROTOCOL_HEADER_LEN );
34
42
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
+ {
37
46
uint16_t magic = ExtractMessageMagic (header );
38
- if (magic == 0xC0DE ) {
47
+ if (magic == 0xC0DE )
48
+ {
39
49
uint16_t protocol = ExtractMessageProtocol (header );
40
50
uint32_t payload_size = ExtractMessageBodySize (header );
41
51
char * recv_buffer = malloc (payload_size );
@@ -47,12 +57,15 @@ void Loop(int socket) {
47
57
reply .size = payload_size ;
48
58
reply .body = recv_buffer ;
49
59
50
- switch (reply .protocol ) {
51
- case ERROR_MESSAGE : {
60
+ switch (reply .protocol )
61
+ {
62
+ case ERROR_MESSAGE :
63
+ {
52
64
fprintf (stderr , "[ ERROR MESSAGE ] : [ %s ]" , reply .body );
53
65
break ;
54
66
}
55
- case ECHO_REPLY : {
67
+ case ECHO_REPLY :
68
+ {
56
69
fprintf (stderr ,
57
70
"MAGIC "
58
71
"[0x%04hX] | PROTOCOL "
@@ -62,15 +75,32 @@ void Loop(int socket) {
62
75
fprintf (stderr , "[ ECHO FROM SERVER ] " );
63
76
break ;
64
77
}
65
- case LIST_DIR_REPLY : {
78
+ case LIST_DIR_REPLY :
79
+ {
66
80
fprintf (stderr , "[ List Dir Result ] : [ %s ]" , reply .body );
67
81
break ;
68
82
}
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
+ {
70
98
break ;
71
99
}
72
100
}
73
- } else {
101
+ }
102
+ else
103
+ {
74
104
break ;
75
105
}
76
106
@@ -83,59 +113,72 @@ void Loop(int socket) {
83
113
}
84
114
85
115
// 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
+ {
88
120
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
+ {
91
125
perror ("fgets error" );
92
126
printf ("restart..." );
93
127
continue ;
94
- } else {
128
+ }
129
+ else
130
+ {
95
131
perror ("fgets else error" );
96
132
break ;
97
133
}
98
134
}
99
135
if (bcmp (choice , "1" , 1 ) && bcmp (choice , "2" , 1 ) &&
100
136
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" );
103
140
continue ;
104
141
}
105
142
system ("clear" );
106
143
107
144
waiting_for_choice = 0 ;
108
145
// Quit-----------------------------------------------------------------------------------------
109
- if (!bcmp (choice , "6" , 1 )) {
146
+ if (!bcmp (choice , "6" , 1 ))
147
+ {
110
148
printf ("Your choice is to Quit the program\n" );
111
149
leave_request (socket );
112
150
exit (0 );
113
151
}
114
152
// Echo-----------------------------------------------------------------------------------------
115
- if (!bcmp (choice , "1" , 1 )) {
153
+ if (!bcmp (choice , "1" , 1 ))
154
+ {
116
155
printf ("Your choice is Echo Protocol\n" );
117
156
EchoProtocolSendRequestToServer (socket );
118
157
}
119
158
// Download-----------------------------------------------------------------------------------------
120
- if (!bcmp (choice , "2" , 1 )) {
159
+ if (!bcmp (choice , "2" , 1 ))
160
+ {
121
161
printf ("Your choice is Download Protocol\n" );
122
162
DownloadProtocolSendRequestToServer (socket );
123
163
}
124
164
// Upload-----------------------------------------------------------------------------------------
125
- if (!bcmp (choice , "3" , 1 )) {
165
+ if (!bcmp (choice , "3" , 1 ))
166
+ {
126
167
printf ("Your choice is Upload Protocol\n" );
127
168
UploadProtocolSendRequestToServer (socket );
128
169
}
129
170
// Change
130
171
// Directory-----------------------------------------------------------------------------------------
131
172
132
- if (!bcmp (choice , "4" , 1 )) {
173
+ if (!bcmp (choice , "4" , 1 ))
174
+ {
133
175
printf ("Your choice is ChangeDirectory Protocol\n" );
134
176
ChangeDirectoryProtocolSendRequestToServer (socket );
135
177
}
136
178
// List
137
179
// Directory-----------------------------------------------------------------------------------------
138
- if (!bcmp (choice , "5" , 1 )) {
180
+ if (!bcmp (choice , "5" , 1 ))
181
+ {
139
182
printf ("Your choice is List Directory Protocol\n" );
140
183
ListDirectoryProtocolSendRequestToServer (socket );
141
184
}
@@ -148,27 +191,31 @@ void Loop(int socket) {
148
191
}
149
192
void establish_connection_with_server (struct sockaddr_in * serverAddr ,
150
193
struct hostent * host ,
151
- int connection_socket , long port ) {
194
+ int connection_socket , long port )
195
+ {
152
196
memset (serverAddr , 0 , sizeof (serverAddr ));
153
197
serverAddr -> sin_family = AF_INET ;
154
198
serverAddr -> sin_addr = * ((struct in_addr * )host -> h_addr_list [0 ]);
155
199
serverAddr -> sin_port = htons (port );
156
200
if (connect (connection_socket , (struct sockaddr * )serverAddr ,
157
- sizeof (struct sockaddr )) < 0 ) {
201
+ sizeof (struct sockaddr )) < 0 )
202
+ {
158
203
perror ("Couldn't connect to server" );
159
204
exit (1 );
160
205
}
161
206
}
162
207
163
- void set_non_blocking (int file_descriptor ) {
208
+ void set_non_blocking (int file_descriptor )
209
+ {
164
210
int flags = fcntl (file_descriptor , F_GETFL );
165
211
if (flags < 0 )
166
212
perror ("fcntl failed" );
167
213
168
214
fcntl (file_descriptor , F_SETFL , flags );
169
215
}
170
216
171
- void leave_request (int socket ) {
217
+ void leave_request (int socket )
218
+ {
172
219
if (write (socket , "/exit\n" , MAX_BUFFER - 1 ) == -1 )
173
220
perror ("write failed: " );
174
221
0 commit comments