david 0.3.0.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ module David
2
+ MAJOR = 0
3
+ MINOR = 3
4
+ PATCH = 0
5
+
6
+ VERSION = [MAJOR, MINOR, PATCH, 'pre'].join('.').freeze
7
+ end
@@ -0,0 +1,59 @@
1
+ module David
2
+ class WellKnown
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ dup._call(env)
9
+ end
10
+
11
+ def _call(env)
12
+ @env = env
13
+
14
+ if env['PATH_INFO'] == '/.well-known/core'
15
+ return [405, {}, []] if env['REQUEST_METHOD'] != 'GET'
16
+
17
+ links = filtered_paths.map { |path| CoRE::Link.new(path) }
18
+
19
+ [200,
20
+ {'Content-Type' => 'application/link-format'},
21
+ [links.map(&:to_s).join(',')]
22
+ ]
23
+ else
24
+ @app.call(env)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def delete_format(spec)
31
+ spec.gsub(/\(\.:format\)\z/, '')
32
+ end
33
+
34
+ def filter(spec)
35
+ href = @env['QUERY_STRING'].split('href=').last
36
+
37
+ return true if href.blank?
38
+
39
+ spec =~ Regexp.new(href)
40
+ end
41
+
42
+ def filtered_paths
43
+ @filtered_paths ||= specs
44
+ .select { |spec| spec if include_spec?(spec) }
45
+ .select { |spec| spec if filter(spec) }
46
+ .map { |spec| delete_format(spec) }
47
+ end
48
+
49
+ def include_spec?(spec)
50
+ !(spec == '/' || spec =~ /\A\/(assets|rails)/)
51
+ end
52
+
53
+ def specs
54
+ @specs ||= Rails.application.routes.routes.map do |route|
55
+ route.path.spec.to_s
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,6 @@
1
+ module Rack
2
+ module Handler
3
+ CoAP = David
4
+ register :coap, CoAP
5
+ end
6
+ end
@@ -0,0 +1,35 @@
1
+ module Rack
2
+ module Handler
3
+ class David
4
+ DEFAULT_OPTIONS = {
5
+ :Host => ENV['RACK_ENV'] == 'development' ? '::1' : '::',
6
+ :Port => ::CoAP::PORT
7
+ }
8
+
9
+ def self.run(app, options={})
10
+ options = DEFAULT_OPTIONS.merge(options)
11
+
12
+ supervisor = ::David::Server.supervise_as(:david, app, options)
13
+
14
+ begin
15
+ sleep
16
+ rescue Interrupt
17
+ supervisor.terminate
18
+ end
19
+ end
20
+
21
+ def self.valid_options
22
+ host, port = DEFAULT_OPTIONS.values_at(:Host, :Port)
23
+
24
+ {
25
+ 'Host=HOST' => "Hostname to listen on (default: #{host})",
26
+ 'Port=PORT' => "Port to listen on (default: #{port})",
27
+ 'CBOR=BOOLEAN' => 'Transparent JSON/CBOR conversion.',
28
+ 'Log=LOG' => 'Change logging (debug|none).'
29
+ }
30
+ end
31
+ end
32
+
33
+ register :david, David
34
+ end
35
+ end
@@ -0,0 +1,45 @@
1
+ module Rack
2
+ class HelloWorld
3
+ # include Celluloid
4
+
5
+ def call(env)
6
+ dup._call(env)
7
+ end
8
+
9
+ def _call(env)
10
+ if env['REQUEST_METHOD'] != 'GET'
11
+ return [405, {}, ['']]
12
+ end
13
+
14
+ return case env['PATH_INFO']
15
+ when '/.well-known/core'
16
+ [200,
17
+ {'Content-Type' => 'application/link-format'},
18
+ ['</hello>;rt="hello";ct=0']
19
+ ]
20
+ when '/hello'
21
+ [200,
22
+ # If Content-Length is not given, Rack assumes chunked transfer.
23
+ {'Content-Type' => 'text/plain', 'Content-Length' => '12'},
24
+ ['Hello World!']
25
+ ]
26
+ when '/wait'
27
+ sleep 10
28
+ [200,
29
+ {'Content-Type' => 'text/plain'},
30
+ ['You waited!']
31
+ ]
32
+ when '/value'
33
+ @@value ||= 0
34
+ @@value += 1
35
+
36
+ [200,
37
+ {'Content-Type' => 'text/plain'},
38
+ ["#{@@value}"]
39
+ ]
40
+ else
41
+ [404, {}, ['']]
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+ require 'benchmark'
3
+
4
+ describe Server, 'performance' do
5
+ before do
6
+ @server = David::Server.supervise_as \
7
+ :david,
8
+ ->(e) { [200, { 'Content-Length' => 0 }, ['']] },
9
+ {:Host => '::1', :Port => CoRE::CoAP::PORT, :Log => 'none'}
10
+ end
11
+
12
+ it "should handle GET request in less than #{max1 = 0.0025} seconds" do
13
+ expect(Benchmark.realtime { CoRE::CoAP::Client.new.get('/', '::1') }).to be < max1
14
+ end
15
+
16
+ after do
17
+ @server.terminate
18
+ end
19
+ end
@@ -0,0 +1,2 @@
1
+ require_relative '../lib/david'
2
+ include David
data/test.rb ADDED
@@ -0,0 +1,58 @@
1
+ require 'bundler/setup'
2
+ Bundler.require
3
+
4
+ require 'benchmark'
5
+
6
+ include CoRE
7
+
8
+ #uri = ARGV.pop
9
+ uri = 'coap://[::1]/value'
10
+ n, c = ARGV.map(&:to_i)
11
+
12
+ threads = []
13
+ value = 0
14
+
15
+ c.times do
16
+ threads << Thread.start do
17
+ n.times do |i|
18
+ new_value = CoAP::Client.new.get_by_uri(uri).payload.to_i
19
+ value = new_value if new_value > value
20
+ end
21
+ end
22
+ end
23
+
24
+ Benchmark.bm do |b|
25
+ b.report { threads.map(&:join) }
26
+ end
27
+
28
+ puts value
29
+
30
+ FileUtils.rm_f('tmp')
31
+
32
+ Benchmark.bm do |b|
33
+ b.report do
34
+ c.times do
35
+ fork do
36
+ value = 0
37
+
38
+ n.times do |i|
39
+ new_value = CoAP::Client.new.get_by_uri(uri).payload.to_i
40
+ value = new_value if new_value > value
41
+ end
42
+
43
+ File.open('tmp', 'a') do |f|
44
+ f.write("#{value}\n")
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ value = 0
52
+
53
+ File.readlines('tmp').each do |line|
54
+ new_value = line.to_i
55
+ value = new_value if new_value > value
56
+ end
57
+
58
+ puts value
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: david
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0.pre
5
+ platform: ruby
6
+ authors:
7
+ - henning mueller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cbor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: celluloid-io
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.16'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coap
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: rack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ description: |-
70
+ David is a CoAP server with Rack interface to bring the
71
+ illustrious family of Rack compatible web frameworks into the Internet of
72
+ Things.
73
+ email: henning@orgizm.net
74
+ executables:
75
+ - david
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE
82
+ - README.md
83
+ - Rakefile
84
+ - bin/david
85
+ - config.ru
86
+ - david.gemspec
87
+ - lib/david.rb
88
+ - lib/david/guerilla/rack/handler.rb
89
+ - lib/david/railties/config.rb
90
+ - lib/david/railties/middleware.rb
91
+ - lib/david/server.rb
92
+ - lib/david/server/mapping.rb
93
+ - lib/david/server/options.rb
94
+ - lib/david/server/response.rb
95
+ - lib/david/server/utility.rb
96
+ - lib/david/version.rb
97
+ - lib/david/well_known.rb
98
+ - lib/rack/handler/coap.rb
99
+ - lib/rack/handler/david.rb
100
+ - lib/rack/hello_world.rb
101
+ - spec/perf/server_perf_spec.rb
102
+ - spec/spec_helper.rb
103
+ - test.rb
104
+ homepage: https://github.com/nning/david
105
+ licenses:
106
+ - GPL-3.0
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">"
120
+ - !ruby/object:Gem::Version
121
+ version: 1.3.1
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.2.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: CoAP server with Rack interface.
128
+ test_files:
129
+ - spec/perf/server_perf_spec.rb
130
+ - spec/spec_helper.rb
131
+ has_rdoc: