« [番外]単純な送信時のメッセージパッシングの雰囲気 | メイン | 2章.Hello, dRuby (その6) »

2章.Hello, dRuby (その5)

本の続きに戻ります。「2.1.4 dRubyのURI」からです。小見出しの通り、DRbServerとURIとの関係についてのお話です。

TCPを使って接続する場合のdRubyのURIの書式は次の通りです。

druby://ホスト名:ポート番号

URIはホスト名とポート番号から構成されます。 URIによってdRubyのサーバを特定することができます。

URIを使用するのは、サービスの起動時とDRbObjectの生成の場面です。 DRb.start_serviceはURIとそれに関連付けるオブジェクトを指定して dRubyのサービス(DRbServer)を開始します。 DRbServerにはサーバソケットの管理やソケットを待ち受けるスレッドなどサーバプログラミングに必要なもの一式が入っています。具体的には、URIのホスト名とポート番号を用いて生成したTCPServerや、 acceptで接続を待ち受けるスレッドなどのオブジェクトです。 URIはDRbServerを特定する情報で、DRbServerは一つの固有なURIを持ちます。

DRb.start_service(uri, front)

2.1.4 dRubyのURI

ふむふむです。今までのエントリで、さらっと中身を覗いたこともあり、大体感じはわかった。ちなみに、本文中のacceptとは、クライアントからの接続があるまで待ち続けるシステムコール(もしくはシステムコールを呼び出すことのできる何か)のことだと思います。ソケット通信とかのサーバーのプログラムを自分で書くときは、大抵の場合、acceptで待ち続け、クライアントからの要求に応じてスレッドを生成したり、forkして子プロセスを作ったりすることが多いと思います(forkとは分身の術のようなものです。)。こうすることでクライアントからの複数の要求を並列に処理することが出来ます。dRubyのDRbServerのmain_loopでも、スレッドを生成して並列に処理しているのだと思います(と想像)。で次と。

URIと関連付けられたオブジェクトは、そのサービスの入り口(受け付け担当)となるため、フロントオブジェクトと呼びます。 DRbObject.new_with_uri()で作成したオブジェクトへのメソッド呼び出しは、全てこのフロントオブジェクトに届くことになります。実際のアプリケーションでは、アプリケーション内部のオブジェクトをそのまま URIに関連付けずに、アクセス制御をしたり、複数の処理をまとめて行うユーティリティ的なメソッドを用意したりした、特別なクラスをフロントに置くことが多いです。

2.1.4 dRubyのURI

DRbServerで公開するリモートオブジェクトは、フロントオブジェクトと言うのですね。で、門番とか総合受付みたいなことをさせることが多いと。図がわかりやすいです。オブジェクトを公開しているDRbServerはURIによって特定されると言うことなので、URIの理解が大事なようです。本ではしばらくURIの話が続きます。URIは下記のように一部省略できるようです。

'druby://hostname:12345' - 全て指定した完全な形
'druby://:12345' - ホスト名を省略した形式
'druby://hostname:0' - ホスト名を指定するがポート番号を省略した形式
'druby://:0' - ホスト名もポート番号も省略した形式
nil - ホスト名もポート番号も省略

2.1.4 dRubyのURI

続いて、本に従いirbで実行っと。。ちゃんとできました!!別のコンピュータとも問題なくやり取り出来ますね、dRubyのオブジェクトのやり取りは、標準でTCPSocketを使っているため、同じコンピュータのプロセス間のみではなく、別コンピュータのプロセスともやり取りできます。可能性無限大。

。。。でも、あれれ?先日のエントリで確認した限りでは、「druby://」のプロトコルでは、ポート番号の後に引数を追加して何か出来るような気がしました。

druby://ホスト名:ポート番号?オプション

URIのパース部分の正規表現はこう


/^druby:\/\/(.*?):(\d+)(\?(.*))?$/

オプションは確かDRbURIOptionになっていたやつでした。これは何に使うんだろうなあ、という疑問がわきます。メッセージ送信部分のソース(DRbMessageのsend_request)を一応みてみると

