myxi 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a50043a96e96ff44cef317e03314866dc254a39c
4
- data.tar.gz: e2a1f27a1bc4a5b839d22ecc64a1bbedb78ddef8
3
+ metadata.gz: cdd11c1300de89acea4cb8a73d3992613d58f7f2
4
+ data.tar.gz: c2ea7abf4a441def1601963eb2ffe7f4e50dedeb
5
5
  SHA512:
6
- metadata.gz: b5befc8b9663554974828df4676d57a4f92faacba6038d50449e5577dce6ea25eba1ea5778a42561c86e87d3e89b7e6a3e137e1592f35ce4f97c6d9257562071
7
- data.tar.gz: e5be4621e3703edb1ae32f5bd4bc1910a37c135573761d63afb11110085e80d72a38af250dc5d9b4c4945650d2f257bad36fd4b1e8e76cfdb29dbdc0dfe4b8f7
6
+ metadata.gz: 2efc388890bf6c0650b658af3179dcba083a970cc9802b39b43892b5d006a87b188e3d3cb05cc19ababdcdbf03fb03fe315572bef505363233b3ab9129cad33f
7
+ data.tar.gz: a102bd2d3fb000bfa6aa700133a2c2ab439e222d69897db61a76b1752fbd8994c8438b85dde6e7efd803e0e5391be4832448a1854ee43c0dd38794b372aeeee1
data/lib/myxi.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'logger'
1
2
  require 'myxi/exchange'
2
3
  require 'myxi/railtie' if defined?(Rails)
3
4
 
@@ -6,6 +7,14 @@ module Myxi
6
7
 
7
8
  class Error < StandardError; end
8
9
 
10
+ #
11
+ # Return a logger
12
+ #
13
+ def logger
14
+ @logger ||= Logger.new(STDOUT)
15
+ end
16
+ attr_writer :logger
17
+
9
18
  #
10
19
  # Return a bunny client instance which will be used by the web socket service.
11
20
  # This can be overriden if you already have a connection RabbitMQ available
data/lib/myxi/server.rb CHANGED
@@ -13,12 +13,6 @@ module Myxi
13
13
  @options = options
14
14
  end
15
15
 
16
- def log(message)
17
- if options[:debug]
18
- puts message
19
- end
20
- end
21
-
22
16
  def sessions
23
17
  @sessions ||= []
24
18
  end
@@ -37,7 +31,7 @@ module Myxi
37
31
  def run
38
32
  Myxi::Exchange.declare_all
39
33
  port = (options[:port] || ENV['MYXI_PORT'] || ENV['PORT'] || 5005).to_i
40
- puts "Running Myxi Web Socket Server on 0.0.0.0:#{port}"
34
+ Myxi.logger.info "Running Myxi Web Socket Server on 0.0.0.0:#{port}"
41
35
  monitor_sessions
42
36
  EM.run do
43
37
  EM::WebSocket.run(:host => options[:bind_address] || ENV['MYXI_BIND_ADDRESS'] || '0.0.0.0', :port => port) do |ws|
@@ -47,7 +41,7 @@ module Myxi
47
41
  ws.onopen do |handshake|
48
42
  case handshake.path
49
43
  when /\A\/pushwss/
50
- log "[#{session.id}] Connection opened"
44
+ Myxi.logger.debug "[#{session.id}] Connection opened"
51
45
  ws.send({:event => 'Welcome', :payload => {:id => session.id}}.to_json)
52
46
 
53
47
  session.queue = Myxi.channel.queue("", :exclusive => true)
@@ -58,15 +52,14 @@ module Myxi
58
52
  end
59
53
  end
60
54
  else
61
- log "[#{session.id}] Invalid path"
55
+ Myxi.logger.debug "[#{session.id}] Invalid path"
62
56
  ws.send({:event => 'Error', :payload => {:error => 'PathNotFound'}}.to_json)
63
57
  ws.close
64
58
  end
65
59
  end
66
60
 
67
61
  ws.onclose do
68
- log "[#{session.id}] Disconnected"
69
- session.queue.delete if session.queue
62
+ session.close
70
63
  sessions.delete(session)
71
64
  end
72
65
 
@@ -76,7 +69,7 @@ module Myxi
76
69
  if json.is_a?(Hash)
77
70
  session.tag = json['tag'] || nil
78
71
  payload = json['payload'] || {}
79
- if action = Myxi::Action::ACTIONS[json['action'].to_sym]
72
+ if action = Myxi::Action::ACTIONS[json['action'].to_s.to_sym]
80
73
  action.execute(session, payload)
81
74
  else
82
75
  ws.send({:event => 'Error', :tag => session.tag, :payload => {:error => 'InvalidAction'}}.to_json)
