Professional Web Applications Themes

Drb and observer pattern - Ruby

Hi all and have a happy new year everybody! I'm trying to built a set of programs that - may run on different machines, or as different processes on one machine - I'd like to have one program to notify other(s) of certain events Now I thought that combining DRb and the Observer pattern might be the way to go, but as I'm posting a question I apparently ran into an error. BTW, I'm running a SuSE Linux 9.0 with ruby 1.8.1 [884] stktao ~/devel/util: ruby -v ruby 1.8.1 (2003-12-25) [i686-linux] I boiled down the server part to this: ==> ...

  1. #1

    Default Drb and observer pattern

    Hi all and have a happy new year everybody!

    I'm trying to built a set of programs that
    - may run on different machines, or as different processes on one machine
    - I'd like to have one program to notify other(s) of certain events

    Now I thought that combining DRb and the Observer pattern might be the
    way to go, but as I'm posting a question I apparently ran into an error.

    BTW, I'm running a SuSE Linux 9.0 with ruby 1.8.1
    [884] stktao ~/devel/util: ruby -v
    ruby 1.8.1 (2003-12-25) [i686-linux]



    I boiled down the server part to this:

    ==> File: Notifier.rb

    require "drb/drb"
    require "observer"

    class Notifier
    include Observable
    def change( i )
    puts "Changing: #{'%2d' % i}"
    changed( true )
    notify_observers( Time.now, i )
    end
    end

    DRb.start_service( 'druby://localhost:4711', Notifier.new )
    puts DRb.uri
    DRb.thread.join



    The program using that service got shortened to this:

    require 'drb'

    class Dummy
    def initialize( observed )
    observed.add_observer( self )
    end

    # define update as required by the Observer module
    def update( *args )
    puts "!!!"
    p *args
    end
    end

    notifier = DRbObject.new( nil, 'druby://localhost:4711' )
    d = Dummy.new( notifier )

    # Do something with the notifier to get d's 'update' called
    notifier.change( rand( 10 ) )


    Now, running the notifier works fine, but starting the client leaves me
    with this:

    [884] stktao ~/devel/util: ruby dummy.rb
    (druby://localhost:4711) /usr/local/lib/ruby/1.8/observer.rb:126:in
    `add_observer': Observer <Dummy> needs to respond to 'update'.
    (NoMethodError)
    from dummy.rb:5:in `initialize'
    from dummy.rb:15:in `new'
    from dummy.rb:15


    { Note that I changed the part of 'observer.rb' to give some information
    about the observer that leads to the exception - just to be sure that
    it's an Object of class Dummy that rose the exception. }

    Now, why do I get the exception? I defined 'update' of objects of class
    Dummy, didn't I?

    Where's my mistake? Is it that way a good way anyway?

    Happy rubying!

    Stephan




    Stephan Guest

  2. #2

    Default Re: Drb and observer pattern

    Stephan Kämper said: 

    Have a look at the thread starting at ruby-talk:88698.

    Regards,
    Andre


    Andre Guest

  3. #3

    Default More from using DRb and observers...

    Andre Nathan wrote: 
    Hi all,

    Andre, thanks for the link to the earlier discussion.

    Now the 'compressed' notifier looks like this...

    require 'drb'
    require 'drb/observer'

    class Notifier
    include Observable
    include DRb::DRbUndumped

    def change( i )
    puts "Changing: #{'%2d' % i}"
    changed( true )
    notify_observers( Time.now, i )
    end
    end

    DRb.start_service( 'druby://tao:4711', Notifier.new )
    puts DRb.uri
    DRb.thread.join

    ....and the client like that:

    require 'drb'

    class Client
    include DRb::DRbUndumped

    def initialize( observed )
    observed.add_observer( self )
    end

    def update( *args )
    puts "!!!"
    p *args
    end
    end
    DRb.start_service
    notifier = DRbObject.new( nil, 'druby://tao:4711' )
    d = Client.new( notifier )

    notifier.change( rand( 10 ) )

    Now, running both programs once works fine.

    For Notifier:
    [979] stktao ~/devel/util: ruby Notifier.rb
    druby://tao:4711
    Changing: 4


    For Client:
    [974] stktao ~/devel/util: ruby Client.rb
    !!!
    Sun Jan 04 00:32:53 CET 2004
    6
    Great! And thanks again for the help!


    Unfortunately starting the client a second time it get an error I
    (partially) don't understand.

    The Server output continues with:

    Changing: 1


    The Client output is this:
    Sun Jan 04 00:40:11 CET 2004
    4
    [974] stktao ~/devel/util: ruby Client.rb
    (druby://tao:4711) /usr/local/lib/ruby/1.8/drb/drb.rb:705:in `open':
    druby://tao:33324 - #<Errno::ECONNREFUSED: Connection refused -
    connect(2)> (DRb::DRbConnError)
    from (druby://tao:4711)
    /usr/local/lib/ruby/1.8/drb/drb.rb:698:in `each'
    from (druby://tao:4711)
    /usr/local/lib/ruby/1.8/drb/drb.rb:698:in `open'
    from (druby://tao:4711)
    /usr/local/lib/ruby/1.8/drb/drb.rb:1084:in `initialize'
    from (druby://tao:4711)
    /usr/local/lib/ruby/1.8/drb/drb.rb:1067:in `new'
    from (druby://tao:4711)
    /usr/local/lib/ruby/1.8/drb/drb.rb:1067:in `open'
    from (druby://tao:4711)
    /usr/local/lib/ruby/1.8/drb/drb.rb:1014:in `method_missing'
    from (druby://tao:4711)
    /usr/local/lib/ruby/1.8/observer.rb:185:in `notify_observers'
    from (druby://tao:4711)
    /usr/local/lib/ruby/1.8/observer.rb:184:in `each'
    from (druby://tao:4711)
    /usr/local/lib/ruby/1.8/observer.rb:184:in `notify_observers'
    from (druby://tao:4711) Notifier.rb:11:in `change'
    from Client.rb:19
    [974] stktao ~/devel/util:

    Now, why does it say >>> druby://tao:33324 <<< in the 1st line of th output?
    And how can I stop this?

    BTW, 'ruby -v' yields: ruby 1.8.1 (2003-12-25) [i686-linux].

    Happy rubying!

    Stephan


    Stephan Guest

  4. #4

    Default Re: More from using DRb and observers...

    --XZLT0nNRngx3qG4/
    Content-Type: text/plain; cht=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    Stephan K?mper (de) wrote:
     
    ut? 

    I take it you mean for the notifier?
     
    ^^^^
    This is why
     

    --=20
    Eric Hodel - net - http://segment7.net
    All messages signed with fingerprint:
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04


    --XZLT0nNRngx3qG4/
    Content-Type: application/pgp-signature
    Content-Disposition: inline

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.2 (FreeBSD)

    iD8DBQE/97A9MypVHHlsnwQRAvfcAJ48rGcH2R9S+LefQy7Mnh+KPC5Gew CdGYXP
    ts0nnSzj/7fYioGKNn8EAFg=
    =caFR
    -----END PGP SIGNATURE-----

    --XZLT0nNRngx3qG4/--

    Eric Guest

  5. #5

    Default Re: More from using DRb and observers...

    Hi all, hi Eric,

    thanks for your answer.

    Eric Hodel - net wrote this: [/ref]
    th output? 
    >
    > I take it you mean for the notifier?[/ref]

    No, I really did mean what I wrote: It's the client program that
    complains (well, sort of) about not being able to connect.
    I do understand that the connection is refused for the service
    'druby://tao:33324'. There's just no partner running on that port.
    However, I do not understand, why port 33324 is tried at all...
     
    > ^^^^
    > This is why[/ref]

    Hmmm, do you really think it's the line with the 'puts', that causes
    the second connection to fail? As the problem remains after removing
    the line you marked, the reason for the refused connection is
    something else, I suspect -- correct me, if I'm wrong.
     

    Cheers, & happay rubying
    Stephan
    Stephan Guest

Similar Threads

  1. Replies: 42
    Last Post: May 31st, 06:26 PM
  2. OOP - Observer/listener
    By sloerentetsen in forum Macromedia Flash Actionscript
    Replies: 0
    Last Post: February 7th, 10:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139