def send_request(stream, ref, msg_id, arg, b) # :nodoc:
  ary = []
  ary.push(dump(ref.__drbref))
  ary.push(dump(msg_id.id2name))
  ary.push(dump(arg.length))
  arg.each do |e|
    ary.push(dump(e))
  end
  ary.push(dump(b))
  stream.write(ary.join(''))
rescue
  raise(DRbConnError, $!.message, $!.backtrace)
end

とあります。ここのref.__drbrefは、オプション引数DRbURIOptionそのものなので、dumpされた情報には確かに含まれているようです。おそらくサーバー側でのマーシャルデータ復元時に、DRbObjectのrecv_requestとかそのあたりでよきに計らってくれるようですが、ちゃんと追いかけることができていません。このあたりの世界は、もうちょっと本を読み進めてから再挑戦することにします。

でも、内部の仕組みはともかく、オプションの利用方法くらいは知りたいなあ。と思って調べたら、咳さんのはてなに載ってました。

URIから参照を作ろうとすると、サービスの入り口のオブジェクトしか参照できないことになるんだけど、数年前に追加されたオプションを使うともうちょっと先のオブジェクトを参照できる。

druby://localhost:12345?option

「?」の後の文字列はURIのオプション引数(?)となり、frontオブジェクトにこの文字列を問い合わせた結果のオブジェクトを参照する。問い合わせはメソッドに?以後の文字列を与えて行う。

I like Ruby too. - dRubyのURIのオプション

ふむふむ、そういうことか。(てか、最近のエントリでこのサイトも紹介してくれてる!)

雰囲気を味わうために、puts00.rbをちょっと改造してみます。

require 'drb/drb'

class Puts
 def initialize(stream=$stdout)
  @stream = stream
 end
 def puts(str)
  @stream.puts("#{str} [Puts]")
 end
end

class Puts2
 def initialize(stream=$stdout)
  @stream = stream
 end
 def puts(str)
  @stream.puts("#{str} [Puts2]")
 end
end

class Front
 def initialize
  @hash = {"puts" => Puts.new, "puts2" => Puts2.new }
 end
 def [](arg)
  @hash[arg]
 end
end

uri = ARGV.shift
DRb.start_service(uri, Front.new)
puts DRb.uri
sleep

Puts2という、Putsのようなものをもうひとつ定義し、それぞれのインスタンスをハッシュに入れて切り替えられるようにしただけです。
ちなみに{}で定義できるものをハッシュといい、キーと値のペアで情報を出し入れできる便利ないれものです。なお#{hoge}は式展開といい、文字列中にオブジェクトの内容を入れることができる書き方です。Ruby好きな人は式展開が好きらしいです。

はい、で、ハッシュのキーをオプションの文字列にしました。で、クライアント側のコードは

require 'drb/drb'

there = DRbObject.new_with_uri('druby://localhost:12345?puts')
there.puts('Hello, World.')

there2 = DRbObject.new_with_uri('druby://localhost:12345?puts2')
there2.puts('Hello, World.')

とかで。するとこんな風に出ます。一応出来たのは出来た。

Hello, World. [Puts]
Hello, World. [Puts2]


今回はこれにて終了。次はリマインダーのサンプルを見ていきます。

トラックバック

このエントリーのトラックバックURL:
http://www.grandnature.net/bin/mt-tb.cgi/59

コメント (90)

kategorycznie udoskonalona bedzie wariant 2.0 systemu HTML Help, dokad znajda sie dodatkowe mozliwosci budowania elementow nawigacyjnych – system ow jest do tego niezmiernie pozny w stosunku az do zapowiedzi.

Good – I should certainly pronounce, impressed with your website. I had no trouble navigating through all tabs and related information ended up being truly simple to do to access. I recently found what I hoped for before you know it at all. Reasonably unusual. Is likely to appreciate it for those who add forums or something, site theme . a tones way for your customer to communicate. Nice task..

hey there and thank you for your info – I’ve definitely picked up anything new from right here. I did however expertise some technical issues using this website, since I experienced to reload the site lots of times previous to I could get it to load correctly. I had been wondering if your hosting is OK? Not that I am complaining, but sluggish loading instances times will sometimes affect your placement in google and could damage your high-quality score if ads and marketing with Adwords. Well I am adding this RSS to my email and could look out for much more of your respective exciting content. Make sure you update this again soon..

