~chipaca/go-dbus/packaging

« back to all changes in this revision

Viewing changes to dbus.go

  • Committer: James Henstridge
  • Date: 2013-02-08 10:54:03 UTC
  • Revision ID: james@jamesh.id.au-20130208105403-ii58dhga3gaoxlzq
Simplify the message receive goroutine, and rename a few more underscore 
prefixed methods.

Show diffs side-by-side

added added

removed removed

Lines of Context:
131
131
        bus.signalMatchRules = make(signalWatchSet)
132
132
        bus.nameInfo = make(map[string] *nameInfo)
133
133
 
134
 
        go bus._RunLoop()
 
134
        go bus.receiveLoop()
135
135
        if bus.UniqueName, err = bus.busProxy.Hello(); err != nil {
136
136
                bus.Close()
137
137
                return nil, err
145
145
        return nil
146
146
}
147
147
 
148
 
func (p *Connection) _MessageReceiver(msgChan chan<- *Message) {
 
148
func (p *Connection) receiveLoop() {
149
149
        for {
150
150
                msg, err := readMessage(p.conn)
151
151
                if err != nil {
154
154
                        }
155
155
                        break
156
156
                }
157
 
                msgChan <- msg
158
 
        }
159
 
        close(msgChan)
160
 
}
161
 
 
162
 
func (p *Connection) _RunLoop() {
163
 
        msgChan := make(chan *Message)
164
 
        go p._MessageReceiver(msgChan)
165
 
        for msg := range msgChan {
166
 
                p._MessageDispatch(msg)
167
 
        }
168
 
}
169
 
 
170
 
func (p *Connection) _MessageDispatch(msg *Message) {
 
157
                if err = p.dispatchMessage(msg); err != nil {
 
158
                        log.Println("Error dispatching message:", err)
 
159
                        break
 
160
                }
 
161
        }
 
162
}
 
163
 
 
164
func (p *Connection) dispatchMessage(msg *Message) error {
171
165
        // Run the message through the registered filters, stopping
172
166
        // processing if a filter returns nil.
173
167
        for _, filter := range p.messageFilters {
174
168
                msg := filter.filter(msg)
175
169
                if msg == nil {
176
 
                        return
 
170
                        return nil
177
171
                }
178
172
        }
179
173
 
182
176
                switch {
183
177
                case msg.Iface == "org.freedesktop.DBus.Peer" && msg.Member == "Ping":
184
178
                        reply := NewMethodReturnMessage(msg)
185
 
                        _ = p.Send(reply)
 
179
                        if err := p.Send(reply); err != nil {
 
180
                                return err
 
181
                        }
186
182
                case msg.Iface == "org.freedesktop.DBus.Peer" && msg.Member == "GetMachineId":
187
183
                        // Should be returning the UUID found in /var/lib/dbus/machine-id
188
184
                        fmt.Println("XXX: handle GetMachineId")
189
185
                        reply := NewMethodReturnMessage(msg)
190
 
                        _ = reply.AppendArgs("machine-id")
191
 
                        _ = p.Send(reply)
 
186
                        if err := reply.AppendArgs("machine-id"); err != nil {
 
187
                                return err
 
188
                        }
 
189
                        if err := p.Send(reply); err != nil {
 
190
                                return err
 
191
                        }
192
192
                default:
193
 
                        // XXX: need to lock the map
194
193
                        p.handlerMutex.Lock()
195
194
                        handler, ok := p.objectPathHandlers[msg.Path]
196
195
                        p.handlerMutex.Unlock()
198
197
                                handler <- msg
199
198
                        } else {
200
199
                                reply := NewErrorMessage(msg, "org.freedesktop.DBus.Error.UnknownObject", "Unknown object path " + string(msg.Path))
201
 
                                _ = p.Send(reply)
 
200
                                if err := p.Send(reply); err != nil {
 
201
                                        return err
 
202
                                }
202
203
                        }
203
204
                }
204
205
        case TypeMethodReturn, TypeError:
220
221
                        watch.handler(msg)
221
222
                }
222
223
        }
 
224
        return nil
223
225
}
224
226
 
225
227
func (p *Connection) Close() error {