perennial 1.2.1 → 1.2.2
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.
- data/lib/perennial/protocols/json_transport.rb +97 -88
- data/lib/perennial.rb +1 -1
- metadata +1 -1
@@ -10,33 +10,28 @@ module Perennial
|
|
10
10
|
def self.included(parent)
|
11
11
|
parent.class_eval do |parent|
|
12
12
|
is :loggable
|
13
|
-
|
14
|
-
cattr_accessor :event_handlers
|
15
|
-
|
16
13
|
include InstanceMethods
|
17
|
-
|
18
|
-
|
19
|
-
self.event_handlers = Hash.new { |h,k| h[k] = [] }
|
20
|
-
|
21
|
-
# Simple built in Methods, applicable both ways.
|
22
|
-
on_action :exception, :handle_exception
|
23
|
-
on_action :noop, :handle_noop
|
24
|
-
on_action :enable_ssl, :handle_enable_ssl
|
25
|
-
on_action :enabled_ssl, :handle_enabled_ssl
|
26
|
-
|
14
|
+
include MessageHandling
|
27
15
|
end
|
28
16
|
end
|
29
17
|
|
30
|
-
module
|
18
|
+
module MessageHandling
|
31
19
|
|
32
|
-
def
|
33
|
-
|
34
|
-
|
20
|
+
def self.included(parent)
|
21
|
+
parent.class_eval do
|
22
|
+
cattr_accessor :event_handlers
|
23
|
+
extend ClassMethods
|
24
|
+
self.event_handlers = Hash.new { |h,k| h[k] = [] }
|
25
|
+
|
26
|
+
# Simple built in Methods, applicable both ways.
|
27
|
+
on_action :exception, :handle_exception
|
28
|
+
on_action :noop, :handle_noop
|
29
|
+
on_action :enable_ssl, :handle_enable_ssl
|
30
|
+
on_action :enabled_ssl, :handle_enabled_ssl
|
35
31
|
end
|
36
32
|
end
|
37
33
|
|
38
|
-
def
|
39
|
-
line.strip!
|
34
|
+
def receive_message(line)
|
40
35
|
response = JSON.parse(line)
|
41
36
|
handle_response(response)
|
42
37
|
rescue Exception => e
|
@@ -44,17 +39,6 @@ module Perennial
|
|
44
39
|
handle_receiving_exception(e)
|
45
40
|
end
|
46
41
|
|
47
|
-
# Typically you'd log a backtrace
|
48
|
-
def handle_receiving_exception(e)
|
49
|
-
end
|
50
|
-
|
51
|
-
def host_with_port
|
52
|
-
@host_with_port ||= begin
|
53
|
-
port, ip = Socket.unpack_sockaddr_in(get_peername)
|
54
|
-
"#{ip}:#{port}"
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
42
|
def message(name, data = {}, &blk)
|
59
43
|
payload = {
|
60
44
|
"action" => name.to_s,
|
@@ -62,7 +46,7 @@ module Perennial
|
|
62
46
|
"sent-at" => Time.now
|
63
47
|
}
|
64
48
|
payload.merge!(options_for_callback(blk))
|
65
|
-
|
49
|
+
send_message JSON.dump(payload)
|
66
50
|
end
|
67
51
|
|
68
52
|
def reply(name, data = {}, &blk)
|
@@ -70,26 +54,8 @@ module Perennial
|
|
70
54
|
message(name, data, &blk)
|
71
55
|
end
|
72
56
|
|
73
|
-
|
74
|
-
|
75
|
-
enable_ssl if connected?
|
76
|
-
end
|
77
|
-
|
78
|
-
def post_connect
|
79
|
-
end
|
80
|
-
|
81
|
-
def post_init
|
82
|
-
if !connected? && !ssl_enabled?
|
83
|
-
@connected = true
|
84
|
-
post_connect
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
def ssl_handshake_complete
|
89
|
-
if !connected?
|
90
|
-
@connected = true
|
91
|
-
post_connect
|
92
|
-
end
|
57
|
+
# Typically you'd log a backtrace
|
58
|
+
def handle_receiving_exception(e)
|
93
59
|
end
|
94
60
|
|
95
61
|
def handle_enable_ssl(data)
|
@@ -110,14 +76,36 @@ module Perennial
|
|
110
76
|
logger.warn "Got exception from remote call of #{data["action"]}: #{data["message"]}"
|
111
77
|
end
|
112
78
|
|
113
|
-
|
79
|
+
def callbacks
|
80
|
+
@callbacks ||= {}
|
81
|
+
end
|
114
82
|
|
115
|
-
def
|
116
|
-
instance_variable_defined?(:@
|
83
|
+
def connected?
|
84
|
+
instance_variable_defined?(:@connected) && @connected
|
117
85
|
end
|
118
86
|
|
119
|
-
def
|
120
|
-
|
87
|
+
def handle_response(response)
|
88
|
+
return unless response.is_a?(Hash) && response.has_key?("action")
|
89
|
+
payload = response["payload"] || {}
|
90
|
+
@callback_id = response.delete("callback-id")
|
91
|
+
process_callback(payload)
|
92
|
+
process_action(response["action"], payload)
|
93
|
+
@callback_id = nil
|
94
|
+
end
|
95
|
+
|
96
|
+
def process_action(name, data)
|
97
|
+
self.event_handlers[name.to_s].each do |handler|
|
98
|
+
if handler.respond_to?(:call)
|
99
|
+
instance_exec(data, &handler)
|
100
|
+
elsif handler.respond_to?(:handle)
|
101
|
+
handler.handle(data)
|
102
|
+
else
|
103
|
+
self.send(handler, data)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
rescue Exception => e
|
107
|
+
reply :exception, :name => e.class.name, :message => e.message,
|
108
|
+
:action => name, :payload => data
|
121
109
|
end
|
122
110
|
|
123
111
|
def options_for_callback(blk)
|
@@ -143,50 +131,71 @@ module Perennial
|
|
143
131
|
Digest::SHA256.hexdigest([base, count].compact.join("-"))
|
144
132
|
end
|
145
133
|
|
146
|
-
|
147
|
-
|
134
|
+
module ClassMethods
|
135
|
+
|
136
|
+
def on_action(name, handler = nil, &blk)
|
137
|
+
real_name = name.to_s
|
138
|
+
self.event_handlers[real_name] << blk if blk.present?
|
139
|
+
self.event_handlers[real_name] << handler if handler.present?
|
140
|
+
end
|
141
|
+
|
148
142
|
end
|
149
143
|
|
150
|
-
|
151
|
-
|
144
|
+
end
|
145
|
+
|
146
|
+
module InstanceMethods
|
147
|
+
|
148
|
+
def receive_data(data)
|
149
|
+
protocol_buffer.extract(data).each do |part|
|
150
|
+
receive_message(part)
|
151
|
+
end
|
152
152
|
end
|
153
153
|
|
154
|
-
def
|
155
|
-
|
154
|
+
def send_message(line)
|
155
|
+
send_data "#{line}#{SEPERATOR}"
|
156
156
|
end
|
157
157
|
|
158
|
-
def
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
process_action(response["action"], payload)
|
164
|
-
@callback_id = nil
|
158
|
+
def host_with_port
|
159
|
+
@host_with_port ||= begin
|
160
|
+
port, ip = Socket.unpack_sockaddr_in(get_peername)
|
161
|
+
"#{ip}:#{port}"
|
162
|
+
end
|
165
163
|
end
|
166
164
|
|
167
|
-
def
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
165
|
+
def use_ssl=(value)
|
166
|
+
@should_use_ssl = value
|
167
|
+
enable_ssl if connected?
|
168
|
+
end
|
169
|
+
|
170
|
+
def post_connect
|
171
|
+
end
|
172
|
+
|
173
|
+
def post_init
|
174
|
+
if !connected? && !ssl_enabled?
|
175
|
+
@connected = true
|
176
|
+
post_connect
|
176
177
|
end
|
177
|
-
rescue Exception => e
|
178
|
-
reply :exception, :name => e.class.name, :message => e.message,
|
179
|
-
:action => name, :payload => data
|
180
178
|
end
|
181
179
|
|
182
|
-
|
183
|
-
|
184
|
-
|
180
|
+
def ssl_handshake_complete
|
181
|
+
if !connected?
|
182
|
+
@connected = true
|
183
|
+
post_connect
|
184
|
+
end
|
185
|
+
end
|
185
186
|
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
187
|
+
protected
|
188
|
+
|
189
|
+
def should_use_ssl?
|
190
|
+
instance_variable_defined?(:@should_use_ssl) && @should_use_ssl
|
191
|
+
end
|
192
|
+
|
193
|
+
def ssl_enabled?
|
194
|
+
instance_variable_defined?(:@ssl_enabled) && @ssl_enabled
|
195
|
+
end
|
196
|
+
|
197
|
+
def protocol_buffer
|
198
|
+
@protocol_buffer ||= BufferedTokenizer.new(SEPERATOR)
|
190
199
|
end
|
191
200
|
|
192
201
|
end
|
data/lib/perennial.rb
CHANGED