File: lcm-example.c

package info (click to toggle)
lcm 1.3.1%2Brepack1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,784 kB
  • sloc: ansic: 16,184; java: 6,843; cs: 2,266; cpp: 1,594; python: 989; makefile: 348; xml: 252; sh: 62
file content (81 lines) | stat: -rw-r--r-- 1,997 bytes parent folder | download | duplicates (5)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/select.h>
#endif

#include <fcntl.h>

#include <lcm/lcm.h>

int test_count = 0;
int test_count2 = 0;
int catchall_count = 0;

void test_handler (const lcm_recv_buf_t *rbuf, const char *channel, void *u)
{
    printf ("handling [%s] msg (content: %s)\n", channel, (char*)rbuf->data);
    test_count++;
}

void test_handler2 (const lcm_recv_buf_t *rbuf, const char *channel, void *u)
{
    printf ("handling [%s] msg (content: %s)\n", channel, (char*)rbuf->data);
    test_count2++;
}

void catchall_handler (const lcm_recv_buf_t *rbuf, const char *channel, void *u)
{
    catchall_count++;
}

int main (int argc, char **argv)
{
    char *data = "payload";
    int datalen = strlen(data) + 1;
    
    lcm_t *lcm = lcm_create (NULL);
    if (! lcm) {
        fprintf (stderr, "couldn't allocate lcm_t\n");
        return 1;
    }

    lcm_subscription_t * s = lcm_subscribe (lcm, "TEST", test_handler, 0);
    lcm_subscribe (lcm, "TEST", test_handler2, 0);
    lcm_unsubscribe ( lcm, s);
//    lcm_subscribe (lcm, ".*", catchall_handler, 0);

    int fd = lcm_get_fileno (lcm);

    int ntransmitted = 0;

    while (ntransmitted < 5) {
        struct timeval timeout = { 1, 0 };
        fd_set readfds;
        FD_ZERO (&readfds);
        FD_SET (fd,&readfds);

        int status=select (fd + 1,&readfds,0,0,&timeout);
        if (0 == status) {
            printf ("sending\n");
            lcm_publish (lcm, "TEST", data, datalen);
            lcm_publish (lcm, "12345", data, datalen);
            ntransmitted++;
        } else if (FD_ISSET (fd,&readfds)) {
            lcm_handle (lcm);
            printf ("=====================================================\n");
        }
    }

    lcm_destroy (lcm);

    printf ("transmitted: %d\n", ntransmitted);
    printf ("test: %d test2: %d catchall: %d\n", 
            test_count, test_count2, catchall_count);

    return 0;
}