github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/state/apiserver/watcher.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package apiserver 5 6 import ( 7 "github.com/juju/juju/state" 8 "github.com/juju/juju/state/api/params" 9 "github.com/juju/juju/state/apiserver/common" 10 "github.com/juju/juju/state/multiwatcher" 11 ) 12 13 type srvClientAllWatcher struct { 14 watcher *multiwatcher.Watcher 15 id string 16 resources *common.Resources 17 } 18 19 func (aw *srvClientAllWatcher) Next() (params.AllWatcherNextResults, error) { 20 deltas, err := aw.watcher.Next() 21 return params.AllWatcherNextResults{ 22 Deltas: deltas, 23 }, err 24 } 25 26 func (w *srvClientAllWatcher) Stop() error { 27 return w.resources.Stop(w.id) 28 } 29 30 type srvNotifyWatcher struct { 31 watcher state.NotifyWatcher 32 id string 33 resources *common.Resources 34 } 35 36 // Next returns when a change has occurred to the 37 // entity being watched since the most recent call to Next 38 // or the Watch call that created the NotifyWatcher. 39 func (w *srvNotifyWatcher) Next() error { 40 if _, ok := <-w.watcher.Changes(); ok { 41 return nil 42 } 43 err := w.watcher.Err() 44 if err == nil { 45 err = common.ErrStoppedWatcher 46 } 47 return err 48 } 49 50 // Stop stops the watcher. 51 func (w *srvNotifyWatcher) Stop() error { 52 return w.resources.Stop(w.id) 53 } 54 55 // srvStringsWatcher notifies about changes for all entities of a 56 // given kind, sending the changes as a list of strings. 57 type srvStringsWatcher struct { 58 watcher state.StringsWatcher 59 id string 60 resources *common.Resources 61 } 62 63 // Next returns when a change has occured to an entity of the 64 // collection being watched since the most recent call to Next 65 // or the Watch call that created the srvStringsWatcher. 66 func (w *srvStringsWatcher) Next() (params.StringsWatchResult, error) { 67 if changes, ok := <-w.watcher.Changes(); ok { 68 return params.StringsWatchResult{ 69 Changes: changes, 70 }, nil 71 } 72 err := w.watcher.Err() 73 if err == nil { 74 err = common.ErrStoppedWatcher 75 } 76 return params.StringsWatchResult{}, err 77 } 78 79 // Stop stops the watcher. 80 func (w *srvStringsWatcher) Stop() error { 81 return w.resources.Stop(w.id) 82 } 83 84 // srvRelationUnitsWatcher notifies about units entering and leaving 85 // the scope of a RelationUnit, and changes to the settings of those 86 // units known to have entered. 87 type srvRelationUnitsWatcher struct { 88 watcher state.RelationUnitsWatcher 89 id string 90 resources *common.Resources 91 } 92 93 // Next returns when a change has occured to an entity of the 94 // collection being watched since the most recent call to Next 95 // or the Watch call that created the srvRelationUnitsWatcher. 96 func (w *srvRelationUnitsWatcher) Next() (params.RelationUnitsWatchResult, error) { 97 if changes, ok := <-w.watcher.Changes(); ok { 98 return params.RelationUnitsWatchResult{ 99 Changes: changes, 100 }, nil 101 } 102 err := w.watcher.Err() 103 if err == nil { 104 err = common.ErrStoppedWatcher 105 } 106 return params.RelationUnitsWatchResult{}, err 107 } 108 109 // Stop stops the watcher. 110 func (w *srvRelationUnitsWatcher) Stop() error { 111 return w.resources.Stop(w.id) 112 }