ユーザ用ツール

サイト用ツール


alworker:ipc_プロセス間通信

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
alworker:ipc_プロセス間通信 [2019/03/21 19:32] – [解説] hirohitoalworker:ipc_プロセス間通信 [2021/06/24 17:06] (現在) – [IPCパススルー] hirohito
行 4: 行 4:
 ---- ----
 UNIX socket を利用した IPC プロセス間通信を実現します。 UNIX socket を利用した IPC プロセス間通信を実現します。
 +典型的な仕様例は、サーバプログラムが常駐し機器類の監視・制御を行います。そのサーバプログラムに対し、別プロセスとして起動したクライアントプログラムから指示を出したり、状態を取得したりします。
 +
 +{{:alworker:IPC_explain1.png?nolink|}}
 +
  
 ====== サンプル ====== ====== サンプル ======
行 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プログラムになります。
 +
 +{{:alworker:IPC_explain2.png?nolink|}}
 +
 +===== サンプル =====
 +
 +<file ruby main.rb>
 +
 +require "al_template"
 +require "al_worker_ipc"
 +
 +SERVER_NODE = "/tmp/ipc_server"
 +
 +class IpcSampleController < AlController
 +
 +  def action_index()
 +    @result_message = "ここにIPCプログラムからの戻り値が表示されます。"
 +    AlTemplate.run("index.rhtml")
 +  end
 +
 +  def action_idle()
 +    ipc = AlWorker::Ipc.open(SERVER_NODE)
 +    @result_message = ipc.call("idle").to_s
 +    AlTemplate.run("index.rhtml")
 +  end
 +
 +  def action_hello()
 +    ipc = AlWorker::Ipc.open(SERVER_NODE)
 +    @result_message = ipc.call("hello").to_s
 +    AlTemplate.run("index.rhtml")
 +  end
 +
 +  def action_upper()
 +    ipc = AlWorker::Ipc.open(SERVER_NODE)
 +    @result_message = ipc.call("upper", {'A'=>'dog','B'=>'Cat'}).to_s
 +    AlTemplate.run("index.rhtml")
 +  end
 +
 +end
 +</file>
 +
 +<file html index.rhtml>
 +<%= header_section %>
 +<title>Test</title>
 +
 +<%= body_section %>
 +IPC 送信コマンド
 +
 +<p>
 +  <button onclick="location.href='<%=h Alone.make_uri(:action=>"idle") %>'">idle</button>
 +  <button onclick="location.href='<%=h Alone.make_uri(:action=>"hello") %>'">hello</button>
 +  <button onclick="location.href='<%=h Alone.make_uri(:action=>"upper") %>'">upper</button>
 +</p>
 +
 +<p>
 +  <textarea rows="3" cols="50"><%=h @result_message %></textarea>
 +</p>
 +
 +<%= footer_section %>
 +</file>
 +
 +===== 解説 =====
 +
 +このサンプルではCGIモジュールのコントローラからIPCを発行しています。
 +IPC通信内容は、先に説明したサンプルと同様です。\\
 +プログラムの動作フローを以下に示します。
 +
 +  - テンプレートファイル index.rhtml で [idle] [hello] [upper] の3つのボタンが作られます。
 +  - ボタンをクリックすると、action_idel, action_hello, action_upper の各アクションが呼ばれます。
 +  - 各アクションでは、AlWorker::IPC.open() にてIPC通信路を確保し、ipc.call() でコマンドを発行します。
 +  - 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>
 +
 +===== クライアント =====
 +<code ruby>
 +require "al_worker_ipc"
 +
 +AlWorker::Ipc.open("/tmp/ipc_server") {|ipc|
 +  # ipcは、AlWorekr::IpcClient < UNIXSocket のインスタンス
 +  ipc.puts("passthrough1")
 +  5.times {
 +    ipc.puts("hello world.")
 +    p ipc.gets()
 +  }
 +}
 </code> </code>
  
alworker/ipc_プロセス間通信.1553164354.txt.gz · 最終更新: 2019/03/21 19:32 by hirohito