easy-serve 0.2 → 0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/examples/passive.rb +39 -0
- data/lib/easy-serve.rb +23 -4
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: affa8e536ee5d6c7d9b703a45267bcc7fec96535
|
4
|
+
data.tar.gz: d389e400e0e5408ac08400cfd61661befeaaf514
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f552e68ca9612ebaacbef58ce31395b7da8121a6753855aebb3bfac58bf07fb435f45dc0f4184395a02d3c9e8590249785307b79f603472e29adaa4f97b0f6f
|
7
|
+
data.tar.gz: 07aedd1e1b46b0ce875e5dee6cc37220e8e90ca929ebf6bff445a7e6f234733ad07b81416a9b89d14dd04be703bc728599ac18d73eef1762bdc0f00196cf44d1
|
data/examples/passive.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'easy-serve'
|
2
|
+
Thread.abort_on_exception = true
|
3
|
+
|
4
|
+
EasyServe.start do |ez|
|
5
|
+
log = ez.log
|
6
|
+
log.level = Logger::DEBUG
|
7
|
+
log.formatter = nil if $VERBOSE
|
8
|
+
log.debug {"starting servers"}
|
9
|
+
|
10
|
+
ez.start_servers do
|
11
|
+
ez.server "simple-server", :unix do |svr|
|
12
|
+
Thread.new do
|
13
|
+
loop do
|
14
|
+
conn = svr.accept
|
15
|
+
conn.write "hello from #{log.progname}"
|
16
|
+
conn.close_write
|
17
|
+
log.info conn.read
|
18
|
+
conn.close
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
ez.client "simple-server", passive: true do |conn|
|
25
|
+
log.progname = "client 1"
|
26
|
+
log.info conn.read
|
27
|
+
conn.write "hello from #{log.progname}, pid = #$$; sleeping..."
|
28
|
+
conn.close_write
|
29
|
+
sleep
|
30
|
+
end
|
31
|
+
|
32
|
+
sleep 0.1
|
33
|
+
|
34
|
+
ez.client "simple-server" do |conn|
|
35
|
+
log.progname = "client 2"
|
36
|
+
log.info conn.read
|
37
|
+
conn.write "hello from #{log.progname}, pid = #$$"
|
38
|
+
end
|
39
|
+
end
|
data/lib/easy-serve.rb
CHANGED
@@ -4,7 +4,7 @@ require 'yaml'
|
|
4
4
|
require 'fileutils'
|
5
5
|
|
6
6
|
class EasyServe
|
7
|
-
VERSION = "0.
|
7
|
+
VERSION = "0.3"
|
8
8
|
|
9
9
|
class Server
|
10
10
|
attr_reader :name, :pid, :addr
|
@@ -37,6 +37,7 @@ class EasyServe
|
|
37
37
|
attr_accessor :log
|
38
38
|
attr_accessor :servers
|
39
39
|
attr_reader :clients
|
40
|
+
attr_reader :passive_clients
|
40
41
|
attr_reader :servers_file
|
41
42
|
attr_reader :interactive
|
42
43
|
|
@@ -55,6 +56,7 @@ class EasyServe
|
|
55
56
|
@interactive = opts[:interactive]
|
56
57
|
@log = opts[:log] || self.class.null_logger
|
57
58
|
@clients = [] # pid
|
59
|
+
@passive_clients = [] # pid
|
58
60
|
@owner = false
|
59
61
|
@servers = nil # name => Server
|
60
62
|
|
@@ -90,7 +92,21 @@ class EasyServe
|
|
90
92
|
|
91
93
|
clients.each do |pid|
|
92
94
|
log.debug {"waiting for client pid=#{pid} to stop"}
|
93
|
-
|
95
|
+
begin
|
96
|
+
Process.waitpid pid
|
97
|
+
rescue Errno::ECHILD
|
98
|
+
log.debug {"client pid=#{pid} was already waited for"}
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
passive_clients.each do |pid|
|
103
|
+
log.debug {"stopping client pid=#{pid}"}
|
104
|
+
Process.kill("TERM", pid)
|
105
|
+
begin
|
106
|
+
Process.waitpid pid
|
107
|
+
rescue Errno::ECHILD
|
108
|
+
log.debug {"client pid=#{pid} was already waited for"}
|
109
|
+
end
|
94
110
|
end
|
95
111
|
|
96
112
|
if @owner
|
@@ -220,12 +236,15 @@ class EasyServe
|
|
220
236
|
end
|
221
237
|
end
|
222
238
|
|
223
|
-
|
224
|
-
|
239
|
+
# A passive client may be stopped after all active clients exit.
|
240
|
+
def client *server_names, passive: false
|
241
|
+
c = fork do
|
225
242
|
conns = server_names.map {|sn| socket_for(*servers[sn].addr)}
|
226
243
|
yield(*conns) if block_given?
|
227
244
|
no_interrupt_if_interactive
|
228
245
|
end
|
246
|
+
(passive ? passive_clients : clients) << c
|
247
|
+
c
|
229
248
|
end
|
230
249
|
|
231
250
|
def local *server_names
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy-serve
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel VanderWerf
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Framework for starting tcp/unix servers and connected clients under one
|
14
14
|
parent process.
|
@@ -25,6 +25,7 @@ files:
|
|
25
25
|
- examples/simple.rb
|
26
26
|
- examples/multi.rb
|
27
27
|
- examples/remote.rb
|
28
|
+
- examples/passive.rb
|
28
29
|
homepage: https://github.com/vjoel/easy-serve
|
29
30
|
licenses:
|
30
31
|
- BSD
|