I loved as much as you’ll receive carried out right here. The sketch is attractive, your authored subject matter stylish. nonetheless, you command get bought an nervousness over that you wish be delivering the following. unwell unquestionably come more formerly again as exactly the same nearly a lot often inside case you shield this hike.

Aw, this was a very nice post. In thought I want to put in writing like this additionally – taking time and actual effort to make an excellent article… but what can I say… I procrastinate alot and not at all appear to get one thing done.

This web site is really a walk-by for the entire data you wished about this and didn’t know who to ask. Glimpse here, and you’ll undoubtedly uncover it.

Hi my friend! I wish to say that this article is awesome, nice written and include almost all important infos. I’d like to see more posts like this .

I am really inspired along with your writing abilities well with the format on your weblog. Is that this a paid subject matter or did you modify it yourself? Either way keep up the excellent high quality writing, it’s uncommon to peer a nice weblog like this one these days..

Took me time to read all the responses, but I truly loved the article. It turned out to be very useful to me and I am sure to all the commenters here! It’s continually awesome when you can not only be informed, but also entertained! I’m sure you had fun writing this article. Regards, Clotilde.

I’d should test with you here. Which isn't something I often do! I get pleasure from studying a put up that will make individuals think. Additionally, thanks for permitting me to remark!

Thanks for sharing excellent informations. Your web site is so cool. I'm impressed by the details that you’ve on this website. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for more articles. You, my pal, ROCK! I found just the info I already searched all over the place and simply could not come across. What a great site.

I precisely wanted to appreciate you once more. I’m not certain the things I would’ve sorted out in the absence of the type of tips and hints provided by you over such a subject matter. It actually was the troublesome dilemma in my view, nevertheless looking at a new specialized mode you managed the issue made me to leap for gladness. I will be happier for this information and sincerely hope you really know what an amazing job that you’re carrying out educating the rest through the use of your website. More than likely you’ve never got to know all of us.

Aw, this was a really nice post. In idea I wish to put in writing like this moreover – taking time and actual effort to make a very good article… but what can I say… I procrastinate alot and in no way seem to get something done.

Pretty good post. I just stumbled upon the blog and also wanted to be able to mention in which I have definitely enjoyed reading the blog page and also blogposts. Anyway I’ll become subscribing the nourish and also My partner and i expect to be able to read the blog page all over again.

It’s actually a great and useful piece of information. I’m glad that you shared this helpful info with us. Please keep us up to date like this. Thanks for sharing.

Thanks for sharing 2章.Hello, dRuby (その5) (dRubyの正面) with us keep update bro love your article about 2章.Hello, dRuby (その5) (dRubyの正面) .

Hi my friend! I want to say that this post is awesome, nice written and include almost all important infos. I’d like to see more posts like this .

Oh my goodness! an amazing article dude. Thank you However I am experiencing concern with ur rss . Don’t know why Unable to subscribe to it. Is there anybody getting identical rss drawback? Anyone who knows kindly respond. Thnkx

Hello There. I found your blog using msn. This is an extremely well written article. I’ll make sure to bookmark it and return to read more of your useful information. Thanks for the post. I will definitely comeback.

Good write-up, I’m normal visitor of one’s website, maintain up the excellent operate, and It is going to be a regular visitor for a long time.

Your place is valueble for me. Thanks!…

I like your weblog 2章.Hello, dRuby (その5) (dRubyの正面). Thank you for that fantastic blog post! I will bookmark your blog and check again here regularly.

hello there and thank you for your info – I have definitely picked up anything new from right here. I did however expertise a few technical points using this web site, as I experienced to reload the site a lot of times previous to I could get it to load properly. I had been wondering if your web hosting is OK? Not that I am complaining, but sluggish loading instances times will very frequently affect your placement in google and could damage your high-quality score if advertising and marketing with Adwords. Anyway I am adding this RSS to my email and could look out for much more of your respective fascinating content. Ensure that you update this again soon..

On the debt ceiling, Mohamed El-Erian, the CEO of Pimco, the world’s biggest bond trader, was a guest on Fareed Zacharia’s program today. He is an expert on global finance. One point he made is the US is in no way like Greece, saying that we still have time to stimulate the growth of our economy.

