App calls now pass a user-defined pointer to the App as first argument

This commit is contained in:
Pierre Pronchery 2014-04-30 19:32:54 +02:00
parent f047f2a6e5
commit d8939635ca
14 changed files with 87 additions and 68 deletions

View File

@ -21,6 +21,8 @@
/* App */
/* types */
typedef struct _App App;
typedef struct _AppMessage AppMessage;
#endif /* !LIBAPP_APP_APP_H */

View File

@ -21,6 +21,7 @@
# include <stdint.h>
# include <System/event.h>
# include <System/variable.h>
# include "app.h"
/* AppClient */
@ -29,9 +30,9 @@ typedef struct _AppClient AppClient;
/* functions */
AppClient * appclient_new(char const * app, char const * name);
AppClient * appclient_new_event(char const * app, char const * name,
Event * event);
AppClient * appclient_new(App * self, char const * app, char const * name);
AppClient * appclient_new_event(App * self, char const * app,
char const * name, Event * event);
void appclient_delete(AppClient * appclient);
/* useful */

View File

@ -19,6 +19,7 @@
# define LIBAPP_APP_APPSERVER_H
# include <System/event.h>
# include "app.h"
/* AppServer */
@ -36,10 +37,10 @@ typedef struct _AppServer AppServer;
/* functions */
AppServer * appserver_new(AppServerOptions options, char const * app,
char const * name);
AppServer * appserver_new_event(AppServerOptions options, char const * app,
char const * name, Event * event);
AppServer * appserver_new(App * self, AppServerOptions options,
char const * app, char const * name);
AppServer * appserver_new_event(App * self, AppServerOptions options,
char const * app, char const * name, Event * event);
void appserver_delete(AppServer * appserver);
/* accessors */

View File

