Commit 2d6305c3d6348431203efcb477726f7897d75fdf
1 parent
2dcc288a
changed class tool. Now multiple interface per class are supported as well as simple inheritence.
Showing
27 changed files
with
519 additions
and
430 deletions
include/cclass.h
deleted
100644 → 0
1 | -/** | ||
2 | - * \file | ||
3 | - * cclass.h: basic "class-like" handling of code and data structures | ||
4 | - * Copyright (C) 2011 Georg Hopp | ||
5 | - * | ||
6 | - * This program is free software: you can redistribute it and/or modify | ||
7 | - * it under the terms of the GNU General Public License as published by | ||
8 | - * the Free Software Foundation, either version 3 of the License, or | ||
9 | - * (at your option) any later version. | ||
10 | - * | ||
11 | - * This program is distributed in the hope that it will be useful, | ||
12 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | - * GNU General Public License for more details. | ||
15 | - * | ||
16 | - * You should have received a copy of the GNU General Public License | ||
17 | - * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
18 | - */ | ||
19 | - | ||
20 | -#ifndef __CCLASS_H__ | ||
21 | -#define __CCLASS_H__ | ||
22 | - | ||
23 | -#include <stdarg.h> | ||
24 | -#include <sys/types.h> | ||
25 | -#include <json/json.h> | ||
26 | - | ||
27 | -#define CCLASS_MAGIC 0xFEFE | ||
28 | - | ||
29 | - | ||
30 | -typedef void (* ctor)(void *, va_list *); | ||
31 | -typedef void (* clr)(void *); | ||
32 | -typedef void (* dtor)(void *); | ||
33 | -typedef void (* jCtor)(void *, struct json_object *); | ||
34 | -typedef void (* jTo)(void *, struct json_object **); | ||
35 | - | ||
36 | -typedef struct _CCLASS { | ||
37 | - const int magic; | ||
38 | - size_t size; | ||
39 | - ctor __construct; | ||
40 | - clr __clear; | ||
41 | - dtor __destruct; | ||
42 | - jCtor __jsonConst; | ||
43 | - jTo __toJson; | ||
44 | -} * CCLASS; | ||
45 | - | ||
46 | -#define CCLASS_SIZE sizeof(struct _CCLASS) | ||
47 | -#define CCLASS_PTR_SIZE sizeof(CCLASS) | ||
48 | - | ||
49 | -#define __construct(_class) \ | ||
50 | - static void __construct(_class this, va_list * params) | ||
51 | -#define __clear(_class) \ | ||
52 | - static void __clear(_class this) | ||
53 | -#define __destruct(_class) \ | ||
54 | - static void __destruct(_class this) | ||
55 | -#define __jsonConst(_class) \ | ||
56 | - static void __jsonConst(_class this, struct json_object * json) | ||
57 | -#define __toJson(_class) \ | ||
58 | - static void __toJson(_class this, struct json_object ** json) | ||
59 | - | ||
60 | -#define CLASS(_class) \ | ||
61 | - struct _##_class; \ | ||
62 | - typedef struct _##_class * _class; \ | ||
63 | - extern const CCLASS const __##_class; \ | ||
64 | - struct _##_class | ||
65 | - | ||
66 | -#define INIT_CLASS(_class) \ | ||
67 | - __construct(_class); \ | ||
68 | - __clear(_class); \ | ||
69 | - __destruct(_class); \ | ||
70 | - __jsonConst(_class); \ | ||
71 | - __toJson(_class); \ | ||
72 | - static const struct _CCLASS _cclass = { \ | ||
73 | - CCLASS_MAGIC, \ | ||
74 | - sizeof(struct _##_class), \ | ||
75 | - (ctor)__construct, \ | ||
76 | - (clr)__clear, \ | ||
77 | - (dtor)__destruct, \ | ||
78 | - (jCtor)__jsonConst, \ | ||
79 | - (jTo)__toJson \ | ||
80 | - }; const CCLASS __##_class = (const CCLASS)&_cclass | ||
81 | - | ||
82 | -void * _new(const CCLASS _class, ...); | ||
83 | -#define new(_class, ...) _new((__##_class), __VA_ARGS__) | ||
84 | - | ||
85 | -void * _newFromJson(const CCLASS _class, struct json_object * json); | ||
86 | -#define newFromJson(_class, json) _newFromJson((__##_class), (json)) | ||
87 | - | ||
88 | -int _instanceOf(const CCLASS _class, void * _object); | ||
89 | -#define instanceOf(_class, object) _instanceOf((__##_class), (object)) | ||
90 | - | ||
91 | -void clear(void * _object); | ||
92 | -void delete(void * _object); | ||
93 | -void toJson(void * _object, struct json_object ** json); | ||
94 | -int isObject(void * _object); | ||
95 | - | ||
96 | -#endif//__CCLASS_H__ | ||
97 | - | ||
98 | -// vim: set et ts=4 sw=4: |
include/class.h
0 → 100644
1 | +#ifndef __CLASS_H__ | ||
2 | +#define __CLASS_H__ | ||
3 | + | ||
4 | +#include <stdarg.h> | ||
5 | +#include <sys/types.h> | ||
6 | +#include <string.h> | ||
7 | +#include <assert.h> | ||
8 | + | ||
9 | +#include "interface.h" | ||
10 | + | ||
11 | +#define _ISOC99_SOURCE | ||
12 | + | ||
13 | +#define CLASS_MAGIC 0xFEFE | ||
14 | + | ||
15 | +#define CLASS(name) \ | ||
16 | + struct c_##name; \ | ||
17 | + typedef struct c_##name * name; \ | ||
18 | + extern struct class * const _##name; \ | ||
19 | + struct c_##name | ||
20 | + | ||
21 | +#define EXTENDS(parent) \ | ||
22 | + const char _[sizeof(struct c_##parent)] | ||
23 | + | ||
24 | +#define _NULL NULL | ||
25 | +#define CREATE_CLASS(name,_parent,...) \ | ||
26 | + static struct class c_##name; \ | ||
27 | + static void _classInit_(void) { \ | ||
28 | + c_##name.parent = _##_parent; \ | ||
29 | + c_##name.init = NULL; \ | ||
30 | + } \ | ||
31 | + static struct class c_##name = { \ | ||
32 | + CLASS_MAGIC, \ | ||
33 | + NULL, \ | ||
34 | + sizeof(struct c_##name), \ | ||
35 | + _classInit_, \ | ||
36 | + INIT_IMPL(__VA_ARGS__) \ | ||
37 | + }; struct class * const _##name = &c_##name | ||
38 | + | ||
39 | +/** | ||
40 | + * @TODO: actually i use gcc feature ## for variadoc... think about | ||
41 | + * a way to make this standard. | ||
42 | + */ | ||
43 | +#define CALL(object,_iface,method,...) \ | ||
44 | + do { \ | ||
45 | + class_ptr class = class_getClass((object)); \ | ||
46 | + struct i_##_iface * iface; \ | ||
47 | + if (class->init) class->init(); \ | ||
48 | + iface = (struct i_##_iface *)class_getInterface(&class, &i_##_iface); \ | ||
49 | + while ((NULL == iface || NULL == iface->method) && HAS_PARENT(class)) { \ | ||
50 | + class = class->parent; \ | ||
51 | + if (class->init) class->init(); \ | ||
52 | + iface = (struct i_##_iface *)class_getInterface(&class, &i_##_iface); \ | ||
53 | + }; \ | ||
54 | + assert(NULL != iface->method); \ | ||
55 | + iface->method(object, ##__VA_ARGS__); \ | ||
56 | + } while(0) | ||
57 | + | ||
58 | + | ||
59 | +#define IFACE_GET(class,iface) (interfaceGet(&((class)->impl),(iface))) | ||
60 | +#define IFACE_EXISTS(class,iface) (NULL != IFACE_GET((class),(iface))) | ||
61 | + | ||
62 | +#define HAS_PARENT(class) (NULL != ((class)->parent)) | ||
63 | + | ||
64 | +typedef void (* fptr_classInit)(void); | ||
65 | + | ||
66 | +struct class; | ||
67 | +typedef struct class * class_ptr; | ||
68 | +struct class { | ||
69 | + const int magic; | ||
70 | + class_ptr parent; | ||
71 | + size_t object_size; | ||
72 | + fptr_classInit init; | ||
73 | + struct iface_impl impl; | ||
74 | +}; | ||
75 | + | ||
76 | +extern inline void * class_getInterface(class_ptr *, iface_ptr); | ||
77 | +extern inline class_ptr class_getClass(void *); | ||
78 | + | ||
79 | +#endif // __CLASS_H__ | ||
80 | + | ||
81 | +// vim: set ts=4 sw=4: |
include/interface.h
0 → 100644
1 | +#ifndef __INTERFACE_H__ | ||
2 | +#define __INTERFACE_H__ | ||
3 | + | ||
4 | +#include <sys/types.h> | ||
5 | + | ||
6 | +#define MAX_IFACE 32 // ATTENTION: every iface_impl will use MAX_IFACE * sizeof(void*) | ||
7 | + | ||
8 | +#define IFACE(name) ((const struct i_##name const*)&i_##name##_impl) | ||
9 | + | ||
10 | +#define INIT_IFACE(name,...) \ | ||
11 | + static const struct i_##name i_##name##_impl = {&i_##name,__VA_ARGS__} | ||
12 | + | ||
13 | +#define NUMARGS(...) (sizeof((const void*[]){__VA_ARGS__})/sizeof(void*)) | ||
14 | + | ||
15 | +#define INIT_IMPL(...) {NUMARGS(__VA_ARGS__), 0, {__VA_ARGS__}} | ||
16 | +#define CREATE_IMPL(...) \ | ||
17 | + static struct iface_impl iface_impl = INIT_IMPL(__VA_ARGS__) | ||
18 | + | ||
19 | +#define METHOD_GET(iface,method) (iface->method) | ||
20 | + | ||
21 | + | ||
22 | +struct interface { | ||
23 | + const char * name; | ||
24 | + const size_t nmethods; | ||
25 | +}; | ||
26 | +typedef const struct interface * iface_ptr; | ||
27 | + | ||
28 | +struct iface_impl { | ||
29 | + const size_t nimpl; // number of interface implementations | ||
30 | + char simpl; // implementations sorted?? | ||
31 | + const void * impl[MAX_IFACE]; // implementations | ||
32 | +}; | ||
33 | +typedef struct iface_impl * iface_impl_ptr; | ||
34 | + | ||
35 | +extern struct interface * interfaceGet(iface_impl_ptr, const iface_ptr); | ||
36 | + | ||
37 | +#endif // __INTERFACE_H__ | ||
38 | + | ||
39 | +// vim: set ts=4 sw=4: |
include/interface/class.h
0 → 100644
1 | +#ifndef __INTERFACE_CLASS_H__ | ||
2 | +#define __INTERFACE_CLASS_H__ | ||
3 | + | ||
4 | +#include <stdarg.h> | ||
5 | + | ||
6 | +#include "class.h" | ||
7 | +#include "interface.h" | ||
8 | + | ||
9 | +typedef void (* fptr_ctor)(void *, va_list *); | ||
10 | +typedef void (* fptr_dtor)(void *); | ||
11 | +typedef void (* fptr_clone)(void *, const void * const); | ||
12 | + | ||
13 | +extern const struct interface i_Class; | ||
14 | + | ||
15 | +struct i_Class { | ||
16 | + const struct interface * const _; | ||
17 | + fptr_ctor ctor; | ||
18 | + fptr_dtor dtor; | ||
19 | + fptr_clone clone; | ||
20 | +}; | ||
21 | + | ||
22 | +extern inline void * classNew(class_ptr, ...); | ||
23 | +extern inline void classDelete(void **); | ||
24 | + | ||
25 | +#define new(class,...) classNew(_##class, __VA_ARGS__) | ||
26 | +#define delete(object) classDelete((void **)(object)) | ||
27 | + | ||
28 | +#endif // __INTERFACE_CLASS_H__ | ||
29 | + | ||
30 | +// vim: set ts=4 sw=4: |
include/interface/logger.h
0 → 100644
1 | +#ifndef __INTERFACE_LOGGER_H__ | ||
2 | +#define __INTERFACE_LOGGER_H__ | ||
3 | + | ||
4 | +#include <stdarg.h> | ||
5 | + | ||
6 | +#include "interface.h" | ||
7 | +#include "logger.h" | ||
8 | + | ||
9 | +typedef void (* fptr_log)(void *, logger_level, const char * const); | ||
10 | + | ||
11 | +extern const struct interface i_Logger; | ||
12 | + | ||
13 | +struct i_Logger { | ||
14 | + const struct interface * const _; | ||
15 | + fptr_log log; | ||
16 | +}; | ||
17 | + | ||
18 | +extern inline void loggerLog(void *, logger_level, const char * const, ...); | ||
19 | + | ||
20 | +#endif // __INTERFACE_LOGGER_H__ | ||
21 | + | ||
22 | +// vim: set ts=4 sw=4: |
1 | #ifndef __LOGGER_H__ | 1 | #ifndef __LOGGER_H__ |
2 | #define __LOGGER_H__ | 2 | #define __LOGGER_H__ |
3 | 3 | ||
4 | -#include "cclass.h" | ||
5 | - | ||
6 | - | ||
7 | -#define LOGGER_EMERG 0 | ||
8 | -#define LOGGER_ALERT 1 | ||
9 | -#define LOGGER_CRIT 2 | ||
10 | -#define LOGGER_ERR 3 | ||
11 | -#define LOGGER_WARNING 4 | ||
12 | -#define LOGGER_NOTICE 5 | ||
13 | -#define LOGGER_INFO 6 | ||
14 | -#define LOGGER_DEBUG 7 | ||
15 | - | ||
16 | -#define MAX_LOG_FNCTS 10 | ||
17 | - | ||
18 | - | ||
19 | -typedef void (*logger_logfnct)(int level, const char * msg); | ||
20 | - | ||
21 | - | ||
22 | -CLASS(LOGGER) { | ||
23 | - logger_logfnct logfncts[MAX_LOG_FNCTS]; | ||
24 | - unsigned int logfncts_count; | 4 | +#include "class.h" |
5 | + | ||
6 | +typedef enum logger_level { | ||
7 | + LOGGER_DEBUG=0, | ||
8 | + LOGGER_INFO, | ||
9 | + LOGGER_NOTICE, | ||
10 | + LOGGER_WARNING, | ||
11 | + LOGGER_ERR, | ||
12 | + LOGGER_CRIT, | ||
13 | + LOGGER_ALERT, | ||
14 | + LOGGER_EMERG | ||
15 | +} logger_level; | ||
16 | + | ||
17 | +extern const char * const logger_level_str[]; | ||
18 | + | ||
19 | +CLASS(Logger) { | ||
20 | + logger_level min_level; | ||
25 | }; | 21 | }; |
26 | 22 | ||
27 | -void logger_log(LOGGER this, int level, const char * msg, ...) | ||
28 | - __attribute__((format (printf, 3, 4))); | 23 | +CLASS(LoggerStderr) { |
24 | + EXTENDS(Logger); | ||
25 | +}; | ||
29 | 26 | ||
30 | -void logger_add(LOGGER this, logger_logfnct logfunc); | 27 | +CLASS(LoggerSyslog) { |
28 | + EXTENDS(Logger); | ||
29 | +}; | ||
31 | 30 | ||
32 | -#endif /* __LOGGER_H__ */ | 31 | +#endif // __LOGGER_H__ |
33 | 32 | ||
34 | // vim: set ts=4 sw=4: | 33 | // vim: set ts=4 sw=4: |
@@ -4,9 +4,9 @@ | @@ -4,9 +4,9 @@ | ||
4 | #include <stdio.h> /* for printf() and fprintf() */ | 4 | #include <stdio.h> /* for printf() and fprintf() */ |
5 | #include <poll.h> /* for select system call and related */ | 5 | #include <poll.h> /* for select system call and related */ |
6 | 6 | ||
7 | +#include "class.h" | ||
7 | #include "socket.h" | 8 | #include "socket.h" |
8 | #include "logger.h" | 9 | #include "logger.h" |
9 | -#include "cclass.h" | ||
10 | 10 | ||
11 | #define POLL_FD_NSIZE 1024 | 11 | #define POLL_FD_NSIZE 1024 |
12 | #define POLL_FD_SIZE (sizeof(struct pollfd) * POLL_FD_NSIZE) | 12 | #define POLL_FD_SIZE (sizeof(struct pollfd) * POLL_FD_NSIZE) |
@@ -23,14 +23,14 @@ | @@ -23,14 +23,14 @@ | ||
23 | 23 | ||
24 | typedef void (*server_read_hook)(const char *, size_t); | 24 | typedef void (*server_read_hook)(const char *, size_t); |
25 | 25 | ||
26 | -CLASS(SERVER) { | ||
27 | - LOGGER logger; | ||
28 | - SOCK sock; | 26 | +CLASS(Server) { |
27 | + Logger logger; | ||
28 | + Sock sock; | ||
29 | nfds_t nfds; | 29 | nfds_t nfds; |
30 | struct pollfd fds[POLL_FD_NSIZE]; | 30 | struct pollfd fds[POLL_FD_NSIZE]; |
31 | 31 | ||
32 | struct { | 32 | struct { |
33 | - SOCK sock; | 33 | + Sock sock; |
34 | char * wbuf; | 34 | char * wbuf; |
35 | char * rbuf; | 35 | char * rbuf; |
36 | unsigned int rpos; | 36 | unsigned int rpos; |
@@ -40,8 +40,8 @@ CLASS(SERVER) { | @@ -40,8 +40,8 @@ CLASS(SERVER) { | ||
40 | server_read_hook read_hook; | 40 | server_read_hook read_hook; |
41 | }; | 41 | }; |
42 | 42 | ||
43 | -void server_run(SERVER this); | ||
44 | -void server_close_conn(SERVER this, unsigned int handle); | 43 | +void serverRun(Server this); |
44 | +void serverCloseConn(Server this, unsigned int handle); | ||
45 | 45 | ||
46 | #endif // __SERVER_H__ | 46 | #endif // __SERVER_H__ |
47 | 47 |
1 | #ifndef __SOCKET_H__ | 1 | #ifndef __SOCKET_H__ |
2 | #define __SOCKET_H__ | 2 | #define __SOCKET_H__ |
3 | 3 | ||
4 | -#include <arpa/inet.h> /* for in_port_t */ | 4 | +#include <arpa/inet.h> /* for in_port_t */ |
5 | 5 | ||
6 | +#include "class.h" | ||
6 | #include "logger.h" | 7 | #include "logger.h" |
7 | -#include "cclass.h" | ||
8 | 8 | ||
9 | - | ||
10 | -CLASS(SOCK) { | ||
11 | - LOGGER logger; | 9 | +CLASS(Sock) { |
10 | + Logger log; | ||
12 | in_port_t port; | 11 | in_port_t port; |
13 | struct sockaddr_in addr; | 12 | struct sockaddr_in addr; |
14 | - int handle; /* socket handle for server */ | 13 | + int handle; |
15 | }; | 14 | }; |
16 | 15 | ||
17 | -void sock_connect(SOCK this, const char * addr); | ||
18 | -void sock_listen(SOCK this, int backlog); | ||
19 | -SOCK sock_accept(SOCK this, char remoteAddr[16]); | 16 | +void socketConnect(Sock this, const char * addr); |
17 | +void socketListen(Sock this, int backlog); | ||
18 | +Sock socketAccept(Sock this, char remoteAddr[16]); | ||
20 | 19 | ||
21 | -#endif /* __SOCKET_H__ */ | 20 | +#endif // __SOCKET_H__ |
22 | 21 | ||
23 | // vim: set ts=4 sw=4: | 22 | // vim: set ts=4 sw=4: |
23 | + |
src/cclass.c
deleted
100644 → 0
1 | -/** | ||
2 | - * \file | ||
3 | - * cclass.c: basic "class-like" handling of code and data structures | ||
4 | - * Copyright (C) 2011 Georg Hopp | ||
5 | - * | ||
6 | - * This program is free software: you can redistribute it and/or modify | ||
7 | - * it under the terms of the GNU General Public License as published by | ||
8 | - * the Free Software Foundation, either version 3 of the License, or | ||
9 | - * (at your option) any later version. | ||
10 | - * | ||
11 | - * This program is distributed in the hope that it will be useful, | ||
12 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | - * GNU General Public License for more details. | ||
15 | - * | ||
16 | - * You should have received a copy of the GNU General Public License | ||
17 | - * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
18 | - */ | ||
19 | - | ||
20 | -#include <stdarg.h> | ||
21 | -#include <string.h> | ||
22 | -#include <stdlib.h> | ||
23 | -#include <json/json.h> | ||
24 | - | ||
25 | -#include "cclass.h" | ||
26 | - | ||
27 | -#undef __construct | ||
28 | -#undef __clear | ||
29 | -#undef __destruct | ||
30 | -#undef __jsonConst | ||
31 | -#undef __toJson | ||
32 | - | ||
33 | -void * | ||
34 | -_new(const CCLASS _class, ...) | ||
35 | -{ | ||
36 | - const CCLASS class = _class; | ||
37 | - void * object = calloc(1, class->size + sizeof(void *)); | ||
38 | - | ||
39 | - * (const struct _CCLASS **)object = class; | ||
40 | - object += sizeof(CCLASS); | ||
41 | - | ||
42 | - if (class->__construct) { | ||
43 | - va_list params; | ||
44 | - | ||
45 | - va_start(params, _class); | ||
46 | - class->__construct(object, ¶ms); | ||
47 | - va_end(params); | ||
48 | - } | ||
49 | - | ||
50 | - return object; | ||
51 | -} | ||
52 | - | ||
53 | -void * | ||
54 | -_newFromJson(const CCLASS _class, struct json_object * json) | ||
55 | -{ | ||
56 | - const CCLASS class = _class; | ||
57 | - void * object = calloc(1, class->size + sizeof(CCLASS)); | ||
58 | - | ||
59 | - * (const struct _CCLASS **) object = class; | ||
60 | - object += sizeof(CCLASS); | ||
61 | - | ||
62 | - if (class->__jsonConst && json) { | ||
63 | - class->__jsonConst(object, json); | ||
64 | - } | ||
65 | - | ||
66 | - return object; | ||
67 | -} | ||
68 | - | ||
69 | -int | ||
70 | -_instanceOf(const CCLASS _class, void * _object) | ||
71 | -{ | ||
72 | - const CCLASS * class = _object - sizeof(CCLASS); | ||
73 | - | ||
74 | - return (class && _class == *class); | ||
75 | -} | ||
76 | - | ||
77 | - | ||
78 | -void | ||
79 | -clear(void * _object) | ||
80 | -{ | ||
81 | - const CCLASS * class = _object - sizeof(CCLASS); | ||
82 | - | ||
83 | - if (_object && *class && (*class)->__clear) { | ||
84 | - (*class)->__clear(_object); | ||
85 | - } | ||
86 | -} | ||
87 | - | ||
88 | -void | ||
89 | -delete(void ** _object) | ||
90 | -{ | ||
91 | - const CCLASS * class = (*_object) - sizeof(CCLASS); | ||
92 | - | ||
93 | - if (*_object && *class && (*class)->__destruct) { | ||
94 | - (*class)->__destruct(*_object); | ||
95 | - } | ||
96 | - | ||
97 | - free((void *)class); | ||
98 | - *_object = NULL; | ||
99 | -} | ||
100 | - | ||
101 | -void | ||
102 | -toJson(void * _object, struct json_object ** json) | ||
103 | -{ | ||
104 | - const CCLASS * class = _object - sizeof(CCLASS); | ||
105 | - | ||
106 | - if (_object && *class && (*class)->__toJson) { | ||
107 | - (*class)->__toJson(_object, json); | ||
108 | - } | ||
109 | -} | ||
110 | - | ||
111 | -int | ||
112 | -isObject(void * _object) | ||
113 | -{ | ||
114 | - const CCLASS * class = _object - sizeof(CCLASS); | ||
115 | - | ||
116 | - return (_object && (*class) && CCLASS_MAGIC == (*class)->magic); | ||
117 | -} | ||
118 | - | ||
119 | -// vim: set et ts=4 sw=4: |
src/class.c
0 → 100644
1 | +#include <stdarg.h> | ||
2 | +#include <stdlib.h> | ||
3 | + | ||
4 | +#include "class.h" | ||
5 | +#include "interface.h" | ||
6 | + | ||
7 | +inline | ||
8 | +void * | ||
9 | +class_getInterface(class_ptr * class, iface_ptr _iface) | ||
10 | +{ | ||
11 | + void * iface = (void *)IFACE_GET(*class, _iface); | ||
12 | + | ||
13 | + while(NULL == iface && HAS_PARENT(*class)) { | ||
14 | + *class = (*class)->parent; | ||
15 | + iface = (void *)IFACE_GET(*class, _iface); | ||
16 | + } | ||
17 | + | ||
18 | + return iface; | ||
19 | +} | ||
20 | + | ||
21 | +inline | ||
22 | +class_ptr | ||
23 | +class_getClass(void * object) | ||
24 | +{ | ||
25 | + return *(class_ptr *)(object - sizeof(void*)); | ||
26 | +} | ||
27 | + | ||
28 | +// vim: set ts=4 sw=4: |
@@ -17,7 +17,7 @@ void daemonize(void) { | @@ -17,7 +17,7 @@ void daemonize(void) { | ||
17 | setsid(); | 17 | setsid(); |
18 | 18 | ||
19 | /* connect all standard streams to /dev/null */ | 19 | /* connect all standard streams to /dev/null */ |
20 | - freopen("/dev/null", "w", stderr); | ||
21 | - freopen("/dev/null", "r", stdin); | ||
22 | - freopen("/dev/null", "w", stdout); | 20 | + stderr = freopen("/dev/null", "w", stderr); |
21 | + stdin = freopen("/dev/null", "r", stdin); | ||
22 | + stdout = freopen("/dev/null", "w", stdout); | ||
23 | } | 23 | } |
src/interface.c
0 → 100644
1 | +#include <sys/types.h> | ||
2 | +#include <stdlib.h> | ||
3 | + | ||
4 | +#include "interface.h" | ||
5 | + | ||
6 | +#ifndef TRUE | ||
7 | +#define TRUE 1 | ||
8 | +#endif // TRUE | ||
9 | + | ||
10 | +static | ||
11 | +inline | ||
12 | +int | ||
13 | +comp(const void * _a, const void * _b) | ||
14 | +{ | ||
15 | + const struct interface * a = **(const struct interface ***)_a; | ||
16 | + const struct interface * b = **(const struct interface ***)_b; | ||
17 | + return ((a)<(b))? -1 : ((a)>(b))? 1 : 0; | ||
18 | +} | ||
19 | + | ||
20 | +/** | ||
21 | + * this one is important in selector functions to get the correct interface | ||
22 | + * implementation of a class. | ||
23 | + */ | ||
24 | +struct interface * | ||
25 | +interfaceGet(iface_impl_ptr iface_impl, const iface_ptr _iface) | ||
26 | +{ | ||
27 | + const iface_ptr * iface = &_iface; | ||
28 | + void * dummy; | ||
29 | + | ||
30 | + if (! iface_impl->simpl) { | ||
31 | + qsort((void**)(iface_impl->impl), iface_impl->nimpl, sizeof(iface_ptr), comp); | ||
32 | + iface_impl->simpl=TRUE; | ||
33 | + } | ||
34 | + | ||
35 | + dummy = bsearch( | ||
36 | + &iface, | ||
37 | + iface_impl->impl, | ||
38 | + iface_impl->nimpl, | ||
39 | + sizeof(iface_ptr), | ||
40 | + comp); | ||
41 | + | ||
42 | + return dummy? *(struct interface **)dummy : dummy; | ||
43 | +} | ||
44 | + | ||
45 | +// vim: set ts=4 sw=4: |
src/interface/class.c
0 → 100644
1 | +#include <stdarg.h> | ||
2 | +#include <stdlib.h> | ||
3 | +#include <assert.h> | ||
4 | + | ||
5 | +#include "class.h" | ||
6 | +#include "interface/class.h" | ||
7 | + | ||
8 | +const | ||
9 | +struct interface i_Class = { | ||
10 | + "class", | ||
11 | + 3 | ||
12 | +}; | ||
13 | + | ||
14 | +inline | ||
15 | +void * | ||
16 | +classNew(class_ptr class, ...) | ||
17 | +{ | ||
18 | + void * object = calloc(1, class->object_size + sizeof(void*)); | ||
19 | + va_list params; | ||
20 | + | ||
21 | + if (class->init) class->init(); | ||
22 | + | ||
23 | + * (class_ptr *)object = class; | ||
24 | + object += sizeof(void*); | ||
25 | + | ||
26 | + va_start(params, class); | ||
27 | + CALL(object, Class, ctor, ¶ms); | ||
28 | + va_end(params); | ||
29 | + | ||
30 | + return object; | ||
31 | +} | ||
32 | + | ||
33 | +inline | ||
34 | +void | ||
35 | +classDelete(void ** object) | ||
36 | +{ | ||
37 | + CALL(*object, Class, dtor); | ||
38 | + | ||
39 | + free(*object - sizeof(void*)); | ||
40 | + *object = NULL; | ||
41 | +} | ||
42 | + | ||
43 | +// vim: set ts=4 sw=4: |
src/interface/logger.c
0 → 100644
1 | +#include <stdlib.h> | ||
2 | + | ||
3 | +#include "logger.h" | ||
4 | +#include "interface/logger.h" | ||
5 | + | ||
6 | +const struct interface i_Logger = { | ||
7 | + "logger", | ||
8 | + 1 | ||
9 | +}; | ||
10 | + | ||
11 | +inline | ||
12 | +void | ||
13 | +loggerLog(void * _object, logger_level level, const char * const fmt, ...) { | ||
14 | + Logger object = _object; | ||
15 | + | ||
16 | + if (level >= object->min_level) { | ||
17 | + char * msg = NULL; | ||
18 | + size_t msg_size = 0; | ||
19 | + va_list params; | ||
20 | + | ||
21 | + va_start(params, fmt); | ||
22 | + msg_size = vsnprintf(msg, msg_size, fmt, params); | ||
23 | + va_end(params); | ||
24 | + | ||
25 | + msg = malloc(msg_size + 1); | ||
26 | + | ||
27 | + va_start(params, fmt); | ||
28 | + vsnprintf(msg, msg_size + 1, fmt, params); | ||
29 | + va_end(params); | ||
30 | + | ||
31 | + CALL(_object, Logger, log, level, msg); | ||
32 | + | ||
33 | + free(msg); | ||
34 | + } | ||
35 | +} | ||
36 | + | ||
37 | +// vim: set ts=4 sw=4: |
1 | -#include "logger.h" | 1 | +#include <stdarg.h> |
2 | 2 | ||
3 | -extern void logger_syslog(int level, const char * msg); | 3 | +#include "logger.h" |
4 | +#include "interface/class.h" | ||
5 | +#include "interface/logger.h" | ||
4 | 6 | ||
5 | -INIT_CLASS(LOGGER); | 7 | +const |
8 | +char * const | ||
9 | +logger_level_str[] = { | ||
10 | + "DEBUG", | ||
11 | + "INFO", | ||
12 | + "NOTICE", | ||
13 | + "WARNING", | ||
14 | + "ERR", | ||
15 | + "CRIT", | ||
16 | + "ALERT", | ||
17 | + "EMERG" | ||
18 | +}; | ||
6 | 19 | ||
7 | -__construct(LOGGER) | 20 | +static |
21 | +void | ||
22 | +ctor(void * _this, va_list * params) | ||
8 | { | 23 | { |
9 | - this->logfncts[0] = logger_syslog; | ||
10 | - this->logfncts_count = 1; | 24 | + Logger this = _this; |
25 | + this->min_level = va_arg(*params, int); | ||
11 | } | 26 | } |
12 | 27 | ||
13 | -__destruct(LOGGER) {} | ||
14 | -__jsonConst(LOGGER) {} | ||
15 | -__toJson(LOGGER) {} | ||
16 | -__clear(LOGGER) {} | 28 | +static void dtor(void * _this) {} |
29 | + | ||
30 | +INIT_IFACE(Class, ctor, dtor, NULL); | ||
31 | +CREATE_CLASS(Logger, NULL, IFACE(Class)); | ||
17 | 32 | ||
18 | // vim: set ts=4 sw=4: | 33 | // vim: set ts=4 sw=4: |
src/logger/add.c
deleted
100644 → 0
src/logger/log.c
deleted
100644 → 0
1 | -#define _ISOC99_SOURCE | ||
2 | - | ||
3 | -#include <stdio.h> | ||
4 | -#include <string.h> | ||
5 | - | ||
6 | -#include "logger.h" | ||
7 | - | ||
8 | -void | ||
9 | -logger_log(LOGGER this, int level, const char * message, ...) { | ||
10 | - va_list args; | ||
11 | - char buffer[1025]; | ||
12 | - logger_logfnct * logfnct; | ||
13 | - | ||
14 | - int maxBuf = sizeof(buffer)/sizeof(buffer[0]); | ||
15 | - | ||
16 | - memset(buffer, 0, maxBuf); | ||
17 | - | ||
18 | - va_start(args, message); | ||
19 | - vsnprintf(buffer, 1024, message, args); | ||
20 | - va_end(args); | ||
21 | - | ||
22 | - logfnct = this->logfncts; | ||
23 | - | ||
24 | - while (NULL != *logfnct) { | ||
25 | - (*logfnct)(level, buffer); | ||
26 | - logfnct++; | ||
27 | - } | ||
28 | -} | ||
29 | - | ||
30 | -// vim: set ts=4 sw=4: |
src/logger/stderr.c
0 → 100644
1 | +#include <stdio.h> | ||
2 | + | ||
3 | +#include "logger.h" | ||
4 | +#include "interface/logger.h" | ||
5 | + | ||
6 | +static | ||
7 | +void | ||
8 | +logStderr(void * this, logger_level level, const char * const msg) | ||
9 | +{ | ||
10 | + fprintf(stderr, "[%s] %s\n", logger_level_str[level], msg); | ||
11 | +} | ||
12 | + | ||
13 | +INIT_IFACE(Logger, logStderr); | ||
14 | +CREATE_CLASS(LoggerStderr, Logger, IFACE(Logger)); | ||
15 | + | ||
16 | +// vim: set ts=4 sw=4: |
1 | #include <syslog.h> | 1 | #include <syslog.h> |
2 | 2 | ||
3 | -const int priority[] = { | ||
4 | - LOG_USER | LOG_EMERG, | ||
5 | - LOG_USER | LOG_ALERT, | ||
6 | - LOG_USER | LOG_CRIT, | ||
7 | - LOG_USER | LOG_ERR, | ||
8 | - LOG_USER | LOG_WARNING, | ||
9 | - LOG_USER | LOG_NOTICE, | ||
10 | - LOG_USER | LOG_INFO, | ||
11 | - LOG_USER | LOG_DEBUG | 3 | +#include "logger.h" |
4 | +#include "interface/logger.h" | ||
5 | + | ||
6 | +static | ||
7 | +const | ||
8 | +int syslog_priority[] = { | ||
9 | + LOG_USER | LOG_DEBUG, | ||
10 | + LOG_USER | LOG_INFO, | ||
11 | + LOG_USER | LOG_NOTICE, | ||
12 | + LOG_USER | LOG_WARNING, | ||
13 | + LOG_USER | LOG_ERR, | ||
14 | + LOG_USER | LOG_CRIT, | ||
15 | + LOG_USER | LOG_ALERT, | ||
16 | + LOG_USER | LOG_EMERG | ||
12 | }; | 17 | }; |
13 | 18 | ||
19 | +static | ||
14 | void | 20 | void |
15 | -logger_syslog(int level, const char * msg) | 21 | +logSyslog(void * this, logger_level level, const char * const msg) |
16 | { | 22 | { |
17 | - syslog(priority[level], "%s", msg); | 23 | + syslog(syslog_priority[level], "[%s] %s", logger_level_str[level], msg); |
18 | } | 24 | } |
19 | 25 | ||
26 | +INIT_IFACE(Logger, logSyslog); | ||
27 | +CREATE_CLASS(LoggerSyslog, Logger, IFACE(Logger)); | ||
28 | + | ||
20 | // vim: set ts=4 sw=4: | 29 | // vim: set ts=4 sw=4: |
@@ -2,34 +2,38 @@ | @@ -2,34 +2,38 @@ | ||
2 | #include <string.h> /* for memset and stuff */ | 2 | #include <string.h> /* for memset and stuff */ |
3 | #include <stdlib.h> /* for getopt */ | 3 | #include <stdlib.h> /* for getopt */ |
4 | 4 | ||
5 | +#include "class.h" | ||
5 | #include "server.h" | 6 | #include "server.h" |
6 | #include "socket.h" | 7 | #include "socket.h" |
7 | -#include "server.h" | ||
8 | #include "logger.h" | 8 | #include "logger.h" |
9 | -#include "cclass.h" | ||
10 | - | ||
11 | -INIT_CLASS(SERVER); | 9 | +#include "interface/class.h" |
12 | 10 | ||
13 | -__construct(SERVER) | 11 | +static |
12 | +void | ||
13 | +ctor(void * _this, va_list * params) | ||
14 | { | 14 | { |
15 | + Server this = _this; | ||
15 | in_port_t port; | 16 | in_port_t port; |
16 | unsigned int backlog; | 17 | unsigned int backlog; |
17 | 18 | ||
18 | - this->logger = va_arg(* params, LOGGER); | 19 | + this->logger = va_arg(* params, Logger); |
19 | port = va_arg(* params, int); | 20 | port = va_arg(* params, int); |
20 | backlog = va_arg(* params, unsigned int); | 21 | backlog = va_arg(* params, unsigned int); |
21 | 22 | ||
22 | - this->sock = new(SOCK, this->logger, port); | ||
23 | - sock_listen(this->sock, backlog); | 23 | + this->sock = new(Sock, this->logger, port); |
24 | + socketListen(this->sock, backlog); | ||
24 | 25 | ||
25 | (this->fds)[0].fd = this->sock->handle; | 26 | (this->fds)[0].fd = this->sock->handle; |
26 | (this->fds)[0].events = POLLIN; | 27 | (this->fds)[0].events = POLLIN; |
27 | this->nfds = 1; | 28 | this->nfds = 1; |
28 | } | 29 | } |
29 | 30 | ||
30 | -__destruct(SERVER) | 31 | +static |
32 | +void | ||
33 | +dtor(void * _this) | ||
31 | { | 34 | { |
32 | - int i; | 35 | + Server this = _this; |
36 | + int i; | ||
33 | 37 | ||
34 | for (i=1; i<this->nfds; i++) { | 38 | for (i=1; i<this->nfds; i++) { |
35 | /* | 39 | /* |
@@ -41,8 +45,7 @@ __destruct(SERVER) | @@ -41,8 +45,7 @@ __destruct(SERVER) | ||
41 | delete(&this->sock); | 45 | delete(&this->sock); |
42 | } | 46 | } |
43 | 47 | ||
44 | -__jsonConst(SERVER) {} | ||
45 | -__toJson(SERVER) {} | ||
46 | -__clear(SERVER) {} | 48 | +INIT_IFACE(Class, ctor, dtor, NULL); |
49 | +CREATE_CLASS(Server, NULL, IFACE(Class)); | ||
47 | 50 | ||
48 | // vim: set ts=4 sw=4: | 51 | // vim: set ts=4 sw=4: |
1 | #include <string.h> | 1 | #include <string.h> |
2 | 2 | ||
3 | #include "server.h" | 3 | #include "server.h" |
4 | +#include "interface/class.h" | ||
4 | 5 | ||
5 | void | 6 | void |
6 | -server_close_conn(SERVER this, unsigned int i) | 7 | +serverCloseConn(Server this, unsigned int i) |
7 | { | 8 | { |
8 | delete(&((this->conns)[i].sock)); | 9 | delete(&((this->conns)[i].sock)); |
9 | CLEAR_CONN(this, i); | 10 | CLEAR_CONN(this, i); |
@@ -4,17 +4,18 @@ | @@ -4,17 +4,18 @@ | ||
4 | #include <errno.h> /* for errno */ | 4 | #include <errno.h> /* for errno */ |
5 | #include <unistd.h> | 5 | #include <unistd.h> |
6 | 6 | ||
7 | -#include "include/logger.h" | ||
8 | -#include "include/server.h" | ||
9 | -#include "include/socket.h" | ||
10 | -#include "include/signalHandling.h" | 7 | +#include "server.h" |
8 | +#include "socket.h" | ||
9 | +#include "logger.h" | ||
10 | +#include "signalHandling.h" | ||
11 | +#include "interface/class.h" | ||
11 | 12 | ||
12 | #undef MAX | 13 | #undef MAX |
13 | #define MAX(x,y) ((x) > (y) ? (x) : (y)) | 14 | #define MAX(x,y) ((x) > (y) ? (x) : (y)) |
14 | 15 | ||
15 | static | 16 | static |
16 | int | 17 | int |
17 | -server_poll(SERVER this) { | 18 | +serverPoll(Server this) { |
18 | int events; | 19 | int events; |
19 | 20 | ||
20 | /* | 21 | /* |
@@ -30,7 +31,7 @@ server_poll(SERVER this) { | @@ -30,7 +31,7 @@ server_poll(SERVER this) { | ||
30 | /* Fallthrough */ | 31 | /* Fallthrough */ |
31 | 32 | ||
32 | case EINTR: | 33 | case EINTR: |
33 | - logger_log(this->logger, LOGGER_CRIT, | 34 | + loggerLog(this->logger, LOGGER_CRIT, |
34 | "poll systemcall failed: [%s] - service terminated", | 35 | "poll systemcall failed: [%s] - service terminated", |
35 | strerror(errno)); | 36 | strerror(errno)); |
36 | //exit(EXIT_FAILURE); /* @TODO do real shutdown here */ | 37 | //exit(EXIT_FAILURE); /* @TODO do real shutdown here */ |
@@ -42,13 +43,13 @@ server_poll(SERVER this) { | @@ -42,13 +43,13 @@ server_poll(SERVER this) { | ||
42 | 43 | ||
43 | static | 44 | static |
44 | void | 45 | void |
45 | -server_handle_accept(SERVER this) | 46 | +serverHandleAccept(Server this) |
46 | { | 47 | { |
47 | if (0 != ((this->fds)[0].revents & POLLIN)) { | 48 | if (0 != ((this->fds)[0].revents & POLLIN)) { |
48 | char remoteAddr[16] = ""; | 49 | char remoteAddr[16] = ""; |
49 | - SOCK acc; | 50 | + Sock acc; |
50 | 51 | ||
51 | - acc = sock_accept(this->sock, remoteAddr); | 52 | + acc = socketAccept(this->sock, remoteAddr); |
52 | 53 | ||
53 | if (-1 != acc->handle) { | 54 | if (-1 != acc->handle) { |
54 | (this->conns)[this->nfds].sock = acc; // save the socket handle | 55 | (this->conns)[this->nfds].sock = acc; // save the socket handle |
@@ -65,7 +66,7 @@ server_handle_accept(SERVER this) | @@ -65,7 +66,7 @@ server_handle_accept(SERVER this) | ||
65 | 66 | ||
66 | static | 67 | static |
67 | int | 68 | int |
68 | -server_read(SERVER this) | 69 | +serverRead(Server this) |
69 | { | 70 | { |
70 | unsigned int i; | 71 | unsigned int i; |
71 | size_t _read; | 72 | size_t _read; |
@@ -85,7 +86,8 @@ server_read(SERVER this) | @@ -85,7 +86,8 @@ server_read(SERVER this) | ||
85 | * read failure / close connection | 86 | * read failure / close connection |
86 | * FALLTHROUGH | 87 | * FALLTHROUGH |
87 | */ | 88 | */ |
88 | - server_close_conn(this, i); | 89 | + loggerLog(this->logger, LOGGER_INFO, "connection closed..."); |
90 | + serverCloseConn(this, i); | ||
89 | break; | 91 | break; |
90 | 92 | ||
91 | default: | 93 | default: |
@@ -102,12 +104,12 @@ server_read(SERVER this) | @@ -102,12 +104,12 @@ server_read(SERVER this) | ||
102 | } | 104 | } |
103 | 105 | ||
104 | void | 106 | void |
105 | -server_run(SERVER this) | 107 | +serverRun(Server this) |
106 | { | 108 | { |
107 | /* | 109 | /* |
108 | * @TODO again...add verbosity to logger.... | 110 | * @TODO again...add verbosity to logger.... |
109 | */ | 111 | */ |
110 | - logger_log(this->logger, LOGGER_INFO, "service started"); | 112 | + loggerLog(this->logger, LOGGER_INFO, "service started"); |
111 | 113 | ||
112 | while (!doShutdown) /* until error or signal */ | 114 | while (!doShutdown) /* until error or signal */ |
113 | { | 115 | { |
@@ -116,16 +118,16 @@ server_run(SERVER this) | @@ -116,16 +118,16 @@ server_run(SERVER this) | ||
116 | * @TODO take return value of poll into account with | 118 | * @TODO take return value of poll into account with |
117 | * further handling! | 119 | * further handling! |
118 | */ | 120 | */ |
119 | - events = server_poll(this); | 121 | + events = serverPoll(this); |
120 | if (doShutdown) break; | 122 | if (doShutdown) break; |
121 | 123 | ||
122 | /* | 124 | /* |
123 | * handle accept | 125 | * handle accept |
124 | */ | 126 | */ |
125 | - server_handle_accept(this); | 127 | + serverHandleAccept(this); |
126 | 128 | ||
127 | /* handle reads */ | 129 | /* handle reads */ |
128 | - server_read(this); | 130 | + serverRead(this); |
129 | } | 131 | } |
130 | } | 132 | } |
131 | 133 |
1 | -#include <stdio.h> /* for printf() and fprintf() */ | ||
2 | -#include <sys/types.h> /* SO_REUSEADDR */ | ||
3 | -#include <sys/socket.h> /* for socket(), bind(), and connect() */ | ||
4 | -#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */ | ||
5 | -#include <stdlib.h> /* for atoi() and exit() */ | ||
6 | -#include <string.h> /* for memset() */ | ||
7 | -#include <unistd.h> /* for close() */ | ||
8 | -#include <errno.h> /* for errno */ | ||
9 | -#include <stdarg.h> | 1 | +#include <errno.h> |
2 | +#include <stdlib.h> | ||
10 | 3 | ||
11 | -#include "logger.h" | ||
12 | -#include "cclass.h" | ||
13 | #include "socket.h" | 4 | #include "socket.h" |
5 | +#include "logger.h" | ||
6 | +#include "interface/class.h" | ||
7 | +#include "interface/logger.h" | ||
14 | 8 | ||
15 | - | ||
16 | -INIT_CLASS(SOCK); | ||
17 | - | ||
18 | -__construct(SOCK) | 9 | +static |
10 | +void | ||
11 | +ctor(void * _this, va_list * params) | ||
19 | { | 12 | { |
20 | - int reUse = 1; /* TODO: make this configurable */ | ||
21 | - | ||
22 | - this->logger = va_arg(* params, LOGGER); | ||
23 | - this->port = va_arg(* params, int); | ||
24 | - | ||
25 | - /* Create socket for incoming connections */ | ||
26 | - if (-1 == (this->handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))) { | ||
27 | - logger_log(this->logger, LOGGER_CRIT, | ||
28 | - "error opening socket: %s - service terminated", | ||
29 | - strerror(errno)); | ||
30 | - exit(EXIT_FAILURE); | ||
31 | - } | 13 | + Sock this = _this; |
14 | + int reUse = 1; /* TODO: make this configurable */ | ||
15 | + | ||
16 | + this->log = va_arg(* params, Logger); | ||
17 | + this->port = va_arg(* params, int); | ||
18 | + | ||
19 | + /* Create socket for incoming connections */ | ||
20 | + if (-1 == (this->handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))) { | ||
21 | + loggerLog(this->log, LOGGER_CRIT, | ||
22 | + "error opening socket: %s - service terminated", | ||
23 | + strerror(errno)); | ||
24 | + exit(EXIT_FAILURE); | ||
25 | + } | ||
32 | 26 | ||
33 | - /* Make the socket REUSE a TIME_WAIT socket */ | ||
34 | - setsockopt(this->handle, SOL_SOCKET, SO_REUSEADDR, &reUse, sizeof (reUse)); | 27 | + /* Make the socket REUSE a TIME_WAIT socket */ |
28 | + setsockopt(this->handle, SOL_SOCKET, SO_REUSEADDR, &reUse, sizeof (reUse)); | ||
35 | } | 29 | } |
36 | 30 | ||
37 | -__destruct(SOCK) | 31 | +static |
32 | +void | ||
33 | +dtor(void * _this) | ||
38 | { | 34 | { |
35 | + Sock this = _this; | ||
36 | + | ||
39 | if (0 != this->handle) { | 37 | if (0 != this->handle) { |
40 | shutdown(this->handle, SHUT_RDWR); | 38 | shutdown(this->handle, SHUT_RDWR); |
41 | close(this->handle); | 39 | close(this->handle); |
42 | } | 40 | } |
43 | } | 41 | } |
44 | 42 | ||
45 | -__jsonConst(SOCK) {} | ||
46 | -__toJson(SOCK) {} | ||
47 | -__clear(SOCK) {} | 43 | +INIT_IFACE(Class, ctor, dtor, NULL); |
44 | +CREATE_CLASS(Sock, NULL, IFACE(Class)); | ||
48 | 45 | ||
49 | // vim: set ts=4 sw=4: | 46 | // vim: set ts=4 sw=4: |
1 | -#include <stdio.h> /* for printf() and fprintf() */ | ||
2 | -#include <sys/types.h> /* SO_REUSEADDR */ | ||
3 | -#include <sys/socket.h> /* for socket(), bind(), and connect() */ | ||
4 | -#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */ | ||
5 | -#include <stdlib.h> /* for atoi() and exit() */ | ||
6 | -#include <string.h> /* for memset() */ | ||
7 | -#include <unistd.h> /* for close() */ | ||
8 | #include <errno.h> /* for errno */ | 1 | #include <errno.h> /* for errno */ |
9 | -#include <stdarg.h> | ||
10 | 2 | ||
11 | -#include "logger.h" | ||
12 | -#include "cclass.h" | ||
13 | #include "socket.h" | 3 | #include "socket.h" |
4 | +#include "interface/class.h" | ||
5 | +#include "interface/logger.h" | ||
14 | 6 | ||
15 | -SOCK | ||
16 | -sock_accept(SOCK this, char remoteAddr[16]) | 7 | +Sock |
8 | +socketAccept(Sock this, char remoteAddr[16]) | ||
17 | { | 9 | { |
18 | - SOCK sock; /* Socket for client */ | 10 | + Sock sock; /* Socket for client */ |
19 | unsigned int len; /* Length of client address data structure */ | 11 | unsigned int len; /* Length of client address data structure */ |
20 | 12 | ||
21 | /* Set the size of the in-out parameter */ | 13 | /* Set the size of the in-out parameter */ |
22 | len = sizeof(this->addr); | 14 | len = sizeof(this->addr); |
23 | 15 | ||
24 | - sock = new(SOCK, this->logger, this->port); | 16 | + sock = new(Sock, this->log, this->port); |
25 | /** | 17 | /** |
26 | * @TODO: change port to remote port on success | 18 | * @TODO: change port to remote port on success |
27 | */ | 19 | */ |
@@ -29,10 +21,10 @@ sock_accept(SOCK this, char remoteAddr[16]) | @@ -29,10 +21,10 @@ sock_accept(SOCK this, char remoteAddr[16]) | ||
29 | /* Wait for a client to connect */ | 21 | /* Wait for a client to connect */ |
30 | sock->handle = accept(this->handle, (struct sockaddr *) &(sock->addr), &len); | 22 | sock->handle = accept(this->handle, (struct sockaddr *) &(sock->addr), &len); |
31 | if (-1 == sock->handle) { | 23 | if (-1 == sock->handle) { |
32 | - logger_log(this->logger, LOGGER_WARNING, | 24 | + loggerLog(this->log, LOGGER_WARNING, |
33 | "error acception connection: %s", strerror(errno)); | 25 | "error acception connection: %s", strerror(errno)); |
34 | } else { | 26 | } else { |
35 | - logger_log(this->logger, LOGGER_INFO, | 27 | + loggerLog(this->log, LOGGER_INFO, |
36 | "handling client %s\n", inet_ntoa((sock->addr).sin_addr)); | 28 | "handling client %s\n", inet_ntoa((sock->addr).sin_addr)); |
37 | } | 29 | } |
38 | 30 |
1 | -#include <stdio.h> /* for printf() and fprintf() */ | ||
2 | -#include <sys/types.h> /* SO_REUSEADDR */ | ||
3 | -#include <sys/socket.h> /* for socket(), bind(), and connect() */ | ||
4 | -#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */ | ||
5 | #include <stdlib.h> /* for atoi() and exit() */ | 1 | #include <stdlib.h> /* for atoi() and exit() */ |
6 | -#include <string.h> /* for memset() */ | ||
7 | -#include <unistd.h> /* for close() */ | ||
8 | #include <errno.h> /* for errno */ | 2 | #include <errno.h> /* for errno */ |
9 | -#include <stdarg.h> | ||
10 | 3 | ||
11 | -#include "logger.h" | ||
12 | -#include "cclass.h" | ||
13 | #include "socket.h" | 4 | #include "socket.h" |
5 | +#include "interface/class.h" | ||
6 | +#include "interface/logger.h" | ||
14 | 7 | ||
15 | 8 | ||
16 | void | 9 | void |
17 | -sock_connect(SOCK this, const char * addr) | 10 | +socketConnect(Sock this, const char * addr) |
18 | { | 11 | { |
19 | inet_pton(AF_INET, addr, &((this->addr).sin_addr)); | 12 | inet_pton(AF_INET, addr, &((this->addr).sin_addr)); |
20 | (this->addr).sin_family = AF_INET; /* Internet address family */ | 13 | (this->addr).sin_family = AF_INET; /* Internet address family */ |
21 | (this->addr).sin_port = htons(this->port); /* Local port */ | 14 | (this->addr).sin_port = htons(this->port); /* Local port */ |
22 | 15 | ||
23 | if (-1 == connect(this->handle, (struct sockaddr*) &(this->addr), sizeof(this->addr))) { | 16 | if (-1 == connect(this->handle, (struct sockaddr*) &(this->addr), sizeof(this->addr))) { |
24 | - logger_log(this->logger, LOGGER_CRIT, | 17 | + loggerLog(this->log, LOGGER_CRIT, |
25 | "error connection socket: %s - service terminated", | 18 | "error connection socket: %s - service terminated", |
26 | strerror(errno)); | 19 | strerror(errno)); |
27 | exit(EXIT_FAILURE); | 20 | exit(EXIT_FAILURE); |
1 | -#include <stdio.h> /* for printf() and fprintf() */ | ||
2 | -#include <sys/types.h> /* SO_REUSEADDR */ | ||
3 | -#include <sys/socket.h> /* for socket(), bind(), and connect() */ | ||
4 | -#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */ | ||
5 | #include <stdlib.h> /* for atoi() and exit() */ | 1 | #include <stdlib.h> /* for atoi() and exit() */ |
6 | -#include <string.h> /* for memset() */ | ||
7 | -#include <unistd.h> /* for close() */ | ||
8 | #include <errno.h> /* for errno */ | 2 | #include <errno.h> /* for errno */ |
9 | -#include <stdarg.h> | ||
10 | 3 | ||
11 | -#include "logger.h" | ||
12 | -#include "cclass.h" | ||
13 | #include "socket.h" | 4 | #include "socket.h" |
5 | +#include "interface/class.h" | ||
6 | +#include "interface/logger.h" | ||
14 | 7 | ||
15 | 8 | ||
16 | void | 9 | void |
17 | -sock_listen(SOCK this, int backlog) | 10 | +socketListen(Sock this, int backlog) |
18 | { | 11 | { |
19 | (this->addr).sin_family = AF_INET; /* Internet address family */ | 12 | (this->addr).sin_family = AF_INET; /* Internet address family */ |
20 | (this->addr).sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */ | 13 | (this->addr).sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */ |
@@ -22,7 +15,7 @@ sock_listen(SOCK this, int backlog) | @@ -22,7 +15,7 @@ sock_listen(SOCK this, int backlog) | ||
22 | 15 | ||
23 | /* Bind to the local address */ | 16 | /* Bind to the local address */ |
24 | if (-1 == bind(this->handle, (struct sockaddr *) &(this->addr), sizeof(this->addr))) { | 17 | if (-1 == bind(this->handle, (struct sockaddr *) &(this->addr), sizeof(this->addr))) { |
25 | - logger_log(this->logger, LOGGER_CRIT, | 18 | + loggerLog(this->log, LOGGER_CRIT, |
26 | "error binding socket: %s - service terminated", | 19 | "error binding socket: %s - service terminated", |
27 | strerror(errno)); | 20 | strerror(errno)); |
28 | exit(EXIT_FAILURE); | 21 | exit(EXIT_FAILURE); |
@@ -30,7 +23,7 @@ sock_listen(SOCK this, int backlog) | @@ -30,7 +23,7 @@ sock_listen(SOCK this, int backlog) | ||
30 | 23 | ||
31 | /* Mark the socket so it will listen for incoming connections */ | 24 | /* Mark the socket so it will listen for incoming connections */ |
32 | if (-1 == listen(this->handle, backlog)) { | 25 | if (-1 == listen(this->handle, backlog)) { |
33 | - logger_log(this->logger, LOGGER_CRIT, | 26 | + loggerLog(this->log, LOGGER_CRIT, |
34 | "error binding socket: %s - service terminated", | 27 | "error binding socket: %s - service terminated", |
35 | strerror(errno)); | 28 | strerror(errno)); |
36 | exit(EXIT_FAILURE); | 29 | exit(EXIT_FAILURE); |
@@ -3,7 +3,9 @@ | @@ -3,7 +3,9 @@ | ||
3 | #include <string.h> | 3 | #include <string.h> |
4 | 4 | ||
5 | #include "server.h" | 5 | #include "server.h" |
6 | +#include "logger.h" | ||
6 | #include "signalHandling.h" | 7 | #include "signalHandling.h" |
8 | +#include "interface/class.h" | ||
7 | 9 | ||
8 | static void | 10 | static void |
9 | read_hook(const char * _buffer, size_t size) | 11 | read_hook(const char * _buffer, size_t size) |
@@ -11,7 +13,7 @@ read_hook(const char * _buffer, size_t size) | @@ -11,7 +13,7 @@ read_hook(const char * _buffer, size_t size) | ||
11 | char buffer[1025]; | 13 | char buffer[1025]; |
12 | 14 | ||
13 | memset(buffer, 0, 1025); | 15 | memset(buffer, 0, 1025); |
14 | - snprintf(buffer, size, _buffer); | 16 | + snprintf(buffer, 1025>size? size : 1024, "%s", _buffer); |
15 | 17 | ||
16 | printf("%s\n", buffer); | 18 | printf("%s\n", buffer); |
17 | } | 19 | } |
@@ -19,13 +21,13 @@ read_hook(const char * _buffer, size_t size) | @@ -19,13 +21,13 @@ read_hook(const char * _buffer, size_t size) | ||
19 | int | 21 | int |
20 | main() | 22 | main() |
21 | { | 23 | { |
22 | - LOGGER logger = new(LOGGER, NULL); | ||
23 | - SERVER server = new(SERVER, logger, 11212, SOMAXCONN); | 24 | + Logger logger = new(LoggerStderr, LOGGER_INFO); |
25 | + Server server = new(Server, logger, 11212, SOMAXCONN); | ||
24 | 26 | ||
25 | server->read_hook = read_hook; | 27 | server->read_hook = read_hook; |
26 | 28 | ||
27 | init_signals(); | 29 | init_signals(); |
28 | - server_run(server); | 30 | + serverRun(server); |
29 | 31 | ||
30 | delete(&server); | 32 | delete(&server); |
31 | delete(&logger); | 33 | delete(&logger); |
Please
register
or
login
to post a comment