CSL 5.2

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

server_thread.c
Go to the documentation of this file.
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 Lesser General Public License as
6 * published by the Free Software Foundation; either version 2.1 of the
7 * License, or (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 Lesser General Public License for more details.
13 *
14 * $Id$
15 */
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <pthread.h>
21 #include <sys/types.h>
22
23 #ifdef WIN32
24 #include <winsock2.h>
25 #include <ws2tcpip.h>
26 #else
27 #include <unistd.h>
28 #include <netdb.h>
29 #include <sys/socket.h>
30 #endif
31
32 #include "lo_types_internal.h"
33 #include "lo/lo.h"
34 #include "lo/lo_throw.h"
35
36 static void thread_func(void *data);
37
38 lo_server_thread lo_server_thread_new(const char *port, lo_err_handler err_h)
39 {
40 return lo_server_thread_new_with_proto(port, LO_DEFAULT, err_h);
41 }
42
43 lo_server_thread lo_server_thread_new_multicast(const char *group, const char *port,
44 lo_err_handler err_h)
45 {
46 lo_server_thread st = malloc(sizeof(struct _lo_server_thread));
47 st->s = lo_server_new_multicast(group, port, err_h);
48 st->active = 0;
49 st->done = 0;
50
51 if (!st->s) {
52 free(st);
53
54 return NULL;
55 }
56
57 return st;
58 }
59
60 lo_server_thread lo_server_thread_new_with_proto(const char *port, int proto,
61 lo_err_handler err_h)
62 {
63 lo_server_thread st = malloc(sizeof(struct _lo_server_thread));
64 st->s = lo_server_new_with_proto(port, proto, err_h);
65 st->active = 0;
66 st->done = 0;
67
68 if (!st->s) {
69 free(st);
70
71 return NULL;
72 }
73
74 return st;
75 }
76
77
78 void lo_server_thread_free(lo_server_thread st)
79 {
80 if (st) {
81 if (st->active) {
82 lo_server_thread_stop(st);
83 }
84 lo_server_free(st->s);
85 }
86 free(st);
87 }
88
89 lo_method lo_server_thread_add_method(lo_server_thread st, const char *path,
90 const char *typespec, lo_method_handler h,
91 void *user_data)
92 {
93 return lo_server_add_method(st->s, path, typespec, h, user_data);
94 }
95
96 void lo_server_thread_del_method(lo_server_thread st, const char *path,
97 const char *typespec)
98 {
99 lo_server_del_method(st->s, path, typespec);
100 }
101
102 int lo_server_thread_start(lo_server_thread st)
103 {
104 int result;
105
106 if (!st->active) {
107 st->active = 1;
108 st->done = 0;
109
110 // Create the server thread
111 result = pthread_create(&(st->thread), NULL, (void *)&thread_func, st);
112 if (result) {
113 fprintf(stderr, "Failed to create thread: pthread_create(), %s",
114 strerror(result));
115 return -result;
116 }
117
118 }
119 return 0;
120 }
121
122 int lo_server_thread_stop(lo_server_thread st)
123 {
124 int result;
125
126 if (st->active) {
127 // Signal thread to stop
128 st->active = 0;
129
130 // pthread_join waits for thread to terminate
131 // and then releases the thread's resources
132 result = pthread_join( st->thread, NULL );
133 if (result) {
134 fprintf(stderr, "Failed to stop thread: pthread_join(), %s",
135 strerror(result));
136 return -result;
137 }
138 }
139
140 return 0;
141 }
142
143 int lo_server_thread_get_port(lo_server_thread st)
144 {
145 return lo_server_get_port(st->s);
146 }
147
148 char *lo_server_thread_get_url(lo_server_thread st)
149 {
150 return lo_server_get_url(st->s);
151 }
152
153 lo_server lo_server_thread_get_server(lo_server_thread st)
154 {
155 return st->s;
156 }
157
158 int lo_server_thread_events_pending(lo_server_thread st)
159 {
160 return lo_server_events_pending(st->s);
161 }
162
163 static void thread_func(void *data)
164 {
165 lo_server_thread st = (lo_server_thread)data;
166
167 while (st->active) {
168 lo_server_recv_noblock(st->s, 10);
169 }
170 st->done = 1;
171
172 pthread_exit(NULL);
173 }
174
175 void lo_server_thread_pp(lo_server_thread st)
176 {
177 lo_server_pp(st->s);
178 }
179
180 /* vi:set ts=8 sts=4 sw=4: */