summaryrefslogtreecommitdiffstats
path: root/.gitignore
blob: fbaa2da15ec1bf3e3e34ae6af7580952c77cebd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
amixer
autom4te.cache
config.log 
build/*
dist/*
!.gitignore
.dep.inc
CMakeFiles/
CMakeCache.txt
nbproject/private/*
Makefile
cmake_install.cmake
*.so
.vscode
stress-out*
olor: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
/* This is a simple TCP client that connects to port 1234 and prints a list
 * of files in a given directory.
 *
 * It directly deserializes and serializes messages from network, minimizing
 * memory use.
 * 
 * For flexibility, this example is implemented using posix api.
 * In a real embedded system you would typically use some other kind of
 * a communication and filesystem layer.
 */

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>

#include <pb_encode.h>
#include <pb_decode.h>

#include "fileproto.h"
#include "common.h"

bool printfile_callback(pb_istream_t *stream, const pb_field_t *field, void *arg)
{
    FileInfo fileinfo;
    
    if (!pb_decode(stream, FileInfo_fields, &fileinfo))
        return false;
    
    printf("%-10lld %s\n", fileinfo.inode, fileinfo.name);
    
    return true;
}

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
    {
        request.has_path = true;
        if (strlen(path) + 1 > sizeof(request.path))
        {
            fprintf(stderr, "Too long path.\n");
            return false;
        }
        
        strcpy(request.path, path);
    }
    
    if (!pb_encode(&output, ListFilesRequest_fields, &request))
    {
        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, "Decoding failed.\n");
        return false;
    }
    
    if (response.path_error)
    {
        fprintf(stderr, "Server reported error.\n");
        return false;
    }
    
    return true;
}

int main(int argc, char **argv)
{
    int sockfd;
    struct sockaddr_in servaddr;
    char *path = NULL;
    
    if (argc > 1)
        path = argv[1];
    
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    servaddr.sin_port = htons(1234);
    
    if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0)
    {
        perror("connect");
        return 1;
    }
    
    if (!listdir(sockfd, path))
        return 2;
    
    close(sockfd);
    
    return 0;
}