summaryrefslogtreecommitdiff
path: root/test/test_track_devices.c
blob: 64c484f315b59047ccbea593aaf186ebbec6c810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* a simple test program, connects to SDB server, and opens a track-devices session */
#include <netdb.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <memory.h>

static void
panic( const char*  msg )
{
    fprintf(stderr, "PANIC: %s: %s\n", msg, strerror(errno));
    exit(1);
}

static int
unix_write( int  fd, const char*  buf, int  len )
{
    int  result = 0;
    while (len > 0) {
        int  len2 = write(fd, buf, len);
        if (len2 < 0) {
            if (errno == EINTR || errno == EAGAIN)
                continue;
            return -1;
        }
        result += len2;
        len -= len2;
        buf += len2;
    }
    return  result;
}

static int
unix_read( int  fd, char*  buf, int  len )
{
    int  result = 0;
    while (len > 0) {
        int  len2 = read(fd, buf, len);
        if (len2 < 0) {
            if (errno == EINTR || errno == EAGAIN)
                continue;
            return -1;
        }
        result += len2;
        len -= len2;
        buf += len2;
    }
    return  result;
}


int  main( void )
{
    int                  ret, s;
    struct sockaddr_in   server;
    char                 buffer[1024];
    const char*          request = "host:track-devices";
    int                  len;

    memset( &server, 0, sizeof(server) );
    server.sin_family      = AF_INET;
    server.sin_port        = htons(5037);
    server.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

    s = socket( PF_INET, SOCK_STREAM, 0 );
    ret = connect( s, (struct sockaddr*) &server, sizeof(server) );
    if (ret < 0) panic( "could not connect to server" );

    /* send the request */
    len = snprintf( buffer, sizeof buffer, "%04x%s", strlen(request), request );
    if (unix_write(s, buffer, len) < 0)
        panic( "could not send request" );

    /* read the OKAY answer */
    if (unix_read(s, buffer, 4) != 4)
        panic( "could not read request" );

    printf( "server answer: %.*s\n", 4, buffer );

    /* now loop */
    for (;;) {
        char  head[5] = "0000";

        if (unix_read(s, head, 4) < 0)
            panic("could not read length");

        if ( sscanf( head, "%04x", &len ) != 1 )
            panic("could not decode length");

        if (unix_read(s, buffer, len) != len)
            panic("could not read data");

        printf( "received header %.*s (%d bytes):\n%.*s", 4, head, len, len, buffer );
    }
    close(s);
}