ganymed-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage/
10
+ doc/
11
+ ganymed.pid
12
+ ganymed.profile*
13
+ lib/bundler/man
14
+ node_modules/
15
+ old/
16
+ pkg
17
+ prof/
18
+ rdoc
19
+ spec/reports
20
+ test.js
21
+ test/tmp
22
+ test/version_tmp
23
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper --color --format documentation
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use --create ruby-1.9.3-p125@ganymed-client
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --protected --no-private
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'bundler'
7
+ gem 'debugger'
8
+ gem 'perftools.rb'
9
+ gem 'pry'
10
+ gem 'pry-doc'
11
+ gem 'redcarpet'
12
+ gem 'reek'
13
+ gem 'rspec'
14
+ gem 'ruby-prof'
15
+ gem 'ruby2ruby', '=1.3.0' # 1.3.1 is broken :(
16
+ gem 'simplecov'
17
+ gem 'yard'
18
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Benedikt Böhm
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # Ganymed::Client
2
+
3
+ A simple client library for [Ganymed](https://github.com/zenops/ganymed).
4
+
5
+ ## Usage
6
+
7
+ TBD
8
+
9
+ ## Contributing
10
+
11
+ 1. Fork it
12
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
13
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
14
+ 4. Push to the branch (`git push origin my-new-feature`)
15
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require "bundler/setup"
4
+ require "bundler/gem_tasks"
5
+
6
+ Dir['tasks/**/*.rake'].each { |t| load t }
7
+
8
+ task :default => [:spec]
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ganymed/client/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ganymed-client"
7
+ s.version = Ganymed::Client::VERSION
8
+ s.authors = ["Benedikt Böhm"]
9
+ s.email = ["bb@xnull.de"]
10
+ s.homepage = "http://github.com/zenops/ganymed-client"
11
+ s.summary = %q{Client library for Ganymed}
12
+ s.description = %q{Client library for Ganymed}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_dependency "msgpack"
20
+ end
@@ -0,0 +1,6 @@
1
+ module Ganymed
2
+ class Client
3
+ # @private
4
+ VERSION = '0.1.0'
5
+ end
6
+ end
@@ -0,0 +1,131 @@
1
+ require 'socket'
2
+ require 'msgpack'
3
+
4
+ module Ganymed
5
+
6
+ ##
7
+ # The Client connects to the Sampler and/or Processor and emits samples
8
+ # and/or events respectively.
9
+ #
10
+ class Client
11
+
12
+ # The {ProcessorSocket Processor socket}.
13
+ attr_reader :processor
14
+
15
+ # The {SamplerSocket Sampler socket}.
16
+ attr_reader :sampler
17
+
18
+ # Create a new client instance.
19
+ #
20
+ # @param [Hash] opts Client options.
21
+ # @option opts [Hash] :processor Options passed to the {ProcessorSocket}.
22
+ # @option opts [Hash] :sampler Options passed to the {SamplerSocket}.
23
+ def initialize(opts)
24
+ @processor = ProcessorSocket.new(opts[:processor]) if opts[:processor]
25
+ @sampler = SamplerSocket.new(opts[:sampler]) if opts[:sampler]
26
+ end
27
+
28
+ ##
29
+ # A simple UDPSocket wrapper.
30
+ #
31
+ class Socket
32
+
33
+ # Create a new Socket instance.
34
+ #
35
+ # @param [Hash] opts Socket options.
36
+ # @option opts [String] :host Host to connect to.
37
+ # @option opts [Fixnum] :port Port to connect to.
38
+ # @option opts [String] :origin Origin of Samples/Events. Defaults to the
39
+ # fully-qualified hostname.
40
+ def initialize(opts)
41
+ @host, @port = opts[:host], opts[:port]
42
+ @socket = UDPSocket.new
43
+ @origin = opts[:origin] || ::Socket.gethostbyname(::Socket.gethostname).first
44
+ end
45
+
46
+ # Send data to the socket.
47
+ #
48
+ # @param [String] data The data to send.
49
+ # @param [Fixnum] flags Socket flags.
50
+ def send(data, flags=0)
51
+ @socket.send(data, flags, @host, @port)
52
+ end
53
+ end
54
+
55
+ ##
56
+ # A {Socket} that emits samples to a Sampler.
57
+ #
58
+ class SamplerSocket < Socket
59
+
60
+ # Emit a new sample.
61
+ #
62
+ # @param [String, Symbol] ds Sample data source.
63
+ # @param [String] ns Event namespace.
64
+ # @param [Fixnum, Float] value Sample value.
65
+ # @param [Time] now Sample timestamp.
66
+ def emit(ds, ns, value, now=Time.now.utc)
67
+ data = [ds.to_s, ns, @origin, now.to_f, value.to_f]
68
+ send(data.pack("Z*Z*Z*GG"))
69
+ end
70
+ end
71
+
72
+ ##
73
+ # A {Socket} that emits events to a Processor.
74
+ #
75
+ class ProcessorSocket < Socket
76
+
77
+ # Emit a new Event.
78
+ #
79
+ # @param [String] ns Event namespace.
80
+ # @param [Object] value Event value.
81
+ # @param [Hash] opts Options
82
+ # @option opts [String] cf Consolidation function used in this event.
83
+ # @option opts [Time] now Event timestamp.
84
+ # @option opts [Fixnum] resolution Event resolution.
85
+ # @option opts [String] origin Event origin.
86
+ def event(ns, value, opts={})
87
+ opts = {
88
+ :cf => nil,
89
+ :now => Time.now.utc,
90
+ :resolution => 0,
91
+ :origin => @origin,
92
+ }.merge(opts)
93
+
94
+ {
95
+ :_type => :event,
96
+ :n => ns,
97
+ :c => opts[:cf],
98
+ :o => opts[:origin],
99
+ :t => opts[:now].to_i,
100
+ :r => opts[:resolution].to_i,
101
+ :v => value
102
+ }.tap do |data|
103
+ send(data.to_msgpack)
104
+ end
105
+ end
106
+
107
+ # Emit a metadata object to the Processor.
108
+ #
109
+ # A metadata object contains arbitrary data related to the origin. This
110
+ # can be queried by Websocket clients for displaying information about
111
+ # origins.
112
+ #
113
+ # @param [Hash] data Metadata object.
114
+ # @param [Hash] opts Options
115
+ # @option opts [String] origin Metadata origin.
116
+ def metadata(data, opts={})
117
+ opts = {
118
+ :origin => @origin,
119
+ }.merge(opts)
120
+
121
+ {
122
+ :_type => :metadata,
123
+ :origin => opts[:origin],
124
+ :data => data,
125
+ }.tap do |data|
126
+ send(data.to_msgpack)
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper --color --format documentation
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ $:.unshift(File.join(File.expand_path("../..", __FILE__), 'lib'))
3
+
4
+ require 'rspec'
5
+ require 'debugger'
6
+ require 'simplecov'
7
+ SimpleCov.start
8
+
9
+ RSpec.configure do |config|
10
+ end
data/tasks/reek.rake ADDED
@@ -0,0 +1,5 @@
1
+ require 'reek/rake/task'
2
+
3
+ Reek::Rake::Task.new do |t|
4
+ t.fail_on_error = false
5
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,7 @@
1
+ require 'rspec'
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc "Run the specs"
5
+ RSpec::Core::RakeTask.new do |t|
6
+ t.rspec_opts = ['--options', "spec/spec.opts"]
7
+ end
data/tasks/yard.rake ADDED
@@ -0,0 +1,5 @@
1
+ require 'yard'
2
+
3
+ YARD::Rake::YardocTask.new do |t|
4
+ t.files = ['lib/**/*.rb', 'README.rdoc']
5
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ganymed-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Benedikt Böhm
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: msgpack
16
+ requirement: &9787780 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *9787780
25
+ description: Client library for Ganymed
26
+ email:
27
+ - bb@xnull.de
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - .rspec
34
+ - .rvmrc
35
+ - .yardopts
36
+ - Gemfile
37
+ - LICENSE
38
+ - README.md
39
+ - Rakefile
40
+ - ganymed-client.gemspec
41
+ - lib/ganymed/client.rb
42
+ - lib/ganymed/client/version.rb
43
+ - spec/spec.opts
44
+ - spec/spec_helper.rb
45
+ - tasks/reek.rake
46
+ - tasks/rspec.rake
47
+ - tasks/yard.rake
48
+ homepage: http://github.com/zenops/ganymed-client
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ segments:
61
+ - 0
62
+ hash: 1972040912805276168
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ segments:
70
+ - 0
71
+ hash: 1972040912805276168
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.17
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Client library for Ganymed
78
+ test_files:
79
+ - spec/spec.opts
80
+ - spec/spec_helper.rb
81
+ has_rdoc: