tra 0.0.2 → 0.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/lib/tra.rb +6 -4
- data/lib/tra/fork.rb +1 -1
- data/lib/tra/mailbox.rb +12 -1
- data/test.rb +34 -0
- data/tra.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: feb23af2174e0f8e50c07497804c47b6442929e7
|
4
|
+
data.tar.gz: f620d3dbff3a2c91360351216904a2721e926e79
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91800584d8ec4999a3cf5e8a49cfff2157a539cbbaaf0cf7e5b54367639f03e5b912fc1c6150657f38a5d19531538031e17fe9f8262745e7d23fd3122be9df29
|
7
|
+
data.tar.gz: 18cb9b1a4a50243bc4627bca9354523eba341fb56bdabd7166f1206707206d27d318d0223f6d39634a93dd74a50ecb97d2698515793d9d37d1b9b8443a5ced32
|
data/lib/tra.rb
CHANGED
@@ -5,10 +5,8 @@ require_relative 'tra/mailbox'
|
|
5
5
|
require_relative 'tra/fork'
|
6
6
|
|
7
7
|
module Tra
|
8
|
-
QUEUE = Queue.new
|
9
|
-
|
10
|
-
DIRECTORY = "/tmp/tra"
|
11
|
-
FILE = -> pid { "#{DIRECTORY}/#{pid}" }
|
8
|
+
QUEUE, PATTERNS = Queue.new, {}
|
9
|
+
DIRECTORY, FILE = '/tmp/tra', -> pid { "#{DIRECTORY}/#{pid}" }
|
12
10
|
|
13
11
|
class << self
|
14
12
|
def enumerator
|
@@ -20,6 +18,10 @@ module Tra
|
|
20
18
|
extend Forwardable
|
21
19
|
delegate [:next, :take] => :enumerator
|
22
20
|
|
21
|
+
def on pattern, &action
|
22
|
+
PATTERNS[pattern] = action
|
23
|
+
end
|
24
|
+
|
23
25
|
def run
|
24
26
|
Mailbox.receive
|
25
27
|
|
data/lib/tra/fork.rb
CHANGED
data/lib/tra/mailbox.rb
CHANGED
@@ -7,7 +7,18 @@ module Tra
|
|
7
7
|
|
8
8
|
Thread.new do
|
9
9
|
Socket.unix_server_loop file do |socket|
|
10
|
-
|
10
|
+
message = Marshal.load socket.read size_of_message_from socket
|
11
|
+
patterns = PATTERNS.select { |pattern, _| pattern === message }
|
12
|
+
|
13
|
+
if patterns.empty?
|
14
|
+
QUEUE << message
|
15
|
+
else
|
16
|
+
Thread.new do
|
17
|
+
patterns.each do |_, action|
|
18
|
+
Thread.new { action.call message }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
11
22
|
end
|
12
23
|
end
|
13
24
|
|
data/test.rb
CHANGED
@@ -15,3 +15,37 @@ p Tra.next
|
|
15
15
|
=end
|
16
16
|
Tra.take(5).each { |message| p message }
|
17
17
|
child.put [:my, :message, "with\n newlines\n"]
|
18
|
+
|
19
|
+
|
20
|
+
# sloppy patterns' test goes underneath
|
21
|
+
class A
|
22
|
+
end
|
23
|
+
|
24
|
+
class B
|
25
|
+
end
|
26
|
+
|
27
|
+
As = []
|
28
|
+
Bs = []
|
29
|
+
|
30
|
+
Tra.on A do |message|
|
31
|
+
As << message
|
32
|
+
end
|
33
|
+
|
34
|
+
Tra.on B do |message|
|
35
|
+
Bs << message
|
36
|
+
end
|
37
|
+
|
38
|
+
fork do
|
39
|
+
Process.ppid.put A.new
|
40
|
+
Process.ppid.put :asd
|
41
|
+
Process.ppid.put B.new
|
42
|
+
Process.ppid.put B.new
|
43
|
+
Process.ppid.put B.new
|
44
|
+
Process.ppid.put A.new
|
45
|
+
Process.ppid.put 42
|
46
|
+
end
|
47
|
+
|
48
|
+
sleep 1
|
49
|
+
p As.size #2
|
50
|
+
p Bs.size #3
|
51
|
+
p Tra.take 2
|
data/tra.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tra
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anatoly Chernow
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-02-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|