File: server_thread.c

package info (click to toggle)
freej 0.10git20080824-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 13,504 kB
  • ctags: 19,398
  • sloc: ansic: 135,255; cpp: 32,550; sh: 9,318; perl: 2,932; asm: 2,355; yacc: 1,178; makefile: 1,119; java: 136; lex: 94; python: 16
file content (156 lines) | stat: -rw-r--r-- 3,461 bytes parent folder | download
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
/*
 *  Copyright (C) 2004 Steve Harris
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 *  $Id: server_thread.c 75 2007-03-08 19:01:44Z nhumfrey $
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>

#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <netdb.h>
#include <sys/socket.h>
#endif

#include "lo_types_internal.h"
#include "lo/lo.h"
#include "lo/lo_throw.h"

static void thread_func(void *data);

lo_server_thread lo_server_thread_new(const char *port, lo_err_handler err_h)
{
    return lo_server_thread_new_with_proto(port, LO_DEFAULT, err_h);
}

lo_server_thread lo_server_thread_new_with_proto(const char *port, int proto,
				   lo_err_handler err_h)
{
    lo_server_thread st = malloc(sizeof(struct _lo_server_thread));
    st->s = lo_server_new_with_proto(port, proto, err_h);
    st->active = 0;
    st->done = 0;

    if (!st->s) {
	free(st);

	return NULL;
    }

    return st;
}


void lo_server_thread_free(lo_server_thread st)
{
    if (st) {
	if (st->active) {
	    lo_server_thread_stop(st);
	}
	lo_server_free(st->s);
    }
    free(st);
}

lo_method lo_server_thread_add_method(lo_server_thread st, const char *path,
			       const char *typespec, lo_method_handler h,
			       void *user_data)
{
    return lo_server_add_method(st->s, path, typespec, h, user_data);
}

void lo_server_thread_del_method(lo_server_thread st, const char *path,
			       const char *typespec)
{
    lo_server_del_method(st->s, path, typespec);
}

void lo_server_thread_start(lo_server_thread st)
{
    int result;
	
    if (!st->active) {
	st->active = 1;
	st->done = 0;
	
	// Create the server thread
	result = pthread_create(&(st->thread), NULL, (void *)&thread_func, st);
	if (result) {
	    fprintf(stderr, "Failed to create thread: pthread_create() returned %s\n", strerror(result));
	}
	
    }
}

void lo_server_thread_stop(lo_server_thread st)
{
    int result;

    if (st->active) {
	// Signal thread to stop
	st->active = 0;
	
	// pthread_join waits for thread to terminate 
	// and then releases the thread's resources
	result = pthread_join( st->thread, NULL );
	if (result) {
	    fprintf(stderr, "Failed to stop thread: pthread_join() returned %s\n", strerror(result));
	}
    }
}

int lo_server_thread_get_port(lo_server_thread st)
{
    return lo_server_get_port(st->s);
}

char *lo_server_thread_get_url(lo_server_thread st) 
{
    return lo_server_get_url(st->s);
}

lo_server lo_server_thread_get_server(lo_server_thread st) 
{
    return st->s;
}

int lo_server_thread_events_pending(lo_server_thread st)
{
    return lo_server_events_pending(st->s);
}

static void thread_func(void *data)
{
    lo_server_thread st = (lo_server_thread)data;

    while (st->active) {
	lo_server_recv_noblock(st->s, 10);
    }
    st->done = 1;
    
    pthread_exit(NULL);
}

void lo_server_thread_pp(lo_server_thread st)
{
    lo_server_pp(st->s);
}

/* vi:set ts=8 sts=4 sw=4: */