drb09

1,000 views

Published on

Slide of lecture on Shimane University on December 4

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,000
On SlideShare
0
From Embeds
0
Number of Embeds
67
Actions
Shares
0
Downloads
5
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

drb09

  1. 1. dRuby Web
  2. 2. hello() hello_server.rb hello_client.rb "Hello, World."
  3. 3. hello_server.rb require 'drb/drb' class Hello def hello puts('Hello, World.') end end DRb.start_service('druby://localhost:54000', Hello.new) while true sleep 1 end
  4. 4. require require 'drb/drb' class Hello def hello puts('Hello, World.') end end DRb.start_service('druby://localhost:54000', Hello.new) while true sleep 1 end
  5. 5. DRb.start_service require 'drb/drb' class Hello def hello puts('Hello, World.') end end DRb.start_service('druby://localhost:54000', Hello.new) while true sleep 1 end
  6. 6. URI require 'drb/drb' class Hello def hello puts('Hello, World.') end end DRb.start_service('druby://localhost:54000', Hello.new) while true sleep 1 end
  7. 7. require 'drb/drb' class Hello def hello puts('Hello, World.') end end DRb.start_service('druby://localhost:54000', Hello.new) while true sleep 1 end
  8. 8. hello_client.rb require 'drb/drb' DRb.start_service ro = DRbObject.new_with_uri('druby://localhost:54000') ro.hello
  9. 9. hello_client.rb require 'drb/drb' DRb.start_service ro = DRbObject.new_with_uri('druby://localhost:54000') ro.hello
  10. 10. hello_client.rb require 'drb/drb' DRb.start_service ro = DRbObject.new_with_uri('druby://localhost:54000') ro.hello
  11. 11. hello_client.rb require 'drb/drb' DRb.start_service ro = DRbObject.new_with_uri('druby://localhost:54000') ro.hello
  12. 12. require 'drb/drb' class Hello def hello puts('Hello, World.') end end DRb.start_service('druby://localhost:54000', Hello.new) while true sleep 1 end require 'drb/drb' DRb.start_service ro = DRbObject.new_with_uri('druby://localhost:54000') ro.hello
  13. 13. class Hello def hello puts('Hello, World.') end end ro = Hello.new ro.hello
  14. 14. KVS... require 'drb/drb' DRb.start_service('druby://localhost:54300', Hash.new) while true sleep 1 end
  15. 15. hash_server.rb irb irb
  16. 16. def hoge(a1, a2, a3, &block) ... raise FooError unless some_cond ... return value end
  17. 17. Marshal dumpable pass by value Number, String, Boolean... can’t dump pass by reference Proc, Thread, File ...
  18. 18. Queue
  19. 19. Queue
  20. 20. irb Queue deque.rb
  21. 21. require 'drb/drb' require 'thread' DRb.start_service('druby://localhost:54320', Queue.new) while true sleep 1 end require 'drb/drb' DRb.start_service queue = DRbObject.new_with_uri('druby://localhost:54320') while true p queue.pop sleep(rand) end
  22. 22. deque.rb irb Queue deque.rb deque.rb
  23. 23. 1 "Hello, World." 2 "Hello, Again." 3 :Three 4 4 5 [5]
  24. 24. 1 "Hello, World." 2 "Hello, Again." 3 :Three 4 4 5 [5]
  25. 25. 1 "Hello, World." 2 "Hello, Again." 3 :Three 4 4
  26. 26. 1 "Hello, World." 2 "Hello, Again." 3 :Three 4 4 5 [5]
  27. 27. 1 "Hello, World." 2 "Hello, Again." 3 :Three 4 4 5 [5]
  28. 28. require 'rinda/tuplespace' require 'drb/drb' class Notifier def initialize @ts = Rinda::TupleSpace.new @ts.write([:tail, 0]) end def notify(it) tuple = @ts.take([:tail, nil]) tail = tuple[1] + 1 @ts.write([tail, it]) @ts.write([:tail, tail]) tail end def receive(key) @ts.read([key, nil]) end end DRb.start_service('druby://localhost:54321', Notifier.new) while true sleep 1 end
  29. 29. class Notifier class Notifier def initialize @ts = Rinda::TupleSpace.new @ts.write([:tail, 0]) end def notify(it) tuple = @ts.take([:tail, nil]) tail = tuple[1] + 1 @ts.write([tail, it]) @ts.write([:tail, tail]) tail end def receive(key) @ts.read([key, nil]) end end
  30. 30. Tuple Space Engine Client Engine TupleSpace Client Engine
  31. 31. Tuple [:chopstick, 2] [:room_ticket] [‘abc’, 2, 5] [:matrix, 1.6, 3.14] [‘family’, ‘is-sister’, ’Carolyn’, ’Elinor’]
  32. 32. Pattern • Tuple === • case equals • Regexp, Range, Class • nil
  33. 33. Pattern [/^A/, nil, nil] [:matrix, Numeric, Numeric] [’family’, ’is-sister’, ’Carolyn’, nil] [nil, ’age’, (0..18)]
  34. 34. [’seki’, ’age’, 20] >> require ‘rinda/tuplespace’ >> ts = Rinda::TupleSpace.new >> ts.write([’seki’, ’age’, 20]) >> ts.write([’sougo’, ’age’, 18]) >> ts.write([’leonard’, ’age’, 18]) >> ts.read_all([nil, ’age’, 0..19]) => [["sougo", "age", 18], ["leonard", "age", 18]] >> ts.read_all([/^s/, ’age’, Numeric]) => [["seki", "age", 20], ["sougo", "age", 18]]
  35. 35. >> ts.write([:seki, :age, 20]) => #<Rinda::TupleEntry:...> >> TupleSpace :seki :age 20
  36. 36. >> ts.write([:seki, :age, 20]) => #<Rinda::TupleEntry:...> >> TupleSpace >> ts.take([:seki, :age, nil]) => [:seki, :age, 20] >> :seki :age 20
  37. 37. class Notifier def initialize @ts = Rinda::TupleSpace.new @ts.write([:tail, 0]) end def notify(it) tuple = @ts.take([:tail, nil]) tail = tuple[1] + 1 @ts.write([tail, it]) @ts.write([:tail, tail]) tail end def receive(key) @ts.read([key, nil]) end end
  38. 38. initialize :tail 0
  39. 39. notify("Hello, World.") :tail 0
  40. 40. take([:tail, nil]) :tail 0
  41. 41. write([tail, it]) 1 "Hello, World."
  42. 42. write([:tail, tail]) :tail 1 1 "Hello, World."
  43. 43. receive(1) :tail 1 1 "Hello, World."
  44. 44. read([1, nil]) :tail 1 1 "Hello, World."
  45. 45. receive(2) :tail 1 1 "Hello, World."
  46. 46. read([2, nil]) :tail 1 1 "Hello, World."
  47. 47. notify("Hello, Again.") :tail 1 1 "Hello, World."
  48. 48. take([:tail, nil]) :tail 1 1 "Hello, World."
  49. 49. write([tail, it]) 1 "Hello, World." 2 "Hello, Again."
  50. 50. write([:tail, tail]) :tail 2 1 "Hello, World." 2 "Hello, Again."
  51. 51. read([2, nil]) :tail 1 1 "Hello, World." 2 "Hello, Again."
  52. 52. receive.rb require 'drb/drb' DRb.start_service ro = DRbObject.new_with_uri('druby://localhost:54321') key = 0 while true key, obj = ro.receive(key + 1) p [key, obj] sleep(3) end
  53. 53. dRuby Web

×