diff options
author | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2014-07-20 14:44:41 +0300 |
---|---|---|
committer | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2014-07-20 14:44:41 +0300 |
commit | eaa3c7b157ffd2a308dfc6d35d79309a9aacbcef (patch) | |
tree | c7822d7097348208e3dfcce82bf626bf59a5cfd2 /examples/network_server/client.c | |
parent | 3cf9668c755560a69e4858dea4f3c415bf807441 (diff) |
Cleanup and comment the code of network_server example.
Update issue 123
Status: FixedInGit
Diffstat (limited to 'examples/network_server/client.c')
-rw-r--r-- | examples/network_server/client.c | 98 |
1 files changed, 62 insertions, 36 deletions
diff --git a/examples/network_server/client.c b/examples/network_server/client.c index e6e9a2e0..00f6dab8 100644 --- a/examples/network_server/client.c +++ b/examples/network_server/client.c @@ -23,9 +23,13 @@ #include "fileproto.pb.h" #include "common.h" +/* This callback function will be called once for each filename received + * from the server. The filenames will be printed out immediately, so that + * no memory has to be allocated for them. + */ bool printfile_callback(pb_istream_t *stream, const pb_field_t *field, void **arg) { - FileInfo fileinfo; + FileInfo fileinfo = {}; if (!pb_decode(stream, FileInfo_fields, &fileinfo)) return false; @@ -35,51 +39,70 @@ bool printfile_callback(pb_istream_t *stream, const pb_field_t *field, void **ar return true; } +/* This function sends a request to socket 'fd' to list the files in + * directory given in 'path'. The results received from server will + * be printed to stdout. + */ bool listdir(int fd, char *path) { - ListFilesRequest request; - ListFilesResponse response; - pb_istream_t input = pb_istream_from_socket(fd); - pb_ostream_t output = pb_ostream_from_socket(fd); - uint8_t zero = 0; - - if (path == NULL) - { - request.has_path = false; - } - else + /* Construct and send the request to server */ { - request.has_path = true; - if (strlen(path) + 1 > sizeof(request.path)) + ListFilesRequest request = {}; + pb_ostream_t output = pb_ostream_from_socket(fd); + uint8_t zero = 0; + + /* In our protocol, path is optional. If it is not given, + * the server will list the root directory. */ + if (path == NULL) + { + request.has_path = false; + } + else { - fprintf(stderr, "Too long path.\n"); + request.has_path = true; + if (strlen(path) + 1 > sizeof(request.path)) + { + fprintf(stderr, "Too long path.\n"); + return false; + } + + strcpy(request.path, path); + } + + /* Encode the request. It is written to the socket immediately + * through our custom stream. */ + if (!pb_encode(&output, ListFilesRequest_fields, &request)) + { + fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&output)); return false; } - strcpy(request.path, path); + /* We signal the end of request with a 0 tag. */ + pb_write(&output, &zero, 1); } - if (!pb_encode(&output, ListFilesRequest_fields, &request)) + /* Read back the response from server */ { - fprintf(stderr, "Encoding failed.\n"); - return false; - } - - /* We signal the end of request with a 0 tag. */ - pb_write(&output, &zero, 1); - - response.file.funcs.decode = &printfile_callback; - - if (!pb_decode(&input, ListFilesResponse_fields, &response)) - { - fprintf(stderr, "Decode failed: %s\n", PB_GET_ERROR(&input)); - return false; - } - - if (response.path_error) - { - fprintf(stderr, "Server reported error.\n"); - return false; + ListFilesResponse response = {}; + pb_istream_t input = pb_istream_from_socket(fd); + + /* Give a pointer to our callback function, which will handle the + * filenames as they arrive. */ + response.file.funcs.decode = &printfile_callback; + + if (!pb_decode(&input, ListFilesResponse_fields, &response)) + { + fprintf(stderr, "Decode failed: %s\n", PB_GET_ERROR(&input)); + return false; + } + + /* If the message from server decodes properly, but directory was + * not found on server side, we get path_error == true. */ + if (response.path_error) + { + fprintf(stderr, "Server reported error.\n"); + return false; + } } return true; @@ -96,6 +119,7 @@ int main(int argc, char **argv) sockfd = socket(AF_INET, SOCK_STREAM, 0); + /* Connect to server running on localhost:1234 */ memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); @@ -107,9 +131,11 @@ int main(int argc, char **argv) return 1; } + /* Send the directory listing request */ if (!listdir(sockfd, path)) return 2; + /* Close connection */ close(sockfd); return 0; |