dbus.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package dbus
  14. import (
  15. godbus "github.com/godbus/dbus"
  16. )
  17. // Interface is an interface that presents a subset of the godbus/dbus API. Use this
  18. // when you want to inject fakeable/mockable D-Bus behavior.
  19. type Interface interface {
  20. // SystemBus returns a connection to the system bus, connecting to it
  21. // first if necessary
  22. SystemBus() (Connection, error)
  23. // SessionBus returns a connection to the session bus, connecting to it
  24. // first if necessary
  25. SessionBus() (Connection, error)
  26. }
  27. // Connection represents a D-Bus connection
  28. type Connection interface {
  29. // Returns an Object representing the bus itself
  30. BusObject() Object
  31. // Object creates a representation of a remote D-Bus object
  32. Object(name, path string) Object
  33. // Signal registers or unregisters a channel to receive D-Bus signals
  34. Signal(ch chan<- *godbus.Signal)
  35. }
  36. // Object represents a remote D-Bus object
  37. type Object interface {
  38. // Call synchronously calls a D-Bus method
  39. Call(method string, flags godbus.Flags, args ...interface{}) Call
  40. }
  41. // Call represents a pending or completed D-Bus method call
  42. type Call interface {
  43. // Store returns a completed call's return values, or an error
  44. Store(retvalues ...interface{}) error
  45. }
  46. // Implements Interface in terms of actually talking to D-Bus
  47. type dbusImpl struct {
  48. systemBus *connImpl
  49. sessionBus *connImpl
  50. }
  51. // Implements Connection as a godbus.Conn
  52. type connImpl struct {
  53. conn *godbus.Conn
  54. }
  55. // Implements Object as a godbus.Object
  56. type objectImpl struct {
  57. object godbus.BusObject
  58. }
  59. // Implements Call as a godbus.Call
  60. type callImpl struct {
  61. call *godbus.Call
  62. }
  63. // New returns a new Interface which will use godbus to talk to D-Bus
  64. func New() Interface {
  65. return &dbusImpl{}
  66. }
  67. // SystemBus is part of Interface
  68. func (db *dbusImpl) SystemBus() (Connection, error) {
  69. if db.systemBus == nil {
  70. bus, err := godbus.SystemBus()
  71. if err != nil {
  72. return nil, err
  73. }
  74. db.systemBus = &connImpl{bus}
  75. }
  76. return db.systemBus, nil
  77. }
  78. // SessionBus is part of Interface
  79. func (db *dbusImpl) SessionBus() (Connection, error) {
  80. if db.sessionBus == nil {
  81. bus, err := godbus.SessionBus()
  82. if err != nil {
  83. return nil, err
  84. }
  85. db.sessionBus = &connImpl{bus}
  86. }
  87. return db.sessionBus, nil
  88. }
  89. // BusObject is part of the Connection interface
  90. func (conn *connImpl) BusObject() Object {
  91. return &objectImpl{conn.conn.BusObject()}
  92. }
  93. // Object is part of the Connection interface
  94. func (conn *connImpl) Object(name, path string) Object {
  95. return &objectImpl{conn.conn.Object(name, godbus.ObjectPath(path))}
  96. }
  97. // Signal is part of the Connection interface
  98. func (conn *connImpl) Signal(ch chan<- *godbus.Signal) {
  99. conn.conn.Signal(ch)
  100. }
  101. // Call is part of the Object interface
  102. func (obj *objectImpl) Call(method string, flags godbus.Flags, args ...interface{}) Call {
  103. return &callImpl{obj.object.Call(method, flags, args...)}
  104. }
  105. // Store is part of the Call interface
  106. func (call *callImpl) Store(retvalues ...interface{}) error {
  107. return call.call.Store(retvalues...)
  108. }