loop.h
Go to the documentation of this file.
1 /* vim: set ts=2 expandtab: */
2 /**
3  * @file loop.h
4  * @brief a simple object-oriented event loop
5  *
6  * @author Josh King (jheretic), jking@chambana.net
7  *
8  * @internal
9  * Created 03/07/2013
10  * Revision $Id: doxygen.commotion.templates,v 0.1 2013/01/01 09:00:00 jheretic Exp $
11  * Compiler gcc/g++
12  * Company The Open Technology Institute
13  * Copyright Copyright (c) 2013, Josh King
14  *
15  * This file is part of Commotion, Copyright (c) 2013, Josh King
16  *
17  * Commotion is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU Affero General Public License as
19  * published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * Commotion is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Affero General Public License for more details.
26  *
27  * You should have received a copy of the GNU Affero General Public License
28  * along with Commotion. If not, see <http://www.gnu.org/licenses/>.
29  *
30  * =====================================================================================
31  */
32 
33 #ifndef _LOOP_H
34 #define _LOOP_H
35 
36 #include <stdlib.h>
37 #include "process.h"
38 #include "socket.h"
39 
40 #define LOOP_MAXPROC 20
41 #define LOOP_MAXSOCK 20
42 #define LOOP_MAXEVENT 64
43 #define LOOP_TIMEOUT 5
44 #define LOOP_MAXTIMER 20
45 
46 typedef struct co_timer_t co_timer_t;
47 
48 struct co_timer_t {
49  co_obj_t _header;
50  uint8_t _exttype;
51  uint8_t _len;
52  bool pending;
53  struct timeval deadline;
54  co_cb_t timer_cb;
55  void *ptr;
56 } __attribute__((packed));
57 
58 //Public functions
59 
60 /**
61  * @brief sets up the event loop
62  */
63 int co_loop_create(void);
64 
65 /**
66  * @brief closes the event loop
67  */
68 int co_loop_destroy(void);
69 
70 /**
71  * @brief starts the event loop, listening for messages on sockets via _co_loop_poll_sockets
72  */
73 void co_loop_start(void);
74 
75 /**
76  * @brief stops the event loop
77  */
78 void co_loop_stop(void);
79 
80 /**
81  * @brief adds a process to the event loop (for it to listen for)
82  * @param proc the process to be added
83  */
84 int co_loop_add_process(co_obj_t *proc);
85 
86 /**
87  * @brief removes a process from the event loop
88  * @param pid the id of the process to be removed
89  */
90 int co_loop_remove_process(pid_t pid);
91 
92 /**
93  * @brief adds a new socket to the event loop (for it to listen on)
94  * @param new_sock the new socket to be added
95  * @param context a co_obj_t context pointer (currently unused)
96  */
97 int co_loop_add_socket(co_obj_t *new_sock, co_obj_t *context);
98 
99 /**
100  * @brief removes a socket from the event loop
101  * @param old_sock the socket to be removed
102  * @param context a co_obj_t context pointer (currently unused)
103  */
104 int co_loop_remove_socket(co_obj_t *old_sock, co_obj_t *context);
105 
106 /**
107  * @brief gets a socket that is registered with the event loop
108  * @param uri a URI to match against the available sockets
109  * @param context a co_obj_t context pointer (currently unused)
110  */
111 co_obj_t *co_loop_get_socket(char *uri, co_obj_t *context);
112 
113 /**
114  * @brief schedules a new timer with the event loop
115  * @param timer the timer to schedule
116  * @param context a co_obj_t context pointer (currently unused)
117  */
118 int co_loop_add_timer(co_obj_t *new_timer, co_obj_t *context);
119 
120 /**
121  * @brief removes a timer from the event loop
122  * @param old_timer the timer to remove from list
123  * @param context a co_obj_t context pointer (currently unused)
124  */
125 int co_loop_remove_timer(co_obj_t *old_timer, co_obj_t *context);
126 
127 /**
128  * @brief sets timer to expire in msecs from now
129  * @param timer the timer to set
130  * @param msecs number of milliseconds
131  * @param context a co_obj_t context pointer (currently unused)
132  */
133 int co_loop_set_timer(co_obj_t *timer, long msecs, co_obj_t *context);
134 
135 /**
136  * @brief gets a socket that is registered with the event loop
137  * @param ptr void pointer to match against the available timers
138  * @param context a co_obj_t context pointer (currently unused)
139  */
140 co_obj_t *co_loop_get_timer(void *ptr, co_obj_t *context);
141 
142 /**
143  * @brief malloc and initialize a timer
144  * @param size timer struct size
145  * @param proto timer protocol
146  */
147 co_obj_t *co_timer_create(struct timeval deadline, co_cb_t timer_cb, void *ptr);
148 
149 #endif
void co_loop_start(void)
starts the event loop, listening for messages on sockets via _co_loop_poll_sockets ...
Definition: loop.c:288
int co_loop_add_timer(co_obj_t *new_timer, co_obj_t *context)
schedules a new timer with the event loop
Definition: loop.c:397
int co_loop_remove_process(pid_t pid)
removes a process from the event loop
Definition: loop.c:319
co_obj_t * co_timer_create(struct timeval deadline, co_cb_t timer_cb, void *ptr)
malloc and initialize a timer
Definition: loop.c:476
int co_loop_add_process(co_obj_t *proc)
adds a process to the event loop (for it to listen for)
Definition: loop.c:310
int co_loop_remove_socket(co_obj_t *old_sock, co_obj_t *context)
removes a socket from the event loop
Definition: loop.c:375
int co_loop_create(void)
sets up the event loop
Definition: loop.c:246
Definition: obj.h:131
a simple object-oriented process manager object model inspired by Zed Shaw
int co_loop_set_timer(co_obj_t *timer, long msecs, co_obj_t *context)
sets timer to expire in msecs from now
Definition: loop.c:456
int co_loop_add_socket(co_obj_t *new_sock, co_obj_t *context)
adds a new socket to the event loop (for it to listen on)
Definition: loop.c:330
void co_loop_stop(void)
stops the event loop
Definition: loop.c:305
int co_loop_destroy(void)
closes the event loop
Definition: loop.c:270
co_obj_t * co_loop_get_timer(void *ptr, co_obj_t *context)
gets a socket that is registered with the event loop
Definition: loop.c:448
Definition: loop.h:48
co_obj_t * co_loop_get_socket(char *uri, co_obj_t *context)
gets a socket that is registered with the event loop
Definition: loop.c:389
a simple object-oriented socket wrapper object model inspired by Zed Shaw
int co_loop_remove_timer(co_obj_t *old_timer, co_obj_t *context)
removes a timer from the event loop
Definition: loop.c:429