From b8dda6ecdfc747d561d170bd3d8512dc6914d238 Mon Sep 17 00:00:00 2001 From: fcolin Date: Mon, 3 Aug 1998 12:58:41 +0000 Subject: eclatement de bussocket pour permettre la creation de mainloop externe autre que XT et Interne buschannel.h devient l'interface avec la gestion MainLoop busxtloop.[ch] MainLoop Style XT busloop.[ch] MainLoop Interne basee sur select --- src/Makefile | 23 +++--- src/bus.c | 13 ++-- src/bus.h | 3 +- src/bussocket.c | 233 ++++++++++---------------------------------------------- src/bussocket.h | 24 +----- src/testbus.c | 50 +++++++++--- 6 files changed, 106 insertions(+), 240 deletions(-) (limited to 'src') diff --git a/src/Makefile b/src/Makefile index 4d73cae..c61c797 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,19 +1,24 @@ -XTINC =-I/usr/local/X11R6.3/include -CC=gcc -OBJ = timer.o bussocket.o bus.o -GOBJ = timer.o bussocket.o gbus.o -XTOBJ = timer.o xtsocket.o bus.o +XTINC =-I/usr/X11R6/include +XTLIB =-L/usr/X11R6/lib +CC=gcc -g -DDEBUG +OBJ = busloop.o timer.o bussocket.o bus.o +GOBJ = busloop.o timer.o bussocket.o gbus.o +XTOBJ = busxtloop.o bussocket.o bus.o -all: libbus.a libgbus.a libxtbus.a testbus +all: libbus.a libgbus.a libxtbus.a testbus testxtbus gbus.o: bus.c $(CC) -DGNU_REGEXP -c $(CFLAGS) -o gbus.o bus.c -xtsocket.o: bussocket.c - $(CC) -DXTMAINLOOP $(XTINC) -c $(CFLAGS) -o xtsocket.o bussocket.c testbus: testbus.o $(OBJ) - $(CC) -o testbus testbus.o $(OBJ) -lsocket -lnsl + $(CC) -o testbus testbus.o $(OBJ) + +testxtbus.o : testbus.c + $(CC) -c testbus.c -o testxtbus.o $(XTINC) -DXTMAINLOOP + +testxtbus: testxtbus.o $(XTOBJ) + $(CC) -o testxtbus testxtbus.o $(XTOBJ) $(XTLIB) -lXt -lX11 libbus.a: $(OBJ) ar q libbus.a $(OBJ) diff --git a/src/bus.c b/src/bus.c index dc291c1..aa1c897 100644 --- a/src/bus.c +++ b/src/bus.c @@ -12,7 +12,7 @@ #include - +#include "buschannel.h" #include "bussocket.h" #include "list.h" #include "bus.h" @@ -79,7 +79,7 @@ static const char *ApplicationName = NULL; /* classes de messages emis par l'application utilise pour le filtrage */ static int messages_classes_count = 0; -static char **messages_classes = NULL; +static const char **messages_classes = NULL; /* callback appele sur reception d'un message direct */ static MsgDirectCallback direct_callback = NULL; @@ -503,7 +503,7 @@ void BusInit(const char *AppName, unsigned short busnumber, const char *ready, BusDieCallback die_callback, void *die_data ) { - ChannelInit(); + SocketInit(); ApplicationName = AppName; SupervisionPort = busnumber; @@ -517,7 +517,7 @@ void BusInit(const char *AppName, unsigned short busnumber, const char *ready, } -void BusClasses( int argc, char **argv) +void BusClasses( int argc, const char **argv) { messages_classes_count = argc; messages_classes = argv; @@ -531,10 +531,7 @@ void BusStart() fprintf(stderr,"Server Ready TCP:%hu\n",ApplicationPort); } -void BusLoop() -{ - ChannelMainLoop(NULL); -} + /* desabonnements */ void UnbindMsg( MsgRcvPtr msg ) { diff --git a/src/bus.h b/src/bus.h index b0971a3..1f0154a 100644 --- a/src/bus.h +++ b/src/bus.h @@ -28,7 +28,7 @@ typedef void (*MsgDirectCallback)( BusClientPtr app, void *user_data, int id, ch typedef struct _msg_rcv *MsgRcvPtr; /* filtrage des regexps */ -void BusClasses( int argc, char **argv); +void BusClasses( int argc, const char **argv); void BusInit( const char *AppName, /* nom de l'application */ @@ -40,7 +40,6 @@ void BusInit( void *die_data ); /* user data */ void BusStart(); /* emission du bonjour */ -void BusLoop(); /* boucle principale */ /* query sur les applications connectees */ char *GetApplicationName( BusClientPtr app ); diff --git a/src/bussocket.c b/src/bussocket.c index 0d75e61..ca5ec08 100644 --- a/src/bussocket.c +++ b/src/bussocket.c @@ -1,12 +1,15 @@ +#ifdef WIN32 +#include +#endif #include #include #include #include #include + #ifdef WIN32 -#include #define close closesocket -#define perror( a ) printf(a" error=%d\n",WSAGetLastError()); +/*#define perror( a ) printf(a" error=%d\n",WSAGetLastError());*/ #else #include #include @@ -17,26 +20,17 @@ #include #include #endif -#ifdef XTMAINLOOP -#include -#endif + #include "list.h" +#include "buschannel.h" #include "bussocket.h" -#include "timer.h" + +static ChannelInit channel_init = NULL; +static ChannelSetUp channel_setup = NULL; +static ChannelClose channel_close = NULL; #define MAX_BUFFER 2048 -struct _channel { - Channel next; - HANDLE fd; -#ifdef XTMAINLOOP - XtInputId id; -#endif - void *data; - int tobedeleted; - void (*handle_delete)( void *data ); - void (*handle_read)( Channel channel, HANDLE fd, void *data); - }; typedef struct _server *Server; @@ -50,6 +44,7 @@ struct _server { struct _client { Client next; + HANDLE fd; Channel channel; unsigned short port; struct sockaddr_in from; @@ -60,17 +55,10 @@ struct _client { void *data; }; -static Channel channels_list = NULL; + static Server servers_list = NULL; static Client clients_list = NULL; -static int channel_initialized = 0; -#ifdef XTMAINLOOP -static XtAppContext app; -#else -static fd_set open_fds; -static int MainLoop = 1; -#endif @@ -78,106 +66,31 @@ static int MainLoop = 1; WSADATA WsaData; #endif -void ChannelClose( Channel channel ) +void BusSetChannelManagement( ChannelInit init_chan, ChannelSetUp setup_chan, ChannelClose close_chan ) { -#ifdef XTMAINLOOP - if ( channel->handle_delete ) - (*channel->handle_delete)( channel->data ); - close(channel->fd); - XtRemoveInput( channel->id ); - LIST_REMOVE( channels_list, channel ); -#else - channel->tobedeleted = 1; -#endif + channel_init = init_chan; + channel_setup = setup_chan; + channel_close = close_chan; } -#ifdef XTMAINLOOP -static void HandleChannel( XtPointer closure, int* source, XtInputId* id ) -{ - Channel channel = (Channel)closure; -#ifdef DEBUG - printf("Handle Channel read %d\n",*source ); -#endif - (*channel->handle_read)(channel,channel->fd,channel->data); -} -#else -static void ChannelDelete( Channel channel ) -{ - if ( channel->handle_delete ) - (*channel->handle_delete)( channel->data ); - close(channel->fd); - FD_CLR(channel->fd, &open_fds); - LIST_REMOVE( channels_list, channel ); -} -static void ChannelDefferedDelete() -{ - Channel channel,next; - LIST_EACH_SAFE( channels_list, channel,next) - { - if ( channel->tobedeleted ) - { - ChannelDelete( channel ); - } - } -} -#endif -Channel ChannelSetUp(HANDLE fd, void *data, - void (*handle_delete)( void *data ), - void (*handle_read)( Channel channel, HANDLE fd, void *data) - ) -{ - Channel channel; - LIST_ADD( channels_list, channel ); - if ( !channel ) - { - fprintf(stderr,"NOK Memory Alloc Error\n"); - close( fd ); - exit(0); - } - channel->fd = fd; - channel->tobedeleted = 0; - channel->handle_delete = handle_delete; - channel->handle_read = handle_read; - channel->data = data; -#ifdef XTMAINLOOP - channel->id = XtAppAddInput( app, fd, (XtPointer)XtInputReadMask, HandleChannel, channel); -#else - FD_SET( channel->fd, &open_fds ); -#endif - return channel; -} -#ifndef XTMAINLOOP -static void ChannelHandleRead(fd_set *current) -{ - Channel channel,next; - - LIST_EACH_SAFE( channels_list, channel, next ) - { - if ( FD_ISSET( channel->fd, current ) ) - { - (*channel->handle_read)(channel,channel->fd,channel->data); - } - } -} -static void ChannelHandleExcpt(fd_set *current) +void SocketInit() { - Channel channel,next; - LIST_EACH_SAFE( channels_list, channel, next ) - { - if (FD_ISSET( channel->fd, current ) ) - { - ChannelClose( channel ); - } - } + if ( ! channel_init ) + { + fprintf( stderr, "You Must call BusSetChannelManagement before all !!!\n"); + exit(-1); + } + (*channel_init)(); } -#endif + static void DeleteSocket(void *data) { Client client = ( Client )data; if ( client->handle_delete ) (*client->handle_delete)( client, client->data ); - shutdown( client->channel->fd, 2 ); + shutdown( client->fd, 2 ); + close( client->fd ); LIST_REMOVE( clients_list, client ); } static void HandleSocket( Channel channel, HANDLE fd, void *data) @@ -200,12 +113,12 @@ static void HandleSocket( Channel channel, HANDLE fd, void *data) nb = recvfrom( fd, client->ptr, nb_to_read,0,(struct sockaddr *)&client->from,&len); if (nb < 0) { perror(" Read Socket "); - ChannelClose( client->channel ); + (*channel_close)( client->channel ); return; }; if ( nb == 0 ) { - ChannelClose( client->channel ); + (*channel_close)( client->channel ); return; } @@ -252,7 +165,8 @@ static void HandleServer(Channel channel, HANDLE fd, void *data) exit(0); } client->from = remote2; - client->channel = ChannelSetUp( ns, client, DeleteSocket, HandleSocket ); + client->fd = ns; + client->channel = (*channel_setup)( ns, client, DeleteSocket, HandleSocket ); client->interpretation = server->interpretation; client->ptr = client->buffer; client->handle_delete = server->handle_delete; @@ -321,7 +235,7 @@ int SocketServer(unsigned short port, fprintf(stderr,"NOK Memory Alloc Error\n"); exit(0); } - server->channel = ChannelSetUp( fd, server, DeleteSocket, HandleServer ); + server->channel = (*channel_setup)( fd, server, DeleteSocket, HandleServer ); server->create = create; server->handle_delete = handle_delete; server->interpretation = interpretation; @@ -333,7 +247,7 @@ char *SocketGetPeerHost( Client client ) struct sockaddr_in name; struct hostent *host; int len = sizeof(name); - err = getpeername( client->channel->fd, (struct sockaddr *)&name, &len ); + err = getpeername( client->fd, (struct sockaddr *)&name, &len ); if ( err < 0 ) return "can't get peer"; host = gethostbyaddr( (char *)&name.sin_addr.s_addr,sizeof(name.sin_addr.s_addr),name.sin_family); if ( host == NULL ) return "can't translate addr"; @@ -355,13 +269,13 @@ void SocketGetRemote( Client client, char **host, unsigned short *port ) } void SocketClose( Client client ) { - ChannelClose( client->channel ); + (*channel_close)( client->channel ); } void SocketSendRaw( Client client, char *buffer, int len ) { int err; - err = send( client->channel->fd, buffer, len, 0 ); + err = send( client->fd, buffer, len, 0 ); if ( err != len ) perror( "*** send ***"); } @@ -448,8 +362,8 @@ if ( !client ) exit(0); } - -client->channel = ChannelSetUp( handle, client, DeleteSocket, HandleSocket ); +client->fd = handle; +client->channel = (*channel_setup)( handle, client, DeleteSocket, HandleSocket ); client->interpretation = interpretation; client->ptr = client->buffer; client->data = data; @@ -472,7 +386,7 @@ int SocketWaitForReply( Client client, char *buffer, int size, int delai) long nb; HANDLE fd; - fd = client->channel->fd; + fd = client->fd; ptr = buffer; timeout.tv_sec = delai; timeout.tv_usec = 0; @@ -512,73 +426,8 @@ int SocketWaitForReply( Client client, char *buffer, int size, int delai) *ptr_nl = '\0'; return (ptr_nl - buffer); } -void ChannelInit(void) -{ -#ifdef WIN32 - int error; -#else - signal( SIGPIPE, SIG_IGN); -#endif - if ( channel_initialized ) return; -#ifndef XTMAINLOOP - FD_ZERO( &open_fds ); -#endif -#ifdef WIN32 - error = WSAStartup( 0x0101, &WsaData ); - if ( error == SOCKET_ERROR ) { - printf( "WSAStartup failed.\n" ); - } -#endif - channel_initialized = 1; -} -#ifdef XTMAINLOOP -void SetSocketAppContext( XtAppContext cntx ) -{ - app = cntx; -} - -void ChannelMainLoop(void(*hook)(void)) -{ - printf("Compiled for Use of XtMainLoop not ChannelMainLoop\n"); - exit(-1); -} -#else -void ChannelStop(void) -{ - MainLoop = 0; -} -void ChannelMainLoop(void(*hook)(void)) -{ - -fd_set rdset; -fd_set exset; -int ready; - - - - while (MainLoop) { - ChannelDefferedDelete(); - if ( hook ) (*hook)(); - rdset = open_fds; - exset = open_fds; - ready = select(64, &rdset, 0, &exset, TimerGetSmallestTimeout()); - if ( ready < 0 && ( errno != EINTR )) - { - perror("select"); - return; - } - TimerScan(); - if ( ready > 0 ) - { - ChannelHandleExcpt(&exset); - ChannelHandleRead(&rdset); - continue; - } - } -} -#endif /* Socket UDP */ Client SocketBroadcastCreate( unsigned short port, @@ -635,8 +484,8 @@ if ( !client ) exit(0); } - -client->channel = ChannelSetUp( handle, client, DeleteSocket, HandleSocket ); +client->fd = handle; +client->channel = (*channel_setup)( handle, client, DeleteSocket, HandleSocket ); client->interpretation = interpretation; client->ptr = client->buffer; client->data = data; @@ -657,7 +506,7 @@ void SocketSendBroadcast( Client client, unsigned long host, unsigned short port remote.sin_family = AF_INET; remote.sin_addr.s_addr = htonl( host ); remote.sin_port = htons(port); - err = sendto( client->channel->fd, + err = sendto( client->fd, buffer, len,0, (struct sockaddr *)&remote,sizeof(remote)); if ( err != len ) diff --git a/src/bussocket.h b/src/bussocket.h index b40571f..956aa1a 100644 --- a/src/bussocket.h +++ b/src/bussocket.h @@ -1,5 +1,5 @@ -#ifndef _SOCKET_H -#define _SOCKET_H +#ifndef _BUSSOCKET_H +#define _BUSSOCKET_H #ifdef __cplusplus extern "C" { @@ -17,24 +17,13 @@ extern "C" { #include #endif -typedef struct _channel *Channel; - -extern void ChannelInit(void); -extern void ChannelStop(void); -extern void ChannelMainLoop(void(*hook)(void) ); -extern Channel ChannelSetUp( - HANDLE fd, - void *data, - void (*handle_delete)( void *data ), - void (*handle_read)( Channel channel, HANDLE fd, void *data) - ); - -extern void ChannelClose( Channel channel ); /* Server Part */ typedef struct _client *Client; typedef void (*SocketInterpretation)( Client client, void *data, char *ligne); +extern void SocketInit(); + extern void SocketClose( Client client ); extern void SocketSend( Client client, char *fmt, ... ); extern void SocketSendRaw( Client client, char *buffer, int len ); @@ -76,11 +65,6 @@ extern void SocketGetRemote( Client client, char **host, unsigned short *port ); /* emmission d'un broadcast UDP */ extern void SocketSendBroadcast( Client client, unsigned long host, unsigned short port, char *fmt, ... ); -#ifdef XTMAINLOOP -#include -extern void SetSocketAppContext( XtAppContext cntx ); -#endif - #ifdef __cplusplus } #endif diff --git a/src/testbus.c b/src/testbus.c index db52985..ae10eea 100644 --- a/src/testbus.c +++ b/src/testbus.c @@ -1,11 +1,22 @@ #include +#include +#ifdef WIN32 +#include +#else #include #include +#endif +#ifdef XTMAINLOOP +#include "busxtloop.h" +#else +#include "busloop.h" +#endif #include "bussocket.h" #include "bus.h" #include "timer.h" #ifdef XTMAINLOOP #include +XtAppContext cntx; #endif int app_count = 0; @@ -19,7 +30,7 @@ void Callback( BusClientPtr app, void *user_data, int argc, char *argv[]) printf(" '%s'",argv[i]); printf("\n"); } -void HandleStdin( Channel channel, int fd, void *data) +void HandleStdin( Channel channel, HANDLE fd, void *data) { char buf[4096]; char *line; @@ -31,8 +42,12 @@ void HandleStdin( Channel channel, int fd, void *data) line = gets( buf); if ( !line ) { - ChannelClose( channel ); - ChannelStop(); +#ifdef XTMAINLOOP + BusXtChannelClose( channel ); +#else + BusLoopChannelClose( channel ); + BusLoopChannelStop(); +#endif return; } if ( *line == '.' ) @@ -144,7 +159,11 @@ void ApplicationCallback( BusClientPtr app, void *user_data, BusApplicationEvent printf("Application(%s): Receive '%s'\n",appname,*msgList++); printf("Application(%s): End Messages\n",appname); if ( app_count == wait_count ) - ChannelSetUp( 0, NULL, NULL, HandleStdin); +#ifdef XTMAINLOOP + BusXtChannelSetUp( 0, NULL, NULL, HandleStdin); +#else + BusLoopChannelSetUp( 0, NULL, NULL, HandleStdin); +#endif break; case BusApplicationDisconnected: app_count--; @@ -156,12 +175,14 @@ void ApplicationCallback( BusClientPtr app, void *user_data, BusApplicationEvent } } +#ifndef XTMAINLOOP void TimerCall(TimerId id, void *user_data, unsigned long delta) { printf("Timer callback: %d delta %lu ms\n", (int)user_data, delta ); SendMsg( "TEST TIMER %d", (int)user_data); /*if ( (int)user_data == 5 ) TimerModify( id, 2000 );*/ } +#endif int main(int argc, char *argv[]) { @@ -181,25 +202,36 @@ int main(int argc, char *argv[]) timer_test = 1; break; } + /* Mainloop management */ #ifdef XTMAINLOOP - XtToolkitInitialize(); - + /*XtToolkitInitialize();*/ + cntx = XtCreateApplicationContext(); + BusXtChannelAppContext( cntx ); + BusSetChannelManagement( BusXtChannelInit, BusXtChannelSetUp, BusXtChannelClose ); +#else + BusSetChannelManagement( BusLoopChannelInit, BusLoopChannelSetUp, BusLoopChannelClose ); #endif BusInit("TEST",bport,"TEST READY",ApplicationCallback,NULL,NULL,NULL); for ( ; optind < argc; optind++ ) BindMsg( Callback, NULL, argv[optind] ); if ( wait_count == 0 ) - ChannelSetUp( 0, NULL, NULL, HandleStdin); +#ifdef XTMAINLOOP + BusXtChannelSetUp( 0, NULL, NULL, HandleStdin); +#else + BusLoopChannelSetUp( 0, NULL, NULL, HandleStdin); +#endif BusStart( ); if ( timer_test ) { +#ifndef XTMAINLOOP TimerRepeatAfter( TIMER_LOOP, 1000, TimerCall, (void*)1 ); TimerRepeatAfter( 5, 5000, TimerCall, (void*)5 ); +#endif } #ifdef XTMAINLOOP - XtMainLoop(); + XtAppMainLoop(cntx); #else - BusLoop(); + BusLoopChannelMainLoop(NULL); #endif return 0; } -- cgit v1.1