-
Notifications
You must be signed in to change notification settings - Fork 243
Closed
Labels
Description
Hi,
I'm referencing server example to create a d-bus server.
The entire code is
import (
"errors"
"fmt"
"github.com/godbus/dbus"
"github.com/godbus/dbus/introspect"
"github.com/rs/zerolog/log"
)
const serviceName = "com.hiveio.vmmanager"
const objectPath = "/com/hiveio/vmmanager"
const intro = `
<node>
<interface name="com.hiveio.vm.Manager">
<method name="CheckHostForMigration">
<arg direction="in" type="s"/>
<arg direction="in" type="s"/>
<arg direction="out" type="b"/>
</method>
</interface>` + introspect.IntrospectDataString + `</node> `
func InitiateServer() error {
conn, err := dbus.SystemBus()
if err != nil {
log.Error().Err(err).Msg("While initiating connection to system bus in initiateServer")
return err
}
iface := VMManagerDbusInterface{}
conn.Export(iface, dbus.ObjectPath(objectPath), serviceName)
conn.Export(introspect.Introspectable(intro), dbus.ObjectPath(objectPath), fmt.Sprintf("%s.Introspectable", serviceName))
reply, err := conn.RequestName(serviceName, dbus.NameFlagDoNotQueue)
if err != nil {
log.Error().Err(err).Stack().Msg("While checking name on system bus in initiateServer")
return err
}
if reply != dbus.RequestNameReplyPrimaryOwner {
log.Error().Err(err).Stack().Interface("reply", reply).Msg("Service name is already taken")
return errors.New("name already taken")
}
log.Info().Msg(fmt.Sprintf("Listening on %s - %s ...", serviceName, objectPath))
select {}
}
type VMManagerDbusInterface struct{}
func (server VMManagerDbusInterface) CheckHostForMigration(guestName string, cpuxml string) (bool, *dbus.Error) {
log.Info().Str("guestName", guestName).Msg("Received request for checking host compatibility")
var body []interface{}
body = append(body, "TestErrorMessage", "TestErrorCode")
e := dbus.NewError("TestErrorName", body)
return true, e
}
I'm trying to test this code using linux terminal before creating a client in my main project just to make sure everything works. The invocation is done using:
root@hive1:~# dbus-send --system \
--dest=com.hiveio.vmmanager \
--type=method_call \
--print-reply \
/com/hiveio/vmmanager \
com.hiveio.vmmanager.CheckHostForMigration \
string:"TestGuest" string:"TestCPU"
If CheckHostForMigration does not return an error, meaning the second parameter as nil, the above call gets executed and returns value correctly. However, if there is an error being returned, then I get following error:
Error org.freedesktop.DBus.Error.NoReply: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.
Can anyone please help? Another question is, what should be correct return type dbus.Error or dbus.DBusError?
Reactions are currently unavailable