fyrehose 0.0.1 → 0.0.2

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.
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "fyrehose"
6
- s.version = "0.0.1"
6
+ s.version = "0.0.2"
7
7
  s.date = Date.today.to_s
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Paul Asmuth"]
@@ -1,2 +1,16 @@
1
+ require "socket"
2
+ require "timeout"
3
+
4
+ require "fyrehose/errors"
1
5
  require "fyrehose/input_stream"
2
- require "fyrehose/protocol_error"
6
+ require "fyrehose/abstract_client"
7
+ require "fyrehose/tcp_client"
8
+ require "fyrehose/udp_client"
9
+
10
+ module Fyrehose
11
+
12
+ def self.next_txid
13
+ rand(8**32).to_s(36)
14
+ end
15
+
16
+ end
@@ -0,0 +1,35 @@
1
+ class Fyrehose::AbstractClient
2
+
3
+ TIMEOUT = 0.1
4
+
5
+ def initialize(host, port, opts = {})
6
+ @host = host
7
+ @port = port
8
+ @opts = opts
9
+
10
+ @timeout = if opts[:timeout]
11
+ opts[:timeout].to_f
12
+ else
13
+ TIMEOUT
14
+ end
15
+ end
16
+
17
+ def deliver(channel, data)
18
+ channel = channel.to_s
19
+ data = data.to_s
20
+
21
+ if channel.include?(" ")
22
+ raise Fyrehose::Error.new("channel names must not include whitespace")
23
+ end
24
+
25
+ txid = Fyrehose.next_txid
26
+ send_data("##{txid} @#{channel} *#{data.size} #{data}\n")
27
+ end
28
+
29
+ private
30
+
31
+ def send_data(data)
32
+ raise "implement me"
33
+ end
34
+
35
+ end
@@ -1,5 +1,8 @@
1
1
  module Fyrehose
2
- class ProtocolError < StandardError
2
+ class Error < StandardError; end
3
+ class ConnectionError < Fyrehose::Error; end
4
+
5
+ class ProtocolError < Fyrehose::Error
3
6
 
4
7
  INFO_SIZE = 30
5
8
 
@@ -16,4 +19,5 @@ module Fyrehose
16
19
  end
17
20
 
18
21
  end
22
+
19
23
  end
@@ -4,7 +4,7 @@ class Fyrehose::Reactor < EventMachine::Connection
4
4
 
5
5
  def self.run(host, port, opts = {}, &block)
6
6
  unless block
7
- raise "missing proc{ |channel,data| } for #run"
7
+ raise Fyrehose::Error.new("missing proc{ |channel,data| } for #run")
8
8
  end
9
9
 
10
10
  EventMachine.run do
@@ -19,11 +19,13 @@ class Fyrehose::Reactor < EventMachine::Connection
19
19
  end
20
20
 
21
21
  def deliver(channel, data)
22
- send_data("##{next_txid} @#{channel} *#{data.size} #{data}\n")
22
+ txid = Fyrehose.next_txid
23
+ send_data("##{txid} @#{channel} *#{data.size} #{data}\n")
23
24
  end
24
25
 
25
26
  def set_flags(channel, flags)
26
- send_data("##{next_txid} @#{channel} +#{flags}\n")
27
+ txid = Fyrehose.next_txid
28
+ send_data("##{txid} @#{channel} +#{flags}\n")
27
29
  end
28
30
 
29
31
  def subscribe(channel)
@@ -50,10 +52,4 @@ class Fyrehose::Reactor < EventMachine::Connection
50
52
  end
51
53
  end
52
54
 
53
- private
54
-
55
- def next_txid
56
- rand(8**32).to_s(36)
57
- end
58
-
59
55
  end
@@ -0,0 +1,21 @@
1
+ class Fyrehose::TCPClient < Fyrehose::AbstractClient
2
+
3
+ def send_data(data)
4
+ Timeout::timeout(@timeout) do
5
+ @sock = TCPSocket.new(@host, @port) unless @sock
6
+ @sock.send(data, 0)
7
+ parse_response(@sock.gets)
8
+ end
9
+ rescue Exception => e
10
+ @sock = nil
11
+ raise e
12
+ end
13
+
14
+ def parse_response(str)
15
+ raise Fyrehose::ConnectionError.new unless str
16
+ m = str.match(/#[^ ]+ \$([0-9]+)\n/)
17
+ raise Fyrehose::ProtocolError.new unless m
18
+ m[1].to_i
19
+ end
20
+
21
+ end
@@ -0,0 +1,18 @@
1
+ class Fyrehose::UDPClient < Fyrehose::AbstractClient
2
+
3
+ def open_connection
4
+ @sock = UDPSocket.new
5
+ @sock.connect(@host, @port)
6
+ end
7
+
8
+ def send_data(data)
9
+ Timeout::timeout(@timeout) do
10
+ open_connection unless @sock
11
+ @sock.send(data, 0); 0
12
+ end
13
+ rescue Exception => e
14
+ @sock = nil
15
+ raise e
16
+ end
17
+
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fyrehose
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -21,9 +21,12 @@ files:
21
21
  - .gitkeep
22
22
  - fyrehose.gemspec
23
23
  - lib/fyrehose.rb
24
+ - lib/fyrehose/abstract_client.rb
25
+ - lib/fyrehose/errors.rb
24
26
  - lib/fyrehose/input_stream.rb
25
- - lib/fyrehose/protocol_error.rb
26
27
  - lib/fyrehose/reactor.rb
28
+ - lib/fyrehose/tcp_client.rb
29
+ - lib/fyrehose/udp_client.rb
27
30
  homepage: http://github.com/paulasmuth/fyrehose
28
31
  licenses:
29
32
  - MIT