Showing
6 changed files
with
164 additions
and
22 deletions
@@ -9,6 +9,8 @@ | @@ -9,6 +9,8 @@ | ||
9 | #include "cclass.h" | 9 | #include "cclass.h" |
10 | 10 | ||
11 | 11 | ||
12 | +typedef void (*server_read_hook)(const char *); | ||
13 | + | ||
12 | CLASS(SERVER) { | 14 | CLASS(SERVER) { |
13 | LOGGER logger; | 15 | LOGGER logger; |
14 | SOCK sock; | 16 | SOCK sock; |
@@ -22,12 +24,13 @@ CLASS(SERVER) { | @@ -22,12 +24,13 @@ CLASS(SERVER) { | ||
22 | unsigned int rpos; | 24 | unsigned int rpos; |
23 | unsigned int wpos; | 25 | unsigned int wpos; |
24 | } conns[FD_SETSIZE]; | 26 | } conns[FD_SETSIZE]; |
25 | -}; | ||
26 | 27 | ||
28 | + server_read_hook read_hook; | ||
29 | +}; | ||
27 | 30 | ||
28 | void server_run(SERVER this); | 31 | void server_run(SERVER this); |
29 | -void server_close(SERVER this); | ||
30 | -void server_shutdown(SERVER this); | 32 | +//void server_close(SERVER this); |
33 | +//void server_shutdown(SERVER this); | ||
31 | 34 | ||
32 | #endif // __SERVER_H__ | 35 | #endif // __SERVER_H__ |
33 | 36 |
src/server_run.c
0 → 100644
1 | +#include <sys/select.h> /* for select system call and related */ | ||
2 | +#include <string.h> /* for memset and stuff */ | ||
3 | +#include <stdlib.h> /* for exit */ | ||
4 | +#include <errno.h> /* for errno */ | ||
5 | +#include <unistd.h> | ||
6 | + | ||
7 | +#include "include/logger.h" | ||
8 | +#include "include/server.h" | ||
9 | +#include "include/socket.h" | ||
10 | +#include "include/signalHandling.h" | ||
11 | + | ||
12 | +#undef MAX | ||
13 | +#define MAX(x,y) ((x) > (y) ? (x) : (y)) | ||
14 | + | ||
15 | +static | ||
16 | +fd_set | ||
17 | +server_select(SERVER this) { | ||
18 | + fd_set rfds; | ||
19 | + | ||
20 | + memcpy(&rfds, &(this->fdset), sizeof(fd_set)); | ||
21 | + | ||
22 | + /* | ||
23 | + * wait for handles to become ready | ||
24 | + */ | ||
25 | + if (-1 == select((this->max_fd)+1, &rfds, NULL, NULL, NULL)) | ||
26 | + { | ||
27 | + switch (errno) { | ||
28 | + default: | ||
29 | + case EBADF: | ||
30 | + case EINVAL: | ||
31 | + case ENOMEM: | ||
32 | + doShutdown = 1; | ||
33 | + /* Fallthrough */ | ||
34 | + | ||
35 | + case EINTR: | ||
36 | + logger_log(this->logger, LOGGER_CRIT, | ||
37 | + "select systemcall failed: [%s] - service terminated", | ||
38 | + strerror(errno)); | ||
39 | + exit(EXIT_FAILURE); /* @TODO do real shutdown here */ | ||
40 | + } | ||
41 | + } | ||
42 | + | ||
43 | + return rfds; | ||
44 | +} | ||
45 | + | ||
46 | +static | ||
47 | +void | ||
48 | +server_handle_accept(SERVER this, fd_set * rfds) | ||
49 | +{ | ||
50 | + if (FD_ISSET(this->sock->handle, rfds)) { | ||
51 | + int fd; | ||
52 | + char remoteAddr[16] = ""; | ||
53 | + SOCK acc; | ||
54 | + | ||
55 | + acc = sock_accept(this->sock, remoteAddr); | ||
56 | + | ||
57 | + if (-1 != acc->handle) { | ||
58 | + ((this->conns)[acc->handle].sock)->handle = fd; // save the socket handle | ||
59 | + FD_SET(fd, &(this->fdset)); | ||
60 | + this->max_fd = MAX(fd, this->max_fd); | ||
61 | + } else { | ||
62 | + delete(&acc); | ||
63 | + } | ||
64 | + | ||
65 | + FD_CLR(this->sock->handle, rfds); | ||
66 | + } | ||
67 | +} | ||
68 | + | ||
69 | +static | ||
70 | +void | ||
71 | +server_close_conn(SERVER this, unsigned int handle) | ||
72 | +{ | ||
73 | + delete(&((this->conns)[handle].sock)); | ||
74 | + FD_CLR(handle, &(this->fdset)); | ||
75 | +} | ||
76 | + | ||
77 | +static | ||
78 | +int | ||
79 | +server_read(SERVER this, fd_set * rfds) | ||
80 | +{ | ||
81 | + unsigned int i; | ||
82 | + size_t _read; | ||
83 | + char buffer[1024]; | ||
84 | + | ||
85 | + for (i=3; i<=this->max_fd; i++) { | ||
86 | + if (FD_ISSET(i, rfds)) { | ||
87 | + memset(buffer, 0, 1024); | ||
88 | + switch (_read = read(i, buffer, 1023)) { | ||
89 | + case 0: | ||
90 | + /* | ||
91 | + * normal close: write remaining data | ||
92 | + */ | ||
93 | + /* FALLTHROUGH */ | ||
94 | + | ||
95 | + case -1: | ||
96 | + /* | ||
97 | + * read failure / close connection | ||
98 | + * FALLTHROUGH | ||
99 | + */ | ||
100 | + server_close_conn(this, i); | ||
101 | + break; | ||
102 | + | ||
103 | + default: | ||
104 | + if (NULL != this->read_hook) { | ||
105 | + this->read_hook(buffer); | ||
106 | + } | ||
107 | + break; | ||
108 | + } | ||
109 | + } | ||
110 | + } | ||
111 | + | ||
112 | + return 0; | ||
113 | +} | ||
114 | + | ||
115 | +void | ||
116 | +server_run(SERVER this) | ||
117 | +{ | ||
118 | + /* | ||
119 | + * @TODO again a hint...add verbosity to logger.... | ||
120 | + */ | ||
121 | + logger_log(this->logger, LOGGER_INFO, "service started"); | ||
122 | + | ||
123 | + while (!doShutdown) /* until error or signal */ | ||
124 | + { | ||
125 | + fd_set rfds; | ||
126 | + int i; | ||
127 | + | ||
128 | + rfds = server_select(this); | ||
129 | + | ||
130 | + /* | ||
131 | + * handle accept | ||
132 | + */ | ||
133 | + server_handle_accept(this, &rfds); | ||
134 | + | ||
135 | + /* handle reads */ | ||
136 | + server_read(this, &rfds); | ||
137 | + } | ||
138 | +} | ||
139 | + | ||
140 | +// vim: set ts=4 sw=4: |
1 | #include <signal.h> /* for signal() and signal names */ | 1 | #include <signal.h> /* for signal() and signal names */ |
2 | 2 | ||
3 | -#include "include/monitor.h" | ||
4 | - | ||
5 | volatile int doShutdown; | 3 | volatile int doShutdown; |
6 | 4 | ||
7 | void terminate(int signum) | 5 | void terminate(int signum) |
8 | { | 6 | { |
9 | signal(signum, SIG_IGN); | 7 | signal(signum, SIG_IGN); |
10 | - syslogMonitor(LOG_INFO, MON_CRITICAL, "signals", | ||
11 | - "caugth deadly signal %d", signum); | ||
12 | - syslogMonitor(LOG_INFO, MON_FAILURE, "signals", | ||
13 | - "caugth deadly signal %d - service terminated", signum); | 8 | + /* |
9 | + * @TODO do logging here | ||
10 | + */ | ||
14 | doShutdown = 1; | 11 | doShutdown = 1; |
15 | } | 12 | } |
16 | 13 |
@@ -15,8 +15,8 @@ | @@ -15,8 +15,8 @@ | ||
15 | SOCK | 15 | SOCK |
16 | sock_accept(SOCK this, char remoteAddr[16]) | 16 | sock_accept(SOCK this, char remoteAddr[16]) |
17 | { | 17 | { |
18 | - SOCK sock; /* Socket for client */ | ||
19 | - unsigned int len; /* Length of client address data structure */ | 18 | + SOCK sock; /* Socket for client */ |
19 | + unsigned int len; /* Length of client address data structure */ | ||
20 | 20 | ||
21 | /* Set the size of the in-out parameter */ | 21 | /* Set the size of the in-out parameter */ |
22 | len = sizeof(this->addr); | 22 | len = sizeof(this->addr); |
@@ -15,7 +15,7 @@ loggerTest_CFLAGS = -Wall -ggdb -O0 -I ../include -I .. -I . | @@ -15,7 +15,7 @@ loggerTest_CFLAGS = -Wall -ggdb -O0 -I ../include -I .. -I . | ||
15 | socketTest_SOURCES = $(SOURCES) socketTest.c ../src/logger.c ../src/socket.c ../src/socket_listen.c ../src/socket_accept.c ../src/socket_connect.c | 15 | socketTest_SOURCES = $(SOURCES) socketTest.c ../src/logger.c ../src/socket.c ../src/socket_listen.c ../src/socket_accept.c ../src/socket_connect.c |
16 | socketTest_CFLAGS = -Wall -ggdb -O0 -I ../include -I .. -I . | 16 | socketTest_CFLAGS = -Wall -ggdb -O0 -I ../include -I .. -I . |
17 | 17 | ||
18 | -serverTest_SOURCES = $(SOURCES) serverTest.c ../src/logger.c ../src/socket.c ../src/socket_listen.c ../src/socket_accept.c ../src/server.c | 18 | +serverTest_SOURCES = $(SOURCES) serverTest.c ../src/logger.c ../src/socket.c ../src/socket_listen.c ../src/socket_accept.c ../src/server.c ../src/server_run.c ../src/signalHandling.c |
19 | serverTest_CFLAGS = -Wall -ggdb -O0 -I ../include -I .. -I . | 19 | serverTest_CFLAGS = -Wall -ggdb -O0 -I ../include -I .. -I . |
20 | 20 | ||
21 | EXTRA_DIST = runtest.h mock/class.h | 21 | EXTRA_DIST = runtest.h mock/class.h |
@@ -12,6 +12,16 @@ | @@ -12,6 +12,16 @@ | ||
12 | 12 | ||
13 | int level = -1; | 13 | int level = -1; |
14 | char * msg = NULL; | 14 | char * msg = NULL; |
15 | +char * buffer = NULL; | ||
16 | + | ||
17 | +static void | ||
18 | +read_hook(const char * _buffer) | ||
19 | +{ | ||
20 | + if (NULL != _buffer) { | ||
21 | + buffer = malloc(strlen(_buffer) + 1); | ||
22 | + strcpy(buffer, _buffer); | ||
23 | + } | ||
24 | +} | ||
15 | 25 | ||
16 | static void | 26 | static void |
17 | logfnct_mock(int _level, const char * _msg) | 27 | logfnct_mock(int _level, const char * _msg) |
@@ -40,6 +50,8 @@ __setUp() | @@ -40,6 +50,8 @@ __setUp() | ||
40 | ASSERT_EQUAL(TEST_PORT, server->sock->port); | 50 | ASSERT_EQUAL(TEST_PORT, server->sock->port); |
41 | ASSERT_EQUAL(server->max_fd, server->sock->handle); | 51 | ASSERT_EQUAL(server->max_fd, server->sock->handle); |
42 | 52 | ||
53 | + server->read_hook = read_hook; | ||
54 | + | ||
43 | return TEST_OK; | 55 | return TEST_OK; |
44 | } | 56 | } |
45 | int (* const setUp)() = __setUp; | 57 | int (* const setUp)() = __setUp; |
@@ -76,16 +88,6 @@ testDummy() | @@ -76,16 +88,6 @@ testDummy() | ||
76 | return TEST_OK; | 88 | return TEST_OK; |
77 | } | 89 | } |
78 | 90 | ||
79 | -static | ||
80 | -int | ||
81 | -testAccept() | ||
82 | -{ | ||
83 | - /* | ||
84 | - * @TODO ... | ||
85 | - */ | ||
86 | - return TEST_OK; | ||
87 | -} | ||
88 | - | ||
89 | const testfunc tests[] = { | 91 | const testfunc tests[] = { |
90 | testDummy | 92 | testDummy |
91 | }; | 93 | }; |
Please
register
or
login
to post a comment