data/lib/myxi/session.rb CHANGED
@@ -8,6 +8,8 @@ module Myxi
8
8
  @server = server
9
9
  @ws = ws
10
10
  @id = SecureRandom.hex(8)
11
+ @closure_callbacks = []
12
+ @data = {}
11
13
  end
12
14
 
13
15
  attr_reader :id
@@ -17,6 +19,15 @@ module Myxi
17
19
  attr_accessor :auth_object
18
20
  attr_accessor :tag
19
21
 
22
+ def [](name)
23
+ @data[name.to_sym]
24
+ end
25
+
26
+ def []=(name, value)
27
+ Myxi.logger.debug "[#{id}] Stored '#{name}' with '#{value}'"
28
+ @data[name.to_sym] = value
29
+ end
30
+
20
31
  #
21
32
  # Keep track of all subscriptions
22
33
  #
@@ -37,11 +48,15 @@ module Myxi
37
48
  def subscribe(exchange_name, routing_key)
38
49
  if exchange = Myxi::Exchange::EXCHANGES[exchange_name.to_sym]
39
50
  if exchange.can_subscribe?(routing_key, self.auth_object)
40
- queue.bind(exchange.exchange_name.to_s, :routing_key => routing_key.to_s)
41
51
  subscriptions[exchange_name.to_s] ||= []
42
- subscriptions[exchange_name.to_s] << routing_key.to_s
43
- server.log "[#{id}] Subscribed to #{exchange_name} / #{routing_key}"
44
- send('Subscribed', :exchange => exchange_name, :routing_key => routing_key)
52
+ if subscriptions[exchange_name.to_s].include?(routing_key.to_s)
53
+ send('Error', :error => 'AlreadySubscribed', :exchange => exchange_name, :routing_key => routing_key)
54
+ else
55
+ queue.bind(exchange.exchange_name.to_s, :routing_key => routing_key.to_s)
56
+ subscriptions[exchange_name.to_s] << routing_key.to_s
57
+ Myxi.logger.debug "[#{id}] Subscribed to #{exchange_name} / #{routing_key}"
58
+ send('Subscribed', :exchange => exchange_name, :routing_key => routing_key)
59
+ end
45
60
  else
46
61
  send('Error', :error => 'SubscriptionDenied', :exchange => exchange_name, :routing_key => routing_key)
47
62
  end
@@ -58,7 +73,7 @@ module Myxi
58
73
  if subscriptions[exchange_name.to_s]
59
74
  subscriptions[exchange_name.to_s].delete(routing_key.to_s)
60
75
  end
61
- server.log "[#{id}] Unsubscribed from #{exchange_name}/#{routing_key}"
76
+ Myxi.logger.debug "[#{id}] Unsubscribed from #{exchange_name}/#{routing_key}"
62
77
  send('Unsubscribed', :exchange_name => exchange_name, :routing_key => routing_key, :auto => auto)
63
78
  end
64
79
 
@@ -91,7 +106,7 @@ module Myxi
91
106
  if exchange = Myxi::Exchange::EXCHANGES[exchange_name.to_sym]
92
107
  routing_keys.each do |routing_key|
93
108
  unless exchange.can_subscribe?(routing_key, self.auth_object)
94
- puts "[#{id}] Session is not longer allowed to subscibe to #{exchange_name}/#{routing_key}"
109
+ Myxi.logger.info "[#{id}] Session is not longer allowed to subscibe to #{exchange_name}/#{routing_key}"
95
110
  unsubscribe(exchange_name, routing_key, true)
96
111
  end
97
112
  end
@@ -99,5 +114,23 @@ module Myxi
99
114
  end
100
115
  end
101
116
 
117
+ #
118
+ # Called when the connection for this session is closed
119
+ #
120
+ def close
121
+ Myxi.logger.debug "[#{id}] Session closed"
122
+ self.queue.delete if self.queue
123
+ while callback = @closure_callbacks.shift
124
+ callback.call
125
+ end
126
+ end
127
+
128
+ #
129
+ # Adds a callback to be executed when this session closes
130
+ #
131
+ def on_close(&block)
132
+ @closure_callbacks << block
133
+ end
134
+
102
135
  end
103
136
  end
data/lib/myxi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Myxi
2
- VERSION = '1.1.1'
2
+ VERSION = '1.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: myxi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-08 00:00:00.000000000 Z
11
+ date: 2017-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny
@@ -89,9 +89,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  version: '0'
90
90
  requirements: []
91
91
  rubyforge_project:
92
- rubygems_version: 2.4.5
92
+ rubygems_version: 2.5.2
93
93
  signing_key:
94
94
  specification_version: 4
95
95
  summary: A RabbitMQ-based web socket server & framework
96
96
  test_files: []
97
- has_rdoc: