Module: OmfCommon

Defined in:
omf_common/lib/omf_common/message.rb,
omf_common/lib/omf_common.rb,
omf_common/lib/omf_common/key.rb,
omf_common/lib/omf_common/comm.rb,
omf_common/lib/omf_common/auth.rb,
omf_common/lib/omf_common/measure.rb,
omf_common/lib/omf_common/version.rb,
omf_common/lib/omf_common/eventloop.rb,
omf_common/lib/omf_common/comm/topic.rb,
omf_common/lib/omf_common/eventloop/em.rb,
omf_common/lib/omf_common/comm/xmpp/topic.rb,
omf_common/lib/omf_common/default_logging.rb,
omf_common/lib/omf_common/comm/xmpp/xmpp_mp.rb,
omf_common/lib/omf_common/eventloop/local_evl.rb,
omf_common/lib/omf_common/message/xml/message.rb,
omf_common/lib/omf_common/comm/amqp/amqp_topic.rb,
omf_common/lib/omf_common/comm/xmpp/communicator.rb,
omf_common/lib/omf_common/comm/local/local_topic.rb,
omf_common/lib/omf_common/message/json/json_message.rb,
omf_common/lib/omf_common/message/xml/relaxng_schema.rb,
omf_common/lib/omf_common/comm/amqp/amqp_file_transfer.rb,
omf_common/lib/omf_common/comm/local/local_communicator.rb

Overview

Copyright © 2012 National ICT Australia Limited (NICTA). This software may be used and distributed solely under the terms of the MIT license (License). You should find a copy of the License in LICENSE.TXT or at opensource.org/licenses/MIT. By downloading or using this software you accept the terms and the liability disclaimer in the License.

Defined Under Namespace

Modules: Auth, Command, DSL, DefaultLogging Classes: Comm, Eventloop, Key, MPMessage, Measure, Message, RelaxNGSchema

Constant Summary

DEFAULTS =
{
  development: {
    eventloop: {
      type: 'em'
    },
    logging: {
      level: {
        default: 'debug'
      },
      appenders: {
        stdout: {
          date_pattern: '%H:%M:%S',
          pattern: '%d %5l %c{2}: %m\n',
          color_scheme: 'default'
        }
      }
    }
  },
  production: {
    eventloop: {
      type: :em
    },
    logging: {
      level: {
        default: 'info'
      },
      appenders: {
        file: {
          log_dir: '/var/log',
          #log_file: 'foo.log',
          date_pattern: '%F %T %z',
          pattern: '[%d] %-5l %c: %m\n'
        }
      }

    }
  },
  daemon: {
    daemonize: {
      dir_mode: :script,
      dir: '/tmp',
      backtrace: true,
      log_dir: '/var/log',
      log_output: true
    },
    eventloop: {
      type: :em
    },
    logging: {
      level: {
        default: 'info'
      },
      appenders: {
        file: {
          log_dir: '/var/log',
          #log_file: 'foo.log',
          date_pattern: '%F %T %z',
          pattern: '[%d] %-5l %c: %m\n'
        }
      }

    }
  },
  local: {
    communication: {
      type: :local,
    },
    eventloop: { type: :local},
    logging: {
      level: {
        default: 'debug'
      },
      appenders: {
        stdout: {
          date_pattern: '%H:%M:%S',
          pattern: '%d %5l %c{2}: %m\n',
          color_scheme: 'none'
        }
      }
    }
  },
  test_daemon: {
    daemonize: {
      dir_mode: :script,
      dir: '/tmp',
      backtrace: true,
      log_dir: '/tmp',
      log_output: true
    },
    eventloop: {
      type: :em
    },
    logging: {
      level: {
        default: 'debug'
      },
      appenders: {
        file: {
          log_dir: '/tmp',
          #log_file: 'foo.log',
          date_pattern: '%F %T %z',
          pattern: '[%d] %-5l %c: %m\n'
        }
      }
    }
  }
}
VERSION =
"6.0.2.pre.2"
PROTOCOL_VERSION =
"6.0"

Class Method Summary collapse

Class Method Details

._init_logging(opts = {}) ⇒ Object

DO NOT CALL DIRECTLY



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'omf_common/lib/omf_common.rb', line 241

def self._init_logging(opts = {})
  logger = Logging.logger.root
  if appenders = opts[:appenders]
    logger.clear_appenders
    appenders.each do |type, topts|
      case type.to_sym
      when :stdout
        $stdout.sync = true
        logger.add_appenders(
          Logging.appenders.stdout('custom',
            :layout => Logging.layouts.pattern(topts)
        ))

      when :file
        dir_name = topts.delete(:log_dir) || DEF_LOG_DIR
        file_name = topts.delete(:log_file) || "#{File.basename($0, File.extname($0))}.log"
        path = File.join(dir_name, file_name)
        logger.add_appenders(
          Logging.appenders.file(path,
            :layout => Logging.layouts.pattern(topts)
        ))
      else
        raise "Unknown logging appender type '#{type}'"
      end
    end
  end
  if level = opts[:level]
    if level.is_a? Hash
      # package level settings
      level.each do |name, lvl|
        if name.to_s == 'default'
          logger.level = lvl.to_sym
        else
          Logging.logger[name.to_s].level = lvl.to_sym
        end
      end
    else
      logger.level = level.to_sym
    end
  end
