Пример #1
0
 def notify(self):
     """when the Subject is modified, it invokes notify;
     then the Observer uses get_state to see the update.
     As a simpler alternative, the Subject could pass the
     new state as a parameter."""
     print 'Notified'
     ns = NameServer()
     proxy = ns.get_proxy(self.subject)
     print 'Got proxy'
     state = proxy.get_state()
     print 'Observer notified; new state =', state
Пример #2
0
 def notify(self):
     """when the Subject is modified, it invokes notify;
     then the Observer uses get_state to see the update.
     As a simpler alternative, the Subject could pass the
     new state as a parameter."""
     print 'Notified'
     ns = NameServer()
     proxy = ns.get_proxy(self.subject)
     print 'Got proxy'
     state = proxy.get_state()
     print 'Observer notified; new state =', state
Пример #3
0
def main(script, to='downey', message='hello', sender='downey', *args):
    """Contact the remote object with the given name and
    invoke popup with the given message"""
    ns = NameServer()
    try:
        name = 'popup_%s' % to
        server = ns.get_proxy(name)
    except Pyro.errors.NamingError:
        print "I can't find a remote object named " + name
        return
    print server.popup(message, sender)
Пример #4
0
    def __init__(self, subject_name):

        # connect to the name server
        id = random.randint(0, 1000000)
        observer_name = subject_name + '_observer%d' % id
        RemoteObject.__init__(self, observer_name)
        
        # register with the subject
        self.subject = subject_name
        ns = NameServer()
        proxy = ns.get_proxy(subject_name)
        proxy.register(observer_name)
        print "I just registered."
Пример #5
0
    def __init__(self, subject_name):

        # connect to the name server
        id = random.randint(0, 1000000)
        observer_name = subject_name + '_observer%d' % id
        RemoteObject.__init__(self, observer_name)

        # register with the subject
        self.subject = subject_name
        ns = NameServer()
        proxy = ns.get_proxy(subject_name)
        proxy.register(observer_name)
        print "I just registered."
Пример #6
0
    def notify_observers(self):
        """notify all registered observers when the state of
        the subject changes"""

        for observer in copy(self.observers):
            try:
                print "Notifying", observer
                ns = NameServer()
                proxy = ns.get_proxy(observer)
                proxy.notify()
            except Exception, x:
                # this clause should catch errors that occur
                # in the Observer code.
                print "".join(Pyro.util.getPyroTraceback(x))
            except:
Пример #7
0
    def notify_observers(self):
        """notify all registered observers when the state of
        the subject changes"""

        for observer in copy(self.observers):
            try:
                print 'Notifying', observer
                ns = NameServer()
                proxy = ns.get_proxy(observer)
                proxy.notify()
            except Exception, x:
                # this clause should catch errors that occur
                # in the Observer code.
                print ''.join(Pyro.util.getPyroTraceback(x))
            except:
Пример #8
0
    def notify_observers(self):
        """notify all registered observers when the state of
        the subject changes"""

        for observer in copy(self.observers):
            try:
                print 'Notifying', observer
                ns = NameServer()
                proxy = ns.get_proxy(observer)
                proxy.notify()
            except Exception as x:
                # this clause should catch errors that occur
                # in the Observer code.
                print ''.join(Pyro.util.getPyroTraceback(x))
            except BaseException:
                # this clause should catch Pyro NamingErrors,
                # which occur when an observer dies.
                print 'Removing ' + observer
                self.observers.remove(observer)
Пример #9
0
"""Example of the Observer pattern using Pyro.

Copyright 2010 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html

"""

import Pyro.errors
from remote_object import NameServer


class Modifier:
    """A Modifier is an object that reads and writes the state of
    a Subject, but it is not a registered Observer."""
    def __init__(self, subject_name):
        self.subject = ns.get_proxy(subject_name)

    def modify(self):
        """Increment the state of the Subject."""
        state = self.subject.get_state()
        self.subject.set_state(state + 1)
        print 'Set state ' + str(state + 1)


ns = NameServer()
subject_name = 'simple_subject'
mod = Modifier(subject_name)
mod.modify()