Class: AlWorker::Tcp
- Inherits:
-
Object
- Object
- AlWorker::Tcp
- Defined in:
- lib/al_worker_tcp.rb
Overview
TCPサーバ
Constant Summary collapse
- @@servers =
Returns リスンサーバ.
[]
Instance Attribute Summary collapse
-
#address ⇒ String
リスンアドレス.
-
#banner ⇒ String
接続後のバナー表示.
-
#cb_prefix ⇒ String
コールバックメソッドのプレフィックス.
-
#mode_param ⇒ Symbol
パラメータのデータ形式 :json :text.
-
#mode_service ⇒ Symbol
動作モード :thread :process.
-
#port ⇒ Integer
リスンポート.
-
#server ⇒ TCPServer
readonly
リスンサーバ.
Instance Method Summary collapse
-
#close ⇒ Object
TCPサービス終了.
-
#initialize(address = "", port = 1944) ⇒ Tcp
constructor
constructor.
-
#run(obj) ⇒ Object
実行開始.
Constructor Details
#initialize(address = "", port = 1944) ⇒ Tcp
constructor
49 50 51 52 53 54 55 |
# File 'lib/al_worker_tcp.rb', line 49 def initialize( address = "", port = 1944 ) @address = address @port = port @mode_service = :thread @mode_param = :json @cb_prefix = "tcp" end |
Instance Attribute Details
#address ⇒ String
Returns リスンアドレス.
22 23 24 |
# File 'lib/al_worker_tcp.rb', line 22 def address @address end |
#banner ⇒ String
Returns 接続後のバナー表示.
40 41 42 |
# File 'lib/al_worker_tcp.rb', line 40 def @banner end |
#cb_prefix ⇒ String
Returns コールバックメソッドのプレフィックス.
37 38 39 |
# File 'lib/al_worker_tcp.rb', line 37 def cb_prefix @cb_prefix end |
#mode_param ⇒ Symbol
Returns パラメータのデータ形式 :json :text.
34 35 36 |
# File 'lib/al_worker_tcp.rb', line 34 def mode_param @mode_param end |
#mode_service ⇒ Symbol
Returns 動作モード :thread :process.
31 32 33 |
# File 'lib/al_worker_tcp.rb', line 31 def mode_service @mode_service end |
#port ⇒ Integer
Returns リスンポート.
25 26 27 |
# File 'lib/al_worker_tcp.rb', line 25 def port @port end |
#server ⇒ TCPServer (readonly)
Returns リスンサーバ.
28 29 30 |
# File 'lib/al_worker_tcp.rb', line 28 def server @server end |
Instance Method Details
#close ⇒ Object
TCPサービス終了
110 111 112 |
# File 'lib/al_worker_tcp.rb', line 110 def close() @server.close() if !@server.closed?() end |
#run(obj) ⇒ Object
Note:
実行開始
プロセスモードで動作時は、syncモードと同等の動作になる。
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/al_worker_tcp.rb', line 65 def run( obj ) @me = obj @server = TCPServer.new( @address, @port ) @@servers << @server case @mode_service when :process Thread.start { loop do sock = @server.accept pid = AlWorker.mutex_sync.synchronize { Process.fork() } # child if !pid @@servers.each {|server| server.close } _start_service( sock ) exit! end # parent sock.close Process.detach( pid ) end } when :thread Thread.start { loop do Thread.start( @server.accept ) { |sock| _start_service( sock ) } end } else raise "Illegal mode_service" end end |