Nilorea Library
C utilities for networking, threading, graphics
n_user.c
Go to the documentation of this file.
1
7#include "nilorea/n_user.h"
8
9
10
17{
18 N_USERLIST *ulist = NULL ;
19 Malloc( ulist, N_USERLIST, 1 );
20 __n_assert( ulist, return NULL );
21 __n_assert( max > 0, return NULL );
22
23 Malloc( ulist -> list, N_USER, max );
24 __n_assert( ulist -> list, Free( ulist ) ; return NULL );
25
26 ulist -> max = max ;
27 ulist -> highter = -1;
28 for( int it = 0; it < max ; it ++ )
29 {
30 ulist -> list[ it ] . state = 0 ;
31 ulist -> list[ it ] . nb_rec_pos = 10 ;
32 ulist -> list[ it ] . only_last_pos = 1 ;
33 ulist -> list[ it ] . id = -1 ;
34 Malloc( ulist -> list[ it ] . last_positions, VECTOR3D, 10 );
35 ulist -> list[ it ] . netw_waitlist = new_generic_list( 0 );
36 memset( ulist -> list[ it ] . position, 0, 3 * sizeof( double ) );
37 ulist -> list[ it ] . netw = NULL ;
38 memset( ulist -> list[ it ] . name, 0, 1024 );
39 ulist -> list[ it ] . netw = NULL ;
40 }
41
42 init_lock( ulist -> user_rwbolt );
43
44 return ulist ;
45} /* userlist_new() */
46
47
48
57int userlist_set_position_behavior( N_USERLIST *ulist, int id, int nb_rec_pos, int only_last_pos )
58{
59 __n_assert( ulist, return FALSE );
60 __n_assert( nb_rec_pos < 1, return FALSE );
61 __n_assert( only_last_pos < 0 || only_last_pos > 1, return FALSE );
62
63 read_lock( ulist -> user_rwbolt );
64 __n_assert( ulist -> list[ id ] . state == 0, return FALSE );
65 unlock( ulist -> user_rwbolt );
66
67 write_lock( ulist -> user_rwbolt );
68 ulist -> list[ id ] . nb_rec_pos = nb_rec_pos ;
69 ulist -> list[ id ] . only_last_pos = only_last_pos ;
70 if( only_last_pos )
71 {
72 Realloc( ulist -> list[ id ] . last_positions, VECTOR3D, 1 );
73 if( ! ulist -> list[ id ] . last_positions )
74 {
75 n_log( LOG_ERR, "could not resize to only_last_pos VECTOR3D" );
76 }
77 }
78 else
79 {
80 Realloc( ulist -> list[ id ] . last_positions, VECTOR3D, nb_rec_pos );
81 if( ! ulist -> list[ id ] . last_positions )
82 {
83 n_log( LOG_ERR, "could not resize to %d VECTOR3D", nb_rec_pos );
84 }
85 ulist -> list[ id ] . nb_rec_pos = 1 ;
86 }
87 __n_assert( ulist -> list[ id ] . last_positions, unlock( ulist -> user_rwbolt ); return FALSE );
88 unlock( ulist -> user_rwbolt );
89
90 return TRUE ;
91} /* userlist_set_position_behavior() */
92
93
94
102{
103 __n_assert( ulist, return -1 );
104 __n_assert( netw, return -1 );
105
106 int it = -1;
107 write_lock( ulist -> user_rwbolt );
108 do
109 {
110 it++;
111 }
112 while( it < ulist -> max && ulist -> list[ it ] . state != 0);
113 if( it < ulist -> max )
114 {
115 ulist -> list[ it ] . state = 1;
116 ulist -> list[ it ] . netw = netw ;
117 if( it > ulist -> highter )
118 ulist -> highter = it;
119 unlock( ulist -> user_rwbolt );
120 return it ;
121 }
122 unlock( ulist -> user_rwbolt );
123 return -1 ;
124} /* userlist_add_user() */
125
126
127
134int userlist_del_user( N_USERLIST *ulist, int id )
135{
136 __n_assert( ulist, return FALSE );
137 __n_assert( id >= 0, return FALSE );
138
139 write_lock( ulist -> user_rwbolt );
140 if( id > ulist -> max )
141 {
142 unlock( ulist -> user_rwbolt );
143 return FALSE ;
144 }
145 if( ulist -> list[ id ] . state == 0 )
146 {
147 unlock( ulist -> user_rwbolt );
148 return FALSE;
149 }
150 ulist -> list[ id ] . state = 0 ;
151 ulist -> list[ id ] . nb_rec_pos = 1 ;
152 ulist -> list[ id ] . only_last_pos = 1 ;
153 ulist -> list[ id ] . id = -1 ;
154 Realloc( ulist -> list[ id ] . last_positions, VECTOR3D, 1 );
155 if( ! ulist -> list[ id ] . last_positions )
156 {
157 n_log( LOG_ERR, "couldn't resize to 1 element" );
158 }
159 list_empty( ulist -> list[ id ] . netw_waitlist );
160 memset( ulist -> list[ id ] . position, 0, 3 * sizeof( double ) );
161 ulist -> list[ id ] . netw = NULL ;
162 memset( ulist -> list[ id ] . name, 0, 1024 );
163
164 if( id >= ulist -> highter )
165 {
166 int it = id ;
167 while( it >= 0 )
168 {
169 if( ulist -> list[ it ] . state == 1 )
170 {
171 ulist -> highter = it ;
172 break ;
173 }
174 it -- ;
175 }
176 }
177 unlock( ulist -> user_rwbolt );
178 return TRUE;
179} /* userlist_del_user() */
180
181
182
191int userlist_add_msg_to_ex( N_USERLIST *ulist, N_STR *msg, int mode, int id )
192{
193 __n_assert( ulist, return FALSE );
194 __n_assert( msg, return FALSE );
195
196 switch( mode )
197 {
198 case( USERLIST_ALL ):
199 read_lock( ulist -> user_rwbolt );
200 for( int it = 0 ; it <= ulist -> highter ; it ++ )
201 {
202 if( ulist -> list[ it ] . state == 1 )
203 netw_add_msg( ulist -> list[ it ] . netw, nstrdup( msg ) );
204 }
205 unlock( ulist -> user_rwbolt );
206 break;
207 case( USERLIST_ALL_EXCEPT ):
208 __n_assert( id >= 0, return FALSE );
209 read_lock( ulist -> user_rwbolt );
210 for( int it = 0 ; it <= ulist -> highter ; it ++ )
211 {
212 if( it == id )
213 continue ;
214 if( ulist -> list[ it ] . state == 1 )
215 netw_add_msg( ulist -> list[ it ] . netw, nstrdup( msg ) );
216 }
217 unlock( ulist -> user_rwbolt );
218 break;
219 default:
220 case( USERLIST_ONE ):
221 __n_assert( id >= 0, return FALSE );
222
223 read_lock( ulist -> user_rwbolt );
224 netw_add_msg( ulist -> list[ id ] . netw, msg );
225 unlock( ulist -> user_rwbolt );
226 break;
227 }
228 return TRUE ;
229} /* userlist_add_msg_to_ex() */
230
231
232
240int userlist_add_msg_to( N_USERLIST *ulist, N_STR *msg, int id )
241{
242 return userlist_add_msg_to_ex( ulist, msg, USERLIST_ONE, id );
243} /* userlist_add_msg_to() */
244
245
253{
254 return userlist_add_msg_to_ex( ulist, msg, USERLIST_ALL, -1 );
255} /* userlist_add_msg_to_all() */
256
257
258
267{
268 return userlist_add_msg_to_ex( ulist, msg, USERLIST_ALL_EXCEPT, id );
269} /* userlist_add_msg_to_all_except() */
270
271
272
279{
280 __n_assert( ulist&&(*ulist), return FALSE );
281
282 write_lock( (*ulist) -> user_rwbolt );
283 for( int it = 0 ; it < (*ulist) -> max ; it ++ )
284 {
285 Free( (*ulist) -> list[it ] . last_positions );
286 list_destroy( &(*ulist) -> list[it ] . netw_waitlist );
287 }
288 Free( (*ulist) -> list );
289 unlock( (*ulist) -> user_rwbolt );
290
291 pthread_rwlock_destroy( &(*ulist) -> user_rwbolt );
292 Free( (*ulist) );
293
294 return FALSE ;
295} /* userlist_destroy() */
296
297
298
306int userlist_user_add_waiting_msg( N_USERLIST *ulist, int id, N_STR *netmsg )
307{
308 __n_assert( ulist, return FALSE );
309 __n_assert( netmsg, return FALSE );
310 __n_assert( id >= 0, return FALSE );
311
312 int ret = FALSE ;
313
314 read_lock( ulist -> user_rwbolt );
315 if( id <= ulist -> highter )
316 {
317 if( ulist -> list[ id ] . state == 1 )
318 {
319 ret = list_push( ulist -> list[ id ] . netw_waitlist, netmsg, NULL );
320 }
321 }
322 unlock( ulist -> user_rwbolt );
323 return ret ;
324} /* userlist_user_add_waiting_msg() */
325
326
327
335{
336 __n_assert( ulist, return FALSE );
337 __n_assert( id >= 0, return FALSE );
338
339 read_lock( ulist -> user_rwbolt );
340 if( id <= ulist -> highter )
341 {
342 if( ulist -> list[ id ] . state == 1 )
343 {
344 list_foreach( node, ulist -> list[ id ] . netw_waitlist )
345 {
346 N_STR *netmsg = (N_STR *)node -> ptr ;
347 netw_add_msg( ulist -> list[ id ] . netw, netmsg );
348 }
349 list_empty( ulist -> list[ id ] . netw_waitlist );
350 }
351 }
352 unlock( ulist -> user_rwbolt );
353 return TRUE ;
354} /* userlist_user_send_waiting_msgs() */
355
356
363{
364 __n_assert( ulist, return FALSE );
365
366 read_lock( ulist -> user_rwbolt );
367 for( int id = 0 ; id <= ulist -> highter ; id ++ )
368 {
369 if( ulist -> list[ id ] . state == 1 )
370 {
371 list_foreach( node, ulist -> list[ id ] . netw_waitlist )
372 {
373 N_STR *netmsg = (N_STR *)node -> ptr ;
374 netw_add_msg( ulist -> list[ id ] . netw, netmsg );
375 }
376 list_empty( ulist -> list[ id ] . netw_waitlist );
377 }
378 }
379 unlock( ulist -> user_rwbolt );
380 return TRUE ;
381} /* userlist_send_waiting_msgs() */
#define init_lock(__rwlock_mutex)
Macro for initializing a rwlock.
Definition: n_common.h:337
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition: n_common.h:183
#define __n_assert(__ptr, __ret)
macro to assert things
Definition: n_common.h:276
#define unlock(__rwlock_mutex)
Macro for releasing read/write lock a rwlock mutex.
Definition: n_common.h:389
#define write_lock(__rwlock_mutex)
Macro for acquiring a write lock on a rwlock mutex.
Definition: n_common.h:373
#define Realloc(__ptr, __struct, __size)
Realloc Handler to get errors.
Definition: n_common.h:217
#define Free(__ptr)
Free Handler to get errors.
Definition: n_common.h:256
#define read_lock(__rwlock_mutex)
Macro for acquiring a read lock on a rwlock mutex.
Definition: n_common.h:357
int list_empty(LIST *list)
Empty a LIST list of pointers.
Definition: n_list.c:547
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
Definition: n_list.c:244
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper.
Definition: n_list.h:70
int list_destroy(LIST **list)
Empty and Free a list container.
Definition: n_list.c:603
LIST * new_generic_list(int max_items)
Initialiaze a generic list container to max_items pointers.
Definition: n_list.c:20
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition: n_log.h:74
#define LOG_ERR
error conditions
Definition: n_log.h:58
N_STR * nstrdup(N_STR *str)
Duplicate a N_STR.
Definition: n_str.c:795
A box including a string and his lenght.
Definition: n_str.h:173
int userlist_del_user(N_USERLIST *ulist, int id)
delete an user from the list
Definition: n_user.c:134
int userlist_add_msg_to(N_USERLIST *ulist, N_STR *msg, int id)
add a N_STR *message to user list (USERLIST_ONE)
Definition: n_user.c:240
int userlist_add_msg_to_ex(N_USERLIST *ulist, N_STR *msg, int mode, int id)
add a N_STR *message to user list
Definition: n_user.c:191
#define USERLIST_ONE
flag to target one user in the list
Definition: n_user.h:33
int userlist_user_send_waiting_msgs(N_USERLIST *ulist, int id)
send all waiting messages in user 'id' waiting list
Definition: n_user.c:334
#define USERLIST_ALL_EXCEPT
flag to target all users in the list except one
Definition: n_user.h:31
N_USERLIST * userlist_new(int max)
create a new N_USERLIST user list with 'max' users
Definition: n_user.c:16
#define USERLIST_ALL
flag to target all users in the list
Definition: n_user.h:29
int userlist_add_msg_to_all(N_USERLIST *ulist, N_STR *msg)
add a N_STR *message to user list (USERLIST_ALL)
Definition: n_user.c:252
int userlist_send_waiting_msgs(N_USERLIST *ulist)
send all waiting messages ofr each user of the lsit
Definition: n_user.c:362
int userlist_set_position_behavior(N_USERLIST *ulist, int id, int nb_rec_pos, int only_last_pos)
set the position parameters for trajectory processing for user 'id'
Definition: n_user.c:57
int userlist_add_user(N_USERLIST *ulist, NETWORK *netw)
add an user to the list
Definition: n_user.c:101
int userlist_add_msg_to_all_except(N_USERLIST *ulist, N_STR *msg, int id)
add a N_STR *message to user list except user 'id' (USERLIST_ALL_EXCEPT)
Definition: n_user.c:266
int userlist_destroy(N_USERLIST **ulist)
destroy and free a N_USERLIST *userlist
Definition: n_user.c:278
int userlist_user_add_waiting_msg(N_USERLIST *ulist, int id, N_STR *netmsg)
add a newtork message to specified user 'id'
Definition: n_user.c:306
USER management cell.
Definition: n_user.h:37
USER list.
Definition: n_user.h:67
int netw_add_msg(NETWORK *netw, N_STR *msg)
Add a message to send in aimed NETWORK.
Definition: n_network.c:1965
Structure of a NETWORK.
Definition: n_network.h:252
double VECTOR3D[3]
struct of a point
Definition: n_3d.h:39
USERS handling for tiny game apps.