Class: RubyRabbitmqJanus::RRJ

Inherits:
Object
  • Object
show all
Defined in:
lib/rrj/init.rb

Overview

Initialize gem and create automatically an session with Janus :reek:BooleanParameter

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(listen_queue_classic = true) ⇒ RRJ

Returns a new instance of RubyRabbitmqJanus



23
24
25
26
27
28
29
30
# File 'lib/rrj/init.rb', line 23

def initialize(listen_queue_classic = true)
  start_instances_tools
  @session = Janus::Keepalive.instance.session
  Janus::Event.instance if listen_queue_classic
  @transaction = nil
rescue => error
  raise Errors::RRJErrorInit, error
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



20
21
22
# File 'lib/rrj/init.rb', line 20

def event
  @event
end

#sessionObject (readonly)

Initialize gem and create automatically an session with Janus :reek:BooleanParameter

Author:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rrj/init.rb', line 19

class RRJ
  attr_reader :session, :event

  # Returns a new instance of RubyRabbitmqJanus
  def initialize(listen_queue_classic = true)
    start_instances_tools
    @session = Janus::Keepalive.instance.session
    Janus::Event.instance if listen_queue_classic
    @transaction = nil
  rescue => error
    raise Errors::RRJErrorInit, error
  end

  # Send an simple message to janus. No options in request with this method.
  # @param [String] type
  #   Given a type to request. JSON request writing in 'config/requests/'
  # @param [Bollean] exclusive
  #   Use an exclusive queue or not
  # @example Sending an message info
  #   RubyRabbitmqJanus::RRJ.new.message_simple('base::info')
  #   #=> {"janus":"server_info","name":"Janus WebRTC Gateway" ... }
  # @return [RubyRabbitmqJanus::Janus::Response] Give an object response to janus server
  def message_simple(type = 'base::info', exclusive = false)
    Janus::Transaction.new(@session).connect(exclusive) do
      Janus::Message.new(type)
    end
  end

  # Send an message simple in current session.
  # @param [String] type
  #   Given a type to request. JSON request writing in 'config/requests/'
  # @param [Hash] options Options update in request
  # @param [Bollean] exclusive
  #   Use an exclusive queue or not
  # @example Sending an message create
  #   RubyRabbitmqJanus::RRJ.new.message_session('base::create')
  #   #=> {"janus":"server_info","name":"Janus WebRTC Gateway" ... }
  # @return [RubyRabbitmqJanus::Janus::Response] Give an object response to janus server
  def message_session(type, options = {}, exclusive = false)
    Janus::TransactionSession.new(@session).session_connect(exclusive) do
      Janus::Message.new(type, use_current_session?(options))
    end
  rescue => error
    raise Errors::RRJErrorPost, error
  end

  # Send a message simple for admin Janus
  # @param [String] type
  #   Given a type to request. JSON request writing in 'config/requests/'
  # @param [Hash] options Options update in request
  # @example Sending an message create
  #   RubyRabbitmqJanus::RRJ.new.message_admin('admin::sessions')
  #   #=> {"janus":"success","sessions": [12345, 8786567465465, ...] }
  # @return [RubyRabbitmqJanus::Janus::Response] Give an object response to janus server
  def message_admin(type, options = {})
    Janus::TransactionAdmin.new(@session).connect do
      Janus::MessageAdmin.new(type, options.merge!('session_id' => @session))
    end
  end

  # Send an message in handle session in current session.
  # @param [String] type
  #   Given a type to request. JSON request writing in 'config/requests/'
  # @param [Hash] replace Options update in request
  # @param [Hash] add Elements adding to request
  # @example Sending an message create
  #   RubyRabbitmqJanus::RRJ.new.message_session('base::create')
  #   #=> {"janus":"server_info","name":"Janus WebRTC Gateway" ... }
  # @return [RubyRabbitmqJanus::Janus::Response] Give an object response to janus server
  def message_handle(type, replace = {}, add = {})
    options = { 'replace' => replace, 'add' => add }
    @transaction.publish_message_handle(type, options)
  end

  # Define an handle transaction and establish connection with janus
  def start_handle(exclusive = false)
    @transaction ||= Janus::TransactionHandle.new(@session)
    @transaction.handle_connect(exclusive) { yield }
  rescue => error
    raise Errors::RRJErrorHandle, error
  end

  # Define an handle admin transaction and establish connection with janus
  def start_handle_admin
    @transaction ||= Janus::TransactionAdmin.new(@session)
    @transaction.handle_connect { yield }
  rescue => error
    raise Errors::RRJErrorHandle, error
  end

  # Stop an handle existing in session running
  def stop_handle
    @transaction.handle_running_stop
  end

  # Start an short transaction, this queue is not exclusive
  def handle_message_simple(type, replace = {}, add = {})
    @transaction ||= Janus::TransactionHandle.new(@session)
    @transaction.handle_connect_and_stop(false) do
      message_handle(type, replace, add).for_plugin
    end
  rescue => error
    raise Errors::RRJErrorHandle, error
  end

  private

  attr_accessor :transaction

  # Start singleton instances
  def start_instances_tools
    Tools::Env.instance
    Tools::Log.instance
    Tools::Config.instance
    Tools::Requests.instance
  end

  # Return a current session if not specified
  def use_current_session?(option)
    { 'session_id' => @session } unless option.key?('session_id')
  end
end

#transactionObject (private)

