1 // Copyright 2015 CoreOS, Inc. 2 // 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package dbus 16 17 import ( 18 "time" 19 ) 20 21 // SubscriptionSet returns a subscription set which is like conn.Subscribe but 22 // can filter to only return events for a set of units. 23 type SubscriptionSet struct { 24 *set 25 conn *Conn 26 } 27 28 func (s *SubscriptionSet) filter(unit string) bool { 29 return !s.Contains(unit) 30 } 31 32 // Subscribe starts listening for dbus events for all of the units in the set. 33 // Returns channels identical to conn.SubscribeUnits. 34 func (s *SubscriptionSet) Subscribe() (<-chan map[string]*UnitStatus, <-chan error) { 35 // TODO: Make fully evented by using systemd 209 with properties changed values 36 return s.conn.SubscribeUnitsCustom(time.Second, 0, 37 mismatchUnitStatus, 38 func(unit string) bool { return s.filter(unit) }, 39 ) 40 } 41 42 // NewSubscriptionSet returns a new subscription set. 43 func (conn *Conn) NewSubscriptionSet() *SubscriptionSet { 44 return &SubscriptionSet{newSet(), conn} 45 } 46 47 // mismatchUnitStatus returns true if the provided UnitStatus objects 48 // are not equivalent. false is returned if the objects are equivalent. 49 // Only the Name, Description and state-related fields are used in 50 // the comparison. 51 func mismatchUnitStatus(u1, u2 *UnitStatus) bool { 52 return u1.Name != u2.Name || 53 u1.Description != u2.Description || 54 u1.LoadState != u2.LoadState || 55 u1.ActiveState != u2.ActiveState || 56 u1.SubState != u2.SubState 57 } 58