omf_common 6.0.2.pre.2 → 6.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data/bin/file_broadcaster.rb +5 -0
  2. data/bin/file_receiver.rb +5 -0
  3. data/bin/omf_keygen +3 -3
  4. data/bin/omf_send_configure +114 -0
  5. data/bin/omf_send_request +19 -4
  6. data/example/engine_alt.rb +13 -7
  7. data/example/viz/garage_monitor.rb +69 -0
  8. data/example/viz/garage_viz.rb +52 -0
  9. data/example/viz/htdocs/image/garage.png +0 -0
  10. data/example/viz/htdocs/template/garage_banner.html +2 -0
  11. data/example/viz/layout.yaml +44 -0
  12. data/example/vm_alt.rb +5 -0
  13. data/lib/omf_common.rb +17 -8
  14. data/lib/omf_common/auth.rb +5 -0
  15. data/lib/omf_common/auth/certificate.rb +21 -2
  16. data/lib/omf_common/auth/certificate_store.rb +50 -20
  17. data/lib/omf_common/auth/ssh_pub_key_convert.rb +7 -0
  18. data/lib/omf_common/comm.rb +6 -1
  19. data/lib/omf_common/comm/amqp/amqp_communicator.rb +88 -12
  20. data/lib/omf_common/comm/amqp/amqp_file_transfer.rb +5 -0
  21. data/lib/omf_common/comm/amqp/amqp_topic.rb +37 -18
  22. data/lib/omf_common/comm/local/local_communicator.rb +5 -0
  23. data/lib/omf_common/comm/local/local_topic.rb +5 -0
  24. data/lib/omf_common/comm/topic.rb +32 -13
  25. data/lib/omf_common/comm/xmpp/communicator.rb +11 -1
  26. data/lib/omf_common/comm/xmpp/topic.rb +5 -0
  27. data/lib/omf_common/comm/xmpp/xmpp_mp.rb +5 -0
  28. data/lib/omf_common/command.rb +5 -0
  29. data/lib/omf_common/core_ext/string.rb +5 -0
  30. data/lib/omf_common/default_logging.rb +23 -5
  31. data/lib/omf_common/eventloop.rb +40 -23
  32. data/lib/omf_common/eventloop/em.rb +18 -5
  33. data/lib/omf_common/eventloop/local_evl.rb +18 -15
  34. data/lib/omf_common/exec_app.rb +44 -24
  35. data/lib/omf_common/key.rb +5 -0
  36. data/lib/omf_common/measure.rb +5 -0
  37. data/lib/omf_common/message.rb +5 -0
  38. data/lib/omf_common/message/json/json_message.rb +13 -5
  39. data/lib/omf_common/message/xml/message.rb +19 -4
  40. data/lib/omf_common/message/xml/relaxng_schema.rb +5 -0
  41. data/lib/omf_common/message/xml/topic_message.rb +5 -0
  42. data/lib/omf_common/version.rb +6 -1
  43. data/omf_common.gemspec +3 -2
  44. data/test/fixture/1st_level.pem +20 -0
  45. data/test/fixture/2nd_level.pem +19 -0
  46. data/test/fixture/3rd_level.pem +19 -0
  47. data/test/fixture/pubsub.rb +5 -0
  48. data/test/fixture/rc.pem +18 -0
  49. data/test/fixture/root.pem +17 -0
  50. data/test/omf_common/auth/certificate_spec.rb +27 -0
  51. data/test/omf_common/auth/certificate_store_spec.rb +58 -0
  52. data/test/omf_common/auth/ssh_pub_key_convert_spec.rb +5 -0
  53. data/test/omf_common/comm/topic_spec.rb +7 -1
  54. data/test/omf_common/comm/xmpp/communicator_spec.rb +5 -0
  55. data/test/omf_common/comm/xmpp/topic_spec.rb +5 -0
  56. data/test/omf_common/comm_spec.rb +5 -0
  57. data/test/omf_common/command_spec.rb +5 -0
  58. data/test/omf_common/core_ext/string_spec.rb +5 -0
  59. data/test/omf_common/message/xml/message_spec.rb +5 -0
  60. data/test/omf_common/message_spec.rb +8 -3
  61. data/test/test_helper.rb +5 -0
  62. metadata +48 -11
@@ -1,3 +1,8 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
1
6
  require 'omf_common/comm/local/local_topic'
2
7
  require 'securerandom'
3
8
 
@@ -1,3 +1,8 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
1
6
 
2
7
 
3
8
  module OmfCommon
@@ -1,5 +1,11 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
1
6
  require 'monitor'
2
7
  require 'securerandom'
8
+ require 'openssl'
3
9
 
4
10
  module OmfCommon
5
11
  class Comm
@@ -94,22 +100,35 @@ module OmfCommon
94
100
  define_method(mname) do |*args, &message_block|
