~ubuntu-branches/ubuntu/maverick/liblo/maverick

« back to all changes in this revision

Viewing changes to src/server_thread.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Jordens
  • Date: 2004-11-04 22:26:05 UTC
  • Revision ID: james.westby@ubuntu.com-20041104222605-9mxbk1jqhg23wi41
Tags: upstream-0.13
ImportĀ upstreamĀ versionĀ 0.13

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2004 Steve Harris
 
3
 *
 
4
 *  This program is free software; you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License as published by
 
6
 *  the Free Software Foundation; either version 2 of the License, or
 
7
 *  (at your option) any later version.
 
8
 *
 
9
 *  This program is distributed in the hope that it will be useful,
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *  GNU General Public License for more details.
 
13
 *
 
14
 *  $Id: server_thread.c,v 1.1.1.1 2004/08/07 22:21:02 theno23 Exp $
 
15
 */
 
16
 
 
17
#include <unistd.h>
 
18
#include <stdlib.h>
 
19
#include <stdio.h>
 
20
#include <netdb.h>
 
21
#include <string.h>
 
22
#include <pthread.h>
 
23
#include <sys/types.h>
 
24
#include <sys/socket.h>
 
25
 
 
26
#include "lo_types_internal.h"
 
27
#include "lo/lo.h"
 
28
#include "lo/lo_throw.h"
 
29
 
 
30
static void thread_func(void *data);
 
31
 
 
32
lo_server_thread lo_server_thread_new(const char *port, lo_err_handler err_h)
 
33
{
 
34
    lo_server_thread st = malloc(sizeof(struct _lo_server_thread));
 
35
    st->s = lo_server_new(port, err_h);
 
36
    st->active = 0;
 
37
    st->done = 0;
 
38
 
 
39
    if (!st->s) {
 
40
        free(st);
 
41
 
 
42
        return NULL;
 
43
    }
 
44
 
 
45
    return st;
 
46
}
 
47
 
 
48
void lo_server_thread_free(lo_server_thread st)
 
49
{
 
50
    if (st) {
 
51
        if (st->active) {
 
52
            lo_server_thread_stop(st);
 
53
            /* pthread_cancel(st->thread); */
 
54
        }
 
55
        lo_server_free(st->s);
 
56
    }
 
57
    free(st);
 
58
}
 
59
 
 
60
lo_method lo_server_thread_add_method(lo_server_thread st, const char *path,
 
61
                               const char *typespec, lo_method_handler h,
 
62
                               void *user_data)
 
63
{
 
64
    return lo_server_add_method(st->s, path, typespec, h, user_data);
 
65
}
 
66
 
 
67
void lo_server_thread_start(lo_server_thread st)
 
68
{
 
69
    if (!st->active) {
 
70
        st->active = 1;
 
71
        st->done = 0;
 
72
        pthread_create(&(st->thread), NULL, (void *)&thread_func, st);
 
73
    }
 
74
}
 
75
 
 
76
void lo_server_thread_stop(lo_server_thread st)
 
77
{
 
78
    if (st->active) {
 
79
        st->active = 0;
 
80
        while (!st->done) {
 
81
            usleep(1000);
 
82
        }
 
83
    }
 
84
}
 
85
 
 
86
int lo_server_thread_get_port(lo_server_thread st)
 
87
{
 
88
    return lo_server_get_port(st->s);
 
89
}
 
90
 
 
91
char *lo_server_thread_get_url(lo_server_thread st) 
 
92
{
 
93
    return lo_server_get_url(st->s);
 
94
}
 
95
 
 
96
static void thread_func(void *data)
 
97
{
 
98
    lo_server_thread st = (lo_server_thread)data;
 
99
 
 
100
    while (st->active) {
 
101
        lo_server_recv_noblock(st->s, 10);
 
102
    }
 
103
    st->done = 1;
 
104
}
 
105
 
 
106
void lo_server_thread_pp(lo_server_thread st)
 
107
{
 
108
    lo_server_pp(st->s);
 
109
}
 
110
 
 
111
/* vi:set ts=8 sts=4 sw=4: */