《Distributed Programming With Ruby》读书笔记四 Callbacks, observers and Renew a service (Part1.2-2)

    • Callbacks and Observers
      • Callbacks and observers: They are a universal design pattern that allows a system to wait for another system or process to tell it that some event has occurred.
      • In Ruby it is quite common for libraries to provide some sort of callback mechanism.One of the most significant examples of this is Ruby on Rails and its libraries.
      • Understanding Callbacks
      • Implementing Callbacks
      • Now that you have a simple grasp on what callbacks are, we can start to look at how you can use callbacks with your distributed applications to make them more powerful,and even more loosely coupled.
      • example
      • server
      • The only thing worth noting is that we are not using a Rinda::SimpleRenewer to handle the expiration of our Tuple.  Instead, we have set it to 30 seconds.
      • client
      • observer
      • To help debug our fantastic distributed application, we would like to know whenever someone takes a Tuple that matches :callback_service, whenever someone writes to the :callback_service Tuple, and when and if the :callback_service Tuple expires. Doing all of this is actually remarkably easy using the callback system built into Rinda.
      • 运行失败了,只收到了“close”。先一放…
      • 介绍了一本设计模式相关的书:
        Design Patterns in Ruby (Addison-Wesley Professional, 2007).
    • Security with Rinda(Page 59)
      • like Drb Security in chapter 1
      • Safe level
      • ACLs
      • SSL
      • Selecting a RingServer
      • Whenever we have discussed security, all we have really talked about is malicious clients trying to either attack our services or send dangerous or bogus messages to our servers.
      • But what about the security of our clients? A hacker could just as easily set up a server that pretends to be a valid server and intercept requests from our clients.Certainly using things like SSL can help prevent this type of interception, because requests are terminated if either side of the relationship does not present the right credentials. The other option we have is to be more selective in choosing our RingServer.
      • This example will get "RingNotFound (RuntimeError)"
      • tuplespace:
      • server:
      • Obviously using the primary method on Rinda::RingFinger won’t work, because it’s looking for a RingServer broadcasting on port 7647.
      • Next:
      • The first thing we have to do is create a new instance of Rinda::RingFinger.Now, if we were to create the instance as follows,
      • ring_finger = Rinda::RingFinger.new
      • we would not be doing anything different than what is happening under the covers when we call the primary class method. We will still only be looking for RingServers broadcasting on port 7647—first on the network, and then on the localhost.The new method on Rinda::RingFinger takes two parameters. The first is an Array. This Array defaults to ['<broadcast>', 'localhost']. These represent the hosts that we want to search for our RingServers. An Array is used to specify order, so be aware that the order in which you have your servers listed in the Array will be the order in which they are searched. If nil is passed in as the first argument, the list defaults to ['localhost']. I don’t know why it defaults to just ['localhost'] and not ['<broadcast>', 'localhost'], but just be aware that it does. If we knew that our RingServer was hosted at ringserver.example.com on the default port of 7647, we would create a new Rinda::RingFinger like this:
      • ring_finger = Rinda::RingFinger.new(['ringserver.example.com'])
      • tuplespace
      • server
      • 不知道是因为用的是windows系统还是哪个地方我理解错了,没有得到demo里演示的结果,报错:`lookup_ring_any': RingNotFound (RuntimeError)
      • 这个例子后面还有一个类似的程序例子,有时间或用得到可以再详细看看。Page68
    • Renewing Rinda Services
      • a Renewer determines how long the Tuple will live and when it gets expired.
      • Both lines of code do the same thing. They tell the RingServer that this Tuple should expire 180 seconds after the last time it was touched. When someone goes to touch the Tuple, it is checked to see if it has expired. If the Tuple has expired, it checks with its Renewer to see if it should be renewed and, if so, for how long.
      • Using a Numeric to Renew a Service
      • example
      • tuplespace
      • 这个还是用原来的了…起码能运行起来
      • server
      • client
      • output
      • Here we are looping six times, reading the Tuple, and sleeping for a second. Because we have a 10-second expiration time, this should not be a problem. Then we sleep for another 6 seconds and try again. When we do that, we are trying to access the Tuple at least 12 seconds after we have created it.
      • Using nil to Renew a Service
      • If we always want our Tuples to live forever, without fear of their expiring, using nil to set the expiration is the easiest way:
        • ring_server = Rinda::RingFinger.primary
        • ring_server.write([:count_service, 0], nil)
      • Using the SimpleRenewer Class
      • By default this class sets a 180-second expiration time for the Tuple. When that expiration is reached, Rinda queries the SimpleRenewer to see if the Tuple should be renewed. The SimpleRenewer class tells Rinda to renew it again for another 180 seconds. You can change this period of time, 180 seconds, when you instantiate the class, like this:
        • Rinda::SimpleRenewer.new(10)
      • Custom Renewers
      • Writing a custom Renewer has two main ingredients.
        • The first is the DRbUndumped module. This ensures that the Renewer can be transported across the wire to the RingServer.
        • The second ingredient is a renew method.
      • Example: Page73
    • Conclusion
    • Endnotes

posted on 2016-12-18 22:11  Liz-  阅读(175)  评论(0编辑  收藏  举报