95
101
  warn_deprecation(mname, :on_message, :on_inform)
96
102
 
97
- add_message_handler(itype, &message_block)
103
+ add_message_handler(itype, args.first, &message_block)
98
104
  end
99
105
  end
100
106
 
101
- def on_message(*args, &message_block)
102
- add_message_handler(:message, &message_block)
107
+ def on_message(key = nil, &message_block)
108
+ add_message_handler(:message, key, &message_block)
103
109
  end
104
110
 
105
- def on_inform(*args, &message_block)
106
- add_message_handler(:inform, &message_block)
111
+ def on_inform(key = nil, &message_block)
112
+ add_message_handler(:inform, key, &message_block)
107
113
  end
108
114
 
109
- # Unsubscribe from the underlying comms layer
115
+ # Remove all registered callbacks for 'key'. Will also unsubscribe from the underlying
116
+ # comms layer if no callbacks remain.
110
117
  #
111
- def unsubscribe()
112
-
118
+ def unsubscribe(key)
119
+ @lock.synchronize do
120
+ @handlers.each do |name, cbks|
121
+ if cbks.delete(key)
122
+ # remove altogether if no callback left
123
+ if cbks.empty?
124
+ @handlers.delete(name)
125
+ end
126
+ end
127
+ end
128
+ if @handlers.empty?
129
+ warn "Should unsubscribe '#{id}'"
130
+ end
131
+ end
113
132
  end
114
133
 
115
134
  def on_subscribed(&block)
@@ -157,8 +176,7 @@ module OmfCommon
157
176
  # Process a message received from this topic.
158
177
  #
159
178
  # @param [OmfCommon::Message] msg Message received
160
- # @param [Hash] auth_info Authentication information
161
- # @option auth_info [Symbol] :signer Id
179
+ #
162
180
  def on_incoming_message(msg)
163
181
  type = msg.operation
164
182
  debug "(#{id}) Deliver message '#{type}': #{msg.inspect}"
@@ -178,7 +196,7 @@ module OmfCommon
178
196
  end
179
197
 
180
198
  debug "(#{id}) Message type '#{htypes.inspect}' (#{msg.class}:#{msg.cid})"
181
- hs = htypes.map { |ht| @handlers[ht] }.compact.flatten
199
+ hs = htypes.map { |ht| (@handlers[ht] || {}).values }.compact.flatten
182
200
  debug "(#{id}) Distributing message to '#{hs.inspect}'"
183
201
  hs.each do |block|
184
202
  block.call msg
@@ -190,11 +208,12 @@ module OmfCommon
190
208
  end
191
209
  end
192
210
 
193
- def add_message_handler(handler_name, &message_block)
211
+ def add_message_handler(handler_name, key, &message_block)
194
212
  raise ArgumentError, 'Missing message callback' if message_block.nil?
195
213
  debug "(#{id}) register handler for '#{handler_name}'"
196
214
  @lock.synchronize do
197
- (@handlers[handler_name] ||= []) << message_block
215
+ key ||= OpenSSL::Digest::SHA1.new(message_block.source_location.to_s).to_s
216
+ (@handlers[handler_name] ||= {})[key] = message_block
198
217
  end
199
218
  self
200
219
  end
@@ -1,3 +1,8 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
1
6
  require 'blather/client/dsl'
2
7
 
3
8
  require 'omf_common/comm/xmpp/xmpp_mp'
@@ -20,7 +25,8 @@ class Comm
20
25
  :fields => [
21
26
  { :var => "FORM_TYPE", :type => 'hidden', :value => "http://jabber.org/protocol/pubsub#node_config" },
22
27
  { :var => "pubsub#persist_items", :value => "0" },
23
- { :var => "pubsub#max_items", :value => "0" },
28
+ { :var => "pubsub#purge_offline", :value => "1" },
29
+ { :var => "pubsub#send_last_published_item", :value => "never" },
24
30
  { :var => "pubsub#notify_retract", :value => "0" },
25
31
  { :var => "pubsub#publish_model", :value => "open" }]
26
32
  })
@@ -160,6 +166,10 @@ class Comm
160
166
  raise StandardError, "Invalid message" unless message.valid?
161
167
 
162
168
  message = message.marshall[1] unless message.kind_of? String
169
+ if message.nil?
170
+ debug "Cannot publish empty message, using authentication and not providing a proper cert?"
171
+ return nil
172
+ end
163
173
 
164
174
  new_block = proc do |stanza|
165
175
  published_messages << OpenSSL::Digest::SHA1.new(message.to_s)
@@ -1,3 +1,8 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
1
6
  module OmfCommon
2
7
  class Comm
3
8
  class XMPP
@@ -1,3 +1,8 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
1
6
  require 'oml4r'
2
7
 
3
8
  module OmfCommon
