-
# frozen_string_literal: true
-
-
# :reek:UtilityFunction
-
-
1
module RubyRabbitmqJanus
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
-
# # RubyRabbitmqJanus - RRJAdmin
-
#
-
# This class inherite to a classic initializer but it's used for
-
# admin request sending to janus (Admin/Monitor API).
-
#
-
# **Is used just for sending a message to Janus Monitor/Admin API.**.
-
# **The queue is always ***exclusive*** for not transmitting data to
-
# anyone.**
-
#
-
# @see https://janus.conf.meetecho.com/docs/admin.html
-
1
class RRJAdmin < RRJ
-
# Create a transaction between Apps and Janus
-
#
-
# @param [Hash] options
-
# Give a session number for use another session in Janus
-
#
-
# @example List all sessions in Janus Instance
-
# instance = { 'instance' => 42 }
-
# @rrj.admin_endpoint(instance) do |transaction|
-
# response = transaction.publish_message('admin:sessions').sessions
-
# end
-
#
-
# @example Change log level to Janus Instance
-
# instance = { 'instance' => 42 }
-
# options = instance.merge({ 'level' => 5 })
-
# @rrj.admin_endpoint(options) do |transaction|
-
# response = transaction.publish_message('admin:set_log_level', options)
-
# end
-
#
-
# @since 2.7.0
-
1
def admin_endpoint(options = {})
-
85
transaction = Janus::Transactions::Admin.new(options)
-
170
transaction.connect { yield(transaction) }
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
require 'singleton'
-
1
require 'yaml'
-
1
require 'json'
-
1
require 'securerandom'
-
1
require 'bunny'
-
1
require 'logger'
-
1
require 'key_path'
-
1
require 'erb'
-
1
require 'rrj/tools/bin/config'
-
1
require 'rrj/tools/gem/logger'
-
-
1
Log = RubyRabbitmqJanus::Tools::Logger.create unless defined?(Log)
-
1
RubyRabbitmqJanus::Tools::Logger.start
-
#
-
# :reek:UtilityFunction
-
# :reek:MissingSafeMethod { exclude: [ cleanup_connection! ] }
-
-
# Primary module for this gem
-
1
module RubyRabbitmqJanus
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
-
# # RubyRabbitmqJanus - RRJ
-
#
-
# Initialize RRJ gem and create automatically a session with janus and
-
# sending a keepalive message. The Time To Live is configured in yaml
-
# configuration file `config/default.yml` or if you a customize config in
-
# `config/ruby-rabbitmq-janus.yml`.
-
#
-
# @!attribute [r] session
-
# @return [Fixnum] Return an session number created when this gem is
-
# instanciate. It's janus who creates the number of the session.
-
#
-
# @see file:/config/requests.md For more information to type requests used.
-
# @see file:/config/default.md For more information to config file used.
-
1
class RRJ
-
1
attr_reader :session
-
-
# Return a new instance of RubyRabbitmqJanus.
-
#
-
# @example Create a instance to this gem
-
# @rrj = RubyRabbitmqJanus::RRJ.new
-
# => #<RubyRabbitmqJanus::RRJ:0x007 @session=123>
-
1
def initialize
-
187
@option = Tools::Option.new
-
end
-
-
# Create a transaction between Apps and Janus in queue public
-
#
-
# @params [Hash] options
-
# @options [String] :instance (mandatory id cluster is enabled)
-
# @options [Integer] :session_id
-
# @options [Hash] :replace
-
# @options [Hash] :add
-
#
-
# @example Create a session
-
# instance : { 'instance' => 42 }
-
# @rrj.session_endpoint_public(instance) do |transaction|
-
# transaction.publish_message('base::create')
-
# end
-
#
-
# @example Destroy session in instance
-
# options = { 'instance' => 42, 'session_id' => 71984735765 }
-
# @rrj.session_endpoint_public(options) do |transaction|
-
# transaction.publish_message('base::destroy', options)
-
# end
-
#
-
# @since 2.7.0
-
1
def session_endpoint_public(options = {})
-
10
session = @option.use_current_session?(options)
-
10
transaction = Janus::Transactions::Session.new(false, session)
-
20
transaction.connect { yield(transaction) }
-
end
-
-
# Create a transaction between Apps and Janus in queue private
-
#
-
# @params [Hash] options
-
# @options [String] :instance (mandatory id cluster is enabled)
-
# @options [Integer] :session_id
-
# @options [Hash] :replace
-
# @options [Hash] :add
-
#
-
# @example Create a session
-
# instance : { 'instance' => 42 }
-
# @rrj.session_endpoint_public(instance) do |transaction|
-
# transaction.publish_message('base::create')
-
# end
-
#
-
# @example Destroy session in instance
-
# options = { 'instance' => 42, 'session_id' => 71984735765 }
-
# @rrj.session_endpoint_public(options) do |transaction|
-
# transaction.publish_message('base::destroy', options)
-
# end
-
#
-
# @since 2.7.0
-
1
def session_endpoint_private(options = {})
-
60
session = @option.use_current_session?(options)
-
60
transaction = Janus::Transactions::Session.new(true, session)
-
120
transaction.connect { yield(transaction) }
-
end
-
-
# Create a transaction between Apps and Janus in private queue
-
#
-
# @param [Hash] options
-
# @options [String] :instance (mandatory id cluster is enabled)
-
# @options [Integer] :session_id (mandatory)
-
# @options [Integer] :handle_id
-
# @options [Hash] :replace
-
# @options [Hash] :add
-
#
-
# @example Post a offer
-
# options = {
-
# 'instance' => 42,
-
# 'session_id' => 71984735765,
-
# 'handle_id' => 56753748917,
-
# 'replace' => {
-
# 'sdp' => 'v=0\r\no=[..more sdp stuff..]'
-
# }
-
# }
-
# @rrj.handle_endpoint_public(options) do |transaction|
-
# transaction.publish_message('peer::offer', options)
-
# end
-
#
-
# @since 2.7.0
-
1
def handle_endpoint_public(options = {})
-
session = @option.use_current_session?(options)
-
handle = @option.use_current_handle?(options)
-
instance = options['instance'] || 1
-
transaction = Janus::Transactions::Handle.new(false,
-
session,
-
handle,
-
instance)
-
transaction.connect { yield(transaction) }
-
end
-
-
# Create a transaction between Apps and Janus
-
#
-
# @param [Hash] options
-
# @options [String] :instance (mandatory id cluster is enabled)
-
# @options [Integer] :session_id (mandatory)
-
# @options [Integer] :handle_id
-
# @options [Hash] :replace
-
# @options [Hash] :add
-
#
-
# @example Post a answer
-
# options = {
-
# 'instance' => 42,
-
# 'session_id' => 71984735765,
-
# 'handle_id' => 56753748917,
-
# 'replace' => {
-
# 'sdp' => 'v=0\r\no=[..more sdp stuff..]'
-
# }
-
# }
-
# @rrj.handle_endpoint_private(options) do |transaction|
-
# transaction.publish_message('peer::answer', options)
-
# end
-
#
-
# @since 2.7.0
-
1
def handle_endpoint_private(options = {})
-
32
session = @option.use_current_session?(options)
-
32
handle = @option.use_current_handle?(options)
-
32
instance = options['instance'] || 1
-
32
transaction = Janus::Transactions::Handle.new(true,
-
session,
-
handle,
-
instance)
-
64
transaction.connect { yield(transaction) }
-
end
-
-
# Delete all resources to JanusInstance reference.
-
# Warning: All data in database and Janus Instance is delete
-
#
-
# @since 2.1.0
-
1
def cleanup_connection!
-
Models::JanusInstance.destroys
-
end
-
-
1
private
-
-
1
attr_reader :option
-
end
-
end
-
# frozen_string_literal: true
-
-
1
require 'rrj/janus/messages/message'
-
1
require 'rrj/janus/transactions/transaction'
-
-
1
require 'rrj/janus/responses/response'
-
1
require 'rrj/janus/responses/standard'
-
1
require 'rrj/janus/responses/admin'
-
1
require 'rrj/janus/responses/error'
-
1
require 'rrj/janus/responses/event'
-
1
require 'rrj/janus/responses/rspec'
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Janus
-
1
module Messages
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# Create an message for janus
-
1
class Admin < Message
-
1
def initialize(template_request, options = {})
-
87
super(template_request, options)
-
end
-
-
# Return options to message for rabbitmq
-
1
def options
-
87
properties.options_admin
-
end
-
-
1
private
-
-
1
def prepare_request(options)
-
87
@request = Tools::Replaces::Admin.new(request,
-
options).transform_request
-
87
super
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Janus
-
# Modules for create message for Janus
-
1
module Messages
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# # Create a message for janus.
-
#
-
# Create a message, in hash format, and sending to json format.
-
# It's loading file base and change elements and configure message for
-
# used in rabbitmq.
-
#
-
# @!attribute [r] type
-
# @return [String]
-
# Type to request sending ('base::info', 'peer::trickle')
-
#
-
# @see file:/config/requests.md For more information to type requests
-
# used.
-
1
class Message
-
1
attr_reader :type
-
-
# Instanciate an message
-
#
-
# @param template_request [String] Name of request prepare
-
# @param [Hash] options Options to request
-
# @option options [String] :session_id Identifier to session
-
# @option options [String] :handle_id Identifier to session manipulate
-
# @option options [Hash] :other Element contains in request sending
-
#
-
# @example Initialize a message
-
# Message.new('test', {
-
# "session_id": 42,
-
# "handle_id": 42,
-
# "replace": {
-
# "audio": false,
-
# "video": true
-
# },
-
# "add": {
-
# "subtitle": true
-
# })
-
# def initialize(template_request, options = { 'instance' => 1 })
-
1
def initialize(template_request, options = {})
-
225
@request = {}
-
225
@type = template_request
-
225
@properties = Rabbit::Propertie.new(options['instance'])
-
225
load_request_file
-
225
prepare_request(options)
-
end
-
-
# Return request to json format
-
#
-
# @return [String] Request to JSON format
-
1
def to_json(*_args)
-
668
@request.to_json
-
end
-
-
# Return request to json format with nice format
-
#
-
# @return [String] Request to JSON format with indent
-
1
def to_nice_json
-
1
JSON.pretty_generate to_hash
-
end
-
-
# Return request to hash format
-
#
-
# @return [Hash] Request to Hash format
-
1
def to_hash
-
2
@request
-
end
-
-
# Return correlation to message
-
#
-
# @return [String] Correlation string
-
1
def correlation
-
208
@properties.correlation
-
end
-
-
1
private
-
-
1
attr_accessor :properties, :request
-
-
1
def load_request_file
-
225
@request = request_instance
-
225
::Log.debug "Opening request : #{to_json}"
-
end
-
-
1
def prepare_request(_options)
-
225
::Log.debug "Prepare request for janus : #{to_json}"
-
end
-
-
1
def request_instance
-
225
JSON.parse File.read Tools::Requests.instance.requests[@type]
-
end
-
-
1
def find_instance(options)
-
if options.key?('instance')
-
options['instance']
-
else
-
Models::JanusInstance.find_by_session(options['session_id'])
-
end
-
end
-
end
-
end
-
end
-
end
-
-
1
require 'rrj/janus/messages/standard'
-
1
require 'rrj/janus/messages/admin'
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Janus
-
1
module Messages
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# # Create an standard message
-
1
class Standard < Message
-
1
def initialize(template_request, options = {})
-
134
super(template_request, options)
-
end
-
-
# Return options to message for rabbitmq
-
1
def options
-
134
properties.options
-
end
-
-
1
private
-
-
1
def prepare_request(options)
-
134
@request = Tools::Replaces::Handle.new(request,
-
options).transform_request
-
134
super
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Janus
-
1
module Responses
-
# Response for admin request
-
#
-
# @see Example request response https://janus.conf.meetecho.com/docs/admin.html
-
1
class Admin < Standard
-
# @return [Array] List of sessions running in Janus Instance.
-
1
def sessions
-
3
read_data(__method__.to_s)
-
end
-
-
# @return [Array] List of handles running
-
# in one session in Janus Instance.
-
1
def handles
-
2
read_data(__method__.to_s)
-
end
-
-
# @return [Hash] Information to session/handle in Janus Instance.
-
1
def info
-
3
read_data(__method__.to_s)
-
end
-
-
# @return [Boolean] Information status to debug mode for libnice.
-
1
def libnice_debug
-
3
read_data(__method__.to_s)
-
end
-
-
# @return [Boolean] Information status to debug mode
-
# in Janus Intance on the fly.
-
1
def locking_debug
-
3
read_data(__method__.to_s)
-
end
-
-
# @return [Boolean] Information about color in log messages.
-
1
def log_colors
-
3
read_data(__method__.to_s)
-
end
-
-
# @return [Integer] Level to debug mode to Janus Instance.
-
1
def level
-
10
read_data(__method__.to_s)
-
end
-
-
# @return [Boolean] Status to timestampping for log messages.
-
1
def log_timestamps
-
3
read_data(__method__.to_s)
-
end
-
-
# @return [Integer] Level to max nack queue configured.
-
1
def max_nack_queue
-
4
read_data(__method__.to_s)
-
end
-
-
# @return [Integer] No-media timer property.
-
1
def no_media_timer
-
4
read_data(__method__.to_s)
-
end
-
-
# @return [Integer] Timeout for session.
-
1
def timeout
-
4
read_data(__method__.to_s)
-
end
-
-
1
private
-
-
1
def read_data(key)
-
42
raise build_exception(key) unless key?(key)
-
-
31
request[key]
-
end
-
-
1
def build_exception(key)
-
11
"RubyRabbitmqJanus::Errors::Janus::Responses::Admin::#{key.camelize}"
-
.constantize
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
# rubocop:disable Layout/LineLength
-
-
1
module RubyRabbitmqJanus
-
1
module Janus
-
1
module Responses
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# @since 3.0.0
-
#
-
# Manage exception to response Janus
-
1
class Errors
-
# Unauthorized (can only happen when using apisecret/auth token)
-
1
def _403(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::Unauthorized, request
-
end
-
-
# Unauthorized access to a plugin (can only
-
# happen when using auth token)
-
1
def _405(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::UnauthorizedPlugin, request
-
end
-
-
# Transport related error
-
1
def _450(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::TransportSpecific, request
-
end
-
-
# The request is missing in the message
-
1
def _452(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::MissingRequest, request
-
end
-
-
# The Janus core does not suppurt this request
-
1
def _453(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::UnknownRequest, request
-
end
-
-
# The payload is not a valid JSON message
-
1
def _454(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::InvalidJSON, request
-
end
-
-
# The object is not a valid JSON object as expected
-
1
def _455(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::InvalidJSONObject, request
-
end
-
-
# A mandatory element is missing in the message
-
1
def _456(request)
-
4
raise RubyRabbitmqJanus::Errors::Janus::Responses::MissingMandatoryElement, request
-
end
-
-
# The request cannot be handled for this webserver path
-
1
def _457(request)
-
14
raise RubyRabbitmqJanus::Errors::Janus::Responses::InvalidRequestPath, request
-
end
-
-
# The session the request refers to doesn't exist
-
1
def _458(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::SessionNotFound, request
-
end
-
-
# The handle the request refers to doesn't exist
-
1
def _459(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::HandleNotFound, request
-
end
-
-
# The plugin the request wants to talk to doesn't exist
-
1
def _460(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::PluginNotFound, request
-
end
-
-
# An error occurring when trying to attach to
-
# a plugin and create a handle
-
1
def _461(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::PluginAttach, request
-
end
-
-
# An error occurring when trying to send a message/request to the plugin
-
1
def _462(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::PluginMessage, request
-
end
-
-
# brief An error occurring when trying to detach from
-
# a plugin and destroy the related handle
-
1
def _463(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::PluginDetach, request
-
end
-
-
# The Janus core doesn't support this SDP type
-
1
def _464(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::JSEPUnknownType, request
-
end
-
-
# The Session Description provided by the peer is invalid
-
1
def _465(request)
-
5
raise RubyRabbitmqJanus::Errors::Janus::Responses::JSEPInvalidSDP, request
-
end
-
-
# The stream a trickle candidate for does not exist or is invalid
-
1
def _466(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::TrickleInvalidStream, request
-
end
-
-
# A JSON element is of the wrong type
-
# (e.g., an integer instead of a string)
-
1
def _467(request)
-
6
raise RubyRabbitmqJanus::Errors::Janus::Responses::InvalidElementType, request
-
end
-
-
# The ID provided to create a new session is already in use
-
1
def _468(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::SessionConflit, request
-
end
-
-
# We got an ANSWER to an OFFER we never made
-
1
def _469(request)
-
3
raise RubyRabbitmqJanus::Errors::Janus::Responses::UnexpectedAnswer, request
-
end
-
-
# The auth token the request refers to doesn't exist
-
1
def _470(request)
-
3
raise RubyRabbitmqJanus::Errors::Janus::Responses::TokenNotFound, request
-
end
-
-
# The current request cannot be handled because
-
# of not compatible WebRTC state
-
1
def _471(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::WebRTCState, request
-
end
-
-
# The server is currently configured not to accept new sessions
-
1
def _472(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::NotAcceptingSession, request
-
end
-
-
# Unknown/undocumented error
-
1
def _490(request)
-
9
raise RubyRabbitmqJanus::Errors::Janus::Responses::Unknown, request
-
end
-
-
1
def respond_to_missing?(name, include_private); end
-
-
1
def method_missing(_method, request)
-
1
default_error(request)
-
end
-
-
1
def default_error(request)
-
1
raise RubyRabbitmqJanus::Errors::Janus::Responses::Nok, request
-
end
-
end
-
end
-
end
-
end
-
# rubocop:enable Layout/LineLength
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Janus
-
# Modules for manipulate responses sending by Janus
-
1
module Responses
-
# Response for events message
-
1
class Event < Standard
-
# Return event to message
-
#
-
# @example Januse response
-
# request.event #=> 'success'
-
#
-
# @return [String] result to request
-
1
def event
-
raise RubyRabbitmqJanus::Errors::Janus::Responses::Event::Event \
-
2
unless key?('janus')
-
-
1
request['janus']
-
end
-
-
# Read plugindata data
-
#
-
# @example Plugindata data
-
# request.data #=> { 'data': { 'audio': false } }
-
#
-
# @return [Hash] body data
-
1
def data
-
raise RubyRabbitmqJanus::Errors::Janus::Responses::Event::Data \
-
2
unless plugin_response?
-
-
1
request['plugindata']['data']
-
end
-
-
# Read jsep data
-
#
-
# @example Data to jsep
-
# request.jsep #=> { 'jsep': { 'type': '...', 'sdp': '...' } }
-
#
-
# @return [Hash] jsep data
-
1
def jsep
-
raise RubyRabbitmqJanus::Errors::Janus::Responses::Event::Jsep \
-
2
unless key?('jsep')
-
-
1
request['jsep']
-
end
-
-
# session_id and handle_id
-
#
-
# @example Data to any request
-
# request.keys #=> [123456789, 987654321]
-
#
-
# @return [Array] Contains session_id and handle_id
-
1
def keys
-
raise RubyRabbitmqJanus::Errors::Janus::Responses::Event::Keys \
-
3
unless key?('session_id') && key?('sender')
-
-
1
[request['session_id'], request['sender']]
-
end
-
-
1
private
-
-
1
def plugin_response?
-
2
request.key?('plugindata') && request['plugindata'].key?('data')
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Janus
-
# Modules for manipulate responses sending by Janus
-
1
module Responses
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# # Read and parse a response to janus.
-
#
-
# Read a message in rabbitmq queue. This message is formatted to JSON
-
# or Hash format. For developpment it's possible to used a `nice` JSON.
-
1
class Response
-
# Instantiate a response
-
#
-
# @param [Hash] response_janus
-
# Request parsing after Janus/RabbitMQ receive a response to request
-
# sending by user
-
1
def initialize(response_janus)
-
298
@request = response_janus
-
-
298
errors if error?
-
235
bad_request if bad_request?
-
end
-
-
# Return request to json format
-
#
-
# @return [String] Response to JSON format
-
1
def to_json(*_args)
-
50
@request.to_json
-
end
-
-
# Return request to json format with nice format
-
#
-
# @return [String] Response to JSON format with indent
-
1
def to_nice_json
-
1
JSON.pretty_generate to_hash
-
end
-
-
# Return request to hash format
-
#
-
# @return [Hash] Response to Hash format
-
1
def to_hash
-
2
@request
-
end
-
-
# Return request error code
-
#
-
# @return [Integer] Code error
-
1
def error_code
-
127
@request['error']['code'].to_i
-
end
-
-
# Return request error reason
-
#
-
# @return [String] Reason error
-
1
def error_reason
-
64
@request['error']['reason']
-
end
-
-
# Read field Janus in response message
-
1
def janus
-
5
request['janus']
-
end
-
-
1
private
-
-
1
def key?(value)
-
204
@request.key?(value)
-
end
-
-
1
def error?
-
298
@request.key?('janus') && @request['janus'].match?('error')
-
end
-
-
1
def bad_request
-
klass = RubyRabbitmqJanus::Janus::Responses::Errors.new
-
klass.default_error(999, self)
-
end
-
-
1
def bad_request?
-
235
@request.nil?
-
end
-
-
1
def errors
-
63
klass = RubyRabbitmqJanus::Janus::Responses::Errors.new
-
63
klass.send("_#{error_code}", self)
-
end
-
-
1
attr_accessor :request
-
end
-
end
-
end
-
end
-
-
1
require 'rrj/janus/responses/error'
-
1
require 'rrj/janus/responses/standard'
-
1
require 'rrj/janus/responses/admin'
-
1
require 'rrj/janus/responses/event'
-
1
require 'rrj/janus/responses/rspec'
-
# frozen_string_literal: true
-
-
# :reek:UtilityFunction
-
-
1
module RubyRabbitmqJanus
-
1
module Janus
-
1
module Responses
-
# Response for RSpec initializer
-
1
class RSpec
-
# Constructor to RSpec response.
-
# Create a fake response for testing library.
-
1
def initialize(type)
-
path = RubyRabbitmqJanus::Tools::Config.instance.rspec_response
-
@json = File.join(Dir.pwd,
-
path,
-
"#{type.gsub('::', '_')}.json")
-
end
-
-
# Read response json file
-
1
def read
-
JSON.parse(File.read(@json))
-
end
-
-
# Create fake session number
-
1
def session
-
(rand * 1_000_000).to_i
-
end
-
-
# Read plugindata field
-
1
def plugin
-
read['plugindata']
-
end
-
-
# Read data to plugindata field
-
1
def plugin_data
-
read['plugindata']['data']
-
end
-
-
# Read data dield
-
1
def data
-
read['data']
-
end
-
-
# Read sdp
-
1
def sdp
-
read['jsep']
-
end
-
-
# Read sessions field
-
1
def sessions
-
read['sessions']
-
end
-
-
# Read info field
-
1
def info
-
read['info']
-
end
-
-
# Read fake keys
-
1
def keys
-
[546_321_963, 546_321_966]
-
end
-
-
# Read first Janusinstance in database
-
1
def instance
-
JanusInstance.first
-
end
-
-
# Read fake status to instance
-
#
-
# @return [Boolean] Random value
-
1
def enable
-
[True, False].sample
-
end
-
-
1
alias id session
-
1
alias session_id session
-
1
alias sender session
-
1
alias handles sessions
-
1
alias jsep sdp
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
# rubocop:disable Layout/LineLength
-
-
1
module RubyRabbitmqJanus
-
1
module Janus
-
# Modules for manipulate responses sending by Janus
-
1
module Responses
-
# Response for events message
-
1
class Standard < Response
-
# Return a integer to session
-
1
def session
-
39
raise_data
-
-
raise RubyRabbitmqJanus::Errors::Janus::Responses::Standard::Session \
-
38
unless request['data'].key?('id')
-
-
37
data_id
-
end
-
-
# Read value created by janus for session/handle message
-
1
def sender
-
33
raise_data
-
-
raise RubyRabbitmqJanus::Errors::Janus::Responses::Standard::Sender \
-
32
unless request['data'].key?('id')
-
-
31
data_id
-
end
-
-
# Return session used in request
-
1
def session_id
-
raise RubyRabbitmqJanus::Errors::Janus::Responses::Standard::SessionId \
-
2
unless key?('session_id')
-
-
1
request['session_id']
-
end
-
-
# Read response for plugin request
-
1
def plugin
-
raise RubyRabbitmqJanus::Errors::Janus::Responses::Standard::Plugin \
-
3
unless key?('plugindata')
-
-
2
request['plugindata']
-
end
-
-
# Read data response for plugin request
-
1
def plugin_data
-
raise RubyRabbitmqJanus::Errors::Janus::Responses::Standard::Plugin \
-
3
unless key?('plugindata')
-
-
raise RubyRabbitmqJanus::Errors::Janus::Responses::Standard::PluginData \
-
2
unless request['plugindata'].key?('data')
-
-
1
plugin['data']
-
end
-
-
# Read data response for normal request
-
1
def data
-
70
raise_data
-
-
69
request['data']
-
end
-
-
# Read SDP response
-
1
def sdp
-
raise RubyRabbitmqJanus::Errors::Janus::Responses::Standard::JSEP \
-
3
unless key?('jsep')
-
-
raise RubyRabbitmqJanus::Errors::Janus::Responses::Standard::SDP \
-
2
unless jsep.key?('sdp')
-
-
1
jsep['sdp']
-
end
-
-
1
private
-
-
1
def raise_data
-
3
raise RubyRabbitmqJanus::Errors::Janus::Responses::Standard::Data \
-
142
unless key?('data')
-
end
-
-
1
def jsep
-
3
request['jsep']
-
end
-
-
1
def data_id
-
68
data['id'].to_i
-
end
-
end
-
end
-
end
-
end
-
# rubocop:enable Layout/LineLength
-
# frozen_string_literal: true
-
-
# :reek:UncommunicativeMethodName
-
-
1
module RubyRabbitmqJanus
-
1
module Janus
-
1
module Transactions
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
-
# This class work with janus and send a series of message
-
1
class Admin < Session
-
1
def initialize(session)
-
85
super(true, session)
-
end
-
-
# Begin connection with RabbitMQ
-
1
def connect
-
85
rabbit.transaction_short do
-
85
@publisher = Rabbit::Publisher::Admin.new(rabbit.channel)
-
85
yield
-
end
-
end
-
-
# Write a message in queue in RabbitMQ
-
1
def publish_message(type, options = {})
-
85
msg = Janus::Messages::Admin.new(type, options.merge(opts2))
-
85
response = read_response(publisher.publish(msg))
-
85
Janus::Responses::Admin.new(response)
-
end
-
-
1
private
-
-
1
def send_a_message
-
Janus::Responses::Admin.new(publisher.publish(yield))
-
end
-
-
1
def admin_secret
-
85
Tools::Config.instance.options['rabbit']['admin_pass']
-
end
-
-
1
def opts
-
{ 'session_id' => session, 'admin_secret' => admin_secret }
-
end
-
-
1
def opts2
-
85
session.merge('admin_secret' => admin_secret)
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
# :reek:TooManyStatements
-
-
1
module RubyRabbitmqJanus
-
1
module Janus
-
1
module Transactions
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# This class work with janus and send a series of message
-
1
class Handle < Transaction
-
# Initialize a transaction with handle
-
#
-
# @param [Fixnum] session
-
# Use a session identifier for created message
-
1
def initialize(exclusive, session, handle = 0, instance = 1)
-
32
super(session)
-
32
@exclusive = exclusive
-
32
@handle = handle
-
32
@instance = instance
-
end
-
-
# Opening a long transaction with rabbitmq and is ending closing
-
# transaction, so delete exclusive queue
-
#
-
# @yield Send a message to Janus
-
#
-
# @return [Fixnum] Sender using in current transaction
-
1
def connect
-
32
rabbit.transaction_short do
-
32
choose_queue
-
32
create_handle if @handle.eql?(0)
-
32
yield
-
end
-
32
handle
-
end
-
-
# Publish an message in handle
-
#
-
# @param [String] type Request file used
-
# @param [Hash] options Replace/add element in request
-
#
-
# @return [Janus::Responses::Standard] Response to message sending
-
1
def publish_message(type, options = {})
-
32
msg = Janus::Messages::Standard.new(type, options.merge(opts))
-
32
response = read_response(publisher.publish(msg))
-
32
Janus::Responses::Standard.new(response)
-
end
-
-
# Send a message detach
-
1
def detach
-
options = opts.merge('instance' => @instance)
-
::Log.debug "Detach handle #{options}"
-
publisher.publish(Janus::Messages::Standard.new('base::detach',
-
options))
-
end
-
-
# Send a message detach and disable session for deleting in
-
# Janus Instance
-
1
def detach_for_deleting
-
detach
-
Models::JanusInstance.disable(opts['session_id'])
-
end
-
-
1
private
-
-
1
def create_handle
-
30
opt = { 'session_id' => session, 'instance' => @instance }
-
30
msg = Janus::Messages::Standard.new('base::attach', opt)
-
60
@handle = send_a_message_exclusive { msg }
-
end
-
-
# rubocop:disable Style/ExplicitBlockArgument
-
1
def send_a_message_exclusive
-
30
Janus::Responses::Standard.new(read_response_exclusive do
-
30
yield
-
end).sender
-
end
-
# rubocop:enable Style/ExplicitBlockArgument
-
-
1
def read_response_exclusive
-
30
chan = rabbit.channel
-
30
tmp_publish = Rabbit::Publisher::Exclusive.new(chan, '')
-
30
tmp_publish.publish(yield)
-
end
-
-
1
def opts
-
32
{ 'session_id' => session, 'handle_id' => @handle }
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Janus
-
1
module Transactions
-
# Transaction for RSpec initializer
-
# @!attribute [r] response
-
# Given a Janus response
-
1
class RSpec
-
1
attr_reader :response
-
-
# Initialize fake object
-
1
def initialize
-
@response = nil
-
end
-
-
# Fake method
-
1
def connect; end
-
-
# Fake method
-
1
def detach; end
-
-
# Fake method
-
1
def detach_for_deleting; end
-
-
# Publish fake message
-
1
def publish_message(type, _options)
-
@response = Janus::Responses::RSpec.new(type)
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Janus
-
1
module Transactions
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# # Manage a transaction
-
#
-
# Manage a transaction with message if contains a session identifier
-
1
class Session < Transaction
-
# Initialize a transaction with handle
-
#
-
# @param [Fixnum] session
-
# Use a session identifier for created message
-
1
def initialize(exclusive, session)
-
155
super(session)
-
155
@exclusive = exclusive
-
end
-
-
# Opening a short transaction with rabbitmq and close when is ending
-
#
-
# @yield Send a message to Janus
-
1
def connect
-
70
rabbit.transaction_short do
-
70
choose_queue
-
70
yield
-
end
-
end
-
-
# Publish a message to "standard" RabbitMQ queue.
-
1
def publish_message(type, options = {})
-
70
msg = Janus::Messages::Standard.new(type, opts.merge!(options))
-
70
response = read_response(publisher.publish(msg))
-
70
Janus::Responses::Standard.new(response)
-
end
-
-
1
private
-
-
1
def opts
-
70
{ 'session_id' => session }
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Janus
-
# Define an module for manipulate messages between apps and Janus
-
1
module Transactions
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# # Manage a transactions
-
#
-
# This class work with Janus and send a series of message
-
1
class Transaction
-
# Initialize a transaction
-
#
-
# @param [Fixnum] session
-
# Use a session identifier for created message
-
1
def initialize(session)
-
187
@rabbit = RubyRabbitmqJanus::Rabbit::Connect.new
-
187
@session = session
-
187
@publisher = @exclusive = nil
-
end
-
-
1
private
-
-
1
attr_reader :rabbit, :session, :response, :handle, :publisher,
-
:exclusive
-
-
1
def choose_queue
-
102
chan = @rabbit.channel
-
102
@publisher = if @exclusive
-
92
::Log.debug \
-
'Choose an queue Exclusive : ampq.gen-xxx'
-
92
Rabbit::Publisher::Exclusive.new(chan, '')
-
else
-
10
::Log.debug \
-
'Choose an queue non Exclusive : to-janus'
-
10
Rabbit::Publisher::NonExclusive.new(chan)
-
end
-
end
-
-
1
def send_a_message
-
::Log.info 'Publish a message ...'
-
response = read_response(@publisher.publish(yield))
-
Janus::Responses::Standard.new(response)
-
end
-
-
1
def read_response(publish)
-
187
@exclusive ? publish : {}
-
end
-
end
-
end
-
end
-
end
-
-
1
require 'rrj/janus/transactions/session'
-
1
require 'rrj/janus/transactions/handle'
-
1
require 'rrj/janus/transactions/admin'
-
1
require 'rrj/janus/transactions/rspec'
-
# frozen_string_literal: true
-
-
# :reek:UtilityFunction
-
-
1
module RubyRabbitmqJanus
-
1
module Models
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# Store instance information for MongoID database
-
1
class JanusInstance < ::ActiveRecord::Base
-
1
include RubyRabbitmqJanus::Models::Instances
-
1
include RubyRabbitmqJanus::Models::Validations
-
-
1
self.primary_key = :id
-
-
1
alias_attribute :instance, :id
-
1
alias_attribute :title, :name
-
1
alias_attribute :session_id, :session
-
-
# Update attributes to document
-
#
-
# @param attributes [Hash] List of attribute to update with this value
-
#
-
# @return [Hash] Current model
-
1
def set(attributes)
-
4
update_columns(attributes)
-
end
-
-
# Destroy data to column
-
#
-
# @param attributes [Array] List to attribute to delete in document
-
#
-
# @return [Hash] Current model
-
#
-
# rubocop:disable Style/HashTransformValues
-
1
def unset(attributes)
-
Hash[attributes.map { |key, _value| [key, nil] }]
-
end
-
# rubocop:enable Style/HashTransformValues
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
# :reek:UtilityFunction
-
-
1
module RubyRabbitmqJanus
-
1
module Models
-
# Add class methods for JanusInstance model
-
1
module Instances
-
1
extend ActiveSupport::Concern
-
-
# Class methods for Janus Instance model
-
1
module ClassMethods
-
# Disable an instance
-
1
def disable(session_id)
-
2
find_by_session(session_id).set(enable: false)
-
end
-
-
# Enable an instance
-
1
def enable(session_id)
-
2
find_by_session(session_id).set(enable: true)
-
end
-
-
# Clean all instance disabled
-
1
def destroys
-
2
where(enable: false).delete_all
-
end
-
-
# Search a record by instance number
-
1
def find_by_instance(instance_search)
-
3
find_by(instance: instance_search)
-
end
-
-
# Search a record by session number
-
1
def find_by_session(session_search)
-
14
find_by(session_id: session_search)
-
end
-
-
# Get all instance active
-
1
def enabled
-
4
where(enable: true)
-
end
-
-
# Get all instance not active
-
1
def disabled
-
2
where(enable: false)
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Models
-
# Configure validation for Janus Instance model
-
1
module Validations
-
1
extend ActiveSupport::Concern
-
-
1
included do
-
# This instance it's a state (enable or disable)
-
1
validates :enable, inclusion: { in: [true, false] }
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Process
-
# Modules for create autonomous process
-
1
module Concurrencies
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# # Class for manage threads
-
#
-
# @abstract Manage thread for listener to queue :
-
# from-janus
-
# from-janus-admin
-
1
class Concurrency
-
# Initialize class with elements for thread communication
-
1
def initialize
-
::Log.info info_thread
-
@rabbit = RubyRabbitmqJanus::Rabbit::Connect.new
-
@lock = Mutex.new
-
@condition = ConditionVariable.new
-
end
-
-
1
private
-
-
1
attr_reader :lock, :condition, :rabbit
-
-
1
def initialize_thread
-
@rabbit.transaction_long { transaction_running }
-
rescue Interrupt
-
::Log.warn "This process has been interupted #{class_name}"
-
::Log.warn \
-
"Close a connection with RabbitMQ instance for #{class_name}"
-
@rabbit.close
-
end
-
-
1
def info_thread
-
"Create an thread -- #{class_name}"
-
end
-
-
1
def class_name
-
self.class.name
-
end
-
end
-
end
-
end
-
end
-
-
1
require 'rrj/process/event'
-
1
require 'rrj/process/event_admin'
-
# frozen_string_literal: true
-
-
1
require 'rrj/process/thread_runner_concern'
-
-
1
module RubyRabbitmqJanus
-
1
module Process
-
1
module Concurrencies
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# # Listen public queue to all Janus instance
-
#
-
# Listen standard queue and sending a block code to thread listen.
-
# The default queue is configured in config file.
-
#
-
# @see file:/config/default.md For more information to config file used.
-
1
class Event < Concurrency
-
1
include RubyRabbitmqJanus::Process::Concurrencies::ThreadRunnerConcern
-
-
1
private
-
-
1
def name_publisher
-
:pub_classic
-
end
-
-
1
def publisher
-
Rabbit::Listener::From.new(rabbit)
-
end
-
-
1
def raise_nil_block
-
raise RubyRabbitmqJanus::Errors::Process::Event::Run
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
require 'rrj/process/thread_runner_concern'
-
-
1
module RubyRabbitmqJanus
-
1
module Process
-
1
module Concurrencies
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# # Listen admin queue to all Janus instance
-
#
-
# Listen admin queue and sending a block code to thread listen.
-
# The default queue is configured in config file.
-
#
-
# @see file:/config/default.md For more information to config file used.
-
1
class EventAdmin < Concurrency
-
1
include RubyRabbitmqJanus::Process::Concurrencies::ThreadRunnerConcern
-
-
1
private
-
-
1
def name_publisher
-
:pub_admin
-
end
-
-
1
def publisher
-
Rabbit::Listener::FromAdmin.new(rabbit)
-
end
-
-
1
def raise_nil_block
-
raise RubyRabbitmqJanus::Errors::Process::EventAdmin::Run
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
require 'active_support/concern'
-
-
1
module RubyRabbitmqJanus
-
1
module Process
-
1
module Concurrencies
-
# Module injected in Concurrencies classes.
-
#
-
# Manage threads for public/admin queue to rabbitmq.
-
#
-
# :reek:ModuleInitialize
-
1
module ThreadRunnerConcern
-
1
extend ActiveSupport::Concern
-
-
1
included do
-
# Initialize an process (event/event_admin) and configure
-
# the life cycle to thread.
-
2
def initialize
-
super
-
@thread = Thread.new { initialize_thread }
-
end
-
-
# Create a thread for execute a block code in a thread.
-
# This code is outside to `RRJ` gem so is very important
-
# to be sure this code execution is not fail.
-
#
-
# @param [Proc] block Block code for execute action when queue
-
# standard 'from-janus' receive a message.This block is sending to
-
# publisher created for this thread.
-
#
-
# @return [Thread] It's a thread who listen queue and execute action
-
2
def run(&block)
-
raise_nil_block unless block
-
-
@thread.join
-
Thread.new do
-
loop do
-
@thread.thread_variable_get(name_publisher)
-
.listen_events(&block)
-
rescue => exception
-
::Log.warn exception
-
end
-
end
-
end
-
-
2
private
-
-
2
def transaction_running
-
@thread.thread_variable_set(name_publisher, publisher)
-
end
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
require 'semaphore'
-
-
1
module RubyRabbitmqJanus
-
1
module Rabbit
-
# Define an module for create an publisher
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
-
# # Parent class for all publisher
-
# This element send and read a message in rabbitmq Queue
-
#
-
# @!attribute [r] responses
-
# @return [RubyRabbitmqJanus::Janus::Responses::Response]
-
# Given an array of Janus response
-
#
-
# @abstract Publish message in RabbitMQ
-
1
class BaseEvent
-
1
attr_reader :responses
-
-
# Define a base publisher
-
1
def initialize
-
218
@responses = []
-
218
@semaphore = Semaphore.new
-
218
@lock = Mutex.new
-
end
-
-
1
private
-
-
1
attr_accessor :semaphore, :lock
-
-
1
def return_response
-
207
@semaphore.wait
-
207
response = nil
-
207
@lock.synchronize do
-
207
response = @responses.shift
-
end
-
207
response
-
end
-
end
-
end
-
end
-
-
1
require 'rrj/rabbit/publisher/base'
-
1
require 'rrj/rabbit/listener/base'
-
# frozen_string_literal: true
-
-
1
require 'timeout'
-
-
1
module RubyRabbitmqJanus
-
1
module Rabbit
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# Class for manage connection with RabbitMQ
-
1
class Connect
-
# Initialize connection to server RabbitMQ
-
1
def initialize
-
190
@rabbit = Bunny.new(bunny_conf)
-
end
-
-
# Create an transaction with rabbitmq and close after response is received
-
38
def transaction_short(&block)
-
187
raise Errors::Rabbit::Connect::MissingAction unless block
-
-
187
response = nil
-
-
187
Timeout.timeout(5) do
-
187
response = transaction_long(&block)
-
end
-
rescue Timeout::Error
-
raise Errors::Rabbit::Connect::TransactionTimeout, \
-
'The "Short transaction" have raised Timeout exception.'
-
ensure
-
187
close
-
response
-
150
end
-
-
# Create an transaction with rabbitmq and not close
-
1
def transaction_long
-
187
raise Errors::Rabbit::Connect::MissingAction unless block_given?
-
-
187
Timeout.timeout(60) do
-
187
start
-
187
yield
-
end
-
rescue Timeout::Error
-
raise Errors::Rabbit::Connect::TransactionTimeout, \
-
'The "Long transaction" have raised Timeout exception.'
-
end
-
-
# Opening a connection with RabbitMQ
-
1
def start
-
188
@rabbit.start
-
end
-
-
# Close connection to server RabbitMQ
-
1
def close
-
189
@rabbit.close
-
end
-
-
# Create an channel
-
1
def channel
-
217
@rabbit.create_channel
-
end
-
-
1
private
-
-
1
def bunny_conf
-
190
Tools::Config.instance.server_settings.merge(bunny_conf_static)
-
end
-
-
1
def bunny_conf_static
-
{
-
190
connection_timeout: 5,
-
connection_name: "[#{rand(999)}] backend",
-
recover_from_connection_close: false
-
}
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
# :reek:NilCheck
-
-
1
module RubyRabbitmqJanus
-
1
module Rabbit
-
1
module Listener
-
# Base for listeners
-
1
class Base < RubyRabbitmqJanus::Rabbit::BaseEvent
-
# Define an publisher
-
#
-
# @param [String] rabbit Information connection to RabbitMQ server
-
1
def initialize(rabbit)
-
super()
-
@rabbit = rabbit.channel
-
subscribe_queue
-
end
-
-
# Listen a queue and return a body response
-
1
def listen_events
-
semaphore.wait
-
response = nil
-
lock.synchronize do
-
response = responses.shift
-
check(response)
-
end
-
yield response.event, response
-
end
-
-
1
private
-
-
1
attr_accessor :rabbit, :responses
-
-
1
def binding
-
@rabbit.direct('amq.direct')
-
end
-
-
1
def opts_subs
-
{ block: false, manual_ack: false, arguments: { 'x-priority': 2 } }
-
end
-
-
1
def info_subscribe(info, _prop, payload)
-
::Log.debug info
-
::Log.debug '[X] Message reading'
-
::Log.info payload
-
end
-
-
1
def check(response)
-
raise Errors::Rabbit::Listener::ResponseNil, response \
-
if response.nil?
-
raise Errors::Rabbit::Listener::ResponseEmpty, response \
-
if response.to_hash.size.zero?
-
end
-
-
1
def subscribe_queue
-
rabbit.prefetch(1)
-
reply.bind(binding).subscribe(opts_subs) do |info, prop, payload|
-
info_subscribe(info, prop, payload)
-
synchronize_response(payload)
-
end
-
end
-
-
1
def synchronize_response(payload)
-
lock.synchronize do
-
response = response_class(payload)
-
responses.push(response)
-
end
-
semaphore.signal
-
end
-
end
-
end
-
end
-
end
-
-
1
require 'rrj/rabbit/listener/from'
-
1
require 'rrj/rabbit/listener/from_admin'
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Rabbit
-
1
module Listener
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# This publisher don't post message. Is listen just an standard queue to
-
# Janus. By default is "from-janus". It's a parameter in config to this
-
# gem.
-
1
class From < Base
-
1
private
-
-
1
def reply
-
rabbit.queue(Tools::Config.instance.queue_from)
-
end
-
-
1
def response_class(payload)
-
Janus::Responses::Event.new(JSON.parse(payload))
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Rabbit
-
1
module Listener
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# This publisher don't post message. Is listen just an admin queue to
-
# Janus. By default is "from-janus-admin". It's a parameter in config
-
# to this gem.
-
1
class FromAdmin < From
-
1
private
-
-
1
def reply
-
rabbit.queue(Tools::Config.instance.queue_admin_from)
-
end
-
-
1
def response_class(payload)
-
Janus::Responses::Admin.new(JSON.parse(payload))
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Rabbit
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
-
# @!attribute [r] correlation
-
# @return [String] Is a string uniq generated by SecureRandom
-
#
-
# Manage properties to message sending in rabbitmq queue
-
1
class Propertie
-
1
attr_reader :correlation
-
-
# Initialize a message sending to rabbitmq
-
1
def initialize(instance = 1)
-
229
::Log.debug 'initalize a propertie to message'
-
229
@correlation = SecureRandom.uuid
-
229
@instance = instance
-
end
-
-
# Define options sending to RabbitMQ
-
1
def options
-
136
base.merge(routing_key: routing_key)
-
end
-
-
# Define option sending to rabbitmq for janus admin message
-
1
def options_admin
-
89
base.merge(routing_key: routing_key_admin)
-
end
-
-
1
private
-
-
1
def determine_routing_key(type_request)
-
cluster = Tools::Cluster.instance
-
-
if type_request.include?('admin')
-
cluster.queue_admin_to(@instance)
-
else
-
cluster.queue_to(@instance)
-
end
-
end
-
-
1
def base
-
225
{ correlation_id: @correlation, content_type: 'application/json' }
-
end
-
-
1
def routing_key
-
136
Tools::Cluster.instance.queue_to(@instance)
-
end
-
-
1
def routing_key_admin
-
89
Tools::Cluster.instance.queue_admin_to(@instance)
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Rabbit
-
1
module Publisher
-
# :reek:InstanceVariableAssumption
-
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
# This publisher send and read an message in admin queues
-
1
class Admin < Base
-
# Initialize an queue non eclusive for admin/monitor API with Janus
-
#
-
# @param [String] exchange Exchange used for the transaction
-
1
def initialize(exchange)
-
85
@reply = exchange.queue('', exclusive: true)
-
# @reply = exchange.queue(Tools::Config.instance.queue_admin_from)
-
85
super(exchange)
-
85
subscribe_to_queue
-
end
-
-
# Send an message to queue and waiting a response
-
#
-
#
-
# @param [String] request JSON request sending to rabbitmq queue
-
#
-
# @return [Janus::Response::Admin] response for an request reading
-
# by janus instance
-
1
def publish(request)
-
85
@message = request
-
85
@exchange.publish(@message.to_json,
-
request.options.merge!(reply_to: reply.name))
-
85
return_response
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
# :reek:UtilityFunction
-
-
1
module RubyRabbitmqJanus
-
1
module Rabbit
-
1
module Publisher
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# This publisher send and read an message in queues
-
1
class Base < RubyRabbitmqJanus::Rabbit::BaseEvent
-
# Intialize a publisher for sending and reading a message
-
#
-
# @param [String] exchange Determine type exchange used for all
-
# transaction between gem and rabbitmq
-
1
def initialize(exchange)
-
217
super()
-
217
@exchange = exchange.default_exchange
-
217
@message = nil
-
end
-
-
# Publish an message in queue
-
#
-
# @param [String] request JSON request sending to rabbitmq queue
-
#
-
# @raise [Errors::RabbitPublishMessage] If request is false the
-
# execption is calling
-
1
def publish(request)
-
132
@message = request
-
132
@exchange.publish(@message.to_json,
-
request.options.merge!(reply_to: reply.name))
-
end
-
-
1
private
-
-
1
def subscribe_to_queue
-
207
reply.subscribe do |_delivery_info, propertie, payload|
-
207
test_correlation(m_correlation, p_correlation(propertie)) do
-
207
synchronize(payload)
-
end
-
end
-
end
-
-
1
def m_correlation
-
207
@message.correlation
-
end
-
-
1
def p_correlation(propertie)
-
207
propertie.correlation_id
-
end
-
-
1
def test_correlation(m_cor, p_cor)
-
raise Errors::Rabbit::Publisher::Base::TestCorrelation, m_cor, p_cor \
-
207
unless m_cor.eql?(p_cor)
-
-
207
yield
-
end
-
-
1
def synchronize(payload)
-
207
lock.synchronize do
-
207
responses.push(JSON.parse(payload))
-
end
-
207
semaphore.signal
-
end
-
-
1
attr_accessor :message
-
1
attr_reader :reply
-
end
-
end
-
end
-
end
-
-
1
require 'rrj/rabbit/publisher/exclusive'
-
1
require 'rrj/rabbit/publisher/admin'
-
1
require 'rrj/rabbit/publisher/keepalive'
-
1
require 'rrj/rabbit/publisher/non_exclusive'
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Rabbit
-
1
module Publisher
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# # Publish message
-
#
-
# Publish message in queue exclusive. Bunny create automaticaly a name for
-
# this queue. The queue name like to 'amq.gen-1A456DGVHDVUS'.
-
1
class Exclusive < Base
-
# Initialize an queue exclusive and generated automaticaly by bunny
-
#
-
# @param [String] exchange Exchange used for the transaction
-
# @param [String] name_queue Name to queue exclusive
-
1
def initialize(exchange, name_queue)
-
122
@reply = exchange.queue(name_queue, exclusive: true)
-
122
super(exchange)
-
122
subscribe_to_queue
-
end
-
-
# Send an message to queue and waiting a response
-
#
-
# @param [String] request JSON request sending to rabbitmq queue
-
#
-
# @return [Janus::Response::Standard] response for an request reading
-
# by janus instance
-
1
def publish(request)
-
122
super(request)
-
122
return_response
-
end
-
-
1
private
-
-
1
attr_reader :reply
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Rabbit
-
1
module Publisher
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# # Publish message for keepalive thread
-
#
-
# The name to queue it's created automatically by Bunny GEM
-
#
-
# @see KeepaliveThread
-
1
class Keepalive < Base
-
1
def initialize(exchange)
-
@reply = exchange.queue('', exclusive: true)
-
super(exchange)
-
subscribe_to_queue
-
end
-
-
1
def publish(request)
-
super(request)
-
return_response
-
end
-
-
1
private
-
-
1
attr_reader :reply
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Rabbit
-
1
module Publisher
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
#
-
# Publish message in queue non exclusive. By default "to-janus".
-
# This an option in config to this gem.
-
1
class NonExclusive < Base
-
# Define an publisher for create non exclusive queue
-
1
def initialize(exchange)
-
10
@reply = exchange.queue(Tools::Config.instance.queue_from)
-
10
super(exchange)
-
end
-
-
# Send an message to queue
-
#
-
# @param [String] request JSON request sending to rabbitmq queue
-
#
-
# rubocop:disable Lint/UselessMethodDefinition
-
1
def publish(request)
-
10
super(request)
-
end
-
# rubocop:enable Lint/UselessMethodDefinition
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
require 'rrj/rabbit/connect'
-
1
require 'rrj/rabbit/base_event'
-
1
require 'rrj/rabbit/propertie'
-
-
1
module RubyRabbitmqJanus
-
# Module rabbit interaction
-
1
module Rabbit
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
# # Rake Action
-
#
-
# Create an action for rails apps
-
1
class Railtie < Rails::Railtie
-
1
railtie_name :rrj
-
-
1
rake_tasks do
-
tasks = File.join(File.dirname(__FILE__), '../tasks', '*.rake')
-
Dir[tasks].each { |file_task| load file_task }
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
require 'rrj/tools/gem/config'
-
-
# :reek:UtilityFunction
-
-
1
module RubyRabbitmqJanus
-
# # RRJRSpec
-
#
-
# Initializer to use with RSpec execution
-
1
class RRJRSpec < RRJTaskAdmin
-
# rubocop:disable Lint/MissingSuper
-
1
def initialize
-
RubyRabbitmqJanus::Tools::Config.instance
-
end
-
# rubocop:enable Lint/MissingSuper
-
-
# @see RubyRabbitmqJanus::RRJ::session_endpoint_public
-
1
def session_endpoint_public(_options)
-
yield(RubyRabbitmqJanus::Janus::Transactions::RSpec.new)
-
end
-
-
# @see RubyRabbitmqJanus::RRJ::session_endpoint_private
-
1
def handle_endpoint_public(_options)
-
transaction = RubyRabbitmqJanus::Janus::Transactions::RSpec.new
-
yield(transaction)
-
transaction.response
-
end
-
-
# @see RubyRabbitmqJanus::RRJAdmin::admin_endpoint
-
1
def admin_endpoint(_options)
-
yield(RubyRabbitmqJanus::Janus::Transactions::RSpec.new)
-
end
-
-
1
alias session_endpoint_private session_endpoint_public
-
1
alias handle_endpoint_private handle_endpoint_public
-
end
-
end
-
# frozen_string_literal: true
-
-
# :reek:UtilityFunction
-
-
1
module RubyRabbitmqJanus
-
# @author VAILLANT jeremy <jeremy.vaillant@dazl.tv>
-
-
# # RubyRabbitmqJanus - Task
-
#
-
# This class is used with rake task.
-
1
class RRJTask < RRJ
-
# rubocop:disable Lint/MissingSuper
-
1
def initialize
-
Tools::Config.instance
-
Tools::Requests.instance
-
end
-
# rubocop:enable Lint/MissingSuper
-
-
# Create a transaction between Apps and Janus in queue private
-
#
-
# @params [Hash] options
-
# @options [String] :instance (mandatory id cluster is enabled)
-
# @options [Integer] :session_id
-
# @options [Hash] :replace
-
# @options [Hash] :add
-
#
-
# @param [Hash] options
-
# Give a session number for use another session in Janus
-
#
-
# @example Get Janus information
-
# @rrj.session_endpoint_private do |transaction|
-
# response = transaction.publish_message('base::info').to_hash
-
# end
-
#
-
# @since 2.7.0
-
1
def session_endpoint_private(options = {})
-
transaction = Janus::Transactions::Session.new(true,
-
options['session_id'])
-
transaction.connect { yield(transaction) }
-
end
-
-
# For task is possible to calling this method, but no action is executed
-
1
def session_endpoint_public(_options)
-
nil
-
end
-
-
# For task is impossible to calling this method
-
1
def handle_endpoint_public(_options)
-
nil
-
end
-
-
# Create a transaction between Apps and Janus in queue private
-
#
-
# @params [Hash] options
-
# @options [String] :instance (mandatory id cluster is enabled)
-
# @options [Integer] :session_id
-
# @options [Hash] :replace
-
# @options [Hash] :add
-
#
-
# @example Post a offer
-
# options = {
-
# 'instance' => 42,
-
# 'session_id' => 71984735765,
-
# 'handle_id' => 56753748917,
-
# 'replace' => {
-
# 'sdp' => 'v=0\r\no=[..more sdp stuff..]'
-
# }
-
# }
-
# @rrj.handle_endpoint_private(options) do |transaction|
-
# transaction.publish_message('peer::offer', options)
-
# end
-
#
-
# @since 2.7.0
-
#
-
# :reek:FeatureEnvy
-
1
def handle_endpoint_private(options = {})
-
janus = session_instance(options)
-
handle = 0 # Create always a new handle
-
transaction = Janus::Transactions::Handle.new(true,
-
janus.session,
-
handle,
-
janus.instance)
-
transaction.connect { yield(transaction) }
-
end
-
-
1
private
-
-
1
def session_instance(options)
-
Models::JanusInstance.find_by_instance(options['instance'])
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
# :reek:UtilityFunction
-
-
1
module RubyRabbitmqJanus
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
-
# # RubyRabbitmqJanus - RRJTaskAdmin
-
#
-
# Used wit sidekiq/console/CI execution for admin queue in Janus gateway
-
1
class RRJTaskAdmin < RRJTask
-
# Create a transaction between Apps and Janus
-
#
-
# This transaction is sending to admin/monitor API.
-
#
-
# @params [Hash] options
-
# @options [String] :instance (mandatory id cluster is enabled)
-
# @options [Integer] :session_id
-
# @options [Hash] :replace
-
# @options [Hash] :add
-
#
-
# @example List sessions
-
# options = { 'instance' => 42 }
-
# @rrj.handle_endpoint_private(options) do |transaction|
-
# transaction.publish_message('admin::list_sessions', options)
-
# end
-
#
-
#
-
# @example List handles
-
# options = { 'instance' => 42, 'session_id' => 71984735765 }
-
# @rrj.handle_endpoint_private(options) do |transaction|
-
# transaction.publish_message('admin::list_handles', options)
-
# end
-
#
-
# @since 2.7.0
-
1
def admin_endpoint(options = {})
-
transaction = Janus::Transactions::Admin.new(options)
-
transaction.connect { yield(transaction) }
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
# Load gem Config if present
-
# @see https://rubygems.org/gems/config
-
-
begin
-
1
require 'config'
-
-
if defined?(Config)
-
config_conf = [
-
File.join(Dir.pwd, 'config', 'settings.yml'),
-
File.join(Dir.pwd, 'config', 'settings', "#{ENV['RAILS_ENV']}.yml")
-
]
-
Config.load_and_set_settings(config_conf)
-
end
-
rescue LoadError => exception
-
1
p "Don't use gem config : #{exception}"
-
rescue => exception
-
p exception
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Tools
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
-
# # Manage Janus instance
-
1
class Cluster
-
1
include Singleton
-
-
# Initialize object for managing each instance to Janus
-
1
def initialize
-
1
@current_instance = nil
-
end
-
-
# Specify a name to queue
-
1
def queue_to(instance = nil)
-
138
Tools::Config.instance.options['queues']['standard']['to'] + \
-
"-#{instance.blank? ? @current_instance : instance}"
-
end
-
-
# Specify a name to admin queue
-
1
def queue_admin_to(instance = nil)
-
91
Tools::Config.instance.options['queues']['admin']['to'] + \
-
"-#{instance.blank? ? @current_instance : instance}"
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
require 'active_support'
-
1
require 'singleton'
-
1
require 'rrj/errors/error'
-
1
require 'yaml'
-
1
require 'erb'
-
1
%w[gem rabbit queues janus].each do |file|
-
4
require File.join('rrj', 'tools', 'gem', 'config', file)
-
end
-
1
%w[instances validations].each do |file|
-
2
require File.join('rrj', 'models', 'concerns', file)
-
end
-
-
1
module RubyRabbitmqJanus
-
1
module Tools
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
-
# # Manage configuration file
-
# Singleton object for reading configuration file
-
#
-
# @!attribute [r] options
-
# @return [Hash] Return all options to configured in config file.
-
# @!attribute [r] configuration
-
# @return [String] Path to configuration file used
-
1
class Config
-
1
include Singleton
-
1
include RubyRabbitmqJanus::Tools::ConfigGem
-
1
include RubyRabbitmqJanus::Tools::ConfigRabbit
-
1
include RubyRabbitmqJanus::Tools::ConfigQueues
-
1
include RubyRabbitmqJanus::Tools::ConfigJanus
-
-
1
attr_reader :options, :configuration
-
-
# Define HOME RRJ gem
-
1
RRJ_HOME = File.realpath(File.join(File.dirname(__FILE__),
-
'..', '..', '..'))
-
-
# Define a default name to file configuration
-
1
CONF_DEFAULT = 'config/default.yml'
-
-
# Define a default override file configuration
-
1
CONF_CUSTOM = 'config/ruby-rabbitmq-janus.yml'
-
-
# Define a default path to file configuration to gem
-
1
PATH_DEFAULT = File.join(RRJ_HOME, CONF_DEFAULT)
-
-
# Initialize configuration file default or customize if exist
-
1
def initialize
-
1
@options = @configuration = nil
-
1
loading_configuration_customize
-
1
@options ||= loading_configuration_default
-
end
-
-
1
private
-
-
1
def load_configuration
-
1
::YAML.safe_load(ERB.new(File.read(@configuration)).result)
-
end
-
-
1
def loading_configuration_customize
-
1
@configuration = File.join(Dir.pwd, CONF_CUSTOM)
-
1
@options = load_configuration if File.exist?(@configuration)
-
end
-
-
1
def loading_configuration_default
-
1
@configuration = PATH_DEFAULT
-
1
load_configuration
-
end
-
end
-
end
-
end
-
-
1
require RubyRabbitmqJanus::Tools::Config.instance.orm
-
1
require File.join('rrj',
-
'models',
-
RubyRabbitmqJanus::Tools::Config.instance.orm)
-
# frozen_string_literal: true
-
-
# :reek:UtilityFunction
-
-
1
module RubyRabbitmqJanus
-
1
module Tools
-
# Subclass for Config
-
#
-
# Options about Gem
-
#
-
# @see RubyRabbitmqJanus::Tools::Config
-
1
module ConfigGem
-
# @return [Boolean] Read option file for a janus cluster section
-
1
def cluster
-
2
@options['gem']['cluster']['enabled'].to_s.match?('true') || false
-
end
-
-
# @return [Symbol] read configuration for log level used in this gem
-
1
def log_level
-
3
@options['gem']['log']['level'].upcase.to_sym || :INFO
-
end
-
-
# @return [Symbol] Read level to log
-
1
def log_type
-
3
@options['gem']['log']['type'].downcase.to_sym || :stdout
-
end
-
-
# @return [String] read configuration for log option
-
1
def log_option
-
11
option = @options['gem']['log']['option']
-
11
option.empty? ? nil : option
-
end
-
-
# @return [String] Get path to classes in project calling this gem.
-
1
def listener_path
-
2
@options['gem']['listener']['public'].to_s ||
-
'app/ruby_rabbitmq_janus/action_events'
-
end
-
-
# @return [String] Get path to classes in project calling this gem.
-
1
def listener_admin_path
-
2
@options['gem']['listener']['admin'].to_s ||
-
'app/ruby_rabbitmq_janus/action_admin_events'
-
end
-
-
# @return [String] Environment gem executed.
-
1
def environment
-
4
@options['gem']['environment'].to_s || 'development'
-
end
-
-
# @return [String] Get orm used (mongoid or active_record)
-
1
def object_relational_mapping
-
6
@options['gem']['orm'].to_s || 'mongoid'
-
end
-
-
# @return [String] Get program name or GEM_NAME
-
1
def program_name
-
5
ENV['PROGRAM_NAME'] || RubyRabbitmqJanus::GEM_NAME
-
end
-
-
# @return [String] Get path for json files contains a Janus response
-
1
def rspec_response
-
2
@options['gem']['response_path'] || 'spec/responses'
-
end
-
-
# @return [Integer] get number of thread created for listen public queues
-
1
def public_queue_process
-
2
@options['gem']['process'] || 1
-
end
-
-
1
alias env environment
-
1
alias orm object_relational_mapping
-
1
alias pg program_name
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Tools
-
# Subclass for Config
-
#
-
# Options about Janus
-
#
-
# @see RubyRabbitmqJanus::Tools::Config
-
1
module ConfigJanus
-
# @return [Integer]
-
# read configuration for janus time to live for keepalive messages
-
1
def time_to_live
-
4
@options['janus']['session']['keepalive'].to_i || 50
-
end
-
-
# @param [Fixnum] index determine what field is readint in array plugins
-
# in configuration file
-
# @return [String] read configuration for plugin with index
-
1
def plugin_at(index = 0)
-
135
@options['janus']['plugins'][index].to_s
-
rescue
-
raise RubyRabbitmqJanus::Errors::Tools::Plugins, index
-
end
-
-
1
alias ttl time_to_live
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Tools
-
# Subclass for Config
-
#
-
# Options about Queues
-
#
-
# @see RubyRabbitmqJanus::Tools::Config
-
1
module ConfigQueues
-
# @return [String] Get to name queue_from (pattern)
-
1
def queue_from
-
12
@options['queues']['standard']['from'].to_s
-
rescue
-
raise RubyRabbitmqJanus::Errors::Tools::QueueFrom
-
end
-
-
# @return [String] Get to name queue_to (pattern)
-
1
def queue_to
-
3
@options['queues']['standard']['to'].to_s
-
rescue
-
raise RubyRabbitmqJanus::Errors::Tools::QueueTo
-
end
-
-
# @return [String] Get to name queue_admin_from (pattern)
-
1
def queue_admin_from
-
2
@options['queues']['admin']['from'].to_s
-
rescue
-
raise RubyRabbitmqJanus::Errors::Tools::QueueAdminFrom
-
end
-
-
# @return [String] Get to name queue_admin_from (pattern)
-
1
def queue_admin_to
-
3
@options['queues']['admin']['to'].to_s
-
rescue
-
raise RubyRabbitmqJanus::Errors::Tools::QueueAdminTo
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
# :reek:FeatureEnvy
-
-
1
module RubyRabbitmqJanus
-
1
module Tools
-
# Subclass for Config
-
#
-
# Options about bunny
-
#
-
# @see RubyRabbitmqJanus::Tools::Config
-
1
module ConfigRabbit
-
# @return [String] read configuration fir queue admin
-
1
def admin_pass
-
2
@options['rabbit']['admin_pass'].to_s
-
rescue
-
raise RubyRabbitmqJanus::Errors::Tools::AdminPassword
-
end
-
-
# @return [Symbol] read configuration for bunny log level
-
1
def log_level_rabbit
-
2
@options['rabbit']['level'].upcase.to_sym || :INFO
-
end
-
-
# @return [Hash] Format hash for bunny settings
-
1
def server_settings
-
192
Hash[%w[host port pass user vhost log_level].map do |value|
-
1152
key = value.to_sym
-
1152
j_value = @options['rabbit'][rabbitmq_conf(value)]
-
-
1152
raise Errors::Tools::Config::Rabbitmq value if j_value.blank?
-
-
1152
[key, j_value]
-
end]
-
end
-
-
1
private
-
-
1
def rabbitmq_conf(value)
-
1152
value.eql?('log_level') ? 'level' : value
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
require 'logger'
-
1
require 'rrj/info'
-
1
require 'rrj/tools/gem/config'
-
-
1
module RubyRabbitmqJanus
-
1
module Tools
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
# @since 2.6.0
-
#
-
# # Manage log in this gem
-
#
-
# Create module for managing logger in many level.
-
# In Thread process.
-
# In rails apps (final app use this gem).
-
#
-
# Prepare different output :
-
# STDOUT
-
# RemoteSyslogger (for papertrail)
-
# File
-
1
module Logger
-
# Write basic information about this gem
-
1
def self.start
-
1
Log.info '### Start bin Ruby Rabbit Janus ###'
-
1
Log.info "Program : #{RubyRabbitmqJanus::Tools::Config.instance.pg}"
-
1
Log.info "RRJ Version : #{RubyRabbitmqJanus::VERSION}"
-
1
Log.debug "\r\n#{RubyRabbitmqJanus::BANNER}"
-
end
-
-
# Configure logger used by RRJ
-
1
def self.create
-
1
@config = RubyRabbitmqJanus::Tools::Config.instance
-
-
1
@log = initialize_logger
-
1
@log.level = @config.log_level
-
-
1
@log
-
end
-
-
# Choose type logger used in application instance
-
1
def self.initialize_logger
-
1
case @config.log_type
-
when :file then logger_file
-
when :remote then logger_remote
-
else
-
1
logger_stdout
-
end
-
end
-
-
# Configure logger with output SDTOUT
-
1
def self.logger_stdout
-
2
::Logger.new($stdout)
-
end
-
-
# Configure logger with output file
-
# default : `log/ruby-rabbitmq-janus.log`
-
1
def self.logger_file
-
1
::Logger.new(@config.log_option || 'log/ruby-rabbitmq-janus.log')
-
end
-
-
# Configure logger with output PaperTrail service
-
1
def self.logger_remote
-
1
require 'remote_syslog_logger'
-
-
1
RemoteSyslogLogger.new(remote_url,
-
remote_port,
-
program: remote_program,
-
local_hostname: remote_hostname)
-
end
-
-
# Read url for PaperTail and split for endpoint
-
1
def self.remote_url
-
2
@config.log_option.split(':').first
-
end
-
-
# Read url for PaperTrail and split for port
-
1
def self.remote_port
-
2
@config.log_option.split('@').first.split(':').last
-
end
-
-
# Read url for PaperTrail and split for name app
-
1
def self.remote_program
-
2
@config.log_option.split('@').last.split(':').first
-
end
-
-
# Read url for PaperTrail and split for host
-
1
def self.remote_hostname
-
2
@config.log_option.split(':').last
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
# :reek:FeatureEnvy
-
# :reek:UtilityFunction
-
-
1
module RubyRabbitmqJanus
-
1
module Tools
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
-
# # Utility for manage option to this gem.
-
#
-
# This class start all singleton, Log, Config, Request and Keepalive
-
# instance. It's also used for testing session/handle used in request.
-
1
class Option
-
1
def initialize
-
187
Config.instance
-
187
Requests.instance
-
end
-
-
# Determine session_id used
-
#
-
# @param [Hash] options Read options used in request
-
#
-
# @return [Fixnum] Session ID
-
#
-
# @since 2.0.0
-
1
def use_current_session?(options)
-
102
if options.key?('session_id')
-
54
options['session_id']
-
else
-
48
Models::JanusInstance.first.session
-
end
-
end
-
-
# Determine handle_id used
-
#
-
# @param [Hash] options Read options used in request
-
#
-
# @return [Fixnum] Handle ID
-
#
-
# @since 2.0.0
-
1
def use_current_handle?(options)
-
32
options.key?('handle_id') ? options['handle_id'] : 0
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Tools
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
-
# # Load files json in `config/request/**/*`.
-
#
-
# This file is used for sending a request to Janus
-
#
-
# @!attribute [r] requests
-
# @return [Hash] It's a hash with name and path to request.
-
#
-
# @see file:/config/requests.md For more information to type requests used.
-
1
class Requests
-
1
include Singleton
-
-
1
attr_reader :requests
-
-
# Define folder to request
-
1
PATH_REQUEST = 'config/requests/'
-
-
# Load all requests in folder
-
1
def initialize
-
1
@requests = {}
-
1
::Log.info "Loading all requests in : #{PATH_REQUEST}"
-
5
Dir[File.join(PATH_REQUEST, '*')].count { |file| each_files(file) }
-
end
-
-
1
private
-
-
1
def each_folder(subfolder)
-
4
Dir[File.join(PATH_REQUEST + subfolder, '*')].count do |file|
-
32
if File.file?(file)
-
32
read_folder("#{subfolder.gsub('/', '::')}::", file)
-
elsif File.directory?(file)
-
each_folder("#{subfolder}/#{File.basename(file)}")
-
end
-
end
-
end
-
-
1
def read_file(file)
-
@requests[File.basename(file, '.json').to_s] = File.path(file)
-
end
-
-
1
def read_folder(folder, file)
-
32
@requests[folder + File.basename(file, '.json').to_s] = File.path(file)
-
end
-
-
1
def each_files(file)
-
4
if File.file?(file)
-
read_file(file)
-
4
elsif File.directory?(file)
-
4
each_folder(File.basename(file))
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Tools
-
1
module Replaces
-
# Format message request with good data to HASH format for Admin request.
-
# Manage level, debug and admin_secret
-
#
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
1
class Admin < Handle
-
1
private
-
-
1
KEY_ACCEPTED = %w[colors
-
debug
-
level
-
filename
-
folder
-
max_nack_queue
-
no_media_timer
-
timestamps
-
token
-
truncate
-
timeout
-
plugins].freeze
-
-
1
def replace_element_classic
-
108
super
-
108
replace_admins if request.key?('admin_secret')
-
108
add_secret if opts.key?('add')
-
end
-
-
1
def add_secret
-
21
values = opts['add']
-
21
request.merge!(values)
-
end
-
-
1
def replace_admins
-
106
replace_admin
-
106
KEY_ACCEPTED.each do |key|
-
1272
replace_component(key) if request.key?(key)
-
end
-
end
-
-
1
def replace_component(key)
-
140
request[key] = type.convert(key, opts)
-
end
-
-
1
def replace_admin
-
106
request['admin_secret'] = admin_pass
-
end
-
-
1
def admin_pass
-
106
Tools::Config.instance.options['rabbit']['admin_pass']
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Tools
-
1
module Replaces
-
# Format message request with good data to HASH format for Handle request.
-
# Manage sdp, handle_id, candidate or candidates.
-
#
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
1
class Handle < Session
-
1
private
-
-
1
def replace_element_classic
-
261
super
-
261
replace_sdp if request.key?('jsep')
-
261
replace_handle if request.key?('handle_id')
-
44
replace_candidate \
-
261
if request.key?('candidate') || request.key?('candidates')
-
end
-
-
1
def replace_handle
-
81
request['handle_id'] = type.convert('handle_id', opts)
-
end
-
-
1
def replace_candidate
-
44
cdn = type.convert(determine_key_candidate, opts)
-
44
request[cdn[0]] = cdn[1]
-
44
delete_key_unless
-
end
-
-
1
def replace_sdp
-
52
request['jsep']['sdp'] = type.convert('sdp', opts)
-
end
-
-
1
def determine_key_candidate
-
44
if request.key?('candidate')
-
2
'candidate'
-
else
-
42
'candidates'
-
end
-
end
-
-
1
def delete_key_unless
-
44
singular = request['candidate']
-
44
plural = request['candidates']
-
44
if singular.eql?('<array>')
-
request.delete('candidate')
-
44
elsif plural.eql?('candidates')
-
request.delete['candidates']
-
end
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
# :reek:UtilityFunction
-
-
1
module RubyRabbitmqJanus
-
1
module Tools
-
# Tools for replace elements in request
-
1
module Replaces
-
# # Prepare request
-
#
-
# Tools for replace elements in request sending to Rabbitmq. It's a basic
-
# class. Manage just transaction element.
-
#
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
1
class Replace
-
# Initialize tool replace.
-
#
-
# @param [Hash] request Request parsing before sending to RabbitMQ/Janus
-
# @param [Hash] options Elements to be replaced in request
-
1
def initialize(request, options = {})
-
305
@request = request
-
305
@opts = options
-
305
@type = Tools::Type.new(@request)
-
end
-
-
# Replace element in hash request with information used for this
-
# transaction.
-
#
-
# @return [Hash] request with element replace
-
1
def transform_request
-
305
replace_element_classic
-
305
unless @opts.empty?
-
301
replace_other if test_presence?('replace')
-
301
add_other if test_presence?('add')
-
end
-
305
@request
-
end
-
-
1
private
-
-
1
attr_reader :request, :opts, :type
-
-
1
def test_presence?(presence_of_key)
-
602
@opts.key?(presence_of_key) && @request.key?('body') &&
-
!@opts[presence_of_key].blank?
-
end
-
-
1
def replace_other
-
values = @opts['replace']
-
running_hash(rewrite_key_to_string(values))
-
end
-
-
1
def add_other
-
values = @opts['add']
-
@request['body'].merge!(values)
-
end
-
-
1
def rewrite_key_to_string(node)
-
Hash[
-
node.map do |key, value|
-
[key.to_s, value?(value)]
-
end
-
]
-
end
-
-
1
def value?(value)
-
value.is_a?(Hash) ? rewrite_key_to_string(value) : value
-
end
-
-
1
def running_hash(hash, parent = 'body')
-
hash.each do |key, value|
-
if value.is_a?(Hash)
-
running_hash(value, new_parent(key, parent))
-
else
-
@request[parent][key] = value unless key.eql? 'sdp'
-
end
-
end
-
end
-
-
1
def new_parent(key, parent)
-
"#{parent}#{'.' unless parent.empty?}#{key}"
-
end
-
-
1
def replace_element_classic
-
305
replace_transaction if @request.key?('transaction')
-
end
-
-
1
def replace_transaction
-
305
@request['transaction'] = @type.convert('transaction')
-
end
-
end
-
end
-
end
-
end
-
-
1
require 'rrj/tools/replaces/session'
-
1
require 'rrj/tools/replaces/handle'
-
1
require 'rrj/tools/replaces/admin'
-
# frozen_string_literal: true
-
-
1
module RubyRabbitmqJanus
-
1
module Tools
-
1
module Replaces
-
# Format message request with good data to HASH format for Session
-
# request.
-
# Manage session and plugin.
-
#
-
# @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
-
1
class Session < Replace
-
1
private
-
-
1
def replace_element_classic
-
280
super
-
280
replace_session if request.key?('session_id')
-
280
replace_plugin if request.key?('plugin')
-
end
-
-
1
def replace_session
-
172
request['session_id'] = type.convert('session_id', opts)
-
end
-
-
1
def replace_plugin
-
123
request['plugin'] = type.convert('plugin')
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
require 'active_support/core_ext/string'
-
-
# :reek:UtilityFunction
-
# :reek:TooManyStatements
-
-
1
module RubyRabbitmqJanus
-
1
module Tools
-
# Class for converting elements given by apps to this gem an type conform
-
# to request sending
-
1
class Type
-
# Initialize an object for cast a type to data given by app
-
#
-
# @param [Hash] request Request parsing before sending to RabbitMQ/Janus
-
1
def initialize(request)
-
344
@request = request
-
344
@key = @data = nil
-
end
-
-
# Return an data with a type corresponding to string in request
-
#
-
# @param [String] key Key testing
-
# @param [Hash] option Datas sending by user and adding/replace in request
-
#
-
# @return data with good type for JSON format
-
1
def convert(key, option = {})
-
956
@key = key
-
956
@data = option[@key] if option.key?(@key)
-
956
convert_data
-
end
-
-
1
private
-
-
1
def convert_data
-
956
case search_key
-
90
when '<string>' then convert_to_type_string
-
326
when '<number>', '<integer>' then convert_to_type_number
-
39
when '<boolean>' then convert_to_type_boolean
-
52
when '<array>' then convert_to_type_array
-
12
when '<plugins>' then convert_to_type_plugins
-
308
when '<transaction>' then convert_to_type_transaction
-
129
when /<plugin\[[0-9]\]>/ then convert_to_type_plugin
-
end
-
end
-
-
1
def search_key
-
956
field = @request[@key]
-
956
if field.blank?
-
52
@request.each do |key, value|
-
512
test = @request[key]
-
field = test[@key] \
-
512
if value.is_a?(Hash) && test.key?(@key)
-
end
-
end
-
956
field
-
end
-
-
1
def convert_to_type_transaction
-
308
[*('A'..'Z'), *('0'..'9')].sample(10).join
-
end
-
-
1
def convert_to_type_string
-
90
@data.to_s
-
end
-
-
1
def convert_to_type_number
-
326
@data.to_i
-
end
-
-
1
def convert_to_type_boolean
-
39
if test_boolean('TRUE', TrueClass)
-
30
true
-
9
elsif test_boolean('FALSE', FalseClass)
-
9
false
-
end
-
end
-
-
1
def convert_to_type_plugin
-
129
index = @request[@key].gsub('<plugin[', '').gsub(']>', ']').to_i
-
129
Config.instance.plugin_at(index)
-
end
-
-
1
def convert_to_type_array
-
52
data = array_alone
-
52
key = data.is_a?(Hash) ? @key : @key.pluralize
-
52
[key, data]
-
end
-
-
1
def convert_to_type_plugins
-
12
@data.is_a?(String) ? [@data] : @data
-
end
-
-
1
def test_boolean(boolean_string, boolean_class)
-
48
@data.is_a?(boolean_class) ||
-
13
(@data.is_a?(String) && @data.casecmp(boolean_string).eql?(0))
-
end
-
-
1
def array_alone
-
52
if @data.is_a?(Array)
-
48
@data.count.eql?(1) ? @data[0] : @data
-
else
-
4
@data
-
end
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
require 'rrj/tools/gem/config'
-
1
require 'rrj/tools/gem/requests'
-
1
require 'rrj/tools/gem/cluster'
-
1
require 'rrj/tools/gem/option'
-
1
require 'rrj/tools/replaces/type'
-
1
require 'rrj/tools/replaces/replace'
-
-
1
module RubyRabbitmqJanus
-
# Contains all tools necessary in this gem
-
1
module Tools
-
end
-
end
-
# frozen_string_literal: true
-
-
# Load all tools necessary to good functionality to this gem
-
1
require 'rrj/init'
-
1
require 'rrj/admin'
-
1
require 'rrj/task'
-
1
require 'rrj/task_admin'
-
1
require 'rrj/rspec'
-
-
# Define tools for this gems
-
1
require 'rrj/tools/tools'
-
-
# Define actions with RabbitMQ
-
1
require 'rrj/rabbit/rabbit'
-
-
# Define actions with Janus
-
1
require 'rrj/janus/janus'
-
-
# Define process create on fly for manage keepalive and public queue
-
1
require 'rrj/process/concurrency'
-
-
# Define errors in gems
-
1
require 'rrj/errors/error'
-
-
1
require 'rrj/railtie' if defined?(Rails)