OLD | NEW |
1 package state | 1 package state |
2 | 2 |
3 import ( | 3 import ( |
4 "launchpad.net/goyaml" | 4 "launchpad.net/goyaml" |
5 "launchpad.net/juju-core/log" | 5 "launchpad.net/juju-core/log" |
6 "launchpad.net/juju-core/state/presence" | 6 "launchpad.net/juju-core/state/presence" |
7 "launchpad.net/juju-core/state/watcher" | 7 "launchpad.net/juju-core/state/watcher" |
8 "launchpad.net/tomb" | 8 "launchpad.net/tomb" |
9 ) | 9 ) |
10 | 10 |
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 return tomb.ErrDying | 424 return tomb.ErrDying |
425 case w.changeChan <- uc: | 425 case w.changeChan <- uc: |
426 } | 426 } |
427 return nil | 427 return nil |
428 } | 428 } |
429 | 429 |
430 func (w *MachineUnitsWatcher) done() { | 430 func (w *MachineUnitsWatcher) done() { |
431 close(w.changeChan) | 431 close(w.changeChan) |
432 } | 432 } |
433 | 433 |
| 434 // MachineWatcher observes changes to the settings of a machine. |
| 435 type MachineWatcher struct { |
| 436 contentWatcher |
| 437 m *Machine |
| 438 changeChan chan *Machine |
| 439 } |
| 440 |
| 441 // newMachineWatcher creates and starts a watcher to watch information |
| 442 // about the machine. |
| 443 func newMachineWatcher(m *Machine) *MachineWatcher { |
| 444 w := &MachineWatcher{ |
| 445 m: m, |
| 446 contentWatcher: newContentWatcher(m.st, m.zkPath()), |
| 447 changeChan: make(chan *Machine), |
| 448 } |
| 449 go w.loop(w) |
| 450 return w |
| 451 } |
| 452 |
| 453 // Changes returns a channel that will receive the new |
| 454 // *Machine when a change is detected. Note that multiple |
| 455 // changes may be observed as a single event in the channel. |
| 456 // The first event on the channel holds the initial state |
| 457 // as returned by Machine.Info. |
| 458 func (w *MachineWatcher) Changes() <-chan *Machine { |
| 459 return w.changeChan |
| 460 } |
| 461 |
| 462 func (w *MachineWatcher) update(change watcher.ContentChange) error { |
| 463 select { |
| 464 case <-w.tomb.Dying(): |
| 465 return tomb.ErrDying |
| 466 case w.changeChan <- w.m: |
| 467 } |
| 468 return nil |
| 469 } |
| 470 |
| 471 func (w *MachineWatcher) done() { |
| 472 close(w.changeChan) |
| 473 } |
| 474 |
434 // ServicesWatcher observes the addition and removal of services. | 475 // ServicesWatcher observes the addition and removal of services. |
435 type ServicesWatcher struct { | 476 type ServicesWatcher struct { |
436 contentWatcher | 477 contentWatcher |
437 knownServices map[string]*Service | 478 knownServices map[string]*Service |
438 knownServiceKeys []string | 479 knownServiceKeys []string |
439 changeChan chan *ServicesChange | 480 changeChan chan *ServicesChange |
440 } | 481 } |
441 | 482 |
442 // ServicesChange holds services that were added or removed | 483 // ServicesChange holds services that were added or removed |
443 // from the environment. | 484 // from the environment. |
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
882 return | 923 return |
883 } | 924 } |
884 select { | 925 select { |
885 case <-t.Dying(): | 926 case <-t.Dying(): |
886 return | 927 return |
887 case w.updates <- unitSettingsChange{name, us}: | 928 case w.updates <- unitSettingsChange{name, us}: |
888 } | 929 } |
889 } | 930 } |
890 } | 931 } |
891 } | 932 } |
OLD | NEW |