rsmp 0.1.13 → 0.1.29

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rsmp/probe.rb DELETED
@@ -1,114 +0,0 @@
1
- # A probe checks incoming messages and store matches
2
- # Once it has collected what it needs, it triggers a condition variable
3
- # and the client wakes up.
4
-
5
- module RSMP
6
- class Probe
7
- attr_reader :condition, :items, :done
8
-
9
- # block should send a message and return message just sent
10
- def self.collect_response proxy, options={}, &block
11
- from = proxy.archive.current_index
12
- sent = yield
13
- raise RuntimeError unless sent && sent[:message].is_a?(RSMP::Message)
14
- item = proxy.archive.capture(options.merge(from: from+1, num: 1, with_message: true)) do |item|
15
- ["CommandResponse","StatusResponse","MessageNotAck"].include?(item[:message].type)
16
- end
17
- if item
18
- item[:message]
19
- else
20
- nil
21
- end
22
- end
23
-
24
-
25
- def initialize archive
26
- raise ArgumentError.new("Archive expected") unless archive.is_a? Archive
27
- @archive = archive
28
- @items = []
29
- @condition = Async::Notification.new
30
- end
31
-
32
- def capture task, options={}, &block
33
- @options = options
34
- @block = block
35
- @num = options[:num]
36
-
37
- if options[:earliest]
38
- from = find_timestamp_index options[:earliest]
39
- backscan from
40
- elsif options[:from]
41
- backscan options[:from]
42
- end
43
-
44
- # if backscan didn't find enough items, then
45
- # insert ourself as probe and sleep until enough items are captured
46
- if @items.size < @num
47
- begin
48
- @archive.probes.add self
49
- task.with_timeout(options[:timeout]) do
50
- @condition.wait
51
- end
52
- ensure
53
- @archive.probes.remove self
54
- end
55
- end
56
-
57
- if @num == 1
58
- @items.first # if one item was requested, return item instead of array
59
- else
60
- @items[0..@num-1] # return array, but ensure we never return more than requested
61
- end
62
- end
63
-
64
- def find_timestamp_index earliest
65
- (0..@archive.items.size).bsearch do |i| # use binary search to find item index
66
- @archive.items[i][:timestamp] >= earliest
67
- end
68
- end
69
-
70
- def backscan from
71
- from.upto(@archive.items.size-1) do |i|
72
- return if process @archive.items[i]
73
- end
74
- end
75
-
76
- def reset
77
- @items.clear
78
- @done = false
79
- end
80
-
81
- def process item
82
- raise ArgumentError unless item
83
- return true if @done
84
- if matches? item
85
- @items << item
86
- if @num && @items.size >= @num
87
- @done = true
88
- @condition.signal
89
- return true
90
- end
91
- end
92
- false
93
- end
94
-
95
- def matches? item
96
- raise ArgumentError unless item
97
-
98
- if @options[:type]
99
- return false if item[:message] == nil
100
- if @options[:type].is_a? Array
101
- return false unless @options[:type].include? item[:message].type
102
- else
103
- return false unless item[:message].type == @options[:type]
104
- end
105
- end
106
- return if @options[:level] && item[:level] != @options[:level]
107
- return false if @options[:with_message] && !(item[:direction] && item[:message])
108
- if @block
109
- return false if @block.call(item) == false
110
- end
111
- true
112
- end
113
- end
114
- end
@@ -1,28 +0,0 @@
1
- # Collection of probes
2
-
3
- module RSMP
4
- class ProbeCollection
5
-
6
- def initialize
7
- @probes = []
8
- end
9
-
10
- def add probe
11
- raise ArgumentError unless probe
12
- @probes << probe
13
- end
14
-
15
- def remove probe
16
- raise ArgumentError unless probe
17
- @probes.delete probe
18
- end
19
-
20
- def process item
21
- @probes.each { |probe| probe.process item }
22
- end
23
-
24
- def clear
25
- @probes.each { |probe| probe.clear }
26
- end
27
- end
28
- end
@@ -1,10 +0,0 @@
1
- # Things shared between sites and site proxies
2
-
3
- module RSMP
4
- module SupervisorBase
5
-
6
- def initialize_supervisor
7
- end
8
-
9
- end
10
- end