@ -33,6 +33,7 @@
/* types */
struct _AppClient
{
App * app;
AppInterface * interface;
Event * event;
int event_free;
@ -50,15 +51,15 @@ static int _appclient_helper_message(void * data, AppTransport * transport,
/* public */
/* functions */
/* appclient_new */
AppClient * appclient_new(char const * app, char const * name)
AppClient * appclient_new(App * self, char const * app, char const * name)
{
return appclient_new_event(app, name, NULL);
return appclient_new_event(self, app, name, NULL);
}
/* appclient_new_event */
AppClient * appclient_new_event(char const * app, char const * name,
Event * event)
AppClient * appclient_new_event(App * self, char const * app,
char const * name, Event * event)
{
AppClient * appclient;
@ -68,6 +69,7 @@ AppClient * appclient_new_event(char const * app, char const * name,
#endif
if((appclient = object_new(sizeof(*appclient))) == NULL)
return NULL;
appclient->app = self;
appclient->interface = appinterface_new(app);
appclient->helper.data = appclient;
appclient->helper.message = _appclient_helper_message;
@ -121,7 +123,8 @@ int appclient_call(AppClient * appclient,
va_list ap;
va_start(ap, method);
ret = appinterface_callv(appclient->interface, result, method, ap);
ret = appinterface_callv(appclient->interface, appclient->app, result,
method, ap);
va_end(ap);
return ret;
}
@ -145,8 +148,8 @@ int appclient_call_variable(AppClient * appclient,
for(i = 0; i < cnt; i++)
argv[i] = va_arg(ap, Variable *);
va_end(ap);
ret = appinterface_call_variablev(appclient->interface, result, method,
cnt, argv);
ret = appinterface_call_variablev(appclient->interface, appclient->app,
result, method, cnt, argv);
free(argv);
return ret;
}

View File

@ -80,7 +80,7 @@ typedef struct _AppInterfaceCall
AppInterfaceCallArg type;
AppInterfaceCallArg * args;
size_t args_cnt;
void * func;
void * (*func)(void *);
} AppInterfaceCall;
struct _AppInterface
@ -387,7 +387,7 @@ int appinterface_get_args_count(AppInterface * appinterface, size_t * count,
/* useful */
/* appinterface_callv */
int appinterface_callv(AppInterface * appinterface, void ** result,
int appinterface_callv(AppInterface * appinterface, App * app, void ** result,
char const * method, va_list args)
{
int ret;
@ -401,8 +401,9 @@ int appinterface_callv(AppInterface * appinterface, void ** result,
r = NULL;
else if((r = variable_new(call->type.type, NULL)) == NULL)
return -1;
if((ret = marshall_call(call->type.type, r, call->func, call->args_cnt,
NULL)) == 0 && r != NULL)
if((ret = marshall_call(app, call->type.type, r, call->func,
call->args_cnt, NULL)) == 0
&& r != NULL)
{
if(result != NULL)
/* XXX return 0 anyway? */
@ -414,8 +415,9 @@ int appinterface_callv(AppInterface * appinterface, void ** result,
/* appinterface_call_variablev */
int appinterface_call_variablev(AppInterface * appinterface, Variable * result,
char const * method, size_t argc, Variable ** argv)
int appinterface_call_variablev(AppInterface * appinterface, App * app,
Variable * result, char const * method,
size_t argc, Variable ** argv)
{
AppInterfaceCall * call;
@ -424,7 +426,8 @@ int appinterface_call_variablev(AppInterface * appinterface, Variable * result,
if(argc != call->args_cnt)
return -1;
/* FIXME implement argv */
return marshall_call(call->type.type, result, call->func, argc, NULL);
return marshall_call(app, call->type.type, result, call->func, argc,
NULL);
}

View File

@ -41,9 +41,10 @@ int appinterface_get_args_count(AppInterface * appinterface, size_t * count,
char const * function);
/* useful */
int appinterface_callv(AppInterface * appinterface, void ** result,
int appinterface_callv(AppInterface * appinterface, App * app, void ** result,
char const * method, va_list args);
int appinterface_call_variablev(AppInterface * appinterface, Variable * result,
char const * method, size_t argc, Variable ** argv);
int appinterface_call_variablev(AppInterface * appinterface, App * app,
Variable * result, char const * method,
size_t argc, Variable ** argv);
#endif /* !LIBAPP_APPINTERFACE_H */

View File

@ -36,6 +36,8 @@
/* types */
struct _AppServer
{
App * app;
AppServerOptions options;
AppInterface * interface;
Event * event;
int event_free;
@ -53,21 +55,23 @@ static int _appserver_helper_message(void * data, AppTransport * transport,
/* public */
/* functions */
/* appserver_new */
AppServer * appserver_new(AppServerOptions options, const char * app,
char const * name)
AppServer * appserver_new(App * self, AppServerOptions options,
const char * app, char const * name)
{
return appserver_new_event(options, app, name, NULL);
return appserver_new_event(self, options, app, name, NULL);
}
/* appserver_new_event */
AppServer * appserver_new_event(AppServerOptions options, char const * app,
char const * name, Event * event)
AppServer * appserver_new_event(App * self, AppServerOptions options,
char const * app, char const * name, Event * event)
{
AppServer * appserver;
if((appserver = object_new(sizeof(*appserver))) == NULL)
return NULL;
appserver->app = self;
appserver->options = options;
appserver->interface = appinterface_new_server(app);
appserver->helper.data = appserver;
appserver->helper.message = _appserver_helper_message;
@ -162,8 +166,8 @@ static int _helper_message_call(AppServer * appserver, AppTransport * transport,
if(!appinterface_can_call(appserver->interface, name, method))
/* XXX report errors */
return -1;
ret = appinterface_call_variablev(appserver->interface, result, method,
0, NULL);
ret = appinterface_call_variablev(appserver->interface, appserver->app,
result, method, 0, NULL);
if(result != NULL)
variable_delete(result);
return ret;

View File

@ -286,7 +286,7 @@ String * apptransport_lookup(char const * app)
error_set_code(1, "%s: %s", app, "Could not lookup");
return NULL;
}
if((appclient = appclient_new(session, NULL)) == NULL)
if((appclient = appclient_new(NULL, session, NULL)) == NULL)
return NULL;
appclient_call(appclient, (void **)&name, "lookup", app);
appclient_delete(appclient);
@ -317,13 +317,14 @@ int apptransport_server_register(AppTransport * transport, char const * app)
{
int ret;
int res = -1;
const char session[] = "Session";
if(transport->mode != ATM_SERVER)
return -error_set_code(1, "%s",
"Only servers can register to sessions");
if(transport->appclient != NULL)
appclient_delete(transport->appclient);
if((transport->appclient = appclient_new("Session", NULL)) == NULL)
if((transport->appclient = appclient_new(NULL, session, NULL)) == NULL)
return -1;
ret = appclient_call(transport->appclient, (void **)&res, "register",
app, transport->name);

View File

@ -24,35 +24,37 @@
/* Marshall */
/* public */
/* functions */
static int _call0(VariableType type, Variable * result, void * func);
static int _call0(App * app, VariableType type, Variable * result,
void * (*func)(void *));
int marshall_call(VariableType type, Variable * result, void * func,
size_t argc, Variable ** argv)
int marshall_call(App * app, VariableType type, Variable * result,
void * (*func)(void *), size_t argc, Variable ** argv)
{
if(argc == 0)
return _call0(type, result, func);
return _call0(app, type, result, func);
/* FIXME implement */
return -1;
}
static int _call0(VariableType type, Variable * result, void * func)
static int _call0(App * app, VariableType type, Variable * result,
void * (*func)(void *))
{
int ret = 0;
union
{
void * call;
void (*call_null)(void);
uint8_t (*call_bool)(void);
int8_t (*call_i8)(void);
uint8_t (*call_u8)(void);
int16_t (*call_i16)(void);
uint16_t (*call_u16)(void);
int32_t (*call_i32)(void);
uint32_t (*call_u32)(void);
int32_t (*call_i64)(void);
uint32_t (*call_u64)(void);
float (*call_f)(void);
double (*call_d)(void);
void (*call_null)(void *);
uint8_t (*call_bool)(void *);
int8_t (*call_i8)(void *);
uint8_t (*call_u8)(void *);
int16_t (*call_i16)(void *);
uint16_t (*call_u16)(void *);
int32_t (*call_i32)(void *);
uint32_t (*call_u32)(void *);
int32_t (*call_i64)(void *);
uint32_t (*call_u64)(void *);
float (*call_f)(void *);
double (*call_d)(void *);
} f;
union
{
@ -74,60 +76,60 @@ static int _call0(VariableType type, Variable * result, void * func)
switch(type)
{
case VT_NULL:
f.call_null();
f.call_null(app);
break;
case VT_BOOL:
res.b = (f.call_bool() != 0) ? 1 : 0;
res.b = (f.call_bool(app) != 0) ? 1 : 0;
if(result != NULL)
ret = variable_set_from(result, type, &res.b);
break;
case VT_INT8:
res.i8 = f.call_i8();
res.i8 = f.call_i8(app);
if(result != NULL)
ret = variable_set_from(result, type, &res.i8);
break;
case VT_UINT8:
res.u8 = f.call_u8();
res.u8 = f.call_u8(app);
if(result != NULL)
ret = variable_set_from(result, type, &res.u8);
break;
case VT_INT16:
res.i16 = f.call_i16();
res.i16 = f.call_i16(app);
if(result != NULL)
ret = variable_set_from(result, type, &res.i16);
break;
case VT_UINT16:
res.u16 = f.call_u16();
res.u16 = f.call_u16(app);
if(result != NULL)
ret = variable_set_from(result, type, &res.u16);
break;
case VT_INT32:
res.i32 = f.call_i32();
res.i32 = f.call_i32(app);
if(result != NULL)
ret = variable_set_from(result, type, &res.i32);
break;
case VT_UINT32:
res.u32 = f.call_u32();
res.u32 = f.call_u32(app);
if(result != NULL)
ret = variable_set_from(result, type, &res.u32);
break;
case VT_INT64:
res.i64 = f.call_i64();
res.i64 = f.call_i64(app);
if(result != NULL)
ret = variable_set_from(result, type, &res.i64);
break;
case VT_UINT64:
res.u64 = f.call_u64();
res.u64 = f.call_u64(app);
if(result != NULL)
ret = variable_set_from(result, type, &res.u64);
break;
case VT_FLOAT:
res.f = f.call_f();
res.f = f.call_f(app);
if(result != NULL)
ret = variable_set_from(result, type, &res.f);
break;
case VT_DOUBLE:
res.d = f.call_d();
res.d = f.call_d(app);
if(result != NULL)
ret = variable_set_from(result, type, &res.d);
break;

View File

@ -19,11 +19,12 @@
# define LIBAPP_MARSHALL_H
# include <System/variable.h>
# include "App/app.h"
/* Marshall */
/* functions */
int marshall_call(VariableType type, Variable * result, void * func,
size_t argc, Variable ** argv);
int marshall_call(App * app, VariableType type, Variable * result,
void * (*func)(void *), size_t argc, Variable ** argv);
#endif /* !LIBAPP_MARSHALL_H */

View File

@ -52,7 +52,7 @@ int main(int argc, char * argv[])
default:
return _usage();
}
if((appclient = appclient_new(app, name)) == NULL)
if((appclient = appclient_new(NULL, app, name)) == NULL)
{
error_print("appclient");
return 2;

View File

@ -53,7 +53,7 @@ int main(int argc, char * argv[])
default:
return _usage();
}
if((appserver = appserver_new(0, app, name)) == NULL)
if((appserver = appserver_new(NULL, 0, app, name)) == NULL)
{
error_print("appserver");
return 2;

View File

@ -112,7 +112,7 @@ static int _appbroker_foreach_call(char const * key, Hash * value, void * data)
if((p = hash_get(value, "ret")) == NULL)
p = "void";
fprintf(appbroker->fp, "%s%s%s%s%s%s", p, " ", appbroker->prefix, "_",
key, "(AppServerClient * client");
key, "(App * app, AppServerClient * client");
for(i = 0; i < APPSERVER_MAX_ARGUMENTS; i++)
{
snprintf(buf, sizeof(buf), "arg%d", i + 1);

View File

@ -74,7 +74,7 @@ static int _appclient(int verbose, char const * app, char const * name,
fprintf(stderr, "DEBUG: %s(%d, %s, %s, %p, %zu)\n", __func__, verbose,
hostname, service, (void *)calls, calls_cnt);
#endif
if((ac = appclient_new(app, name)) == NULL)
if((ac = appclient_new(NULL, app, name)) == NULL)
return _error(PROGNAME, 1);
if(verbose != 0)
puts("Connected.");