wamp 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/lib/wamp.rb CHANGED
@@ -13,10 +13,10 @@ module WAMP
13
13
  autoload :MessageType, File.join(ROOT, "wamp", "message_type")
14
14
 
15
15
  module Engines
16
- autoload :Memory, File.join(ROOT, "wamp", "engines", "memory")
16
+ autoload :Memory, File.join(ROOT, "wamp", "engines", "memory")
17
+ autoload :Redis, File.join(ROOT, "wamp", "engines", "redis")
17
18
  end
18
19
 
19
- # autoload :Protocols, File.join(ROOT, "wamp", "protocols")
20
20
  module Protocols
21
21
  autoload :Version1, File.join(ROOT, "wamp", "protocols", "version_1")
22
22
  end
@@ -11,7 +11,7 @@ module WAMP
11
11
  # Creates a new instance of the memory engine as well as some empty hashes
12
12
  # for holding clients and topics.
13
13
  # @param options [Hash] Optional. Options hash for the memory engine.
14
- def initialize(options = {})
14
+ def initialize(options)
15
15
  @options = options
16
16
  @clients = {}
17
17
  @topics = {}
@@ -86,8 +86,18 @@ module WAMP
86
86
  topic
87
87
  end
88
88
 
89
+ def create_event(client, topic_uri, payload, exclude, include)
90
+ topic = find_or_create_topic(topic_uri)
91
+
92
+ topic.publish(client, protocol, payload, exclude, include) if topic
93
+ end
94
+
89
95
  private
90
96
 
97
+ def protocol
98
+ WAMP::Protocols::Version1.new
99
+ end
100
+
91
101
  def new_client(websocket)
92
102
  WAMP::Socket.new(random_uuid, websocket)
93
103
  end
@@ -0,0 +1,82 @@
1
+ require 'redis'
2
+ require 'json'
3
+
4
+ module WAMP
5
+ module Engines
6
+ class Redis < Memory
7
+ attr_accessor :clients, :topics
8
+
9
+ DEFAULT_HOST = 'localhost'
10
+ DEFAULT_PORT = 6379
11
+ DEFAULT_DATABASE = 0
12
+ DEFAULT_GC = 60
13
+ LOCK_TIMEOUT = 120
14
+
15
+ # Creates a new instance of the Redis engine and sets up the connections
16
+ # to the Redis server
17
+ def initialize(options)
18
+ super
19
+
20
+ @host = options[:host] || DEFAULT_HOST
21
+ @port = options[:port] || DEFAULT_PORT
22
+ @database = options[:database] || DEFAULT_DATABASE
23
+ @gc = options[:gc] || DEFAULT_GC
24
+ @password = options[:password]
25
+ @namespace = options[:namespace] || ''
26
+
27
+ @clients_ns = @namespace + ":clients"
28
+ @topics_ns = @namespace + ":topics"
29
+ @prefixes_ns = @namespace + ":prefixes"
30
+ @events_ns = @namespace + ":events"
31
+
32
+ @completed_events = []
33
+
34
+ @redis = ::Redis.new(host: @host, port: @port)
35
+ @subscriber = ::Redis.new(host: @host, port: @port)
36
+
37
+ if @password
38
+ @redis.auth(@password)
39
+ @subscriber.auth(@password)
40
+ end
41
+
42
+ @redis.select(@database)
43
+ @subscriber.select(@database)
44
+
45
+ redis_subscribe_to_events
46
+ end
47
+
48
+ def create_event(client, topic_uri, payload, excluded, included)
49
+ redis_create_event(client.id, topic_uri, protocol, payload, excluded, included)
50
+ end
51
+
52
+ private
53
+
54
+ def redis_subscribe_to_events
55
+ Thread.new do
56
+ @subscriber.subscribe(@events_ns) do |on|
57
+ on.message do |channel, message|
58
+ redis_handle_message(message)
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ def redis_handle_message(message)
65
+ begin
66
+ id, client_id, topic_uri, payload, excluded, included = JSON.parse(message)
67
+
68
+ client = find_clients(id: client_id).first
69
+ topic = find_or_create_topic(topic_uri)
70
+
71
+ topic.publish(client, protocol, payload, excluded, included) if topic
72
+ rescue => e
73
+ puts e
74
+ end
75
+ end
76
+
77
+ def redis_create_event(client_id, topic_uri, protocol, payload, excluded, included)
78
+ @redis.publish @events_ns, [random_uuid, client_id, topic_uri, payload, excluded, included].to_json
79
+ end
80
+ end
81
+ end
82
+ end
data/lib/wamp/server.rb CHANGED
@@ -5,14 +5,16 @@ module WAMP
5
5
  class Server
