status_server.c 2.76 KB
Newer Older
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
/*
	status server

	Copyright Jeroen Vreeken (jeroen@vreeken.net), 2013
	Copyright Stichting C.A. Muller Radioastronomiestation, 2013

	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 3 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.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include "tcp_listen.h"
 
struct status_client {
	struct status_client *next;
	
	int fd;
};

struct status_server {
	int fd;

	struct status_client *clients;
};



struct status_server *status_server_create(int port, int local, int backlog)
{
	struct status_server *srv;
	
	srv = calloc(1, sizeof(struct status_server));
	if (!srv)
		goto err_alloc;
	
	srv->fd = tcp_listen(port, local, backlog);
	if (srv->fd < 0)
		goto err_listen;
	
	ioctl(srv->fd, FIONBIO, &(int){ 1 });
	
	return srv;

err_listen:
	close(srv->fd);
err_alloc:
	return NULL;
}

void status_server_destroy(struct status_server *srv)
{
	struct status_client *client;

	if (srv) {
		while (srv->clients) {
			client = srv->clients;
			srv->clients = client->next;
			
			close(client->fd);
			free(client);
		}
	
		close(srv->fd);
		free(srv);
	}
}



int status_server_fdset_add(struct status_server *srv, fd_set *set, int *high)
{
	FD_SET(srv->fd, set);
	if (srv->fd >= *high)
		*high = srv->fd + 1;
		
	return 0;
}


int status_server_fdset_handle(struct status_server *srv, fd_set *set)
{
	struct status_client *client;

	if (FD_ISSET(srv->fd, set)) {
		/* New client */
		int client_fd;
		
		client_fd = tcp_accept(srv->fd);
		if (client_fd < 0)
			return -1;
		ioctl(client_fd, FIONBIO, &(int){ 1 });
		
		client = calloc(1, sizeof(struct status_client));
		if (!client) {
			close(client_fd);
		} else {
			client->fd = client_fd;
			client->next = srv->clients;
			srv->clients = client;
		}
	}
	return 0;	
}

int status_server_send(struct status_server *srv, char *status)
{
	struct status_client **clientp, **nextp;

	for (clientp = &srv->clients; *clientp; clientp = nextp){
		int ret;
		
		ret = write((*clientp)->fd, status, strlen(status));
		if (ret <= 0) {
			struct status_client *tmp;
			
			tmp = *clientp;
			close(tmp->fd);
			*clientp = (*clientp)->next;
			free(tmp);
			nextp = clientp;
		} else {
			nextp = &(*clientp)->next;
		}
	}
	return 0;
}