Finally, let's take a look at the distributed programming in Ruby. Now that the network is very common, we sometimes want to deliver various objects on the Internet, but unfortunately, like CORBA, RMI these protocols use very laborious, need special provisions, abnormal processing, but also in any call Define the interface.
Ruby has a simple solution to eliminate the cumbersome of the above method. Distributed ruby (also called DRB or DRUBY) is a separate library, which is completely written by Ruby. With this library, you can transfer a variety of objects (Ruby objects) through TCP in different Ruby processes, and only need little step.
Listing 8 shows such an example, this server shared an object, through this object, you can get the server's time. Third lines to the 7th line define this object to be shared on local time, line 9 binding this object to a DRB server (in this case 2222), because the server program is in a separate thread, 10 lines ensure that the main program will exit after this thread.
Listing 8: A Simple Distributed Ruby Server
1 Require 'DRB'
2
3 Class Info
4 Def Get_Time
5 "it is now # {time.now}"
6 end
7 end
8
9 DRB.Start_Service ("druby: //your.host.name: 2222", info.new)
10 DRB.THREAD.JOIN
Listing 9 is a client program, is also very simple, the DRBOBJECT call and the remote server of the fourth line have established a connection, returns a proxy for the remote object, and you can call the remote object like the local object. .
Listing 9: a Simple Distributed Ruby Client
1 Require 'DRB'
2
3 DRB.START_SERVICE
4 info = drubject.new (nil, "druby: //your.host.name: 2222")
5
6 3.Times Do
7 puts info.get_time
8 SLEEP 2
9 end
Let's take a look at another very interesting tuplesaps, which is the first presentation of David Gelernter in the Linda system, Tuplespaces like a shared BBS system, the program can post a message, or get a message from it, here The news is an array of some values. OUT method is used to write messages to Tuplespace, so the following code code creates a Tuplespace containing 4 contents:
Require 'tuplespace'
Ts = tuplespace.new
Ts.out ["Dave", "Car", "Blazer"]
Ts.out ["DAVE", "Computer", "Dell"]
Ts.out ["Andy", "Car", "Explorer"]
Ts.out ["Andy", "LINUX"]
Make Tuplespaces interesting is that you want to get the content inside, you are not based on the address, but based on the content itself (by matching). Ruby implemented Tuplespace is more powerful, and the matching mode of obtaining the message can be a value, the object of the object, the regular expression, the Range, etc. NIL means that you don't care what it is, that is, it means that anything matches nil. To get the message content, you can use the in method. If the mode specified in the IN method can find a match in tuplespace, then this matching item will be removed from the tuplespace and the returns to the caller; otherwise, IN will wait until there is a matching item. If there are multiple records matching the mode specified in I, one of the records will be randomly returned. Continue the previous example, the following example reads the already deposited record from the tuplespace, pay attention to the last statement, which uses the regular expression as a matching mode.
# Read One of Dave's Possessions
Res1 = Ts.in ["Dave", NIL, NIL]
# SomeOwning a car
Res2 = Ts.in [NIL, "car", nil]
# a Possession Containing The "x" or "z"
Res3 = ts.in [nil, nil, / [xz] /]
From this simple example, you can easily write complex, collaborate, and parallel systems.
For example, you can solve complex AI issues with Tuplespace. A process can generate a problem by combining a set of data into the tuplespace, other processes read it from tuplespace from tuplespace from tuplespace; when a process gets an incorrectly, it may also put this The problem is subdivided into smaller issues, then put them in tuplespace. Other processes have achieved this problem and then resolve them or continue to decompose these issues.
This process will continue until all issues are resolved.
To accomplish this article, we will write a simple P2P chat program using DRB and Tuplespace, which stores messages consisting of three elements in tuplespace, namely, the sender, recipient, and message content of the message.
The client's program In Listing 10, the client runs together by two threads, the sending thread is the main thread, and the received thread is created in it. Send a thread from line 16 to 22 lines, it reads the string from the user's console, the format is as follows:
TO: Message Text
TO is the name of the recipient, and the first line will be divided into two parts. The front is the name, and the remaining is the message content. Then, Chapter 19 makes this message into a array writes to Tuplespace.
Listing 10: a chat client based on tuplespaces
1 Require 'DRB'
2 Require 'tuplespace'
3
4 DRB.START_SERVICE
5 TS = DRBOBJECT.NEW (NIL, / "Druby: //server.host: 12321")
6
7 my_name = argv [0]
8
9 thread.new do
10 loop do
11 from, unused, line = ts.in / [nil, my_name, nil]
12 puts "# {from} Says: # {line}" 13 end
14 END
15
16 While Line = Gets
17 TO, Text = line.split (/: /, 2)
18 IF text
19 ts.out [my_name, to, text]
20 else
21 Puts '** Use "to: message"'
22 END
23 end
The receiving thread is also very simple, and the 9th line to 14 lines run a simple loop, read data from the tuplespace, the matching condition is the message recipient is our own name, and then print the result on the console.
Tuplespaces requires a server to store tuples, with a DRB server in Ruby to store Tuplespace objects, and can be implemented with the following code:
Require 'DRB'
Require 'tuplespace'
DRB.Start_Service ("DRuby: //server.host: 12321", / /
Tuplespace.new)
DRB.THREAD.JOIN
First run the server program, run the client, and specify the name you want to use as a parameter in the client program.
If you run it, you will find that if you are not online, the server is saved to your message on the server. Our app is not an en suite Yahoo IM, Jabber or IRC, but this code is very useful.