ruby_rabbitmq_janus 0.3.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +64 -7
  3. data/.reek +7 -0
  4. data/.rubocop.yml +0 -3
  5. data/README.md +40 -2
  6. data/config/default.md +22 -6
  7. data/config/default.yml +24 -6
  8. data/config/requests/{test.json → admin/handle_info.json} +2 -4
  9. data/config/requests/admin/handles.json +6 -0
  10. data/config/requests/admin/log_level.json +6 -0
  11. data/config/requests/admin/sessions.json +5 -0
  12. data/config/requests/{attach.json → base/attach.json} +0 -0
  13. data/config/requests/{create.json → base/create.json} +0 -0
  14. data/config/requests/{destroy.json → base/destroy.json} +0 -0
  15. data/config/requests/{detach.json → base/detach.json} +0 -0
  16. data/config/requests/{info.json → base/info.json} +0 -0
  17. data/config/requests/base/keepalive.json +5 -0
  18. data/lib/generators/ruby_rabbitmq_janus/configuration_generator.rb +23 -0
  19. data/lib/generators/ruby_rabbitmq_janus/create_request_generator.rb +90 -0
  20. data/lib/generators/ruby_rabbitmq_janus/default_request_generator.rb +21 -0
  21. data/lib/generators/ruby_rabbitmq_janus/initializer_generator.rb +17 -0
  22. data/lib/rrj/errors/config.rb +21 -0
  23. data/lib/rrj/errors/error.rb +56 -0
  24. data/lib/rrj/errors/janus.rb +14 -0
  25. data/lib/rrj/errors/janus_message.rb +45 -0
  26. data/lib/rrj/errors/janus_response.rb +78 -0
  27. data/lib/rrj/errors/janus_transaction.rb +31 -0
  28. data/lib/rrj/errors/rabbit.rb +21 -0
  29. data/lib/rrj/errors/request.rb +21 -0
  30. data/lib/rrj/info.rb +40 -0
  31. data/lib/rrj/init.rb +113 -44
  32. data/lib/rrj/janus/admin.rb +27 -0
  33. data/lib/rrj/janus/janus.rb +10 -33
  34. data/lib/rrj/janus/keepalive.rb +69 -0
  35. data/lib/rrj/janus/message.rb +77 -22
  36. data/lib/rrj/janus/response.rb +79 -41
  37. data/lib/rrj/janus/transaction.rb +53 -0
  38. data/lib/rrj/janus/transaction_admin.rb +43 -0
  39. data/lib/rrj/janus/transaction_handle.rb +40 -0
  40. data/lib/rrj/janus/transaction_session.rb +17 -0
  41. data/lib/rrj/rabbit/connect.rb +75 -0
  42. data/lib/rrj/rabbit/propertie.rb +37 -0
  43. data/lib/rrj/rabbit/publish.rb +100 -0
  44. data/lib/rrj/rabbit/rabbit.rb +11 -0
  45. data/lib/rrj/tools/config.rb +63 -0
  46. data/lib/rrj/tools/env.rb +21 -0
  47. data/lib/rrj/tools/log.rb +106 -0
  48. data/lib/rrj/tools/replaces.rb +115 -0
  49. data/lib/rrj/tools/replaces_admin.rb +44 -0
  50. data/lib/rrj/tools/requests.rb +57 -0
  51. data/lib/rrj/tools/tools.rb +14 -0
  52. data/lib/ruby_rabbitmq_janus.rb +8 -13
  53. data/rrj.gemspec +7 -3
  54. metadata +108 -28
  55. data/config.reek +0 -3
  56. data/lib/rrj/config.rb +0 -57
  57. data/lib/rrj/janus/error.rb +0 -8
  58. data/lib/rrj/janus/message_async.rb +0 -54
  59. data/lib/rrj/janus/message_sync.rb +0 -34
  60. data/lib/rrj/log.rb +0 -63
  61. data/lib/rrj/rabbitmq/rabbitmq.rb +0 -82
  62. data/lib/rrj/request/path.rb +0 -21
  63. data/lib/rrj/request/replaces.rb +0 -109
  64. data/lib/rrj/request/requests.rb +0 -55
  65. data/lib/rrj/request/type_data.rb +0 -30
  66. data/lib/rrj/version.rb +0 -17
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+ # :reek:FeatureEnvy
3
+
4
+ module RubyRabbitmqJanus
5
+ module Errors
6
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
7
+ # Define an error primary for response
8
+ class JanusResponse < JanusError
9
+ def initialize(message)
10
+ super "[Response] #{message}"
11
+ end
12
+ end
13
+
14
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
15
+ # Define an error for response initalize
16
+ class JanusResponseInit < JanusResponse
17
+ def initalize(message)
18
+ super "Error create object : #{message}"
19
+ end
20
+ end
21
+
22
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
23
+ # Define an error for simple message
24
+ class JanusResponseSimple < JanusResponse
25
+ def initialize(message)
26
+ super "[#{message['code']}] #{message['reason']}"
27
+ end
28
+ end
29
+
30
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
31
+ # Define an error for plugin
32
+ class JanusResponsePlugin < JanusResponse
33
+ def initialize(message)
34
+ super "[#{message['error_code']}] #{message['error']}"
35
+ end
36
+ end
37
+
38
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
39
+ # Define an exception for json
40
+ class JanusResponseJson < JanusResponse
41
+ def initialize(message)
42
+ super "Error transform to JSON : #{message}"
43
+ end
44
+ end
45
+
46
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
47
+ # Define an exception for nice_json
48
+ class JanusResponsePrettyJson < JanusResponse
49
+ def initialize(message)
50
+ super "Error transform to Pretty JSON : #{message}"
51
+ end
52
+ end
53
+
54
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
55
+ # Define an exception for hash
56
+ class JanusResponseHash < JanusResponse
57
+ def initialize(message)
58
+ super "Error transform to Hash : #{message}"
59
+ end
60
+ end
61
+
62
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
63
+ # Define an error for response plugin
64
+ class JanusResponsePluginData < JanusResponse
65
+ def initialize(message)
66
+ super "Error data response : #{message}"
67
+ end
68
+ end
69
+
70
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
71
+ # Define an error for response data
72
+ class JanusResponseDataId < JanusResponse
73
+ def initalize(message)
74
+ super "Error Data : #{message}"
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyRabbitmqJanus
4
+ module Errors
5
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
6
+ # Define errors to message sending and response to janus
7
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
8
+ # Define an exception for initaliser transaction
9
+ class JanusTransaction < JanusError
10
+ def initialize(message)
11
+ super "[Transaction] Error initialize : #{message}"
12
+ end
13
+ end
14
+
15
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
16
+ # Define an exception for running_handle
17
+ class JanusTransactionHandle
18
+ def initalize(message)
19
+ super "[Transaction] Error handle #{message}"
20
+ end
21
+ end
22
+
23
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
24
+ # Define an exception for sending_message
25
+ class JanusTransactionPost < JanusError
26
+ def initialize(message)
27
+ super "[Transaction] Error sending message : #{message}"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyRabbitmqJanus
4
+ module Errors
5
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
6
+ # Define errors with Rabbitmq when request is not sending
7
+ class RequestNotExecuted < Errors::RRJError
8
+ def initialize(request)
9
+ super "Error in request executed : #{request}", :error
10
+ end
11
+ end
12
+
13
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
14
+ # Define errors if connection to rabbitmq is failed
15
+ class ConnectionRabbitmqFailed < Errors::RRJError
16
+ def initialize(message)
17
+ super message, :fatal
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyRabbitmqJanus
4
+ module Errors
5
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
6
+ # Define errors with Rabbitmq when connection is failed
7
+ class RequestTemplateNotExist < Errors::RRJError
8
+ def initialize(request_name)
9
+ super "The template request (#{request_name}) does not exist", :fatal
10
+ end
11
+ end
12
+
13
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
14
+ # Define errors if request folder does not exist
15
+ class RequestFolerDoesntExist < Errors::RRJError
16
+ def initialize
17
+ super 'The requests folder does not exist', :fatal
18
+ end
19
+ end
20
+ end
21
+ end
data/lib/rrj/info.rb ADDED
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
3
+ # Define constant to gem.
4
+ module RubyRabbitmqJanus
5
+ # Define version to gem
6
+ VERSION = '1.0.1'
7
+
8
+ # Define a summary description to gem
9
+ SUMMARY = 'Ruby RabbitMQ Janus'
10
+
11
+ # Define a long description to gem
12
+ DESCRIPTION = 'This gem is used to communicate to a server Janus through RabbitMQ '\
13
+ 'software (Message-oriented middleware). It waiting a messages to Rails API who ' \
14
+ 'send to RabbitMQ server in a queue for janus server. Janus processes a message ' \
15
+ 'and send to RabbitMQ server in a queue for gem. Once the received message is ' \
16
+ 'decoded and returned through the Rails API.'
17
+
18
+ # Define homepage
19
+ HOMEPAGE = 'https://github.com/dazzl-tv/ruby-rabbitmq-janus'
20
+
21
+ # Define a post install message
22
+ POST_INSTALL = \
23
+ "# ====================================================== #\n" \
24
+ "# Thanks for installing! #\n" \
25
+ "# #{HOMEPAGE}. #\n" \
26
+ "# ;;;;;;;;;;;:. #\n" \
27
+ "# ;;;;;;;;;;;;;;;;;; #\n" \
28
+ "# ;;;;;;;;;;;:;;;;;;;; #\n" \
29
+ "# ;;;;;;;;;;` ;;;;;;;; #\n" \
30
+ "# ;;;;;;;;; :;;;;;;;;. #\n" \
31
+ "# ;;;;;;;; :::::;;;;; #\n" \
32
+ "# ;;;;;;, ,;;;;;; #\n" \
33
+ "# ;;;;; ;;;;;;;; #\n" \
34
+ "# ;;;;;;;;; ;;;;;;;;, #\n" \
35
+ "# ;;;;;;;;; `;;;;;;;;; A ZZZZZZZ ZZZZZZZ LL #\n" \
36
+ "# ;;;;;;;;.:;;;;;;;;;; A A ZZZ ZZZ LL #\n" \
37
+ "# ;;;;;;;;;;;;;;;;; AAAAA ZZ ZZ LL #\n" \
38
+ "# ;;;;;;;;;;;;;, A A ZZZZZZZ ZZZZZZZ LLLLLLL #\n" \
39
+ '# ====================================================== #'
40
+ end
data/lib/rrj/init.rb CHANGED
@@ -1,73 +1,142 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'singleton'
4
+ require 'yaml'
5
+ require 'json'
6
+ require 'securerandom'
7
+ require 'bunny'
8
+ require 'logger'
9
+ require 'key_path'
10
+ require 'active_support'
11
+ require 'concurrent'
12
+ require 'colorize'
13
+
3
14
  module RubyRabbitmqJanus
