22static pthread_t netw_thr ;
26 fprintf( stderr,
" -v version\n"
27 " -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
29 " -a serveur address name/ip to bind (server mode) (optionnal)\n"
30 " -s serveur address name/ip to connect (client mode)\n"
32 " -i [v4,v6] ip version (default support both ipv4 and ipv6 )\n" );
35void process_args(
int argc,
char **argv,
char **address,
char **server,
char **port,
int *nb,
int *ip_version )
52 fprintf( stderr,
"No arguments given, help:\n" );
56 while( ( getoptret = getopt( argc, argv,
"hvs:V:p:n:i:a:" ) ) != EOF)
61 if( !strcmp(
"v4", optarg ) )
66 else if( !strcmp(
"v6", optarg ) )
77 fprintf( stderr,
"Date de compilation : %s a %s.\n", __DATE__, __TIME__ );
80 if( !strncmp(
"LOG_NULL", optarg, 8 ) )
86 if( !strncmp(
"LOG_NOTICE", optarg, 10 ) )
92 if( !strncmp(
"LOG_INFO", optarg, 8 ) )
98 if( !strncmp(
"LOG_ERR", optarg, 7 ) )
104 if( !strncmp(
"LOG_DEBUG", optarg, 9 ) )
110 fprintf( stderr,
"%s n'est pas un niveau de log valide.\n", optarg );
119 (*server) = strdup( optarg );
122 (*address) = strdup( optarg );
125 (*nb) = atoi( optarg );
128 (*port) = strdup( optarg );
135 fprintf( stderr,
"\n Missing log level\n" );
137 else if( optopt ==
'p' )
139 fprintf( stderr,
"\n Missing port\n" );
141 else if( optopt !=
's' )
143 fprintf( stderr,
"\n Unknow missing option %c\n", optopt );
162int main(
int argc,
char **argv)
172 process_args( argc, argv, &addr, &srv, &port,&NB_ATTEMPTS, &ip_version );
182 n_log(
LOG_ERR,
"Please specify only one of the following options: -a (server, addr to bind to) or -s (server on which to connect to)" );
187 n_log(
LOG_INFO,
"Client mode, connecting to %s:%s", srv, port );
192 n_log(
LOG_INFO,
"Server mode , waiting client on port %s", port );
198 sa.sa_handler = sigchld_handler;
199 sigemptyset(&sa.sa_mask);
200 sa.sa_flags = SA_RESTART;
201 if (sigaction(SIGCHLD, &sa, NULL) == -1)
209 n_log(
LOG_INFO,
"Creating listening network for %s:%s %d",
_str( addr ),
_str( port ), ip_version );
213 n_log(
LOG_ERR,
"Fatal error with network initialization" );
218 for( it = 0 ; it < NB_ATTEMPTS/2 ; it ++ )
231 int pthread_error = 0 ;
233 pthread_error = pthread_create ( &netw_thr, NULL, &
manage_client, (
void *)netw );
235 if( pthread_error != 0 )
237 n_log(
LOG_ERR,
"Error creating client management pthread:%d , error: %s", pthread_error, strerror( error ) );
241 pthread_join( netw_thr, NULL );
247 while( it < NB_ATTEMPTS )
255 n_log(
LOG_ERR,
"Error ading client management to thread pool" );
273 else if( mode == CLIENT )
275 for(
int it = 1 ; it <= NB_ATTEMPTS ; it ++ )
277 if(
netw_connect( &netw, srv, port, ip_version ) != TRUE )
280 n_log(
LOG_ERR,
"Unable to connect to %s:%s", srv, port );
289 N_STR *sended_data = NULL, *recved_data = NULL, *hostname = NULL, *tmpstr = NULL ;
304 n_log(
LOG_NOTICE,
"RECEIVED DATAS: %s - %s", recved_data ->data, hostname ->data );
311 n_log(
LOG_ERR,
"Error getting back answer from server" );
313 n_log(
LOG_NOTICE,
"Closing client in 5 seconds. See synchronisation on server side..." );
Nilorea Library n_network api test.
void * manage_client(void *ptr)
recv/send datas if any for the client
int get_net_datas(N_STR *str, N_STR **hostname, N_STR **data)
decode data we got from network
int send_net_datas(NETWORK *netw, N_STR *data)
send data to specified network
#define FreeNoLog(__ptr)
Free Handler without log.
#define _str(__PTR)
define true
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_NOTICE
normal but significant condition
#define LOG_NULL
no log output
#define LOG_INFO
informational
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
A box including a string and his lenght.
void u_sleep(unsigned int usec)
wrapper around usleep for API consistency
N_STR * netw_wait_msg(NETWORK *netw, size_t refresh, size_t timeout)
Wait a message from aimed NETWORK.
#define NETWORK_IPV6
Flag to force IPV6
int netw_make_listening(NETWORK **netw, char *addr, char *port, int nbpending, int ip_version)
Make a NETWORK be a Listening network.
int netw_start_thr_engine(NETWORK *netw)
Start the NETWORK netw Threaded Engine.
#define NETWORK_IPV4
Flag to force IPV4
NETWORK * netw_accept_from_ex(NETWORK *from, int send_list_limit, int recv_list_limit, int non_blocking, int *retval)
make a normal 'accept' .
#define NETWORK_IPALL
Flag for auto detection by OS of ip version to use.
NETWORK * netw_accept_from(NETWORK *from)
make a normal blocking 'accept' .
int netw_close(NETWORK **netw)
Closing a specified Network, destroy queues, free the structure.
int netw_connect(NETWORK **netw, char *host, char *port, int ip_version)
Use this to connect a NETWORK to any listening one, unrestricted send/recv lists.
int destroy_threaded_pool(THREAD_POOL **pool, int delay)
delete a thread_pool, exit the threads and free the structs
int add_threaded_process(THREAD_POOL *thread_pool, void *(*func_ptr)(void *param), void *param, int mode)
add a function and params to a thread pool
int refresh_thread_pool(THREAD_POOL *thread_pool)
try to add some waiting DIRECT_PROCs on some free thread slots, else do nothing
#define DIRECT_PROC
processing mode for added func, direct start
int wait_for_threaded_pool(THREAD_POOL *thread_pool, int delay)
Wait for all the launched process in the thread pool to terminate.
THREAD_POOL * new_thread_pool(int nbmaxthr, int nb_max_waiting)
Create a new pool of nbmaxthr threads.
Structure of a trhead pool.