"Distributed programming with Ruby" Reading notes three Hello world rinda and Tuplespace (part1.2-1)

Source: Internet
Author: User



Chapter2:rinda
including 7 parts:
"Hello World" the Rinda
Understanding Tuples and Tuplespaces
Callbacks and observers
Security with Rinda
Renewing Rinda Services
Conclusion
Endnotes
When reviewing all of our DRb applications, you'll notice, for a start, then we hardcoded IP addresses and ports into Bo Th our servers and clients.
What does need to start the service on a different machine which has a different IP address? We could also create and attempt to maintain complex configurations, if the modern world of cloud computing IP address ES fluctuate every time launch an instance.
What is about finding-what services, as a client, is available in the distributed applications? Again, we have been hardcoding our connection to the service we want to use.
Enter Rinda, a Ruby port of the LINDA1 distributed computing paradigm. This paradigm allows an application, through the use of a ringserver, to detect and interact with other services on the NE Twork. This should help us solve some of the problems we were facing and our earlier DRb applications.
"Hello World" the Rinda
A ringserver is essentially a central clearinghouse for services,the life cycle for a typical ringserver with one service And one client basically goes something like This:the client asks the ringserver where to find a particular service. The Ringserver responds with the service ' s address. The client and service then communicate directly with each of the other.
Building a ringserver is straightforward and simple. In Chapter 3, "ringydingy," we'll discuss ringydingy, a library that makes it even easier to use ringservers; But first we should write our own so we can understand exactly what it does.
Example:Rewrite "Hello World" use rinda
Start Tuplespace, set the port number under Windows, do not know why run the server program error (confirm that the DRB is running), use the default value to run correctly
Code
tuplespaces and tuples: We can think of a tuplespace as that bulletin board. It's a place where we can post our services. The flyer is like the Tuple.
If they find a posting they like, they can either take it off the board so that no one else would see it, or they can make A copy for themselves and leave the original up there for others to see.
In our current example we would is posting a "Hello World" ervice tuples to the tuplespace.
Server
Client
Index 2: After we had retrieved a tuple from the ringserver, we need to get the Helloworldserver instance out of the tuple Array W ITH server = service[2]. It might seem odd in order to retrieve our Helloworldserver instance from the Tuple we retrieved from the Ringserver Using an Array syntax, service[2], it does, in fact, make sense. We stored an Array containing the original description of the service and the instance of the Helloworldserver in the Ring Server as an Array, the index of the Helloworldserver is 2.
Output
You can also see that the ' Hello World ' server is running on 192.168.80.172:53062. That is the IP address of the machine running the server, and the port is picked basically at random.
Understanding Tuples and Tuplespaces
Tuplespaces allow us to perform a variety of tasks. We can write to a tuplespace to post our service. Someone can come along and get a read-only copy of a service from that tuplespace.
Writing a Tuple to a tuplespace
Createing a tuple:pass in an Array containing the template you want to store and if you were trying to look up a service , you must match the template.
There does seem to bes a loose "standard" in the community of using a fourentry Tuple template with the following pattern
We would write this template like so:
Under the covers, Rinda takes the Array and creates the appropriate Rinda::tuple class, so we don ' t have to worry about LE Arning yet another class API.
The Write method also takes an optional second argument, which can is either an Integer, representing the number of second s you want this Tuple to live, or a renewer.
The second parameter to the Write method determines how and when the Tuple gets expired.
Reading a Tuple from a tuplespace
In our ' Hello World ' Application server code, we called the Write method on the Ringserver, passing it a Tuple template. To retrieve this tuple we call the Read method and pass it a variation for our Tuple template. Because Our original tuple template had four entries and our client also have to pass in a the tuple template with the four entries. When you pass the nil as an entry in a Tuple template, you ' re doing a wildcard search on any entry in that position.
In our case we is asking for the first service whose first Tuple entry is:hello_world_service. We don ' t care about the other template entries is.
Example
Remerber to start Tuplespace service
First start DRb.thread.join, then comment it, and run the read code.
Output
As can see, we do three different requests for services, each with different Tuple templates. Yet each time we get back a reference to the first Tuple entry we created. When Rinda attempts to find a tuple based on a tuple template, it returns the first tuple that matches the tuple template Passed into it. This should demonstrate the importance isn't only of using a consistent tuple template, but also of writing your As specific as possible.
Taking a Tuple from a tuplespace
Let's see what happens if we throw a bit of concurrency into the mix.
A fairly contrived example:
Server
Client
In our client we'll create ten different Threads. Each of those Threads reads the current Integer from the server. The client then sleeps for a random time between 0 and 5 seconds. When the client is wakes up, it increments the "the Integer by 1" and "writes that" to the server. Because We have ten Threads running, and each Thread loops through our code ten times, we should see the final count is 10 0. But output:
...
Well, each time a Thread reads the Integer it got from the service, it holds onto this integer,sleeps for a bit, and then Updates that Integer and writes it back to the service. When it does that, it overwrites the previous Integer that another thread placed in there while the first Thread was sleep Ing.
Fix it:
Change ring_server.read to ring_server.take
When you use the Take method, each thread takes the Tuple from the server,holds it while it sleeps, and then updates it an D puts it back, it prevents the other threads from doing the same thing.
Out: #用sublime运行的结果有点问题, ran down with cmd
The question you should being asking is, if the Threads hits the service, and the first one calls the Take method, gets the Tu PLE, and then sleeps, why doesn ' t the other raise an exception? Shouldn ' t the second Thread get back nil from the server, causing the rest of the code to blow up? The answer is, it does not get back nil from the server. In fact, what actually happens was that the second Thread just sits there and waits for the Take method to return the Tuple . The Take method was basically wrapped in a big infinite loop this keeps checking to see if the Tuple had been returned. When it was returned, the loop is broken, and the Tuple was returned to the client. In the real world, this code would hang forever while it waited for the Tuple if we never wrote it back. So it's wise to always wrap any take method calls in a Timeout to prevent your code from simply hanging.
Reading all tuples in a tuplespace
The Read method returns the first Tuple that matches the template
If you want to get back a list of services,the solution in Rinda are to use the Read_all method.
An example on Read_all Page 52-53


"Distributed programming with Ruby" Reading notes three Hello world rinda and Tuplespace (part1.2-1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.