4
15
  # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
5
- # Initialize gem
16
+ # Initialize gem and create automatically an session with Janus
6
17
  # @!attribute [r] session
7
- # @return [Hash] Response to request sending in transaction
18
+ # :reek:BooleanParameter
8
19
  class RRJ
9
20
  attr_reader :session
10
21
 
11
22
  # Returns a new instance of RubyRabbitmqJanus
12
23
  def initialize
13
- Log.instance
14
- Config.instance
15
- Requests.instance
24
+ Tools::Env.instance
25
+ Tools::Log.instance
26
+ Tools::Config.instance
27
+ Tools::Requests.instance
16
28
 
17
- @rabbit = RabbitMQ.new
29
+ @session = Janus::Keepalive.new.session
18
30
 
19
- @session = nil
31
+ @transaction = nil
32
+ rescue => error
33
+ raise Errors::RRJErrorInit, error
20
34
  end
21
35
 
22
- # Send a message, to RabbitMQ, with a template JSON
23
- # @return [Hash] Contains information to request sending
24
- # @param template_used [String] Json used to request sending in RabbitMQ
25
- # @param [Hash] opts the options sending with a request
26
- # @option opts [String] :janus The message type
27
- # @option opts [String] :transaction The transaction identifier
28
- # @option opts [Hash] :data The option data to request
29
- def message_template_ask_sync(template_used = 'info', opts = {})
30
- @rabbit.ask_request_sync(template_used, opts)
36
+ # Send an simple message to janus. No options in request with this method.
37
+ # @param [String] type
38
+ # Given a type to request. JSON request writing in 'config/requests/'
39
+ # @param [Bollean] exclusive
40
+ # Use an exclusive queue or not
41
+ # @exemple Sending an message info
42
+ # RubyRabbitmqJanus::RRJ.new.message_simple('base::info')
43
+ # #=> {"janus":"server_info","name":"Janus WebRTC Gateway" ... }
44
+ # @return [RubyRabbitmqJanus::Janus::Response] Give an object response to janus server
45
+ def message_simple(type = 'base::info', exclusive = false)
46
+ Janus::Transaction.new(@session).connect(exclusive) do
47
+ Janus::Message.new(type)
48
+ end
31
49
  end
