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