6
6
  include WAMP::Bindable
7
7
 
8
- attr_accessor :options, :topics, :callbacks
8
+ attr_accessor :options, :topics, :callbacks, :engine
9
9
 
10
10
  def initialize(options = {})
11
11
  @options = options
12
+ @options[:engine] ||= {}
13
+ @options[:engine][:type] ||= :memory
12
14
 
13
15
  @topics = {}
14
16
  @callbacks = {}
15
- @engine = WAMP::Engines::Memory.new
17
+ @engine = WAMP::Engines.const_get(camelize(@options[:engine][:type])).new(@options[:engine])
16
18
  @protocol = WAMP::Protocols::Version1.new
17
19
  end
18
20
 
@@ -40,9 +42,13 @@ module WAMP
40
42
 
41
43
  private
42
44
 
45
+ def camelize(str)
46
+ str.to_s.split('_').map {|w| w.capitalize}.join
47
+ end
48
+
43
49
  def handle_open(websocket, event)
44
50
  client = @engine.create_client(websocket)
45
- client.websocket.send @protocol.welcome(client.id)
51
+ client.websocket.send @protocol.welcome(client.id)
46
52
 
47
53
  trigger(:connect, client)
48
54
  end
@@ -105,12 +111,11 @@ module WAMP
105
111
  # Handle a message published by a client
106
112
  # PUBLISH data structure [TOPIC, DATA, EXCLUDE, INCLUDE]
107
113
  def handle_publish(client, data)
108
- topic_name, payload, exclude, include = data
109
- topic = @engine.find_or_create_topic(topic_name)
114
+ topic_uri, payload, exclude, include = data
110
115
 
111
- topic.publish(client, @protocol, payload, exclude, include)
116
+ @engine.create_event(client, topic_uri, payload, exclude, include)
112
117
 
113
- trigger(:publish, client, topic.uri, payload, exclude, include)
118
+ trigger(:publish, client, topic_uri, payload, exclude, include)
114
119
  end
115
120
 
116
121
  def handle_close(websocket, event)
data/lib/wamp/socket.rb CHANGED
@@ -25,6 +25,14 @@ module WAMP
25
25
  prefixes[prefix] = uri
26
26
  end
27
27
 
28
+ def to_json
29
+ {
30
+ id: id,
31
+ topics: topics,
32
+ prefixes: prefixes
33
+ }.to_json
34
+ end
35
+
28
36
  private
29
37
 
30
38
  def send_welcome_message
data/wamp.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "wamp"
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brady Love"]
@@ -35,6 +35,7 @@ Gem::Specification.new do |s|
35
35
  "lib/wamp/bindable.rb",
36
36
  "lib/wamp/client.rb",
37
37
  "lib/wamp/engines/memory.rb",
38
+ "lib/wamp/engines/redis.rb",
38
39
  "lib/wamp/message_type.rb",
39
40
  "lib/wamp/protocols/version_1.rb",
40
41
  "lib/wamp/server.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wamp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -230,6 +230,7 @@ files:
230
230
  - lib/wamp/bindable.rb
231
231
  - lib/wamp/client.rb
232
232
  - lib/wamp/engines/memory.rb
233
+ - lib/wamp/engines/redis.rb
233
234
  - lib/wamp/message_type.rb
234
235
  - lib/wamp/protocols/version_1.rb
235
236
  - lib/wamp/server.rb
@@ -261,7 +262,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
261
262
  version: '0'
262
263
  segments:
263
264
  - 0
264
- hash: 2829213371230437822
265
+ hash: 2060316815058376274
265
266
  required_rubygems_version: !ruby/object:Gem::Requirement
266
267
  none: false
267
268
  requirements: