From 6b006dd7a227fb6d14161cab3759a467a69f21d3 Mon Sep 17 00:00:00 2001 From: Thiago de Arruda Date: Wed, 28 May 2014 08:42:08 -0300 Subject: API: Events: Automatically pass channel ids to API functions The dispatch function generator was customized to allow for API functions to declare a 'channel_id' as first argument. --- scripts/msgpack-gen.lua | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'scripts') diff --git a/scripts/msgpack-gen.lua b/scripts/msgpack-gen.lua index 3cc1558fd7..fac1b10b45 100644 --- a/scripts/msgpack-gen.lua +++ b/scripts/msgpack-gen.lua @@ -62,6 +62,12 @@ for i = 1, #arg - 1 do api.functions[#api.functions + 1] = tmp[i] local fn_id = #api.functions local fn = api.functions[fn_id] + if #fn.parameters ~= 0 and fn.parameters[1][2] == 'channel_id' then + -- this function should receive the channel id + fn.receives_channel_id = true + -- remove the parameter since it won't be passed by the api client + table.remove(fn.parameters, 1) + end if #fn.parameters ~= 0 and fn.parameters[#fn.parameters][1] == 'error' then -- function can fail if the last parameter type is 'Error' fn.can_fail = true @@ -111,7 +117,7 @@ end output:write([[ }; -void msgpack_rpc_dispatch(uint64_t id, msgpack_object *req, msgpack_packer *res) +void msgpack_rpc_dispatch(uint64_t channel_id, msgpack_object *req, msgpack_packer *res) { Error error = { .set = false }; uint64_t method_id = (uint32_t)req->via.array.ptr[2].via.u64; @@ -121,7 +127,7 @@ void msgpack_rpc_dispatch(uint64_t id, msgpack_object *req, msgpack_packer *res) msgpack_pack_nil(res); // The result is the [channel_id, metadata] array msgpack_pack_array(res, 2); - msgpack_pack_uint64(res, id); + msgpack_pack_uint64(res, channel_id); msgpack_pack_raw(res, sizeof(msgpack_metadata)); msgpack_pack_raw_body(res, msgpack_metadata, sizeof(msgpack_metadata)); return; @@ -169,8 +175,19 @@ for i = 1, #api.functions do output:write(fn.return_type..' rv = ') end - -- write the call without the closing parenthesis - output:write(fn.name..'('..call_args) + -- write the function name and the opening parenthesis + output:write(fn.name..'(') + + if fn.receives_channel_id then + -- if the function receives the channel id, pass it as first argument + if #args > 0 then + output:write('channel_id, '..call_args) + else + output:write('channel_id)') + end + else + output:write(call_args) + end if fn.can_fail then -- if the function can fail, also pass a pointer to the local error object -- cgit