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 */ /* App */
/* types */ /* types */
typedef struct _App App;
typedef struct _AppMessage AppMessage; typedef struct _AppMessage AppMessage;
#endif /* !LIBAPP_APP_APP_H */ #endif /* !LIBAPP_APP_APP_H */

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -53,7 +53,7 @@ int main(int argc, char * argv[])
default: default:
return _usage(); return _usage();
} }
if((appserver = appserver_new(0, app, name)) == NULL) if((appserver = appserver_new(NULL, 0, app, name)) == NULL)
{ {
error_print("appserver"); error_print("appserver");
return 2; 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) if((p = hash_get(value, "ret")) == NULL)
p = "void"; p = "void";
fprintf(appbroker->fp, "%s%s%s%s%s%s", p, " ", appbroker->prefix, "_", 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++) for(i = 0; i < APPSERVER_MAX_ARGUMENTS; i++)
{ {
snprintf(buf, sizeof(buf), "arg%d", i + 1); 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, fprintf(stderr, "DEBUG: %s(%d, %s, %s, %p, %zu)\n", __func__, verbose,
hostname, service, (void *)calls, calls_cnt); hostname, service, (void *)calls, calls_cnt);
#endif #endif
if((ac = appclient_new(app, name)) == NULL) if((ac = appclient_new(NULL, app, name)) == NULL)
return _error(PROGNAME, 1); return _error(PROGNAME, 1);
if(verbose != 0) if(verbose != 0)
puts("Connected."); puts("Connected.");