pressure 0.1.0

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: b313cd4248c6fc51984e770ec69faca2306dc0d6
4
+ data.tar.gz: 2465595bc00aa0ef952cee0c8058cd1573f4d10d
5
+ SHA512:
6
+ metadata.gz: be64475b738ae0de55510eabc30cddcf56f68ed2e07db4a147d563a949543addc5a976e8ec740aacf02985c697801e0c1f6831e08c28f8f121802210825840a3
7
+ data.tar.gz: bca07e33be85ebaf307a3b45fbcf1057a37f94408772c14fd8fc2d61369f894195e877ad21ed7711adf2ae40fb595059592f6183533e6c80f5f17148921e8f63
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,35 @@
1
+ AllCops:
2
+ Include:
3
+ - Gemfile
4
+ - Guardfile
5
+ - Rakefile
6
+ - config.ru
7
+ - '*.gemspec'
8
+ - '**/*.rb'
9
+ Exclude:
10
+ - ignore/**/*
11
+ - node_modules/**/*
12
+ Metrics/AbcSize:
13
+ Enabled: no
14
+ Metrics/CyclomaticComplexity:
15
+ Enabled: no
16
+ Metrics/MethodLength:
17
+ Enabled: no
18
+ Metrics/PerceivedComplexity:
19
+ Enabled: no
20
+ Style/CaseIndentation:
21
+ Enabled: no
22
+ Style/Documentation:
23
+ Enabled: no
24
+ Style/GuardClause:
25
+ Enabled: no
26
+ Style/LineEndConcatenation:
27
+ Enabled: no
28
+ Style/RegexpLiteral:
29
+ Enabled: no
30
+ Style/RedundantSelf:
31
+ Enabled: no
32
+ Style/SpaceInsideHashLiteralBraces:
33
+ Enabled: no
34
+ Style/UnneededPercentQ:
35
+ Enabled: no
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.3
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pressure.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Brian Davidson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Pressure
2
+
3
+ Multicast upstream data to downstream websocket clients.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pressure'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install pressure
20
+
21
+ ## Usage
22
+
23
+ See examples directory.
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it ( https://github.com/bsdavidson/pressure/fork )
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 a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ rescue LoadError
7
+ nil
8
+ end
9
+
10
+ begin
11
+ require 'yard'
12
+ YARD::Rake::YardocTask.new
13
+ rescue LoadError
14
+ nil
15
+ end
16
+
17
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'pressure'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/examples/demo.rb ADDED
@@ -0,0 +1,81 @@
1
+ require 'sinatra'
2
+ require 'sinatra-websocket'
3
+ require 'pressure'
4
+
5
+ set :server, 'thin'
6
+ set :sockets, []
7
+
8
+ data = {}
9
+
10
+ Thread.new do
11
+ loop do
12
+ data = (0...8).map { (65 + rand(26)).chr }.join
13
+ sleep(1.0 / 0.2)
14
+ end
15
+ end
16
+
17
+ pressure = Pressure.new do
18
+ data
19
+ end
20
+
21
+ pressure.wrapper_template = {
22
+ someKey: 'Some Value',
23
+ anotherKey: 'Another Value'
24
+ }
25
+
26
+ get '/' do
27
+ if !request.websocket?
28
+ erb :index
29
+ else
30
+ request.websocket do |ws|
31
+ ws.onopen do
32
+ pressure << ws
33
+ puts "Connected: #{pressure.sockets.length}"
34
+ end
35
+ ws.onmessage do |msg|
36
+ EM.next_tick { pressure.sockets.each { |socket| socket.send(msg) } }
37
+ end
38
+ ws.onclose do
39
+ warn('websocket closed')
40
+ pressure.delete ws
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ __END__
47
+ @@ index
48
+ <html>
49
+ <body>
50
+ <h1>Pressure Demo</h1>
51
+ <form id="form">
52
+ <input type="text" id="input" value="send a message"></input>
53
+ </form>
54
+ <div id="msgs"></div>
55
+ </body>
56
+
57
+ <script type="text/javascript">
58
+ window.onload = function(){
59
+ (function(){
60
+ var show = function(el){
61
+ return function(msg){ el.innerHTML = msg + '<br />' + el.innerHTML; }
62
+ }(document.getElementById('msgs'));
63
+
64
+ var ws = new WebSocket('ws://' + window.location.host + window.location.pathname);
65
+ ws.onopen = function() { show('websocket opened'); };
66
+ ws.onclose = function() { show('websocket closed'); }
67
+ ws.onmessage = function(m) { show('websocket message: ' + m.data); };
68
+
69
+ var sender = function(f){
70
+ var input = document.getElementById('input');
71
+ input.onclick = function(){ input.value = "" };
72
+ f.onsubmit = function(){
73
+ ws.send(input.value);
74
+ input.value = "";
75
+ return false;
76
+ }
77
+ }(document.getElementById('form'));
78
+ })();
79
+ }
80
+ </script>
81
+ </html>
@@ -0,0 +1,3 @@
1
+ class Pressure
2
+ VERSION = '0.1.0'
3
+ end
data/lib/pressure.rb ADDED
@@ -0,0 +1,76 @@
1
+ require 'pressure/version'
2
+ require 'logger'
3
+ require 'time'
4
+ require 'json'
5
+
6
+ class Pressure
7
+ attr_accessor :wrapper_template
8
+ attr_reader :sockets
9
+
10
+ def initialize(options = {}, &data_source_block)
11
+ @wrapper_template = {}
12
+ @current_upstream = {}
13
+ @send_queue = Queue.new
14
+ @sockets = []
15
+ @websocket_worker_delay = options[:websocket_worker_delay] || (1.0 / 20.0)
16
+ incoming_monitor(&data_source_block)
17
+ websocket_worker_loop
18
+ end
19
+
20
+ def <<(socket)
21
+ @sockets << socket
22
+ end
23
+
24
+ def delete(socket)
25
+ @sockets.delete socket
26
+ end
27
+
28
+ def wrap_data(data)
29
+ @wrapper_template.merge(upstream_data: data.clone,
30
+ last_update_ts: Time.now.utc.to_i)
31
+ end
32
+
33
+ def data_changed?(current_data, previous_data)
34
+ current_data != previous_data
35
+ end
36
+
37
+ def incoming_monitor(&data_source_block)
38
+ Thread.new do
39
+ begin
40
+ data = {}
41
+ loop do
42
+ upstream_data = data_source_block.call
43
+ if data_changed?(data[:upstream_data], upstream_data)
44
+ data = wrap_data(upstream_data)
45
+ @send_queue << data
46
+ end
47
+ sleep(1.0 / 20.0)
48
+ end
49
+ rescue => e
50
+ puts "error #{e}"
51
+ end
52
+ end
53
+ end
54
+
55
+ def websocket_worker
56
+ # logger.info data.inspect
57
+ queued_data = JSON.generate(@send_queue.shift)
58
+ @sockets.each do |socket|
59
+ socket.send queued_data
60
+ end
61
+ end
62
+
63
+ def websocket_worker_loop
64
+ Thread.new do
65
+ begin
66
+ loop do
67
+ websocket_worker
68
+ sleep(@websocket_worker_delay)
69
+ end
70
+ rescue => e
71
+ puts "Worker error: #{e}"
72
+ retry
73
+ end
74
+ end
75
+ end
76
+ end
data/pressure.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pressure/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'pressure'
8
+ spec.version = Pressure::VERSION
9
+ spec.authors = ['Brian Davidson']
10
+ spec.email = ['bsdavidson@gmail.com']
11
+
12
+ spec.summary = %q(Multicast upstream data to websocket clients)
13
+ spec.homepage = 'https://github.com/bsdavidson/pressure'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.9'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency 'redcarpet', '~> 3.3'
27
+ spec.add_development_dependency 'yard', '~> 0.8'
28
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pressure
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Davidson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-25 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redcarpet
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.8'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.8'
83
+ description:
84
+ email:
85
+ - bsdavidson@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - examples/demo.rb
102
+ - lib/pressure.rb
103
+ - lib/pressure/version.rb
104
+ - pressure.gemspec
105
+ homepage: https://github.com/bsdavidson/pressure
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.2.2
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Multicast upstream data to websocket clients
129
+ test_files: []
130
+ has_rdoc: