atech-network-log-io 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/atech/network_log_io.rb +56 -0
  2. metadata +45 -0
@@ -0,0 +1,56 @@
1
+ require 'socket'
2
+
3
+ module Atech
4
+ class NetworkLogIO < IO
5
+ attr_reader :options
6
+
7
+ def initialize(options)
8
+ @options = options
9
+
10
+ raise(ArgumentError, ':host is required') unless @options[:host]
11
+ raise(ArgumentError, ':app_name is required') unless @options[:app_name]
12
+ raise(ArgumentError, ':log_name is required') unless @options[:log_name]
13
+
14
+ @options[:port] ||= 4455
15
+ @options[:buffer_size] ||= 512
16
+
17
+ @socket = UDPSocket.new
18
+ empty_buffer!
19
+ end
20
+
21
+ def empty_buffer!
22
+ @buffer = fresh_buffer
23
+ end
24
+
25
+ def fresh_buffer
26
+ [@options[:app_name].bytesize, @options[:app_name], @options[:log_name].bytesize, @options[:log_name]].pack('nA*nA*')
27
+ end
28
+
29
+ def write(data)
30
+ data.force_encoding('BINARY')
31
+ data = data[0,@options[:buffer_size] - fresh_buffer.bytesize - 2]
32
+
33
+ ## Pre-flush if the existing buffer plus the new data plus the terminator would be more than the buffer size
34
+ if @buffer.bytesize + 2 + data.bytesize + 2 > @options[:buffer_size]
35
+ self.flush
36
+ end
37
+
38
+ @buffer << [data.bytesize].pack('n')
39
+ @buffer << data
40
+
41
+ ## Flush is autoflush is set or the buffer is full
42
+ self.flush if @options[:auto_flush] or @buffer.bytesize + 2 >= @options[:buffer_size]
43
+
44
+ return data.bytesize
45
+ end
46
+
47
+ def flush
48
+ return self if @buffer == @api_key
49
+ @buffer << "\0\0"
50
+ @socket.send(@buffer, 0, @options[:host], @options[:port])
51
+ empty_buffer!
52
+ return self
53
+ end
54
+
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: atech-network-log-io
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Cooke
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-18 00:00:00.000000000Z
13
+ dependencies: []
14
+ description:
15
+ email: adam@atechmedia.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/atech/network_log_io.rb
21
+ homepage: http://atechmedia.com
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.10
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Client for the aTech Log Server.
45
+ test_files: []