Returns the value of attribute transaction.



126
127
128
# File 'lib/rrj/init.rb', line 126

def transaction
  @transaction
end

Instance Method Details

#handle_message_simple(type, replace = {}, add = {}) ⇒ Object

Start an short transaction, this queue is not exclusive



115
116
117
118
119
120
121
122
# File 'lib/rrj/init.rb', line 115

def handle_message_simple(type, replace = {}, add = {})
  @transaction ||= Janus::TransactionHandle.new(@session)
  @transaction.handle_connect_and_stop(false) do
    message_handle(type, replace, add).for_plugin
  end
rescue => error
  raise Errors::RRJErrorHandle, error
end

#message_admin(type, options = {}) ⇒ RubyRabbitmqJanus::Janus::Response

Send a message simple for admin Janus

Examples:

Sending an message create

RubyRabbitmqJanus::RRJ.new.message_admin('admin::sessions')
#=> {"janus":"success","sessions": [12345, 8786567465465, ...] }

Parameters:

  • type (String)

    Given a type to request. JSON request writing in 'config/requests/'

  • options (Hash) (defaults to: {})

    Options update in request

Returns:



73
74
75
76
77
# File 'lib/rrj/init.rb', line 73

def message_admin(type, options = {})
  Janus::TransactionAdmin.new(@session).connect do
    Janus::MessageAdmin.new(type, options.merge!('session_id' => @session))
  end
end

#message_handle(type, replace = {}, add = {}) ⇒ RubyRabbitmqJanus::Janus::Response

Send an message in handle session in current session.

Examples:

Sending an message create

RubyRabbitmqJanus::RRJ.new.message_session('base::create')
#=> {"janus":"server_info","name":"Janus WebRTC Gateway" ... }

Parameters:

  • type (String)

    Given a type to request. JSON request writing in 'config/requests/'

  • replace (Hash) (defaults to: {})

    Options update in request

  • add (Hash) (defaults to: {})

    Elements adding to request

Returns:



88
89
90
91
# File 'lib/rrj/init.rb', line 88

def message_handle(type, replace = {}, add = {})
  options = { 'replace' => replace, 'add' => add }
  @transaction.publish_message_handle(type, options)
end

#message_session(type, options = {}, exclusive = false) ⇒ RubyRabbitmqJanus::Janus::Response

Send an message simple in current session.

Examples:

Sending an message create

RubyRabbitmqJanus::RRJ.new.message_session('base::create')
#=> {"janus":"server_info","name":"Janus WebRTC Gateway" ... }

Parameters:

  • type (String)

    Given a type to request. JSON request writing in 'config/requests/'

  • options (Hash) (defaults to: {})

    Options update in request

  • exclusive (Bollean) (defaults to: false)

    Use an exclusive queue or not

Returns:



57
58
59
60
61
62
63
# File 'lib/rrj/init.rb', line 57

def message_session(type, options = {}, exclusive = false)
  Janus::TransactionSession.new(@session).session_connect(exclusive) do
    Janus::Message.new(type, use_current_session?(options))
  end
rescue => error
  raise Errors::RRJErrorPost, error
end

#message_simple(type = 'base::info', exclusive = false) ⇒ RubyRabbitmqJanus::Janus::Response

Send an simple message to janus. No options in request with this method.

Examples:

Sending an message info

RubyRabbitmqJanus::RRJ.new.message_simple('base::info')
#=> {"janus":"server_info","name":"Janus WebRTC Gateway" ... }

Parameters:

  • type (String) (defaults to: 'base::info')

    Given a type to request. JSON request writing in 'config/requests/'

  • exclusive (Bollean) (defaults to: false)

    Use an exclusive queue or not

Returns:



41
42
43
44
45
# File 'lib/rrj/init.rb', line 41

def message_simple(type = 'base::info', exclusive = false)
  Janus::Transaction.new(@session).connect(exclusive) do
    Janus::Message.new(type)
  end
end

#start_handle(exclusive = false) ⇒ Object

Define an handle transaction and establish connection with janus



94
95
96
97
98
99
# File 'lib/rrj/init.rb', line 94

def start_handle(exclusive = false)
  @transaction ||= Janus::TransactionHandle.new(@session)
  @transaction.handle_connect(exclusive) { yield }
rescue => error
  raise Errors::RRJErrorHandle, error
end

#start_handle_adminObject

Define an handle admin transaction and establish connection with janus



102
103
104
105
106
107
# File 'lib/rrj/init.rb', line 102

def start_handle_admin
  @transaction ||= Janus::TransactionAdmin.new(@session)
  @transaction.handle_connect { yield }
rescue => error
  raise Errors::RRJErrorHandle, error
end

#start_instances_toolsObject (private)

Start singleton instances



129
130
131
132
133
134
# File 'lib/rrj/init.rb', line 129

def start_instances_tools
  Tools::Env.instance
  Tools::Log.instance
  Tools::Config.instance
  Tools::Requests.instance
end

#stop_handleObject

Stop an handle existing in session running



110
111
112
# File 'lib/rrj/init.rb', line 110

def stop_handle
  @transaction.handle_running_stop
end

#use_current_session?(option) ⇒ Boolean (private)

Return a current session if not specified

Returns:

  • (Boolean)


137
138
139
# File 'lib/rrj/init.rb', line 137

def use_current_session?(option)
  { 'session_id' => @session } unless option.key?('session_id')
end