/
bang-types.h
275 lines (247 loc) · 4.61 KB
/
bang-types.h
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* \file bang-types.h
* \author Nikhil Bysani
* \date December 20, 2008
*
* \brief Defines commonly used types in the BANG library.
*/
#ifndef __BANG_TYPES_H
#define __BANG_TYPES_H
#include"bang-module-api.h"
#define BANG_VERSION .01
/**
* The structure that represents a module for the program.
*/
typedef struct _BANG_module {
/**
* The name of the module.
*/
char *module_name;
double *module_version;
/**
* The module initialize method.
*/
int (*module_init)(BANG_api*);
/**
* The module run method.
*/
void (*module_run)();
/**
* The hash of the module.
*/
unsigned char *md;
/**
* The actual handle of the module.
*/
void *handle;
/**
* The path to the module.
*/
char *path;
} BANG_module;
/**
* This is the structure that is sent to each signal handler.
*/
typedef struct {
/**
* Arguments you want to send to signal handlers.
*/
void *args;
/**
* Length of the arguments.
*/
int length;
} BANG_sigargs;
/**
* A signal handler function, it receives the BANGSIGNUM, and arguments depending on the signal.
*/
typedef void (*BANGSignalHandler)(int,int,void*);
/**
* The signals that can be sent by library.
* TODO: Detail all the signals, especially their arguments.
*/
enum BANG_signals {
/**
* The bang server has successfully bound.
*/
BANG_BIND_SUC = 0,
/**
* A peer has been added and is now in active status.
*/
BANG_PEER_ADDED,
/**
* A peer has been removed.
*/
BANG_PEER_REMOVED,
/**
* A module is going to be run.
* arg: the BANG_module being run.
*/
BANG_RUNNING_MODULE,
/**
* The bang server has stopped.
* arg: the server socket.
*/
BANG_SERVER_STARTED,
/**
* The bang server has stopped.
*/
BANG_SERVER_STOPPED,
/**
* The server's bind has failed.
*/
BANG_BIND_FAIL,
/**
* The server could not get addrinfo.
*/
BANG_GADDRINFO_FAIL,
/**
* The thread could not connect.
*/
BANG_CONNECT_FAIL,
/**
* The server could not listen.
*/
BANG_LISTEN_FAIL,
/**
* There was an error loading the module.
* arg: the output of dlerror
*/
BANG_MODULE_ERROR,
/**
* A peer has connected and is being set up.
*/
BANG_PEER_CONNECTED,
/**
* A peer has disconnected and is being removed.
*/
BANG_PEER_DISCONNECTED,
/**
* Request all the peers a request.
*/
BANG_REQUEST_ALL,
/**
* Don't know if we can (will) implement this signals
*/
BANG_CLOSE_ALL,
/**
* The number of signals. All new signals should be above this line.
*/
BANG_NUM_SIGS
} BANG_signal;
/**
*
* \page The Protocol
*
* \brief The protocol is specified, like this:
* - First, an unsigned 4 bytes will be sent specifying what kind of communication it will be
* - Then, everything else will follow
*
* Following are possible unsigned to be sent.
*
* Communications currently can be done by this:
* assuming peer1, peer2
*
* peer1: BANG_HELLO
* peer2: BANG_HELLO
*
* ... any number of BANG_DEBUG_MESSAGE
*
* peer2: BANG_BYE
*
*/
/**
* Length of the headers.
*/
#define LENGTH_OF_HEADER 4
/**
* Length of any lengths sent.
*/
#define LENGTH_OF_LENGTHS 4
/**
* Length of any versions sent.
*/
#define LENGTH_OF_VERSION 8
enum BANG_headers {
/**
* message:
* -BANG_HELLO (unsigned 4 bytes)
* -BANG_VERSION (8 bytes to a double)
* -length of name (unsigned 4 bytes)
* -peer name (char*)
*/
BANG_HELLO = 0,
/**
* message:
* -BANG_DEBUG_MESSAGE (unsigned 4 bytes)
* -length of message (unsigned 4 bytes)
* -message (char*)
*/
BANG_DEBUG_MESSAGE,
/**
* message:
* -BANG_SEND_MODULE
* -length of module
* -module
* NOTE: The file, not the struct.
*/
BANG_SEND_MODULE,
/**
* message:
* -BANG_MISMATCH_VERSION (unsigned 4 bytes)
* -our version (8 bytes)
*/
BANG_MISMATCH_VERSION,
/**
* message:
* -BANG_BYE (unsigned 4 bytes)
*/
BANG_BYE,
/**
* Number of headers.*/
BANG_NUM_HEADERS
} BANG_header;
/**
* A request to be made to a write peer thread.
*/
typedef struct {
/**
* The type of request.
*/
char type;
/**
* Information part of the request.
*/
void *request;
/**
* Length of the information.
*/
unsigned int length;
} BANG_request;
enum BANG_request_types {
/**
* Ask for the peer to start a sign off.
*/
BANG_CLOSE_REQUEST = 0,
/**
* Just quit without doing anything.
*/
BANG_SUDDEN_CLOSE_REQUEST,
/**
* do:
* -send BANG_DEBUG_MESSAGE
* -send length of message
* -send message
*/
BANG_DEBUG_REQUEST,
/**
* do:
* -send BANG_SEND_MODULE
* -send length of module
* -send module
* NOTE: NOT THE struct, the actual file.
*/
BANG_SEND_MODULE_REQUEST,
BANG_NUM_REQUESTS
} BANG_request_type;
#endif