ganymed-client 0.1.0 → 0.2.0

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.
@@ -1,131 +1,3 @@
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
1
+ require 'ganymed/client/processor'
2
+ require 'ganymed/client/sampler'
3
+ require 'ganymed/client/version'
@@ -0,0 +1,102 @@
1
+ require 'eventmachine'
2
+ require 'msgpack'
3
+
4
+ module Ganymed
5
+ module Client
6
+
7
+ ##
8
+ # An EventMachine Protocol that can send events to a Ganymed processor.
9
+ #
10
+ module Processor
11
+ include EM::Deferrable
12
+
13
+ # Emit a new Event.
14
+ #
15
+ # @param [String] ns Event namespace.
16
+ # @param [Object] value Event value.
17
+ # @param [Hash] opts Options
18
+ # @option opts [String] cf Consolidation function used in this event.
19
+ # @option opts [Time] now Event timestamp.
20
+ # @option opts [Fixnum] resolution Event resolution.
21
+ # @option opts [String] origin Event origin.
22
+ def event(ns, value, opts={})
23
+ opts = {
24
+ :cf => nil,
25
+ :now => Time.now.utc,
26
+ :resolution => 0,
27
+ :origin => @origin,
28
+ }.merge(opts)
29
+
30
+ data = {
31
+ :_type => :event,
32
+ :n => ns,
33
+ :c => opts[:cf],
34
+ :o => opts[:origin],
35
+ :t => opts[:now].to_i,
36
+ :r => opts[:resolution].to_i,
37
+ :v => value
38
+ }
39
+
40
+ send_data(data.to_msgpack)
41
+ end
42
+
43
+ # Emit a metadata object to the Processor.
44
+ #
45
+ # A metadata object contains arbitrary data related to the origin. This
46
+ # can be queried by Websocket clients for displaying information about
47
+ # origins.
48
+ #
49
+ # @param [Hash] data Metadata object.
50
+ # @param [Hash] opts Options
51
+ # @option opts [String] origin Metadata origin.
52
+ def metadata(data, opts={})
53
+ opts = {
54
+ :origin => @origin,
55
+ }.merge(opts)
56
+
57
+ data = {
58
+ :_type => :metadata,
59
+ :origin => opts[:origin],
60
+ :data => data,
61
+ }
62
+
63
+ send_data(data.to_msgpack)
64
+ end
65
+
66
+ # Connect to a Ganymed processor.
67
+ #
68
+ # @param [String] host Host to connect to.
69
+ # @param [Fixnum] port Port to connect to.
70
+ # @param [String] origin Origin of events. Defaults to the
71
+ # fully-qualified hostname.
72
+ def self.connect(host, port, origin=nil)
73
+ EM.connect(host, port, self, host, port, origin)
74
+ end
75
+
76
+ # @private
77
+ def initialize(host, port, origin=nil)
78
+ @host, @port = host, port
79
+ @origin = origin || ::Socket.gethostbyname(::Socket.gethostname).first
80
+ end
81
+
82
+ # @private
83
+ def connection_completed
84
+ @reconnecting = false
85
+ @connected = true
86
+ succeed
87
+ end
88
+
89
+ # @private
90
+ def unbind
91
+ if @connected or @reconnecting
92
+ EM.add_timer(1) { reconnect(@host, @port) }
93
+ @connected = false
94
+ @reconnecting = true
95
+ @deferred_status = nil
96
+ else
97
+ raise "unable to connect #{@origin} to ganymed processor at #{@host}:#{@port}"
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,39 @@
1
+ require 'eventmachine'
2
+
3
+ module Ganymed
4
+ module Client
5
+
6
+ ##
7
+ # An EventMachine Protocol that can send samples to a Ganymed sampler.
8
+ #
9
+ module Sampler
10
+ include EM::Deferrable
11
+
12
+ # Emit a new sample.
13
+ #
14
+ # @param [String, Symbol] ds Sample data source.
15
+ # @param [String] ns Event namespace.
16
+ # @param [Fixnum, Float] value Sample value.
17
+ def emit(ds, ns, value)
18
+ data = [ds.to_s, ns, @origin, value.to_f]
19
+ send_datagram(data.pack("Z*Z*Z*G"), @host, @port)
20
+ end
21
+
22
+ # Connect to a Ganymed sampler.
23
+ #
24
+ # @param [String] host Host to connect to.
25
+ # @param [Fixnum] port Port to connect to.
26
+ # @param [String] origin Origin of events. Defaults to the
27
+ # fully-qualified hostname.
28
+ def self.connect(host, port, origin=nil)
29
+ EM.open_datagram_socket("0.0.0.0", 0, self, host, port, origin)
30
+ end
31
+
32
+ # @private
33
+ def initialize(host, port, origin=nil)
34
+ @host, @port = host, port
35
+ @origin = origin || ::Socket.gethostbyname(::Socket.gethostname).first
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,6 +1,6 @@
1
1
  module Ganymed
2
- class Client
2
+ module Client
3
3
  # @private
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ganymed-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-27 00:00:00.000000000 Z
12
+ date: 2012-06-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: msgpack
16
- requirement: &9787780 !ruby/object:Gem::Requirement
16
+ requirement: &18990080 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *9787780
24
+ version_requirements: *18990080
25
25
  description: Client library for Ganymed
26
26
  email:
27
27
  - bb@xnull.de
@@ -39,6 +39,8 @@ files:
39
39
  - Rakefile
40
40
  - ganymed-client.gemspec
41
41
  - lib/ganymed/client.rb
42
+ - lib/ganymed/client/processor.rb
43
+ - lib/ganymed/client/sampler.rb
42
44
  - lib/ganymed/client/version.rb
43
45
  - spec/spec.opts
44
46
  - spec/spec_helper.rb
@@ -59,7 +61,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
61
  version: '0'
60
62
  segments:
61
63
  - 0
62
- hash: 1972040912805276168
64
+ hash: -1617282239461546353
63
65
  required_rubygems_version: !ruby/object:Gem::Requirement
64
66
  none: false
65
67
  requirements:
@@ -68,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
70
  version: '0'
69
71
  segments:
70
72
  - 0
71
- hash: 1972040912805276168
73
+ hash: -1617282239461546353
72
74
  requirements: []
73
75
  rubyforge_project:
74
76
  rubygems_version: 1.8.17