32
50
 
33
- # Send a message to RabbitMQ for reading a response
34
- # @return [Hash] Contains a response to request sending
35
- # @param info_request [Hash] Contains information to request sending
36
- # @option info_request [String] :janus The message type
37
- # @option info_request [String] :transaction The transaction identifier
38
- # @option info_request [Hash] :data The option data to request
39
- def message_template_response(info_request)
40
- @rabbit.ask_response(info_request)
51
+ # Send an message simple in current session.
52
+ # @param [String] type
53
+ # Given a type to request. JSON request writing in 'config/requests/'
54
+ # @param [Hash] options Options update in request
55
+ # @param [Bollean] exclusive
56
+ # Use an exclusive queue or not
57
+ # @exemple Sending an message create
58
+ # RubyRabbitmqJanus::RRJ.new.message_session('base::create')
59
+ # #=> {"janus":"server_info","name":"Janus WebRTC Gateway" ... }
60
+ # @return [RubyRabbitmqJanus::Janus::Response] Give an object response to janus server
61
+ def message_session(type, options = {}, exclusive = false)
62
+ Janus::TransactionSession.new(@session).session_connect(exclusive) do
63
+ Janus::Message.new(type, use_current_session?(options))
64
+ end
65
+ rescue => error
66
+ raise Errors::RRJErrorPost, error
41
67
  end