end

._rec_merge(this_hash, other_hash) ⇒ Object



283
284
285
286
287
288
289
290
291
# File 'omf_common/lib/omf_common.rb', line 283

def self._rec_merge(this_hash, other_hash)
  # if the dominant side is not a hash we stop recursing and pick the primitive value
  return other_hash unless other_hash.is_a? Hash

  r = {}
  this_hash.merge(other_hash) do |key, oldval, newval|
    r[key] = oldval.is_a?(Hash) ? _rec_merge(oldval, newval) : newval
  end
end

._rec_sym_keys(hash) ⇒ Object

Recusively Symbolize keys of hash



295
296
297
298
299
300
301
302
303
304
305
306
# File 'omf_common/lib/omf_common.rb', line 295

def self._rec_sym_keys(hash)
  h = {}
  hash.each do |k, v|
    if v.is_a? Hash
      v = _rec_sym_keys(v)
    elsif v.is_a? Array
      v = v.map {|e| e.is_a?(Hash) ? _rec_sym_keys(e) : e }
    end
    h[k.to_sym] = v
  end
  h
end

.commObject

Return the communication driver instance



181
182
183
# File 'omf_common/lib/omf_common.rb', line 181

def self.comm()
  Comm.instance
end

.eventloopObject

Return the communication driver instance



187
188
189
# File 'omf_common/lib/omf_common.rb', line 187

def self.eventloop()
  Eventloop.instance
end

.init(op_mode, opts = {}, &block) ⇒ Object

Initialize the OMF runtime. Options are:

:communication
  :type
  ... specific opts
:eventloop
  :type {:em|:local...}


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'omf_common/lib/omf_common.rb', line 144

def self.init(op_mode, opts = {}, &block)
  if op_mode && defs = DEFAULTS[op_mode.to_sym]
    opts = _rec_merge(defs, opts)
  end
  if dopts = opts.delete(:daemonize)
    dopts[:app_name] ||= "#{File.basename($0, File.extname($0))}_daemon"
    require 'daemons'
    Daemons.run_proc(dopts[:app_name], dopts) do
      init(nil, opts, &block)
    end
    return
  end

  if lopts = opts[:logging]
    _init_logging(lopts) unless lopts.empty?
  end
  unless copts = opts[:communication]
    raise "Missing :communication description"
  end

  if aopts = opts[:auth]
    require 'omf_common/auth/credential_store'
    OmfCommon::Auth::CredentialStore.init(aopts)
  end

  # Initialise event loop
  eopts = opts[:eventloop]
  Eventloop.init(eopts)
  # start eventloop immediately if we received a run block
  eventloop.run do
    Comm.init(copts)
    block.call(eventloop) if block
  end
end

.load_yaml(file_name, opts = {}) ⇒ Object

Load a YAML file and return it as hash.

options:

:symbolize_keys FLAG: Symbolize keys if set
:path:
   :same - Look in the same directory as '$0'
:remove_root ROOT_NAME: Remove the root node. Throw exception if not ROOT_NAME
:wait_for_readable SECS: Wait until the yaml file becomes readable. Check every SECS
:erb_process flag: Run the content of the loaded file through ERB first before YAML parsing
:erb_safe_level level: If safe_level is set to a non-nil value, ERB code will be run in a
                                separate thread with $SAFE set to the provided level.
:erb_binding binding: Optional binding given to ERB#result


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'omf_common/lib/omf_common.rb', line 204

def self.load_yaml(file_name, opts = {})
  if path_opt = opts[:path]
    case path_opt
    when :same
      file_name = File.join(File.dirname($0), file_name)
    else
      raise "Unknown value '#{path_opt}' for 'path' option"
    end
  end
  if readable_check = opts[:wait_for_readable]
    while not File.readable?(file_name)
      puts "WAIT #{file_name}"
      sleep readable_check # wait until file shows up
    end
  end

  str = File.read(file_name)
  if opts[:erb_process]
    require 'erb'
    str = ERB.new(str, opts[:erb_safe_level]).result(opts[:erb_binding] || binding)
  end
  yh = YAML.load(str)

  if opts[:symbolize_keys]
    yh = _rec_sym_keys(yh)
  end
  if root = opts[:remove_root]
    if yh.length != 1 && yh.key?(root)
      raise "Expected root '#{root}', but found '#{yh.keys.inspect}"
    end
    yh = yh.delete(root)
  end
  yh
end