Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to extract result from Gio.SimpleAsyncResult #76

Closed
Elv13 opened this issue May 20, 2014 · 1 comment
Closed

Unable to extract result from Gio.SimpleAsyncResult #76

Elv13 opened this issue May 20, 2014 · 1 comment

Comments

@Elv13
Copy link

Elv13 commented May 20, 2014

Hi,

I try to use GDbus with lgi but I have some issue. When I try to do a synchronous call it works fine (remove the 3 last arguments from the example below and use call_sync and it will work as expected). However, I cannot find a way to extract SimpleAsyncResult results. Most methods seems to be there but the only useful one. I was wondering if I missed something or it just required unimplemented and/or undocumented hacks to get the result.

Here is a semi working example:

local lgi  = require     'lgi'
local Gio  = lgi.require 'Gio'
local core = require     'lgi.core'
local GLib = lgi.require 'GLib'
local type,unpack = type,unpack
local bus = Gio.bus_get_sync(Gio.BusType.SYSTEM)


local ret,err = bus:call(
    "org.freedesktop.UPower",
    "/org/freedesktop/UPower",
    "org.freedesktop.UPower",
    "HibernateAllowed",
     nil,
     nil,
     Gio.DBusConnectionFlags.NONE,
    -1, -- Timeout
     nil, -- Cancellable
     function(conn,res)
         print("async",conn,Gio.SimpleAsyncResult.get_op_res_gpointer,res:get_op_res_gssize()) --Ok
         print("async",conn,Gio.SimpleAsyncResult.get_op_res_gpointer,res:set_op_res_gpointer()) --Fail
     end
)

local main_loop = GLib.MainLoop()
main_loop:run()

I failed to find a way to get tge boolean back. C examples use set_op_res_gpointer, but it doesn't seem to exist in LGI

Thanks

@pavouk
Copy link
Collaborator

pavouk commented Oct 11, 2014

Sorry that it took so long.

I don't think that you need or should use SImpleAsyncResult, because it is intended to use when implementing asynchronous API, not when you are using it. You should use appropriate _finish() method to retrieve call result. Following modification of your callback argument makes this work:

function(conn, res)
   local ret, err = bus:call_finish(res)
   print(ret, err)
end

Please check giostreams.lua sample distributed with lgi to see possible ways how to deal with Gio async API, particularly Gio.Async could be useful. Your example rewritten in Gio.Async looks like this:

   ... 

   Gio.Async.call(function()
      print(bus:async_call(
         'org.freedesktop.UPower',
          '/org/freedesktop/UPower',
          'org.freedesktop.UPower',
          'HibernateAllowed',
          nil,
          nil,
          'NONE',
          -1))
   end)()

   ...

See Gio.Async docs in https://github.com/pavouk/lgi/blob/master/docs/gio.md

I hope that this helps, feel free to reopen if you still have problems with it.

@pavouk pavouk closed this as completed Oct 11, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants