dat-tcp 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/dat-tcp.gemspec CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |gem|
19
19
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
20
  gem.require_paths = ["lib"]
21
21
 
22
- gem.add_dependency("dat-worker-pool", ["~> 0.4"])
22
+ gem.add_dependency("dat-worker-pool", ["~> 0.5"])
23
23
 
24
- gem.add_development_dependency('assert', ['~> 2.12'])
24
+ gem.add_development_dependency('assert', ['~> 2.15'])
25
25
  end
data/lib/dat-tcp.rb CHANGED
@@ -9,6 +9,8 @@ module DatTCP
9
9
 
10
10
  class Server
11
11
 
12
+ attr_reader :worker_start_procs, :worker_shutdown_procs
13
+ attr_reader :worker_sleep_procs, :worker_wakeup_procs
12
14
  attr_reader :logger
13
15
  private :logger
14
16
 
@@ -22,6 +24,11 @@ module DatTCP
22
24
  @signal_reader, @signal_writer = IO.pipe
23
25
  @serve_proc = serve_proc || raise(ArgumentError, "no block given")
24
26
 
27
+ @worker_start_procs = []
28
+ @worker_shutdown_procs = []
29
+ @worker_sleep_procs = []
30
+ @worker_wakeup_procs = []
31
+
25
32
  @logger = DatTCP::Logger.new(@debug)
26
33
 
27
34
  @tcp_server = nil
@@ -31,7 +38,7 @@ module DatTCP
31
38
  end
32
39
 
33
40
  def ip
34
- @tcp_server.addr[2] if self.listening?
41
+ @tcp_server.addr[3] if self.listening?
35
42
  end
36
43
 
37
44
  def port
@@ -88,6 +95,11 @@ module DatTCP
88
95
  wait_for_shutdown if wait
89
96
  end
90
97
 
98
+ def on_worker_start(&block); @worker_start_procs << block; end
99
+ def on_worker_shutdown(&block); @worker_shutdown_procs << block; end
100
+ def on_worker_sleep(&block); @worker_sleep_procs << block; end
101
+ def on_worker_wakeup(&block); @worker_wakeup_procs << block; end
102
+
91
103
  def inspect
92
104
  reference = '0x0%x' % (self.object_id << 1)
93
105
  "#<#{self.class}:#{reference}".tap do |s|
@@ -107,9 +119,7 @@ module DatTCP
107
119
 
108
120
  def work_loop(client_file_descriptors = nil)
109
121
  logger.info "Starting work loop..."
110
- @worker_pool = DatWorkerPool.new(@min_workers, @max_workers) do |socket|
111
- serve(socket)
112
- end
122
+ @worker_pool = build_worker_pool
113
123
  add_client_sockets_from_fds client_file_descriptors
114
124
  @worker_pool.start
115
125
  process_inputs while @signal.start?
@@ -128,6 +138,29 @@ module DatTCP
128
138
  logger.info "Stopped work loop"
129
139
  end
130
140
 
141
+ def build_worker_pool
142
+ wp = DatWorkerPool.new(
143
+ @min_workers,
144
+ @max_workers
145
+ ){ |socket| serve(socket) }
146
+
147
+ # add any configured callbacks
148
+ self.worker_start_procs.each do |cb|
149
+ wp.on_worker_start(&cb)
150
+ end
151
+ self.worker_shutdown_procs.each do |cb|
152
+ wp.on_worker_shutdown(&cb)
153
+ end
154
+ self.worker_sleep_procs.each do |cb|
155
+ wp.on_worker_sleep(&cb)
156
+ end
157
+ self.worker_wakeup_procs.each do |cb|
158
+ wp.on_worker_wakeup(&cb)
159
+ end
160
+
161
+ wp
162
+ end
163
+
131
164
  def add_client_sockets_from_fds(file_descriptors)
132
165
  (file_descriptors || []).each do |file_descriptor|
133
166
  @worker_pool.add_work TCPSocket.for_fd(file_descriptor)
@@ -7,6 +7,8 @@ module DatTCP
7
7
  attr_reader :ip, :port, :file_descriptor
8
8
  attr_reader :client_file_descriptors
9
9
  attr_reader :logger
10
+ attr_reader :worker_start_procs, :worker_shutdown_procs
11
+ attr_reader :worker_sleep_procs, :worker_wakeup_procs
10
12
  attr_reader :waiting_for_pause, :waiting_for_stop, :waiting_for_halt
11
13
  attr_accessor :listen_called, :start_called
12
14
  attr_accessor :stop_listen_called, :pause_called
@@ -21,9 +23,14 @@ module DatTCP
21
23
  @client_file_descriptors = []
22
24
  @logger = DatTCP::Logger::Null.new
23
25
 
26
+ @worker_start_procs = []
27
+ @worker_shutdown_procs = []
28
+ @worker_sleep_procs = []
29
+ @worker_wakeup_procs = []
30
+
24
31
  @waiting_for_pause = nil
25
32
  @waiting_for_stop = nil
26
- @waiting_for_hale = nil
33
+ @waiting_for_halt = nil
27
34
 
28
35
  @listen_called = false
29
36
  @stop_listen_called = false
@@ -77,6 +84,11 @@ module DatTCP
77
84
  @halt_called = true
78
85
  end
79
86
 
87
+ def on_worker_start(&block); @worker_start_procs << block; end
88
+ def on_worker_shutdown(&block); @worker_shutdown_procs << block; end
89
+ def on_worker_sleep(&block); @worker_sleep_procs << block; end
90
+ def on_worker_wakeup(&block); @worker_wakeup_procs << block; end
91
+
80
92
  end
81
93
 
82
94
  end
@@ -1,3 +1,3 @@
1
1
  module DatTCP
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dat-tcp
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 6
8
+ - 7
9
9
  - 0
10
- version: 0.6.0
10
+ version: 0.7.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Collin Redding
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2015-01-30 00:00:00 Z
19
+ date: 2015-09-08 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  requirement: &id001 !ruby/object:Gem::Requirement
@@ -24,14 +24,14 @@ dependencies:
24
24
  requirements:
25
25
  - - ~>
26
26
  - !ruby/object:Gem::Version
27
- hash: 3
27
+ hash: 1
28
28
  segments:
29
29
  - 0
30
- - 4
31
- version: "0.4"
32
- version_requirements: *id001
30
+ - 5
31
+ version: "0.5"
33
32
  type: :runtime
34
33
  name: dat-worker-pool
34
+ version_requirements: *id001
35
35
  prerelease: false
36
36
  - !ruby/object:Gem::Dependency
37
37
  requirement: &id002 !ruby/object:Gem::Requirement
@@ -39,14 +39,14 @@ dependencies:
39
39
  requirements:
40
40
  - - ~>
41
41
  - !ruby/object:Gem::Version
42
- hash: 27
42
+ hash: 29
43
43
  segments:
44
44
  - 2
45
- - 12
46
- version: "2.12"
47
- version_requirements: *id002
45
+ - 15
46
+ version: "2.15"
48
47
  type: :development
49
48
  name: assert
49
+ version_requirements: *id002
50
50
  prerelease: false
51
51
  description: A generic threaded TCP server API. It is designed for use as a base for application servers.
52
52
  email:
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  requirements: []
96
96
 
97
97
  rubyforge_project:
98
- rubygems_version: 1.8.29
98
+ rubygems_version: 1.8.25
99
99
  signing_key:
100
100
  specification_version: 3
101
101
  summary: A generic threaded TCP server API