import random
from RemoteObject import *

class Observer(RemoteObject):
    """An Observer is an object that watches another object and reacts
    whenever the Subject changes state."""

    def __init__(self, observer_name, subject_name):
        RemoteObject.__init__(self)

        # connect to the name server
        self.connect(ns, observer_name)
        
        # register with the subject
        self.subject = ns.get_proxy(subject_name)
        self.subject.register(observer_name)
        print "I just registered."

    # the following methods are intended to be invoked remotely

    def notify(self):
        """when the subject is modified, it invokes notify"""
        state = self.subject.get_state()
        print 'Observer notified; new state =', state

ns = NameServer()
subject_name = 'bob'
observer_name = subject_name + '_observer%d' % random.randint(0, 1000000)
obs = Observer(observer_name, subject_name)
obs.requestLoop()

