alworker:ipc_プロセス間通信
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| alworker:ipc_プロセス間通信 [2019/03/21 19:32] – [解説] hirohito | alworker:ipc_プロセス間通信 [2021/06/24 17:06] (現在) – [IPCパススルー] hirohito | ||
|---|---|---|---|
| 行 4: | 行 4: | ||
| ---- | ---- | ||
| UNIX socket を利用した IPC プロセス間通信を実現します。 | UNIX socket を利用した IPC プロセス間通信を実現します。 | ||
| + | 典型的な仕様例は、サーバプログラムが常駐し機器類の監視・制御を行います。そのサーバプログラムに対し、別プロセスとして起動したクライアントプログラムから指示を出したり、状態を取得したりします。 | ||
| + | |||
| + | {{: | ||
| + | |||
| ====== サンプル ====== | ====== サンプル ====== | ||
| 行 16: | 行 20: | ||
| def initialize2() | def initialize2() | ||
| @ipc = Ipc.new() | @ipc = Ipc.new() | ||
| + | @ipc.chmod = 0666 | ||
| @ipc.run( self ) | @ipc.run( self ) | ||
| end | end | ||
| 行 70: | 行 75: | ||
| サーバー/クライアント間のパラメータ送受は、Hashを使います。\\ | サーバー/クライアント間のパラメータ送受は、Hashを使います。\\ | ||
| 各ハンドラでは、trueを返すとクライアントとのセッションを継続し、falseだとサーバー側から切断します。上記例ではreply()メソッドが必ずtrueを返すので、セッションが継続します。\\ | 各ハンドラでは、trueを返すとクライアントとのセッションを継続し、falseだとサーバー側から切断します。上記例ではreply()メソッドが必ずtrueを返すので、セッションが継続します。\\ | ||
| + | |||
| + | ====== CGIコントローラから使う ====== | ||
| + | |||
| + | ウェブブラウザを使って状態監視等を行う場合には、クライアントプログラムがCGIプログラムになります。 | ||
| + | |||
| + | {{: | ||
| + | |||
| + | ===== サンプル ===== | ||
| + | |||
| + | <file ruby main.rb> | ||
| + | |||
| + | require " | ||
| + | require " | ||
| + | |||
| + | SERVER_NODE = "/ | ||
| + | |||
| + | class IpcSampleController < AlController | ||
| + | |||
| + | def action_index() | ||
| + | @result_message = " | ||
| + | AlTemplate.run(" | ||
| + | end | ||
| + | |||
| + | def action_idle() | ||
| + | ipc = AlWorker:: | ||
| + | @result_message = ipc.call(" | ||
| + | AlTemplate.run(" | ||
| + | end | ||
| + | |||
| + | def action_hello() | ||
| + | ipc = AlWorker:: | ||
| + | @result_message = ipc.call(" | ||
| + | AlTemplate.run(" | ||
| + | end | ||
| + | |||
| + | def action_upper() | ||
| + | ipc = AlWorker:: | ||
| + | @result_message = ipc.call(" | ||
| + | AlTemplate.run(" | ||
| + | end | ||
| + | |||
| + | end | ||
| + | </ | ||
| + | |||
| + | <file html index.rhtml> | ||
| + | <%= header_section %> | ||
| + | < | ||
| + | |||
| + | <%= body_section %> | ||
| + | IPC 送信コマンド | ||
| + | |||
| + | <p> | ||
| + | <button onclick=" | ||
| + | <button onclick=" | ||
| + | <button onclick=" | ||
| + | </p> | ||
| + | |||
| + | <p> | ||
| + | < | ||
| + | </p> | ||
| + | |||
| + | <%= footer_section %> | ||
| + | </ | ||
| + | |||
| + | ===== 解説 ===== | ||
| + | |||
| + | このサンプルではCGIモジュールのコントローラからIPCを発行しています。 | ||
| + | IPC通信内容は、先に説明したサンプルと同様です。\\ | ||
| + | プログラムの動作フローを以下に示します。 | ||
| + | |||
| + | - テンプレートファイル index.rhtml で [idle] [hello] [upper] の3つのボタンが作られます。 | ||
| + | - ボタンをクリックすると、action_idel, | ||
| + | - 各アクションでは、AlWorker:: | ||
| + | - IPCの結果は、@result_message → index.rhtml 経由で画面に表示されます。 | ||
| ====== IPCソケット ====== | ====== IPCソケット ====== | ||
| 行 174: | 行 253: | ||
| コマンドごとに、以下のように実装します。 | コマンドごとに、以下のように実装します。 | ||
| + | ===== サーバサイド ===== | ||
| <code ruby> | <code ruby> | ||
| def ipc_passthrough1( sock, param ) | def ipc_passthrough1( sock, param ) | ||
| 行 185: | 行 265: | ||
| return false | return false | ||
| end | end | ||
| + | </ | ||
| + | |||
| + | ===== クライアント ===== | ||
| + | <code ruby> | ||
| + | require " | ||
| + | |||
| + | AlWorker:: | ||
| + | # ipcは、AlWorekr:: | ||
| + | ipc.puts(" | ||
| + | 5.times { | ||
| + | ipc.puts(" | ||
| + | p ipc.gets() | ||
| + | } | ||
| + | } | ||
| </ | </ | ||
alworker/ipc_プロセス間通信.1553164354.txt.gz · 最終更新: 2019/03/21 19:32 by hirohito