liblo: example_tcp_echo_server.c (raw)

  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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 *  Copyright (C) 2012 Steve Harris, Stephen Sinclair
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  $Id$
 */

/* Run this program in one terminal with no arguments,
 *   $ ./example_tcp_echo_server
 * and in another terminal with argument 1,
 *   $ ./example_tcp_echo_server 1
 * Now watch the /test message ping back and forth a few times!
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

#include "lo/lo.h"

int done = 0;
int count = 0;

void error(int num, const char *m, const char *path);

int echo_handler(const char *path, const char *types, lo_arg ** argv,
                    int argc, void *data, void *user_data);

int quit_handler(const char *path, const char *types, lo_arg ** argv,
                 int argc, void *data, void *user_data);

void ctrlc(int sig)
{
    done = 1;
}

int main(int argc, char *argv[])
{
    const char *port = "7770";
    int do_send = 0;

    if (argc > 1 && argv[1][0]=='1') {
        do_send = 1;
        port = "7771";
    }

    /* start a new server on port 7770 */
    lo_server_thread st = lo_server_thread_new_with_proto(port, LO_TCP, error);
    if (!st) {
        printf("Could not create server thread.\n");
        exit(1);
    }

    lo_server s = lo_server_thread_get_server(st);

    /* add method that will match the path /quit with no args */
    lo_server_thread_add_method(st, "/quit", "", quit_handler, NULL);

    /* add method that will match any path and args */
    lo_server_thread_add_method(st, NULL, NULL, echo_handler, s);

    lo_server_thread_start(st);

    signal(SIGINT,ctrlc);

    printf("Listening on TCP port %s\n", port);

    lo_address a = 0;
    if (do_send) {
        a = lo_address_new_with_proto(LO_TCP, "localhost", "7770");
        if (!a) {
            printf("Error creating destination address.\n");
            exit(1);
        }
        int r = lo_send_from(a, s, LO_TT_IMMEDIATE, "/test", "ifs",
                             1, 2.0f, "3");
        if (r < 0)
            printf("Error sending initial message.\n");
        if (a) lo_address_free(a);
        a = 0;
    }

    while (!done) {
#ifdef WIN32
        Sleep(1);
#else
        sleep(1);
#endif
    }

    lo_server_thread_free(st);

    return 0;
}

void error(int num, const char *msg, const char *path)
{
    printf("liblo server error %d in path %s: %s\n", num, path, msg);
    fflush(stdout);
}

/* catch any incoming messages, display them, and send them
 * back. */
int echo_handler(const char *path, const char *types, lo_arg ** argv,
                    int argc, void *data, void *user_data)
{
    int i;
    lo_message m = (lo_message)data;
    lo_address a = lo_message_get_source(m);
    lo_server s = (lo_server)user_data;
    const char *host = lo_address_get_hostname(a);
    const char *port = lo_address_get_port(a);

    count ++;

#ifdef WIN32
    Sleep(1);
#else
    sleep(1);
#endif

    printf("path: <%s>\n", path);
    for (i = 0; i < argc; i++) {
        printf("arg %d '%c' ", i, types[i]);
        lo_arg_pp((lo_type)types[i], argv[i]);
        printf("\n");
    }

    if (!a) {
        printf("Couldn't get message source, quitting.\n");
        done = 1;
        return 0;
    }

    int r = lo_send_message_from(a, s, path, m);
    if (r < 0)
        printf("Error sending back message, socket may have closed.\n");
    else
        printf("Sent message back to %s:%s.\n", host, port);
    
    if (count >= 3) {
        printf("Got enough messages, quitting.\n");
        done = 1;
    }

    return 0;
}

int quit_handler(const char *path, const char *types, lo_arg ** argv,
                 int argc, void *data, void *user_data)
{
    done = 1;
    printf("quitting\n\n");
    fflush(stdout);

    return 0;
}