42
68
 
43
- # Manage a transaction with an plugin in janus
44
- # Is create an session and attach with a plugin configured in file conf to gem, then
45
- # when a treatment is complet is destroy a session
46
- # @yieldparam session_attach [Hash] Use a session created
47
- # @yieldreturn [Hash] Contains a result to transaction with janus server
48
- def transaction_plugin
49
- @session = yield attach_session
50
- destroy_session
69
+ # Send a message simple for admin Janus
70
+ # @param [String] type
71
+ # Given a type to request. JSON request writing in 'config/requests/'
72
+ # @param [Hash] options Options update in request
73
+ # @exemple Sending an message create
74
+ # RubyRabbitmqJanus::RRJ.new.message_admin('admin::sessions')
75
+ # #=> {"janus":"success","sessions": [12345, 8786567465465, ...] }
76
+ # @return [RubyRabbitmqJanus::Janus::Response] Give an object response to janus server
77
+ def message_admin(type, options = {})
78
+ Janus::TransactionAdmin.new(@session).connect do
79
+ Janus::MessageAdmin.new(type, options.merge!('session_id' => @session))
80
+ end
51
81
  end
52
82
 
53
- def message_template_ask_async(template_used = 'info', opts = {})
54
- @rabbit.ask_request_async(template_used, opts)
83
+ # Send an message in handle session in current session.
84
+ # @param [String] type
85
+ # Given a type to request. JSON request writing in 'config/requests/'
86
+ # @param [Hash] options Options update in request
87
+ # @param [Bollean] exclusive
88
+ # Use an exclusive queue or not
89
+ # @exemple Sending an message create
90
+ # RubyRabbitmqJanus::RRJ.new.message_session('base::create')
91
+ # #=> {"janus":"server_info","name":"Janus WebRTC Gateway" ... }
92
+ # @return [RubyRabbitmqJanus::Janus::Response] Give an object response to janus server
93
+ def message_handle(type, replace = {}, add = {})
94
+ options = { 'replace' => replace, 'add' => add }
95
+ Tools::Log.instance.debug "Transaction : #{@transaction}"
96
+ @transaction.publish_message_handle(type, options)
55
97
  end