certainly like your web-site but you have to check the spelling on quite a few of your posts. A number of them are rife with spelling problems and I find it very bothersome to tell the truth nevertheless I’ll definitely come back again.

Spot on with this write-up, I actually think this web site wants way more consideration. I’ll most likely be once more to read far more, thanks for that info.

I discovered your blog web site on google and check a couple of of your early posts. Continue to maintain up the excellent operate. I simply further up your RSS feed to my MSN News Reader. Looking for forward to studying more from you afterward!…

Hi there, just became aware of your blog through Google, and found that it is really informative. I’m gonna watch out for brussels. I will be grateful if you continue this in future. Lots of people will be benefited from your writing. Cheers!

F*ckin’ awesome things here. I’m very glad to see your article. Thanks a lot and i am looking forward to contact you. Will you please drop me a e-mail?

Good post. I learn something more difficult on totally different blogs everyday. It is going to at all times be stimulating to read content material from other writers and practice a little bit something from their store. I’d desire to use some with the content on my blog whether you don’t mind. Natually I’ll offer you a hyperlink in your internet blog. Thanks for sharing.

Hey very cool site!! Man .. Excellent .. Amazing .. I will bookmark your site and take the feeds also…I'm happy to find numerous useful info here in the post, we need work out more strategies in this regard, thanks for sharing. . . . . .

Hey there, You've done an incredible job. I’ll definitely digg it and personally recommend to my friends. I am confident they'll be benefited from this website.

I found your blog site on google and test a few of your early posts. Proceed to keep up the excellent operate. I simply further up your RSS feed to my MSN Information Reader. Seeking ahead to reading extra from you later on!…

There are actually lots of particulars like that to take into consideration. That could be a great level to bring up. I offer the ideas above as normal inspiration but clearly there are questions like the one you carry up the place crucial factor shall be working in trustworthy good faith. I don?t know if greatest practices have emerged round issues like that, but I'm certain that your job is clearly identified as a good game. Each girls and boys feel the impression of only a moment’s pleasure, for the remainder of their lives.

I like what you guys are up also. Such intelligent work and reporting! Carry on the superb works guys I’ve incorporated you guys to my blogroll. I think it'll improve the value of my website :)

obviously like your web-site but you have to check the spelling on several of your posts. Several of them are rife with spelling problems and I find it very troublesome to tell the truth nevertheless I’ll definitely come back again.

I’m impressed, I need to say. Really hardly ever do I encounter a weblog that’s each educative and entertaining, and let me inform you, you might have hit the nail on the head. Your concept is excellent; the difficulty is one thing that not enough individuals are speaking intelligently about. I'm very completely happy that I stumbled across this in my seek for something referring to this.

I found your blog web site on google and test a few of your early posts. Continue to keep up the very good operate. I just additional up your RSS feed to my MSN Information Reader. Seeking ahead to reading extra from you in a while!…

I like Your Article about 2章.Hello, dRuby (その5) (dRubyの正面) Perfect just what I was searching for! .

I like Your Article about 2章.Hello, dRuby (その5) (dRubyの正面) Perfect just what I was searching for! .

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post…

It’s arduous to find knowledgeable folks on this topic, but you sound like you understand what you’re speaking about! Thanks

I was very pleased to find this web-site.I wanted to thanks for your time for this wonderful read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post.

There is noticeably a bundle to know about this. I assume you made certain nice points in features also.

Hi, Neat post. There is a problem with your web site in internet explorer, would test this… IE still is the market leader and a good portion of people will miss your excellent writing due to this problem.

hey there and thank you for your info – I have definitely picked up anything new from right here. I did however expertise some technical issues using this website, as I experienced to reload the web site lots of times previous to I could get it to load properly. I had been wondering if your web host is OK? Not that I'm complaining, but sluggish loading instances times will very frequently affect your placement in google and could damage your quality score if ads and marketing with Adwords. Anyway I am adding this RSS to my email and could look out for much more of your respective intriguing content. Make sure you update this again very soon..

