Class: DatTCP::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/dat-tcp.rb

Defined Under Namespace

Modules: TCPServer Classes: Signal

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, &serve_proc) ⇒ Server

Returns a new instance of Server.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dat-tcp.rb', line 15

def initialize(config = nil, &serve_proc)
  config ||= {}
  @backlog_size     = config[:backlog_size]     || 1024
  @debug            = config[:debug]            || false
  @min_workers      = config[:min_workers]      || 2
  @max_workers      = config[:max_workers]      || 4
  @shutdown_timeout = config[:shutdown_timeout] || 15
  @signal_reader, @signal_writer = IO.pipe
  @serve_proc = serve_proc || raise(ArgumentError, "no block given")

  @logger = DatTCP::Logger.new(@debug)

  @tcp_server       = nil
  @work_loop_thread = nil
  @worker_pool      = nil
  @signal = Signal.new(:stop)
end

Instance Method Details

#client_file_descriptorsObject



45
46
47
# File 'lib/dat-tcp.rb', line 45

def client_file_descriptors
  @worker_pool ? @worker_pool.work_items.map(&:fileno) : []
end

#file_descriptorObject



41
42
43
# File 'lib/dat-tcp.rb', line 41

def file_descriptor
  @tcp_server.fileno if self.listening?
end

#halt(wait = false) ⇒ Object



86
87
88
89
# File 'lib/dat-tcp.rb', line 86

def halt(wait = false)
  @signal_writer.write_nonblock('h')
  wait_for_shutdown if wait
end

#inspectObject



91
92
93
94
95
96
97
98
# File 'lib/dat-tcp.rb', line 91

def inspect
  reference = '0x0%x' % (self.object_id << 1)
  "#<#{self.class}:#{reference}".tap do |s|
    s << " @ip=#{ip.inspect} @port=#{port.inspect}"
    s << " @work_loop_status=#{@work_loop_thread.status.inspect}" if running?
    s << ">"
  end
end

#ipObject



33
34
35
# File 'lib/dat-tcp.rb', line 33

def ip
  @tcp_server.addr[2] if self.listening?
end

#listen(*args) {|@tcp_server| ... } ⇒ Object

Yields:

  • (@tcp_server)

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
# File 'lib/dat-tcp.rb', line 57

def listen(*args)
  @signal.set :listen
  @tcp_server = TCPServer.build(*args)
  raise ArgumentError, "takes ip and port or file descriptor" if !@tcp_server
  yield @tcp_server if block_given?
  @tcp_server.listen(@backlog_size)
end

#listening?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/dat-tcp.rb', line 49

def listening?
  !!@tcp_server
end

#pause(wait = false) ⇒ Object



76
77
78
79
# File 'lib/dat-tcp.rb', line 76

def pause(wait = false)
  @signal_writer.write_nonblock('p')
  wait_for_shutdown if wait
end

#portObject



37
38
39
# File 'lib/dat-tcp.rb', line 37

def port
  @tcp_server.addr[1] if self.listening?
end

#running?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/dat-tcp.rb', line 53

def running?
  !!(@work_loop_thread && @work_loop_thread.alive?)
end

#start(client_file_descriptors = nil) ⇒ Object

Raises:



70
71
72
73
74
# File 'lib/dat-tcp.rb', line 70

def start(client_file_descriptors = nil)
  raise NotListeningError.new unless listening?
  @signal.set :start
  @work_loop_thread = Thread.new{ work_loop(client_file_descriptors) }
end

#stop(wait = false) ⇒ Object



81
82
83
84
# File 'lib/dat-tcp.rb', line 81

def stop(wait = false)
  @signal_writer.write_nonblock('s')
  wait_for_shutdown if wait
end

#stop_listenObject



65
66
67
68
# File 'lib/dat-tcp.rb', line 65

def stop_listen
  @tcp_server.close rescue false
  @tcp_server = nil
end