56
98
 
57
- alias ask_async message_template_ask_async
58
- alias ask_sync message_template_ask_sync
59
- alias response_sync message_template_response
99
+ # Manage a transaction simple. Just for create element or destroy
100
+ # def transaction_simple(type, replace = {}, add = {})
101
+ # options = { 'replace' => replace, 'add' => add }
102
+ # Janus::Transaction.new(@session).handle_running_simple(type, options)
103
+ # end
60
104
 
61
- private
105
+ # Define an handle and establish connection with janus
106
+ def start_handle(exclusive = false)
107
+ @transaction = Janus::TransactionHandle.new(@session)
108
+ @transaction.handle_connect(exclusive) { yield }
109
+ rescue => error
110
+ raise Errors::RRJErrorHandle, error
111
+ end
112
+
113
+ def start_handle_admin
114
+ @transaction = Janus::TransactionAdmin.new(@session)
115
+ @transaction.handle_connect { yield }
116
+ rescue => error
117
+ raise Errors::RRJErrorHandle, error
118
+ end
62
119
 
63
- def attach_session
64
- Log.instance.debug 'Create an session'
65
- response_sync(ask_sync('attach', ask_sync('create')))
120
+ # Stop an handle existing in session running
121
+ def stop_handle
122
+ @transaction.handle_running_stop
66
123
  end
67
124
 
68
- def destroy_session
69
- Log.instance.debug 'Destroy an session'
70
- response_sync(ask_sync('destroy', response_sync(ask_sync('detach', @session))))
125
+ # Start an short transaction, this queue is not exclusive
126
+ def handle_message_simple(type, replace = {}, add = {})
127
+ @transaction = Janus::TransactionHandle.new(@session)
128
+ @transaction.handle_connect_and_stop(false) do
129
+ message_handle(type, replace, add).for_plugin
130
+ end
131
+ rescue => error
132
+ raise Errors::RRJErrorHandle, error
133
+ end
134
+
135
+ private
136
+
137
+ # Return a current session if not specified
138
+ def use_current_session?(option)
139
+ { 'session_id' => @session } unless option.key?('session_id')
71
140
  end
72
141
  end
