launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/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  	"launchpad.net/juju-core/state"
     8  	"launchpad.net/juju-core/state/api/params"
     9  	"launchpad.net/juju-core/state/apiserver/common"
    10  	"launchpad.net/juju-core/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  	if err := w.watcher.Err(); err != nil {
    44  		return mask(err)
    45  	}
    46  	return common.ErrStoppedWatcher
    47  }
    48  
    49  // Stop stops the watcher.
    50  func (w *srvNotifyWatcher) Stop() error {
    51  	return w.resources.Stop(w.id)
    52  }
    53  
    54  // srvStringsWatcher notifies about changes for all entities of a
    55  // given kind, sending the changes as a list of strings.
    56  type srvStringsWatcher struct {
    57  	watcher   state.StringsWatcher
    58  	id        string
    59  	resources *common.Resources
    60  }
    61  
    62  // Next returns when a change has occured to an entity of the
    63  // collection being watched since the most recent call to Next
    64  // or the Watch call that created the srvStringsWatcher.
    65  func (w *srvStringsWatcher) Next() (params.StringsWatchResult, error) {
    66  	if changes, ok := <-w.watcher.Changes(); ok {
    67  		return params.StringsWatchResult{
    68  			Changes: changes,
    69  		}, nil
    70  	}
    71  	err := w.watcher.Err()
    72  	if err == nil {
    73  		err = common.ErrStoppedWatcher
    74  	}
    75  	return params.StringsWatchResult{}, err
    76  }
    77  
    78  // Stop stops the watcher.
    79  func (w *srvStringsWatcher) Stop() error {
    80  	return w.resources.Stop(w.id)
    81  }
    82  
    83  // srvRelationUnitsWatcher notifies about units entering and leaving
    84  // the scope of a RelationUnit, and changes to the settings of those
    85  // units known to have entered.
    86  type srvRelationUnitsWatcher struct {
    87  	watcher   state.RelationUnitsWatcher
    88  	id        string
    89  	resources *common.Resources
    90  }
    91  
    92  // Next returns when a change has occured to an entity of the
    93  // collection being watched since the most recent call to Next
    94  // or the Watch call that created the srvRelationUnitsWatcher.
    95  func (w *srvRelationUnitsWatcher) Next() (params.RelationUnitsWatchResult, error) {
    96  	if changes, ok := <-w.watcher.Changes(); ok {
    97  		return params.RelationUnitsWatchResult{
    98  			Changes: changes,
    99  		}, nil
   100  	}
   101  	err := w.watcher.Err()
   102  	if err == nil {
   103  		err = common.ErrStoppedWatcher
   104  	}
   105  	return params.RelationUnitsWatchResult{}, err
   106  }
   107  
   108  // Stop stops the watcher.
   109  func (w *srvRelationUnitsWatcher) Stop() error {
   110  	return w.resources.Stop(w.id)
   111  }