@@ -1,3 +1,8 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
1
6
  require 'open3'
2
7
 
3
8
  module OmfCommon::Command
@@ -1,3 +1,8 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
1
6
  class String
2
7
  def ducktype
3
8
  Integer(self) rescue Float(self) rescue self
@@ -1,3 +1,8 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
1
6
  require 'logging'
2
7
 
3
8
  module OmfCommon
@@ -14,28 +19,35 @@ module OmfCommon
14
19
  Logging.logger.root.level = :info
15
20
 
16
21
  # Alias logging method using default logger
22
+ #
23
+ # @example
24
+ #
25
+ # info 'Information'
26
+ # # Additional logger name will generate a new child logger in the context of default logger
27
+ # info 'Information', 'logger name'
28
+ #
17
29
  def info(*args, &block)
18
- logger.info(*args, &block)
30
+ get_logger(args[1]).info(args[0], &block)
19
31
  end
20
32
 
21
33
  # @see #info
22
34
  def debug(*args, &block)
23
- logger.debug(*args, &block)
35
+ get_logger(args[1]).debug(args[0], &block)
24
36
  end
25
37
 
26
38
  # @see #info
27
39
  def error(*args, &block)
28
- logger.error(*args, &block)
40
+ get_logger(args[1]).error(args[0], &block)
29
41
  end
30
42
 
31
43
  # @see #info
32
44
  def fatal(*args, &block)
33
- logger.fatal(*args, &block)
45
+ get_logger(args[1]).fatal(args[0], &block)
34
46
  end
35
47
 
36
48
  # @see #info
37
49
  def warn(*args, &block)
38
- logger.warn(*args, &block)
50
+ get_logger(args[1]).warn(args[0], &block)
39
51
  end
40
52
 
41
53
  # Log a warning message for deprecated methods
@@ -48,5 +60,11 @@ module OmfCommon
48
60
  logger.warn "[DEPRECATION] '#{deprecated_name}' is deprecated and not supported. Please do not use it."
49
61
  end
50
62
  end
63
+
64
+ private
65
+
66
+ def get_logger(name = nil)
67
+ name.nil? ? logger : Logging::Logger["#{logger.name}::#{name.to_s}"]
68
+ end
51
69
  end
52
70
  end
@@ -1,7 +1,12 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
1
6
  module OmfCommon
2
7
  # Providing event loop support.
3
8
  class Eventloop
4
-
9
+
5
10
  @@providers = {
6
11
  em: {
7
12
  require: 'omf_common/eventloop/em',
@@ -13,7 +18,8 @@ module OmfCommon
13
18
  }
14
19
  }
15
20
  @@instance = nil
16
-
21
+ @@on_stop_proc = []
22
+
17
23
  #
18
24
  # opts:
19
25
  # :type - eventloop provider
@@ -43,66 +49,77 @@ module OmfCommon
43
49
  @@instance = inst
44
50
  inst
45
51
  end
46
-
52
+
47
53
  def self.instance
48
54
  @@instance
49
55
  end
50
-
56
+
51
57
  # Execute block after some time
52
58
  #
53
- # @param [float] delay in sec
54
- # @param [block] block to execute
59
+ # @param [Float] delay_sec in sec
55
60
  #
56
61
  def after(delay_sec, &block)
57
62
  raise "Missing implementation 'after'"
58
63
  end
59
-
64
+
60
65
  # Periodically call block every interval_sec
61
66
  #
62
- # @param [float] interval in sec
63
- # @param [block] block to execute
67
+ # @param [Float] interval_sec in sec
64
68
  #
65
69
  def every(interval_sec, &block)
66
70
  raise "Missing implementation 'every'"
67
71
  end
68
-
72
+
73
+ # Call 'block' in the context of a separate thread.
74
+ #
75
+ def defer(&block)
76
+ raise "Missing implementation 'defer'"
77
+ end
78
+
69
79
  # Block calling thread until eventloop exits
70
80
  def join()
71
81
  raise "Missing implementation 'join'"
72
82
  end
73
-
83
+
74
84
  def run()
75
- raise "Missing implementation 'run'"
85
+ raise "Missing implementation 'run'"
76
86
  end
77
-
87
+
78
88
  def stop()
79
- raise "Missing implementation 'stop'"
80
- end
81
-
89
+ @@on_stop_proc.each do |block|
90
+ begin
91
+ block.call()
92
+ rescue => ex
93
+ error "Exception '#{ex}'"
94
+ debug "#{ex}\n\t#{ex.backtrace.join("\n\t")}"
95
+ end
96
+ end
97
+ end
98
+
82
99
  # Calling 'block' before stopping eventloop
83
100
  #
84
101
  def on_stop(&block)
85
- warn "Missing implementation 'on_stop'"
102
+ @@on_stop_proc << block
86
103
  end
