QmpReturn and associated functions are used internally for synchronous dispatch return for now. They will be used for returning in an asynchronous way, by holding the pre-prepared response, and having some bookkeeping in QmpSession.
They also help to factor out a bit of code preparing the json reply and calling the return_cb. Signed-off-by: Marc-André Lureau <marcandre.lur...@redhat.com> --- include/qapi/qmp/dispatch.h | 33 +++++++++++++++++++ monitor.c | 7 +--- qapi/qmp-dispatch.c | 65 ++++++++++++++++++++++++------------- 3 files changed, 76 insertions(+), 29 deletions(-) diff --git a/include/qapi/qmp/dispatch.h b/include/qapi/qmp/dispatch.h index 5329d0052a..7ca8ab17d9 100644 --- a/include/qapi/qmp/dispatch.h +++ b/include/qapi/qmp/dispatch.h @@ -17,6 +17,8 @@ #include "qemu/queue.h" #include "qapi/qmp/json-streamer.h" +typedef struct QmpReturn QmpReturn; + typedef void (QmpCommandFunc)(QDict *, QObject **, Error **); typedef enum QmpCommandOptions @@ -48,6 +50,37 @@ struct QmpSession { QmpCommandList *cmds; }; +struct QmpReturn { + QmpSession *session; + QDict *rsp; +}; + +/* + * @qmp_return_new: + * + * Allocates and initializes a QmpReturn. + */ +QmpReturn *qmp_return_new(QmpSession *session, const QDict *req); + +/* + * @qmp_return_free: + * + * Free a QmpReturn. This shouldn't be needed if you actually return + * with qmp_return{_error}. + */ +void qmp_return_free(QmpReturn *qret); + +/* + * @qmp_return{_error}: + * + * Construct the command reply, and call the + * return_cb() associated with the session. + * + * Finally, free the QmpReturn. + */ +void qmp_return(QmpReturn *qret, QObject *rsp); +void qmp_return_error(QmpReturn *qret, Error *err); + void qmp_register_command(QmpCommandList *cmds, const char *name, QmpCommandFunc *fn, QmpCommandOptions options); void qmp_unregister_command(QmpCommandList *cmds, const char *name); diff --git a/monitor.c b/monitor.c index 94ad366c2d..73a06d4156 100644 --- a/monitor.c +++ b/monitor.c @@ -4010,7 +4010,6 @@ static void monitor_qmp_bh_dispatcher(void *data) static void qmp_dispatch_cb(QmpSession *session, QDict *req) { - QDict *rsp; QObject *id; Monitor *mon = container_of(session, Monitor, qmp.session); Error *err = NULL; @@ -4081,11 +4080,7 @@ static void qmp_dispatch_cb(QmpSession *session, QDict *req) return; err: - rsp = qdict_new(); - qdict_put_obj(rsp, "error", qmp_build_error_object(err)); - error_free(err); - monitor_json_emitter(mon, QOBJECT(rsp)); - QDECREF(rsp); + qmp_return_error(qmp_return_new(session, req), err); } static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size) diff --git a/qapi/qmp-dispatch.c b/qapi/qmp-dispatch.c index 53d099d303..7b57fa7987 100644 --- a/qapi/qmp-dispatch.c +++ b/qapi/qmp-dispatch.c @@ -19,6 +19,42 @@ #include "qapi/qmp/qjson.h" #include "qapi/qmp/qbool.h" +QmpReturn *qmp_return_new(QmpSession *session, const QDict *req) +{ + QmpReturn *qret = g_new0(QmpReturn, 1); + QObject *id = req ? qdict_get(req, "id") : NULL; + + qret->session = session; + qret->rsp = qdict_new(); + if (id) { + qobject_incref(id); + qdict_put_obj(qret->rsp, "id", id); + } + + return qret; +} + +void qmp_return_free(QmpReturn *qret) +{ + QDECREF(qret->rsp); + g_free(qret); +} + +void qmp_return(QmpReturn *qret, QObject *rsp) +{ + qdict_put_obj(qret->rsp, "return", rsp ?: QOBJECT(qdict_new())); + qret->session->return_cb(qret->session, qret->rsp); + qmp_return_free(qret); +} + +void qmp_return_error(QmpReturn *qret, Error *err) +{ + qdict_put_obj(qret->rsp, "error", qmp_build_error_object(err)); + error_free(err); + qret->session->return_cb(qret->session, qret->rsp); + qmp_return_free(qret); +} + QDict *qmp_dispatch_check_obj(const QObject *request, Error **errp) { const QDictEntry *ent; @@ -150,7 +186,7 @@ static void qmp_json_parser_emit(JSONMessageParser *parser, GQueue *tokens) { QmpSession *session = container_of(parser, QmpSession, parser); QObject *obj; - QDict *req; + QDict *req = NULL; Error *err = NULL; obj = json_parser_parse_err(tokens, NULL, &err); @@ -167,11 +203,7 @@ static void qmp_json_parser_emit(JSONMessageParser *parser, GQueue *tokens) end: if (err) { - QDict *rsp = qdict_new(); - qdict_put_obj(rsp, "error", qmp_build_error_object(err)); - error_free(err); - session->return_cb(session, rsp); - QDECREF(rsp); + qmp_return_error(qmp_return_new(session, req), err); } qobject_decref(obj); } @@ -204,28 +236,15 @@ void qmp_session_destroy(QmpSession *session) void qmp_dispatch(QmpSession *session, QDict *req) { + QmpReturn *qret = qmp_return_new(session, req); Error *err = NULL; QObject *ret; - QDict *rsp; - QObject *id = qdict_get(req, "id"); ret = do_qmp_dispatch(session->cmds, req, &err); - - rsp = qdict_new(); - if (id) { - qobject_incref(id); - qdict_put_obj(rsp, "id", id); - } if (err) { - qdict_put_obj(rsp, "error", qmp_build_error_object(err)); - error_free(err); + assert(!ret); + qmp_return_error(qret, err); } else if (ret) { - qdict_put_obj(rsp, "return", ret); - } else { - QDECREF(rsp); - return; + qmp_return(qret, ret); } - - session->return_cb(session, rsp); - QDECREF(rsp); } -- 2.17.0.rc1.1.g4c4f2b46a3