I’ve been exploring for a little for any high quality articles or blog posts on this sort of area . Exploring in Yahoo I at last stumbled upon this website. Reading this info So i’m happy to convey that I have an incredibly good uncanny feeling I discovered exactly what I needed. I most certainly will make sure to do not forget this site and give it a glance on a constant basis.

Good post. I be taught one thing more challenging on different blogs everyday. It's going to always be stimulating to read content material from different writers and practice a bit of something from their store. I’d desire to use some with the content on my weblog whether or not you don’t mind. Natually I’ll provide you with a link on your net blog. Thanks for sharing.

Good write-up, I am normal visitor of one’s web site, maintain up the nice operate, and It's going to be a regular visitor for a lengthy time.

I like the helpful information you provide in your articles. I will bookmark your blog and check again here frequently. I am quite sure I’ll learn lots of new stuff right here! Good luck for the next!

Thanks for sharing 2章.Hello, dRuby (その5) (dRubyの正面) with us keep update bro love your article about 2章.Hello, dRuby (その5) (dRubyの正面) .

I like Your Article about 2章.Hello, dRuby (その5) (dRubyの正面) Perfect just what I was looking for! .

Fantastic goods from you, man. 2章.Hello, dRuby (その5) (dRubyの正面) I've understand your stuff previous to and you are just too fantastic. I actually like what you've acquired here, really like what you are stating and the way in which you say it. You make it entertaining and you still take care of to keep it wise. I can't wait to read far more from you. This is actually a wonderful 2章.Hello, dRuby (その5) (dRubyの正面) informations.

Aw, this was a really nice post. In thought I would like to put in writing like this moreover – taking time and actual effort to make a very good article… however what can I say… I procrastinate alot and on no account seem to get one thing done.

There are definitely plenty of details like that to take into consideration. That is a nice level to deliver up. I offer the ideas above as normal inspiration but clearly there are questions like the one you deliver up where a very powerful thing will probably be working in sincere good faith. I don?t know if best practices have emerged around issues like that, but I'm positive that your job is clearly identified as a good game. Each boys and girls really feel the affect of just a second’s pleasure, for the remainder of their lives.

I actually have noticed plenty of useful points on your internet site 2章.Hello, dRuby (その5) (dRubyの正面) regarding personal computers. Even so, We have the opinion that notebook computers remain nearly strong sufficiently to become a sensible choice if you normally do jobs that require a lot of energy, including video enhancing. But for internet exploring, statement producing, and the majority of additional well-known pc functions they are just fine, supposing you can't mind the small display size. Thank you for giving your ideas.

We love your current website 2章.Hello, dRuby (その5) (dRubyの正面). Thanks a lot just for this particular nice post! I most certainly will book mark your current webpage and also check again here frequently.

That is the right weblog for anyone who desires to find out about this topic. You notice so much its almost arduous to argue with you (not that I actually would want…HaHa). You undoubtedly put a new spin on a subject thats been written about for years. Great stuff, just nice!

I've been browsing online more than 3 hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. Personally, if all site owners and bloggers made good content as you did, the internet will be much more useful than ever before.

攻めようにも勝手に進んでればやりようがないの。一応第三者機関の検査が基礎工事で1回入ってるはずなので それだけが頼りのような???

Hi, i read your blog from time to time and i own a similar one and i was just wondering if you get a lot of spam remarks? If so how do you reduce it, any plugin or anything you can recommend? I get so much lately it's driving me mad so any assistance is very much appreciated.

I simply want to mention I am newbie to blogs and really loved your blog site. Probably I’m going to bookmark your site . You absolutely come with beneficial stories. Thanks a lot for sharing with us your web-site.

I just want to say I'm beginner to blogs and absolutely savored your website. Probably I’m likely to bookmark your site . You actually have exceptional stories. Thanks a bunch for sharing with us your blog site.

This is the right blog for anyone who desires to find out about this topic. You notice so much its nearly onerous to argue with you (not that I truly would need…HaHa). You definitely put a new spin on a topic thats been written about for years. Great stuff, just nice!

This document 2ç« .Hello, dRuby (その5) (dRubyの正面) appears to be very good. I think this really is just about the most important information for me. I was checking out this blog site and I am impressed. I just can't wait to read a lot more from you mainly because this is actually a great website. I’ll bookmark your current blog and also check again here regularly.

