easy_wamp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 319bba03e93e5f511544960b5e898018354f7a93
4
+ data.tar.gz: 41341b9ed62dff61a39df9997c65b4ab34812a40
5
+ SHA512:
6
+ metadata.gz: 3c7b2c2488b47e31ef86965b5c7a7838fed621a9c649ca34eb3f40a57b2707b26d671effdc6e4d86a09248a38fb0ca0c59075dfee17a3346ddc9d4d6b92b1e11
7
+ data.tar.gz: c750f17df7b685fafd88a1364b3535826eec05a565836f0fb5a575642d7d240709089534f5a1e004fd3d079ee19caf258fc6c354e29076a4144cd46bf60adb74
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in easy_wamp.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Curtis Bissonnette
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # EasyWamp
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'easy_wamp'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install easy_wamp
18
+
19
+ ## Usage
20
+
21
+ I designed the syntax to be close to DRB. The following will start the wamp server.
22
+
23
+ EasyWamp::WampServer.start_service("localhost", 9090, TestApi.new)
24
+
25
+ Where all WAMP remote procedure calls will be sent to the TestApi class. To publish an event on the server simply call:
26
+
27
+ EasyWamp::WampServer.publish_event("localhost/test/uri, "event_name", [exclusion_list], [inclusion_list])
28
+
29
+ For WAMP message details: http://wamp.ws/spec
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/easy_wamp.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'easy_wamp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "easy_wamp"
8
+ spec.version = EasyWamp::VERSION
9
+ spec.authors = ["Curtis Bissonnette"]
10
+ spec.email = ["cbissonnette@gmail.com"]
11
+ spec.description = %q{Provides a simple but full implementation of the wamp websocket protocol}
12
+ spec.summary = %q{A simple full implementation of the wamp websocket protocol (http://wamp.ws/), with similar syntac as DRB.}
13
+ spec.homepage = "https://github.com/cjbissonnette/EasyWamp"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency "websocket-eventmachine-server", "~> 1.0.1"
25
+ end
@@ -0,0 +1,3 @@
1
+ module EasyWamp
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,166 @@
1
+ module EasyWamp
2
+ class WampServer
3
+
4
+ PROTOCOL_VERSION = 1
5
+ TYPE_ID_WELCOME = 0
6
+ TYPE_ID_PREFIX = 1
7
+ TYPE_ID_CALL = 2
8
+ TYPE_ID_CALLRESULT = 3
9
+ TYPE_ID_CALLERROR = 4
10
+ TYPE_ID_SUBSCRIBE = 5
11
+ TYPE_ID_UNSUBSCRIBE = 6
12
+ TYPE_ID_PUBLISH = 7
13
+ TYPE_ID_EVENT = 8
14
+
15
+ @@prefix = {}
16
+ @@events = {}
17
+ @@clients = {}
18
+ @@thread = nil
19
+
20
+ def self.thread()
21
+ return @@thread
22
+ end
23
+
24
+ def self.expand(curi)
25
+ @@prefix[curi.split(':')[0]] || curi
26
+ end
27
+
28
+ def self.get_api_call(uri)
29
+ uri = expand(uri)
30
+ uri.include?('#') ? uri.split('#')[1] : uri
31
+ end
32
+
33
+ def self.subscribe(uri, client)
34
+ uri = expand(uri)
35
+ @@events[uri] ||= Set.new
36
+ @@events[uri].add(client)
37
+ end
38
+
39
+ def self.unsubscribe(uri, client)
40
+ uri = expand(uri)
41
+ @@events[uri] ||= Set.new
42
+ @@events[uri].delete(client)
43
+ end
44
+
45
+ def self.session_id
46
+ SecureRandom.uuid
47
+ end
48
+
49
+ def self.register_new_client(ws, id)
50
+ @@clients[ws] = id
51
+ end
52
+
53
+ def self.remove_client(ws)
54
+ @@clients.delete(ws)
55
+ end
56
+
57
+ def self.get_client_id(ws)
58
+ @@clients[ws]
59
+ end
60
+
61
+ def self.get_client_ws(id)
62
+ @@clients.key(id)
63
+ end
64
+
65
+ def self.add_prefix(prefix, value)
66
+ @@prefix[prefix] = value
67
+ end
68
+
69
+ def self.each_registered(uri, bad, good)
70
+ @@events[uri].each do |e|
71
+ yield(get_client_ws(e)) if !bad.include?(e) && (good == nil || good.empty? || good.include?(e))
72
+ end if(@@events[uri])
73
+ end
74
+
75
+ def self.send_msg(ws, msg)
76
+ ws.send(msg.to_json)
77
+ end
78
+
79
+ def self.send_welcome(ws, id)
80
+ send_msg(ws, [TYPE_ID_WELCOME,
81
+ id,
82
+ PROTOCOL_VERSION,
83
+ "EasyWampServer/#{EasyWamp::VERSION}"])
84
+ end
85
+
86
+ def self.send_error(ws, id, type, desc, details)
87
+ send_msg(ws, [TYPE_ID_CALLERROR,
88
+ id,
89
+ "#{@host}/error##{type}",
90
+ desc,
91
+ details])
92
+ end
93
+
94
+ def self.send_result(ws, id, result)
95
+ send_msg(ws, [TYPE_ID_CALLRESULT, id, result])
96
+ end
97
+
98
+ def self.send_event(ws, uri, event)
99
+ send_msg(ws, [TYPE_ID_EVENT, uri, event])
100
+ end
101
+
102
+ def self.publish_event(uri, event, ex_list=[], inc_list=[])
103
+ uri = expand(uri)
104
+ each_registered(uri, ex_list, inc_list) do |ws|
105
+ send_event(ws, uri, event)
106
+ end
107
+ end
108
+
109
+ def self.handle_open(ws)
110
+ id = session_id()
111
+ register_new_client(ws, id)
112
+ send_welcome(ws, id)
113
+ end
114
+
115
+ def self.handle_close(ws)
116
+ remove_client(ws)
117
+ end
118
+
119
+ def self.handle_msg(ws, msg)
120
+ client_id = get_client_id(ws)
121
+ msg = JSON.parse(msg)
122
+ case msg[0]
123
+ when TYPE_ID_PREFIX
124
+ add_prefix(msg[1], msg[2])
125
+ when TYPE_ID_CALL
126
+ begin
127
+ send_result(ws, msg[1], @@api.send(get_api_call(msg[2]), *msg[3..-1]))
128
+ rescue Exception => e
129
+ send_error(ws, msg[1], e.class.name, e.to_s, e.backtrace.join("\n"))
130
+ end
131
+ when TYPE_ID_SUBSCRIBE
132
+ subscribe(msg[1], client_id)
133
+ when TYPE_ID_UNSUBSCRIBE
134
+ unsubscribe(msg[1], client_id)
135
+ when TYPE_ID_PUBLISH
136
+ publish_event(*msg[1..-1])
137
+ end
138
+ end
139
+
140
+ def self.start_service(host, port, api)
141
+ @@api = api
142
+ @@host = host
143
+ @@port = port
144
+
145
+ @@thread = Thread.new do
146
+ EM.run do
147
+ WebSocket::EventMachine::Server.start(:host => host, :port => port) do |ws|
148
+ ws.onopen do
149
+ handle_open(ws)
150
+ end
151
+ ws.onmessage do |msg, type|
152
+ begin
153
+ handle_msg(ws, msg)
154
+ rescue Exception => e
155
+ puts "Error Handling Message: #{e.to_s}"
156
+ end
157
+ end
158
+ ws.onclose do
159
+ handle_close(ws)
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end
data/lib/easy_wamp.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'bundler/setup'
2
+
3
+ require 'websocket-eventmachine-server'
4
+ require 'json'
5
+ require 'securerandom'
6
+ require 'set'
7
+ require 'thread'
8
+
9
+ require 'easy_wamp/version'
10
+ require 'easy_wamp/wamp'
11
+
12
+ module EasyWamp
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_wamp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Curtis Bissonnette
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: websocket-eventmachine-server
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.1
55
+ description: Provides a simple but full implementation of the wamp websocket protocol
56
+ email:
57
+ - cbissonnette@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - easy_wamp.gemspec
68
+ - lib/easy_wamp.rb
69
+ - lib/easy_wamp/version.rb
70
+ - lib/easy_wamp/wamp.rb
71
+ homepage: https://github.com/cjbissonnette/EasyWamp
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.0.0
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: A simple full implementation of the wamp websocket protocol (http://wamp.ws/),
95
+ with similar syntac as DRB.
96
+ test_files: []