Moved the lookup code to the AppTransport class

This commit is contained in:
Pierre Pronchery 2014-03-29 03:28:10 +01:00
parent 4984a8f4b8
commit d579c7047d
13 changed files with 202 additions and 166 deletions

View File

@ -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 \

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -15,10 +15,12 @@
#include <stdlib.h>
#ifdef DEBUG
# include <stdio.h>
#endif
#include <string.h>
#include <errno.h>
#include <System.h>
#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;
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,

View File

@ -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,

View File

@ -1,76 +0,0 @@
/* $Id$ */
/* Copyright (c) 2012-2014 Pierre Pronchery <khorben@defora.org> */
/* 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 <http://www.gnu.org/licenses/>. */
#include <stdlib.h>
#include <string.h>
#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;
}

View File

@ -1,33 +0,0 @@
/* $Id$ */
/* Copyright (c) 2012-2014 Pierre Pronchery <khorben@defora.org> */
/* 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 <http://www.gnu.org/licenses/>. */
#ifndef LIBAPP_LOOKUP_H
# define LIBAPP_LOOKUP_H
# include <System/event.h>
# include <System/string.h>
# 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 */

View File

@ -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

View File

@ -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

71
tests/lookup.c Normal file
View File

@ -0,0 +1,71 @@
/* $Id$ */
/* Copyright (c) 2014 Pierre Pronchery <khorben@defora.org> */
/* 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 <http://www.gnu.org/licenses/>. */
#include <unistd.h>
#include <stdio.h>
#include <System/error.h>
#include <System/string.h>
#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;
}

View File

@ -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

View File

@ -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