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.
@@ -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
- extend ClassMethods
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 InstanceMethods
18
+ module MessageHandling
31
19
 
32
- def receive_data(data)
33
- protocol_buffer.extract(data).each do |part|
34
- receive_line(part)
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 receive_line(line)
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
- send_data "#{JSON.dump(payload)}#{SEPERATOR}"
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
- def use_ssl=(value)
74
- @should_use_ssl = value
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
- protected
79
+ def callbacks
80
+ @callbacks ||= {}
81
+ end
114
82
 
115
- def should_use_ssl?
116
- instance_variable_defined?(:@should_use_ssl) && @should_use_ssl
83
+ def connected?
84
+ instance_variable_defined?(:@connected) && @connected
117
85
  end
118
86
 
119
- def ssl_enabled?
120
- instance_variable_defined?(:@ssl_enabled) && @ssl_enabled
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
- def protocol_buffer
147
- @protocol_buffer ||= BufferedTokenizer.new(SEPERATOR)
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
- def callbacks
151
- @callbacks ||= {}
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 connected?
155
- instance_variable_defined?(:@connected) && @connected
154
+ def send_message(line)
155
+ send_data "#{line}#{SEPERATOR}"
156
156
  end
157
157
 
158
- def handle_response(response)
159
- return unless response.is_a?(Hash) && response.has_key?("action")
160
- payload = response["payload"] || {}
161
- @callback_id = response.delete("callback-id")
162
- process_callback(payload)
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 process_action(name, data)
168
- self.event_handlers[name.to_s].each do |handler|
169
- if handler.respond_to?(:call)
170
- instance_exec(data, &handler)
171
- elsif handler.respond_to?(:handle)
172
- handler.handle(data)
173
- else
174
- self.send(handler, data)
175
- end
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
- end
183
-
184
- module ClassMethods
180
+ def ssl_handshake_complete
181
+ if !connected?
182
+ @connected = true
183
+ post_connect
184
+ end
185
+ end
185
186
 
186
- def on_action(name, handler = nil, &blk)
187
- real_name = name.to_s
188
- self.event_handlers[real_name] << blk if blk.present?
189
- self.event_handlers[real_name] << handler if handler.present?
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
@@ -9,7 +9,7 @@ require 'perennial/exceptions'
9
9
 
10
10
  module Perennial
11
11
 
12
- VERSION = [1, 2, 1]
12
+ VERSION = [1, 2, 2]
13
13
 
14
14
  has_library :dispatchable, :hookable, :loader, :logger, :nash,
15
15
  :loggable, :manifest, :settings, :argument_parser,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: perennial
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darcy Laycock