73
142
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+ # :reek:InstanceVariableAssumption
3
+
4
+ module RubyRabbitmqJanus
5
+ module Janus
6
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
7
+ # Create an message for janus
8
+ class MessageAdmin < Message
9
+ # Return options to message for rabbitmq
10
+ def options
11
+ Tools::Log.instance.debug 'Options used for admin message'
12
+ @properties.options_admin
13
+ rescue => error
14
+ raise Errors::JanusMessagePropertie, error
15
+ end
16
+
17
+ private
18
+
19
+ # Transform raw request in request to janus, so replace element <string>, <number>
20
+ # and other with real value
21
+ def prepare_request(options)
22
+ @request = Tools::AdminReplace.new(@request, options).transform_request
23
+ Tools::Log.instance.debug "Prepare request admin for janus : #{to_json}"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,39 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
3
+ require 'rrj/janus/keepalive'
4
+ require 'rrj/janus/message'
5
+ require 'rrj/janus/admin'
6
+ require 'rrj/janus/response'
7
+ require 'rrj/janus/transaction'
8
+ require 'rrj/janus/transaction_session'
9
+ require 'rrj/janus/transaction_handle'
10
+ require 'rrj/janus/transaction_admin'
4
11
 
5
12
  module RubyRabbitmqJanus
6
- # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
7
- # Communication to RabbitMQ with format for Janus message
8
- class Janus
9
- # Returns a new instance of Janus
10
- # @param connection [String] Connection to RabbitMQ server
11
- # @param logs [RRJ::Log] Instance to log
12
- def initialize(connection)
13
- @channel = connection.create_channel
14
- end
15
-
16
- # Send a message to RabbitMQ
17
- # @param request [String] Type request used by transaction
18
- # @param opts [Hash] Contains the parameters used by request if necessary
19
- # @return [Hash] Result to request
20
- def send(request, opts)
21
- message = Sync.new(opts, @channel)
22
- message.send(request)
23
- end
24
-
25
- def send_async(request, opts)
26
- message = ASync.new(opts, @channel)
27
- message.send(request)
28
- end
29
-
30
- # Read a message to RabbitMQ
31
- # @param info_message [Hash] Information request asking to janus
32
- # @param connection [Object] Object contains a information to connection
33
- # @return [Hash] Result to request
34
- def read(info_message, connection)
35
- response = ResponseJanus.new(@channel, connection, info_message)
36
- response.read
37
- end
13
+ # Module interaction with Janus
14
+ module Janus
38
15
  end
39
16
  end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyRabbitmqJanus
4
+ module Janus
5
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
6
+ # Manage sending keepalive message
7
+ # :reek:TooManyInstanceVariables { max_instance_variables: 6 }
8
+ class Keepalive
9
+ # Initalize a keepalive message
10
+ def initialize
11
+ @response, @publish = nil
12
+ @rabbit = Rabbit::Connect.new
13
+ @lock = Mutex.new
14
+ @condition = ConditionVariable.new
15
+ session_live
16
+ end
17
+
18
+ # Return an number session created
19
+ def session
20
+ @lock.synchronize { @condition.wait(@lock) }
21
+ @response.session
22
+ end
23
+
24
+ private
25
+
26
+ # Send to regular interval a message keepalive
27
+ def session_live
28
+ Thread.new do
29
+ Tools::Log.instance.debug 'Create an session for keepalive'
30
+ initialize_thread
31
+ end
32
+ end
33
+
34
+ # Initialize an session with janus and start a keepalive transaction
35
+ def initialize_thread
36
+ @rabbit.start
37
+ @response = session_start
38
+ @lock.synchronize { @condition.signal }
39
+ session_keepalive(ttl)
40
+ @rabbit.close
41
+ end
42
+
43
+ # Star an session janus
44
+ def session_start
45
+ msg_create = Janus::Message.new 'base::create'
46
+ @publish = Rabbit::PublishExclusive.new(@rabbit.channel, '')
47
+ Janus::Response.new @publish.send_a_message(msg_create)
48
+ end
49
+
50
+ # Create an loop for sending a keepalive message
51
+ def session_keepalive(time_to_live)
52
+ loop do
53
+ sleep time_to_live
54
+ @publish.send_a_message(Janus::Message.new('base::keepalive',
55
+ 'session_id' => @response.session))
56
+ end
57
+ rescue => message
58
+ Tools::Log.instance.debug "Error keepalive : #{message}"
59
+ end
60
+
61
+ # Define a Time To Live between each request sending to janus
62
+ def ttl
63
+ Tools::Config.instance.options['gem']['session']['keepalive'].to_i
64
+ rescue => error
65
+ Tools::Log.instance.debug "TTL Not loading - #{error}"
66
+ end
67
+ end
68
+ end
69
+ end
@@ -1,31 +1,86 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'securerandom'
4
- require 'thread'
5
-
6
3
  module RubyRabbitmqJanus
