Class: AlWorker::Tcp

Inherits:
Object
  • Object
show all
Defined in:
lib/al_worker_tcp.rb

Overview

TCPサーバ

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Tcp) initialize(address = "", port = 1944)

constructor

Parameters:

  • address (String) (defaults to: "")

    リスンアドレス

  • port (Integer) (defaults to: 1944)

    リスンポート



39
40
41
42
43
44
# File 'lib/al_worker_tcp.rb', line 39

def initialize( address = "", port = 1944 )
  @address = address
  @port = port
  @mode_service = :thread
  @cb_prefix = "tcp"
end

Instance Attribute Details

- (String) address

Returns リスンアドレス

Returns:

  • (String)

    リスンアドレス



18
19
20
# File 'lib/al_worker_tcp.rb', line 18

def address
  @address
end

Returns 接続後のバナー表示

Returns:

  • (String)

    接続後のバナー表示



30
31
32
# File 'lib/al_worker_tcp.rb', line 30

def banner
  @banner
end

- (String) cb_prefix

Returns コールバックメソッドのプレフィックス

Returns:

  • (String)

    コールバックメソッドのプレフィックス



27
28
29
# File 'lib/al_worker_tcp.rb', line 27

def cb_prefix
  @cb_prefix
end

- (Symbol) mode_service

Returns 動作モード :thread :process

Returns:

  • (Symbol)

    動作モード :thread :process



24
25
26
# File 'lib/al_worker_tcp.rb', line 24

def mode_service
  @mode_service
end

- (Integer) port

Returns リスンポート

Returns:

  • (Integer)

    リスンポート



21
22
23
# File 'lib/al_worker_tcp.rb', line 21

def port
  @port
end

Instance Method Details

- (Object) run(obj)

Note:

実行開始

プロセスモードで動作時は、syncモードと同等の動作になる。

Parameters:

  • obj (AlWorker)

    ワーカオブジェクト



54
55
56
57
58
59
60
61
62
63
64
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
# File 'lib/al_worker_tcp.rb', line 54

def run( obj )
  @me = obj
  server = TCPServer.new( @address, @port )

  case @mode_service
  when :process
    Thread.start {
      loop do
        sock = server.accept
        pid = AlWorker.mutex_sync.synchronize { Process.fork() }

        if ! pid
          # child
          _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