From d579c7047d1e4c5b69c0ae8c09e85882ceaa293e Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Sat, 29 Mar 2014 03:28:10 +0100 Subject: [PATCH] Moved the lookup code to the AppTransport class --- Makefile | 2 - src/Makefile | 4 +- src/appclient.c | 28 ++++--------- src/appserver.c | 28 ++++--------- src/apptransport.c | 102 +++++++++++++++++++++++++++++++++++++++++++-- src/apptransport.h | 7 ++++ src/lookup.c | 76 --------------------------------- src/lookup.h | 33 --------------- src/project.conf | 6 +-- tests/Makefile | 2 +- tests/lookup.c | 71 +++++++++++++++++++++++++++++++ tests/project.conf | 2 +- tests/tests.sh | 7 ++-- 13 files changed, 202 insertions(+), 166 deletions(-) delete mode 100644 src/lookup.c delete mode 100644 src/lookup.h create mode 100644 tests/lookup.c diff --git a/Makefile b/Makefile index 9a480b0..c2b80a1 100644 --- a/Makefile +++ b/Makefile @@ -50,8 +50,6 @@ dist: $(PACKAGE)-$(VERSION)/src/appinterface.h \ $(PACKAGE)-$(VERSION)/src/appmessage.h \ $(PACKAGE)-$(VERSION)/src/apptransport.h \ - $(PACKAGE)-$(VERSION)/src/lookup.c \ - $(PACKAGE)-$(VERSION)/src/lookup.h \ $(PACKAGE)-$(VERSION)/src/project.conf \ $(PACKAGE)-$(VERSION)/src/transport/tcp.c \ $(PACKAGE)-$(VERSION)/src/transport/tcp4.c \ diff --git a/src/Makefile b/src/Makefile index ada4162..d5c773e 100644 --- a/src/Makefile +++ b/src/Makefile @@ -33,7 +33,7 @@ libApp.so.0.0 libApp.so.0 libApp.so: $(libApp_OBJS) $(LN) -s -- libApp.so.0.0 libApp.so.0 $(LN) -s -- libApp.so.0.0 libApp.so -appclient.o: appclient.c appinterface.h ../include/App/appclient.h lookup.h lookup.c +appclient.o: appclient.c appinterface.h ../include/App/appclient.h $(CC) $(libApp_CFLAGS) -c appclient.c appinterface.o: appinterface.c ../include/App/appserver.h ../config.h @@ -42,7 +42,7 @@ appinterface.o: appinterface.c ../include/App/appserver.h ../config.h appmessage.o: appmessage.c ../include/App/appmessage.h appmessage.h $(CC) $(libApp_CFLAGS) -c appmessage.c -appserver.o: appserver.c appinterface.h ../include/App/appserver.h lookup.h lookup.c ../config.h +appserver.o: appserver.c appinterface.h ../include/App/appserver.h ../config.h $(CC) $(libApp_CFLAGS) -c appserver.c apptransport.o: apptransport.c apptransport.h ../include/App/apptransport.h ../config.h diff --git a/src/appclient.c b/src/appclient.c index 2346766..bb0dc74 100644 --- a/src/appclient.c +++ b/src/appclient.c @@ -52,24 +52,11 @@ static int _appclient_helper_message(void * data, AppTransport * transport, /* appclient_new */ AppClient * appclient_new(char const * app, char const * name) { - AppClient * appclient; - Event * event; - - if((event = event_new()) == NULL) - return NULL; - if((appclient = appclient_new_event(app, name, event)) == NULL) - { - event_delete(event); - return NULL; - } - appclient->event_free = 1; - return appclient; + return appclient_new_event(app, name, NULL); } /* appclient_new_event */ -#include "lookup.h" - AppClient * appclient_new_event(char const * app, char const * name, Event * event) { @@ -87,12 +74,13 @@ AppClient * appclient_new_event(char const * app, char const * name, appclient->interface = appinterface_new_server(app); appclient->helper.data = appclient; appclient->helper.message = _appclient_helper_message; - appclient->transport = _new_event_transport(&appclient->helper, - ATM_CLIENT, event, app, name); - appclient->event = event; - appclient->event_free = 0; + appclient->transport = apptransport_new_app(ATM_CLIENT, + &appclient->helper, app, name, event); + appclient->event = (event != NULL) ? event : event_new(); + appclient->event_free = (event != NULL) ? 0 : 1; /* check for errors */ - if(appclient->interface == NULL || appclient->transport == NULL) + if(appclient->interface == NULL || appclient->transport == NULL + || appclient->event == NULL) { appclient_delete(appclient); return NULL; @@ -104,8 +92,6 @@ AppClient * appclient_new_event(char const * app, char const * name, return appclient; } -#include "lookup.c" - /* appclient_delete */ void appclient_delete(AppClient * appclient) diff --git a/src/appserver.c b/src/appserver.c index 18ba64f..572d30d 100644 --- a/src/appserver.c +++ b/src/appserver.c @@ -55,24 +55,11 @@ static int _appserver_helper_message(void * data, AppTransport * transport, /* appserver_new */ AppServer * appserver_new(const char * app, char const * name) { - AppServer * appserver; - Event * event; - - if((event = event_new()) == NULL) - return NULL; - if((appserver = appserver_new_event(app, name, event)) == NULL) - { - event_delete(event); - return NULL; - } - appserver->event_free = 1; - return appserver; + return appserver_new_event(app, name, NULL); } /* appserver_new_event */ -#include "lookup.h" - AppServer * appserver_new_event(char const * app, char const * name, Event * event) { @@ -83,12 +70,13 @@ AppServer * appserver_new_event(char const * app, char const * name, appserver->interface = appinterface_new_server(app); appserver->helper.data = appserver; appserver->helper.message = _appserver_helper_message; - appserver->transport = _new_event_transport(&appserver->helper, - ATM_SERVER, event, app, name); - appserver->event = event; - appserver->event_free = 0; + appserver->transport = apptransport_new_app(ATM_SERVER, + &appserver->helper, app, name, event); + appserver->event = (event != NULL) ? event : event_new(); + appserver->event_free = (event != NULL) ? 0 : 1; /* check for errors */ - if(appserver->interface == NULL || appserver->transport == NULL) + if(appserver->interface == NULL || appserver->transport == NULL + || appserver->event == NULL) { appserver_delete(appserver); return NULL; @@ -96,8 +84,6 @@ AppServer * appserver_new_event(char const * app, char const * name, return appserver; } -#include "lookup.c" - /* appserver_delete */ void appserver_delete(AppServer * appserver) diff --git a/src/apptransport.c b/src/apptransport.c index e3d6461..c85b0f1 100644 --- a/src/apptransport.c +++ b/src/apptransport.c @@ -15,10 +15,12 @@ +#include #ifdef DEBUG # include #endif #include +#include #include #include "appmessage.h" #include "apptransport.h" @@ -39,6 +41,7 @@ struct _AppTransport { AppTransportMode mode; AppTransportHelper helper; + String * name; /* plug-in */ AppTransportPluginHelper thelper; @@ -83,17 +86,23 @@ AppTransport * apptransport_new(AppTransportMode mode, { AppTransport * transport; +#ifdef DEBUG + fprintf(stderr, "DEBUG: %s(\"%s\", \"%s\")\n", __func__, plugin, name); +#endif /* allocate the transport */ if((transport = object_new(sizeof(*transport))) == NULL) return NULL; memset(transport, 0, sizeof(*transport)); transport->mode = mode; - transport->helper = *helper; + if(helper != NULL) + transport->helper = *helper; + transport->name = string_new(name); /* initialize the plug-in helper */ _new_helper(transport, mode, event); /* load the transport plug-in */ - if((transport->plugin = plugin_new(LIBDIR, "App", "transport", plugin)) - == NULL + if(transport->name == NULL + || (transport->plugin = plugin_new(LIBDIR, "App", + "transport", plugin)) == NULL || (transport->definition = plugin_lookup( transport->plugin, "transport")) == NULL || transport->definition->init == NULL @@ -120,6 +129,69 @@ static void _new_helper(AppTransport * transport, AppTransportMode mode, } +/* apptransport_new_app */ +static String * _new_app_name(char const * app, char const * name); +static String * _new_app_transport(String ** name); + +AppTransport * apptransport_new_app(AppTransportMode mode, + AppTransportHelper * helper, char const * app, + char const * name, Event * event) +{ + AppTransport * apptransport; + String * n; + String * transport; + +#ifdef DEBUG + fprintf(stderr, "DEBUG: %s(\"%s\", \"%s\")\n", __func__, app, name); +#endif + if((n = _new_app_name(app, name)) == NULL) + return NULL; + if((transport = _new_app_transport(&n)) == NULL) + { + string_delete(n); + return NULL; + } + apptransport = apptransport_new(mode, helper, transport, n, event); + string_delete(transport); + string_delete(n); + return apptransport; +} + +static String * _new_app_name(char const * app, char const * name) +{ + String * var; + + if(name != NULL) + return string_new(name); + /* obtain the desired transport and name from the environment */ + if((var = string_new_append("APPSERVER_", app, NULL)) == NULL) + return NULL; + if((name = getenv(var)) == NULL) + error_set_code(-errno, "%s", strerror(errno)); + string_delete(var); + return (name != NULL) ? string_new(name) : NULL; +} + +static String * _new_app_transport(String ** name) +{ + String * p; + String * transport; + + if((p = strchr(*name, ':')) == NULL) + /* XXX hard-coded default value */ + return string_new("tcp"); + /* XXX */ + *(p++) = '\0'; + transport = *name; + if((*name = string_new(p)) == NULL) + { + string_delete(transport); + return NULL; + } + return transport; +} + + /* apptransport_delete */ void apptransport_delete(AppTransport * transport) { @@ -127,10 +199,34 @@ void apptransport_delete(AppTransport * transport) transport->definition->destroy(transport->tplugin); if(transport->plugin != NULL) plugin_delete(transport->plugin); + if(transport->name != NULL) + string_delete(transport->name); object_delete(transport); } +/* accessors */ +/* apptransport_get_mode */ +AppTransportMode apptransport_get_mode(AppTransport * transport) +{ + return transport->mode; +} + + +/* apptransport_get_name */ +String const * apptransport_get_name(AppTransport * transport) +{ + return transport->name; +} + + +/* apptransport_get_transport */ +String const * apptransport_get_transport(AppTransport * transport) +{ + return transport->definition->name; +} + + /* useful */ /* apptransport_client_send */ int apptransport_client_send(AppTransport * transport, AppMessage * message, diff --git a/src/apptransport.h b/src/apptransport.h index 7685dc2..93b410b 100644 --- a/src/apptransport.h +++ b/src/apptransport.h @@ -37,8 +37,15 @@ typedef struct _AppTransportHelper AppTransport * apptransport_new(AppTransportMode mode, AppTransportHelper * helper, char const * plugin, char const * name, Event * event); +AppTransport * apptransport_new_app(AppTransportMode mode, + AppTransportHelper * helper, char const * app, + char const * name, Event * event); void apptransport_delete(AppTransport * transport); +/* accessors */ +String const * apptransport_get_name(AppTransport * transport); +String const * apptransport_get_transport(AppTransport * transport); + /* useful */ /* ATM_CLIENT */ int apptransport_client_send(AppTransport * transport, AppMessage * message, diff --git a/src/lookup.c b/src/lookup.c deleted file mode 100644 index ab5cbf5..0000000 --- a/src/lookup.c +++ /dev/null @@ -1,76 +0,0 @@ -/* $Id$ */ -/* Copyright (c) 2012-2014 Pierre Pronchery */ -/* This file is part of DeforaOS System libApp */ -/* This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . */ - - - -#include -#include -#include "lookup.h" - - -/* new_event_transport */ -static AppTransport * _new_event_transport(AppTransportHelper * helper, - AppTransportMode mode, Event * event, char const * app, - char const * name) -{ - AppTransport * ret; - String * n; - String * transport; - - if((n = _new_server_name(app, name)) == NULL) - return NULL; - if((transport = _new_server_transport(&n)) == NULL) - { - string_delete(n); - return NULL; - } - ret = apptransport_new(mode, helper, transport, n, event); - string_delete(transport); - string_delete(n); - return ret; -} - -static String * _new_server_name(char const * app, char const * name) -{ - String * var; - - if(name != NULL) - return string_new(name); - /* obtain the desired transport and name from the environment */ - if((var = string_new_append("APPSERVER_", app, NULL)) == NULL) - return NULL; - name = getenv(var); - string_delete(var); - return string_new(name); -} - -static String * _new_server_transport(String ** name) -{ - String * p; - String * transport; - - if((p = strchr(*name, ':')) == NULL) - /* XXX hard-coded default value */ - return string_new("tcp"); - /* XXX */ - *(p++) = '\0'; - transport = *name; - if((*name = string_new(p)) == NULL) - { - string_delete(transport); - return NULL; - } - return transport; -} diff --git a/src/lookup.h b/src/lookup.h deleted file mode 100644 index 47a57ff..0000000 --- a/src/lookup.h +++ /dev/null @@ -1,33 +0,0 @@ -/* $Id$ */ -/* Copyright (c) 2012-2014 Pierre Pronchery */ -/* This file is part of DeforaOS System libApp */ -/* This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . */ - - - -#ifndef LIBAPP_LOOKUP_H -# define LIBAPP_LOOKUP_H - -# include -# include -# include "apptransport.h" - - -/* functions */ -static AppTransport * _new_event_transport(AppTransportHelper * helper, - AppTransportMode mode, Event * event, char const * app, - char const * name); -static String * _new_server_name(char const * app, char const * name); -static String * _new_server_transport(String ** name); - -#endif /* !LIBAPP_LOOKUP_H */ diff --git a/src/project.conf b/src/project.conf index 8193cb1..45f42ca 100644 --- a/src/project.conf +++ b/src/project.conf @@ -5,7 +5,7 @@ cflags_force=-W -fPIC `pkg-config --cflags libSystem` cflags=-Wall -g -O2 -pedantic ldflags_force=`pkg-config --libs libSystem` ldflags= -dist=Makefile,appinterface.h,appmessage.h,apptransport.h,lookup.c,lookup.h +dist=Makefile,appinterface.h,appmessage.h,apptransport.h [libApp] type=library @@ -14,7 +14,7 @@ ldflags=-lsocket -lws2_32 install=$(LIBDIR) [appclient.c] -depends=appinterface.h,../include/App/appclient.h,lookup.h,lookup.c +depends=appinterface.h,../include/App/appclient.h [appinterface.c] depends=../include/App/appserver.h,../config.h @@ -26,7 +26,7 @@ depends=../include/App/appmessage.h,appmessage.h depends=../include/App/appmessage.h,../config.h [appserver.c] -depends=appinterface.h,../include/App/appserver.h,lookup.h,lookup.c,../config.h +depends=appinterface.h,../include/App/appserver.h,../config.h [apptransport.c] depends=apptransport.h,../include/App/apptransport.h,../config.h diff --git a/tests/Makefile b/tests/Makefile index c2fa710..3cf8d0d 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -65,7 +65,7 @@ appmessage.o: appmessage.c ../src/libApp.a appserver.o: appserver.c ../src/libApp.a $(CC) $(appserver_CFLAGS) -c appserver.c -lookup.o: lookup.c ../src/lookup.h ../src/lookup.c +lookup.o: lookup.c $(CC) $(lookup_CFLAGS) -c lookup.c transport.o: transport.c ../src/libApp.a diff --git a/tests/lookup.c b/tests/lookup.c new file mode 100644 index 0000000..255fabe --- /dev/null +++ b/tests/lookup.c @@ -0,0 +1,71 @@ +/* $Id$ */ +/* Copyright (c) 2014 Pierre Pronchery */ +/* This file is part of DeforaOS System libApp */ +/* This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + + + +#include +#include +#include +#include +#include "../src/apptransport.h" + + +/* private */ +/* functions */ +/* usage */ +static int _usage(void) +{ + fputs("Usage: lookup [-a app][-n name]\n", stderr); + return 1; +} + + +/* public */ +/* functions */ +/* main */ +int main(int argc, char * argv[]) +{ + int o; + char const * app = NULL; + char const * name = NULL; + Event * event; + AppTransport * transport; + + while((o = getopt(argc, argv, "a:n:")) != -1) + switch(o) + { + case 'a': + app = optarg; + break; + case 'n': + name = optarg; + break; + default: + return _usage(); + } + if((event = event_new()) == NULL) + return error_print("lookup"); + if((transport = apptransport_new_app(ATM_CLIENT, NULL, app, name, + event)) == NULL) + { + event_delete(event); + return error_print("lookup"); + } + printf("transport: %s, name: %s\n", + apptransport_get_transport(transport), + apptransport_get_name(transport)); + apptransport_delete(transport); + return 0; +} diff --git a/tests/project.conf b/tests/project.conf index b50731d..d6ffbdc 100644 --- a/tests/project.conf +++ b/tests/project.conf @@ -35,7 +35,7 @@ type=binary sources=lookup.c [lookup.c] -depends=../src/lookup.h,../src/lookup.c +depends=../src/apptransport.h [tests.log] type=script diff --git a/tests/tests.sh b/tests/tests.sh index 37c26b5..92a1433 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -112,9 +112,8 @@ FAILED= echo "Performing tests:" 1>&2 $DATE > "$target" _test "appmessage" "appmessage" -_test "lookup" "lookup" -a "VFS" -n "localhost" -_test "lookup" "lookup" -a "VFS" -n "tcp:localhost" -_test "lookup" "lookup" -a "VFS" -n "tcp4:localhost" +_test "lookup" "lookup" -a "VFS" -n "tcp:localhost:4242" +_test "lookup" "lookup" -a "VFS" -n "tcp4:localhost:4242" _test "transport" "tcp4 127.0.0.1:4242" -p tcp4 127.0.0.1:4242 _test "transport" "tcp4 localhost:4242" -p tcp4 localhost:4242 _test "transport" "tcp6 ::1.4242" -p tcp6 ::1.4242 @@ -130,8 +129,10 @@ _test "transport" "udp 127.0.0.1:4242" -p udp 127.0.0.1:4242 _test "transport" "udp ::1.4242" -p udp ::1.4242 _test "transport" "udp localhost:4242" -p udp localhost:4242 echo "Expected failures:" 1>&2 +#XXX appclient, appserver and lookup should really succeed _fail "appclient" "appclient" _fail "appserver" "appserver" +_fail "lookup" "lookup" -a "VFS" -n "localhost" _fail "transport" "tcp6 ::1:4242" -p tcp6 ::1:4242 _fail "transport" "tcp ::1:4242" -p tcp ::1:4242 _fail "transport" "udp6 ::1:4242" -p udp6 ::1:4242