87
-
104
+
88
105
  # Calling 'block' when having trapped an INT signal
89
106
  #
90
107
  def on_int_signal(&block)
91
108
  # trap(:INT)
92
- warn "Missing implementation 'on_int_signal'"
109
+ warn "Missing implementation 'on_int_signal'"
93
110
  end
94
111
 
95
112
  # Calling 'block' when having trapped a TERM signal
96
113
  #
97
114
  def on_term_signal(&block)
98
115
  # trap(:TERM) {}
99
- warn "Missing implementation 'on_term_signal'"
116
+ warn "Missing implementation 'on_term_signal'"
100
117
  end
101
-
118
+
102
119
  private
103
120
  def initialize(opts = {}, &block)
104
121
  #run(&block) if block
105
122
  end
106
-
123
+
107
124
  end
108
- end
125
+ end
@@ -1,3 +1,8 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
1
6
  require 'eventmachine'
2
7
 
3
8
  module OmfCommon
@@ -14,13 +19,20 @@ module OmfCommon
14
19
 
15
20
  # Execute block after some time
16
21
  #
17
- # @param [Float] delay in sec
22
+ # @param [Float] delay_sec in sec
18
23
  def after(delay_sec, &block)
19
24
  if EM.reactor_running?
20
- EM.add_timer(delay_sec, &block)
25
+ EM.add_timer(delay_sec) do
26
+ begin
27
+ block.call()
28
+ rescue => ex
29
+ error "Exception '#{ex}'"
30
+ debug "#{ex}\n\t#{ex.backtrace.join("\n\t")}"
31
+ end
32
+ end
21
33
  else
22
34
  @deferred << lambda do
23
- EM.add_timer(delay_sec, &block)
35
+ after(delay_sec, &block)
24
36
  end
25
37
  end
26
38
  end
@@ -41,7 +53,7 @@ module OmfCommon
41
53
 
42
54
  # Periodically call block every interval_sec
43
55
  #
44
- # @param [Float] interval in sec
56
+ # @param [Float] interval_sec in sec
45
57
  def every(interval_sec, &block)
46
58
  # to allow canceling the periodic timer we need to
47
59
  # hand back a reference to it which responds to 'cancel'
@@ -82,7 +94,8 @@ module OmfCommon
82
94
  end
83
95
 
84
96
  def stop()
85
- EM.stop
97
+ super
98
+ EM.next_tick { EM.stop }
86
99
  end
87
100
  end # class
88
101
  end
@@ -1,3 +1,8 @@
1
+ # Copyright (c) 2012 National ICT Australia Limited (NICTA).
2
+ # This software may be used and distributed solely under the terms of the MIT license (License).
3
+ # You should find a copy of the License in LICENSE.TXT or at http://opensource.org/licenses/MIT.
4
+ # By downloading or using this software you accept the terms and the liability disclaimer in the License.
5
+
1
6
 
2
7
 
3
8
  module OmfCommon
@@ -5,32 +10,30 @@ module OmfCommon
5
10
  # Implements a simple eventloop which only deals with timer events
6
11
  #
7
12
  class Local < Eventloop
8
-
13
+
9
14
  def initialize(opts = {}, &block)
10
15
  super
11
16
  @tasks = []
12
17
  @running = false
13
18
  after(0, &block) if block
14
19
  end
15
-
20
+
16
21
  # Execute block after some time
17
22
  #
18
- # @param [float] delay in sec
19
- # @param [block] block to execute
23
+ # @param [Float] delay_sec in sec
20
24
  #
21
25
  def after(delay_sec, &block)
22
26
  @tasks << [Time.now + delay_sec, block]
23
27
  end
24
-
28
+
25
29
  # Periodically call block every interval_sec
26
30
  #
27
- # @param [float] interval in sec
28
- # @param [block] block to execute
31
+ # @param [Float] interval_sec in sec
29
32
  #
30
33
  def every(interval_sec, &block)
31
34
  @tasks << [Time.now + interval_sec, block, :periodic => interval_sec]
32
35
  end
33
-
36
+
34
37
  # Call 'block' in the context of a separate thread.
35
38
  #
36
39
  def defer(&block)
@@ -44,17 +47,17 @@ module OmfCommon
44
47
  end
45
48
  end
46
49
  end
47
-
48
-
49
- def stop
50
+
51
+
52
+ def stop
50
53
  @running = false
51
54
  end
52
-
55
+
53
56
  def run(&block)
54
57
  after(0, &block) if block
55
58
  return if @running
56
59
  @running = true
57
-
60
+
58
61
  while @running do
59
62
  now = Time.now
60
63
  @tasks = @tasks.sort
@@ -86,8 +89,8 @@ module OmfCommon
86
89
  end
87
90
  end
88
91
  end
89
- end
92
+ end
90
93
  end # class
91
94
  end
92
95
  end
93
-
96
+