7
- # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
8
- # Message Janus sending to rabbitmq server
9
- class MessageJanus
10
- # Initialiaze a message posting to RabbitMQ
11
- # @param plugins [String] Name of plugin used by transaction
12
- # @param logs [RubyRabbitmqJanus::Log] Instance log
13
- # @param opts [Hash] Contains information to request sending
14
- def initialize(opts_request, channel)
15
- @correlation = SecureRandom.uuid
16
- @options_request = opts_request
17
- @channel = channel
18
- end
4
+ module Janus
5
+ # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
6
+ # Create an message for janus
7
+ class Message
8
+ attr_reader :type
9
+
10
+ # Instanciate an message
11
+ # @param template_request [String] Name of request prepare
12
+ # @param [Hash] options Options to request (replace element or add in body)
13
+ # @option options [String] :session_id Identifier to session
14
+ # @option options [String] :handle_id Identifier to session manipulate
15
+ # @option options [Hash] :other Element contains in request sending to janus
16
+ # @example Initialize a message
17
+ # Message.new('test', {
18
+ # "session_id": 42,
19
+ # "handle_id": 42,
20
+ # "replace": {
21
+ # "audio": false,
22
+ # "video": true
23
+ # },
24
+ # "add": {
25
+ # "subtitle": true
26
+ # })
27
+ def initialize(template_request, options = {})
28
+ @request = {}
29
+ @type = template_request
30
+ @properties = Rabbit::Propertie.new
31
+ load_request_file
32
+ prepare_request(options)
33
+ rescue => error
34
+ raise Errors::JanusMessage, error
35
+ end
36
+
37
+ # Return request to json format
38
+ def to_json
39
+ @request.to_json
40
+ rescue => error
41
+ raise Errors::JanusMessageJson, error
42
+ end
43
+
44
+ # Return request to json format with nice format
45
+ def to_nice_json
46
+ JSON.pretty_generate to_hash
47
+ rescue => error
48
+ raise Errors::JanusMessagePrettyJson, error
49
+ end
50
+
51
+ # Return request to hash format
52
+ def to_hash
53
+ @request
54
+ rescue => error
55
+ raise Errors::JanusMessageHash, error
56
+ end
57
+
58
+ # Return options to message for rabbitmq
59
+ def options
60
+ @properties.options
61
+ rescue => error
62
+ raise Errors::JanusMessagePropertie, error
63
+ end
64
+
65
+ # Return correlation to message
66
+ def correlation
67
+ @properties.correlation
68
+ end
19
69
 
20
- private
70
+ private
21
71
 
22
- attr_reader :channel, :options_request, :opts, :correlation, :my_request, :message
23
- attr_reader :log
72
+ # Load raw request
73
+ def load_request_file
74
+ @request = JSON.parse File.read Tools::Requests.instance.requests[@type]
75
+ Tools::Log.instance.debug "Opening request : #{to_json}"
76
+ end
24
77
 
25
- def define_request_sending(json)
26
- request = Replace.new(json, @options_request)
27
- @my_request = request.to_hash
28
- request.to_json
78
+ # Transform raw request in request to janus, so replace element <string>, <number>
79
+ # and other with real value
80
+ def prepare_request(options)
81
+ @request = Tools::Replace.new(@request, options).transform_request
82
+ Tools::Log.instance.debug "Prepare request for janus : #{to_json}"
83
+ end
29
84
  end
30
85
  end
31
86
  end