Drb and observer pattern

Discussion in 'Ruby' started by Stephan Kämper, Jan 1, 2004.

  1. 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] stk@tao ~/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] stk@tao ~/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 Kämper, Jan 1, 2004
    #1
    1. Advertising

  2. Stephan Kämper

    Andre Nathan Guest

    Stephan Kämper said:
    > Where's my mistake? Is it that way a good way anyway?


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

    Regards,
    Andre
     
    Andre Nathan, Jan 1, 2004
    #2
    1. Advertising

  3. More from using DRb and observers...

    Andre Nathan wrote:
    > Have a look at the thread starting at ruby-talk:88698.
    >

    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] stk@tao ~/devel/util: ruby Notifier.rb
    druby://tao:4711
    Changing: 4


    For Client:
    [974] stk@tao ~/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] stk@tao ~/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] stk@tao ~/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 Kämper, Jan 4, 2004
    #3
  4. Stephan Kämper

    Eric Hodel Guest

    Re: More from using DRb and observers...

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

    Stephan K?mper () wrote:

    > For Notifier:
    > [979] stk@tao ~/devel/util: ruby Notifier.rb
    > druby://tao:4711
    > Changing: 4
    >=20
    > Now, why does it say >>> druby://tao:33324 <<< in the 1st line of th outp=

    ut?
    > And how can I stop this?


    I take it you mean for the notifier?

    > Now the 'compressed' notifier looks like this...
    >=20
    > require 'drb'
    > require 'drb/observer'
    >=20
    > class Notifier
    > include Observable
    > include DRb::DRbUndumped
    >=20
    > def change( i )
    > puts "Changing: #{'%2d' % i}"
    > changed( true )
    > notify_observers( Time.now, i )
    > end
    > end
    >=20
    > DRb.start_service( 'druby://tao:4711', Notifier.new )
    > puts DRb.uri

    ^^^^
    This is why

    > DRb.thread.join


    --=20
    Eric Hodel - - 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+KPC5GewCdGYXP
    ts0nnSzj/7fYioGKNn8EAFg=
    =caFR
    -----END PGP SIGNATURE-----

    --XZLT0nNRngx3qG4/--
     
    Eric Hodel, Jan 4, 2004
    #4
  5. Stephan Kämper

    Stephan Guest

    Re: More from using DRb and observers...

    Hi all, hi Eric,

    thanks for your answer.

    Eric Hodel - wrote this:
    >Stephan K?mper () wrote:
    >> For Notifier:
    >> [979] stk@tao ~/devel/util: ruby Notifier.rb
    >> druby://tao:4711
    >> Changing: 4
    >> Now, why does it say >>> druby://tao:33324 <<< in the 1st line of

    th output?
    >> And how can I stop this?

    >
    > I take it you mean for the notifier?


    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...

    >> 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

    > ^^^^
    > This is why


    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.

    > DRb.thread.join


    Cheers, & happay rubying
    Stephan
     
    Stephan, Jan 5, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Beatrice Rutger
    Replies:
    0
    Views:
    729
    Beatrice Rutger
    Jun 5, 2005
  2. Miles Keaton
    Replies:
    3
    Views:
    182
    Miles Keaton
    Mar 30, 2005
  3. Kirk Haines

    More DRb; SSL & DRB & errors

    Kirk Haines, Jul 1, 2005, in forum: Ruby
    Replies:
    0
    Views:
    129
    Kirk Haines
    Jul 1, 2005
  4. J. Wook
    Replies:
    16
    Views:
    285
    Robert Klemme
    May 16, 2007
  5. Ittay Dror
    Replies:
    1
    Views:
    138
    Ittay Dror
    Oct 21, 2008
Loading...

Share This Page