I like Your Article about 2章.Hello, dRuby (その5) (dRubyの正面) Perfect just what I was looking for! .

An unputdownable word is couturier mention. I cogitate that you should create author on this matter, it might not be a prejudice topic but generally group are not sufficiency to verbalise on specified topics. To the succeeding. Cheers like your 2章.Hello, dRuby (その5) (dRubyの正面).

After I originally commented I clicked the -Notify me when new comments are added- checkbox and now every time a comment is added I get four emails with the identical comment. Is there any way you'll be able to remove me from that service? Thanks!

I like Your Article about 2章.Hello, dRuby (その5) (dRubyの正面) Perfect just what I was looking for! .

I was wondering if you ever thought of changing the page layout of your site? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having 1 or two pictures. Maybe you could space it out better?

Excellent website. Plenty of useful info here. I’m sending it to several friends ans also sharing in delicious. And naturally, thanks for your sweat!

It's always nice when you read a posting that is not just educational but also entertaining. I need to store 2章.Hello, dRuby (その5) (dRubyの正面) in my personal computer. I have been searching for more knowledge about this topic for days and this is the best May possibly proudly located. I really liked your posting.

It’s really a great and useful piece of info. I am glad that you shared this helpful information with us. Please keep us up to date like this. Thanks for sharing.

I found your weblog site on google and verify just a few of your early posts. Continue to keep up the excellent operate. I just extra up your RSS feed to my MSN News Reader. Seeking ahead to reading more from you later on!…

Spot on with this write-up, I actually assume this website wants much more consideration. I’ll in all probability be again to learn far more, thanks for that info.

terrific site. I find something new on a blogs a lot. I’d like to copy some of the information on my page hope you don’t care. of course I’ll give a link to your web site. cheers!

hey there and thank you for your info – I have certainly picked up something new from right here. I did however expertise a few technical points using this site, as I experienced to reload the site lots of times previous to I could get it to load correctly. I had been wondering if your hosting is OK? Not that I am complaining, but slow loading instances times will very frequently affect your placement in google and could damage your quality score if advertising and marketing with Adwords. Anyway I am adding this RSS to my email and could look out for much more of your respective intriguing content. Ensure that you update this again very soon..

cool post. I come upon something new on a blogs often. I’d like to use some of the information on my website i hope you don’t mind. of course I’ll give a link to your site. congrats!

man, I’ve been tracking down for this page all over the the web. thanks

OMG, I’ve been tracking down for this blog everywhere the the net. cheers

I just stumble upon your post on online and went through some of your early posts.brilliant content,keep up the good work. I just added up your RSS feed to my MSN News Reader. Looking forward to reading more from your website, Thanks for sharing!

naturally like your website but you have to check the spelling on quite a few of your posts. Several of them are rife with spelling issues and I find it very troublesome to tell the truth nevertheless I’ll definitely come back again.

excellent site. I come upon something new on a blogs a lot. I’d like to post some of the information on my post hope you don’t mind. of course I’ll give a link to your web-site. cheers!

amazing, I’ve been investigating for this information everywhere the the web. right on

I have been surfing online more than 3 hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. In my view, if all website owners and bloggers made good content as you did, the internet will be much more useful than ever before.

I can‘t believe it, I’ve been looking for this information all over the the interwebs. right on

Youre so cool! I dont suppose Ive read something similar to this before. So nice to seek out somebody by incorporating authentic ideas on this subject. realy i appreciate you for starting this up. this amazing site is a thing that's needed on the internet, somebody after some bit originality. helpful project for bringing something new towards the web!

I can‘t believe it, I’ve been seeking for this blog all over the the interwebs. cheers

amazing, I’ve been cashing after for this advice in every direction the the net. thanks

cool site. I unearth something new on a blogs a lot. I’d like to copy some of the work on my website i hope you don’t mind. of course I’ll give a link to your site. cheers!

コメントを投稿

About

2007年09月14日 10:14に投稿されたエントリーのページです。

ひとつ前の投稿は「[番外]単純な送信時のメッセージパッシングの雰囲気」です。

次の投稿は「2章.Hello, dRuby (その6)」です。

他にも多くのエントリーがあります。メインページアーカイブページも見てください。