uv-rays 2.3.2 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d63b99f6e70b3be80d704ecb87b5ce15fe045076
4
- data.tar.gz: 9694e4109fd53fa215814bd5ed349146548a24b2
3
+ metadata.gz: a54cf48a95b6b5741a9fc2b6d7958f31bb95f967
4
+ data.tar.gz: 8d03f743d6112dd2de00f60cb62db1735ec3b69e
5
5
  SHA512:
6
- metadata.gz: da34409c84b8c7e016fb3c72f3fcc7e19f33b4f9a0e370e426b0bb99b5d8e5b8a6155dc58fe0a852b06a98574ec9dd6cacf35d939bb4f08ceb37a7a887bd5bdb
7
- data.tar.gz: b09991e8da993700b8ab20f723472ada5aeb4cf202fcc155aeda2bc7e58dfeef2fe4957010b22511d640f5a817d4c587bad0195b4c2a33eb16662bf8f982f3f6
6
+ metadata.gz: 7dc9088e375515895366078388804e9853f4e38653ba362861fc8ea5856d6232a8f83fd0070134f5d1e5549dbf3983b31eecf8da6afb4cf95e26a4bc08e53f0c
7
+ data.tar.gz: 0ea0dd82810514209115682293d096ceea550e5ebfacc2082dd63a74b9ab204665c962f6ae513dd07f0aa5fe514897af67c0c58438d0f0096608471857164366
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ require 'yard' # yard documentation
8
8
  task :default => :limited_spec
9
9
  RSpec::Core::RakeTask.new(:limited_spec) do |t|
10
10
  # Exclude network tests
11
- t.rspec_opts = "--tag ~network --tag ~mri_only"
11
+ t.rspec_opts = "--tag ~mri_only --tag ~travis_skip"
12
12
  end
13
13
  RSpec::Core::RakeTask.new(:spec)
14
14
 
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'ipaddress' # IP Address parser
4
+
3
5
  module UV
4
6
  def self.try_connect(tcp, handler, server, port)
5
7
  if IPAddress.valid? server
@@ -1,6 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'uri'
4
+ require 'cookiejar' # Manages cookies
5
+ require 'http-parser' # Parses HTTP request / responses
6
+ require 'addressable/uri' # URI parser
7
+ require 'uv-rays/http/encoding'
8
+ require 'uv-rays/http/request'
9
+ require 'uv-rays/http/parser'
4
10
 
5
11
  module UV
6
12
  class CookieJar
@@ -0,0 +1,190 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ipaddress' # IP Address parser
4
+ require 'ipaddr'
5
+
6
+ # Based on code from https://github.com/chernesk/net-ping/blob/master/lib/net/ping/external.rb
7
+
8
+ module UV; end
9
+ class UV::Ping
10
+
11
+ def initialize(host, count: 1, interval: 1, timeout: 5)
12
+ @host = host
13
+ @count = count
14
+ @interval = interval
15
+ @timeout = timeout
16
+ end
17
+
18
+ attr_reader :host, :ip, :count, :interval, :timeout, :exception, :warning, :duration, :pingable
19
+
20
+ def ping
21
+ @ip = if IPAddress.valid?(@host)
22
+ @host
23
+ else
24
+ nslookup(@host)
25
+ end
26
+
27
+ if @ip.nil?
28
+ @pingable = false
29
+ @exception = 'DNS lookup failed for both IPv4 and IPv6'
30
+ return false
31
+ end
32
+
33
+ ipaddr = IPAddr.new @ip
34
+ if ipaddr.ipv4?
35
+ ping4(@ip, @count, @interval, @timeout)
36
+ else
37
+ ping6(@ip, @count, @interval, @timeout)
38
+ end
39
+ end
40
+
41
+
42
+ protected
43
+
44
+
45
+ def nslookup(domain)
46
+ value = nil
47
+ reactor = Libuv::Reactor.current
48
+ begin
49
+ value = reactor.lookup(domain)[0][0]
50
+ rescue => e
51
+ begin
52
+ value = reactor.lookup(domain, :IPv6)[0][0]
53
+ rescue; end
54
+ end
55
+ value
56
+ end
57
+
58
+ def ping4(host, count, interval, timeout)
59
+ pargs = nil
60
+ bool = false
61
+
62
+ case ::RbConfig::CONFIG['host_os']
63
+ when /linux/i
64
+ pargs = ['-c', count.to_s, '-W', timeout.to_s, host, '-i', interval.to_s]
65
+ when /aix/i
66
+ pargs = ['-c', count.to_s, '-w', timeout.to_s, host]
67
+ when /bsd|osx|mach|darwin/i
68
+ pargs = ['-c', count.to_s, '-t', timeout.to_s, host]
69
+ when /solaris|sunos/i
70
+ pargs = [host, timeout.to_s]
71
+ when /hpux/i
72
+ pargs = [host, "-n#{count.to_s}", '-m', timeout.to_s]
73
+ when /win32|windows|msdos|mswin|cygwin|mingw/i
74
+ pargs = ['-n', count.to_s, '-w', (timeout * 1000).to_s, host]
75
+ else
76
+ pargs = [host]
77
+ end
78
+
79
+ start_time = Time.now
80
+ exitstatus, info, err = spawn_ping('ping', pargs)
81
+
82
+ case exitstatus
83
+ when 0
84
+ if info =~ /unreachable/ix # Windows
85
+ bool = false
86
+ @exception = "host unreachable"
87
+ else
88
+ bool = true # Success, at least one response.
89
+ end
90
+
91
+ if err =~ /warning/i
92
+ @warning = err.chomp
93
+ end
94
+ when 2
95
+ bool = false # Transmission successful, no response.
96
+ @exception = err.chomp if err
97
+ else
98
+ bool = false # An error occurred
99
+ if err
100
+ @exception = err.chomp
101
+ else
102
+ info.each_line do |line|
103
+ if line =~ /(timed out|could not find host|packet loss)/i
104
+ @exception = line.chomp
105
+ break
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ @duration = Time.now - start_time if bool
112
+ @pingable = bool
113
+ bool
114
+ end
115
+
116
+ def ping6(host, count, interval, timeout)
117
+ pargs = nil
118
+ bool = false
119
+
120
+ case RbConfig::CONFIG['host_os']
121
+ when /linux/i
122
+ pargs =['-c', count.to_s, '-W', timeout.to_s, '-i', interval.to_s, host]
123
+ when /aix/i
124
+ pargs =['-c', count.to_s, '-w', timeout.to_s, host]
125
+ when /bsd|osx|mach|darwin/i
126
+ pargs =['-c', count.to_s, host]
127
+ when /solaris|sunos/i
128
+ pargs =[host, timeout.to_s]
129
+ when /hpux/i
130
+ pargs =[host, "-n#{count.to_s}", '-m', timeout.to_s]
131
+ when /win32|windows|msdos|mswin|cygwin|mingw/i
132
+ pargs =['-n', count.to_s, '-w', (timeout * 1000).to_s, host]
133
+ else
134
+ pargs =[host]
135
+ end
136
+
137
+ start_time = Time.now
138
+ exitstatus, info, err = spawn_ping('ping6', pargs)
139
+
140
+ case exitstatus
141
+ when 0
142
+ if info =~ /unreachable/ix # Windows
143
+ bool = false
144
+ @exception = "host unreachable"
145
+ else
146
+ bool = true # Success, at least one response.
147
+ end
148
+
149
+ if err =~ /warning/i
150
+ @warning = err.chomp
151
+ end
152
+ when 2
153
+ bool = false # Transmission successful, no response.
154
+ @exception = err.chomp if err
155
+ else
156
+ bool = false # An error occurred
157
+ if err
158
+ @exception = err.chomp
159
+ else
160
+ info.each_line do |line|
161
+ if line =~ /(timed out|could not find host|packet loss)/i
162
+ @exception = line.chomp
163
+ break
164
+ end
165
+ end
166
+ end
167
+ end
168
+
169
+ @duration = Time.now - start_time if bool
170
+ @pingable = bool
171
+ bool
172
+ end
173
+
174
+ def spawn_ping(cmd, args)
175
+ stdout = String.new
176
+ stderr = String.new
177
+
178
+ process = Libuv::Reactor.current.spawn(cmd, args: args)
179
+ process.stdout.progress { |data| stdout << data }
180
+ process.stderr.progress { |data| stderr << data }
181
+ process.stdout.start_read
182
+ process.stderr.start_read
183
+ begin
184
+ process.value
185
+ [0, stdout, stderr]
186
+ rescue => e
187
+ [e.exit_status, stdout, stderr]
188
+ end
189
+ end
190
+ end
@@ -1,5 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'set' # ruby std lib
4
+ require 'bisect' # insert into a sorted array
5
+ require 'tzinfo' # timezone information
6
+ require 'uv-rays/scheduler/time'
7
+
3
8
  module UV
4
9
 
5
10
  class ScheduledEvent < ::Libuv::Q::DeferredPromise
@@ -44,8 +49,6 @@ module UV
44
49
  def to_time(internal_time)
45
50
  if internal_time
46
51
  ((internal_time + @scheduler.time_diff) / 1000).to_i
47
- else
48
- internal_time
49
52
  end
50
53
  end
51
54
 
@@ -398,12 +401,3 @@ module UV
398
401
  end
399
402
  end
400
403
  end
401
-
402
- module Libuv
403
- class Reactor
404
- def scheduler
405
- @scheduler ||= UV::Scheduler.new(@reactor)
406
- @scheduler
407
- end
408
- end
409
- end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'ipaddress' # IP Address parser
4
+
3
5
  module UV
4
6
  class TcpServer < ::Libuv::TCP
5
7
  def initialize(reactor, server, port, klass, *args)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UV
4
- VERSION = '2.3.2'
4
+ VERSION = '2.4.0'
5
5
  end
data/lib/uv-rays.rb CHANGED
@@ -2,35 +2,27 @@
2
2
 
3
3
  require 'libuv'
4
4
 
5
+ module UV
6
+ autoload :Ping, 'uv-rays/ping'
5
7
 
6
- # In-memory event scheduling
7
- require 'set' # ruby std lib
8
- require 'bisect' # insert into a sorted array
9
- require 'tzinfo' # timezone information
10
- require 'uv-rays/scheduler/time'
11
- require 'uv-rays/scheduler'
12
-
13
- # Intelligent stream buffering
14
- require 'uv-rays/buffered_tokenizer'
15
- require 'uv-rays/abstract_tokenizer'
16
-
17
- # TCP Connections
18
- require 'ipaddress' # IP Address parser
19
- require 'uv-rays/tcp_server'
20
- require 'uv-rays/connection'
8
+ # In-memory event scheduling
9
+ autoload :Scheduler, 'uv-rays/scheduler'
21
10
 
22
- # HTTP related methods
23
- require 'cookiejar' # Manages cookies
24
- require 'http-parser' # Parses HTTP request / responses
25
- require 'addressable/uri' # URI parser
26
- require 'uv-rays/http/encoding'
27
- require 'uv-rays/http/request'
28
- require 'uv-rays/http/parser'
29
- require 'uv-rays/http_endpoint'
11
+ # Intelligent stream buffering
12
+ autoload :BufferedTokenizer, 'uv-rays/buffered_tokenizer'
13
+ autoload :AbstractTokenizer, 'uv-rays/abstract_tokenizer'
30
14
 
15
+ # TCP Connections
16
+ autoload :TcpServer, 'uv-rays/tcp_server'
17
+ autoload :Connection, 'uv-rays/connection'
18
+ autoload :TcpConnection, 'uv-rays/connection'
19
+ autoload :InboundConnection, 'uv-rays/connection'
20
+ autoload :OutboundConnection, 'uv-rays/connection'
21
+ autoload :DatagramConnection, 'uv-rays/connection'
31
22
 
23
+ # HTTP related methods
24
+ autoload :HttpEndpoint, 'uv-rays/http_endpoint'
32
25
 
33
- module UV
34
26
 
35
27
  # @private
36
28
  def self.klass_from_handler(klass, handler = nil, *args)
@@ -91,3 +83,12 @@ module UV
91
83
  c
92
84
  end
93
85
  end
86
+
87
+ module Libuv
88
+ class Reactor
89
+ def scheduler
90
+ @scheduler ||= ::UV::Scheduler.new(@reactor)
91
+ @scheduler
92
+ end
93
+ end
94
+ end
data/spec/ping_spec.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'uv-rays'
2
+
3
+
4
+ describe UV::Ping do
5
+ before :each do
6
+ @reactor = Libuv::Reactor.default
7
+ @reactor.notifier do |error, context|
8
+ begin
9
+ @general_failure << "Log called: #{context}\n#{error.message}\n#{error.backtrace.join("\n") if error.backtrace}\n"
10
+ rescue Exception
11
+ @general_failure << "error in logger #{e.inspect}"
12
+ end
13
+ end
14
+ @general_failure = []
15
+ @timeout = @reactor.timer do
16
+ @reactor.stop
17
+ @general_failure << "test timed out"
18
+ end
19
+ @timeout.start(5000)
20
+ end
21
+
22
+ after :each do
23
+ @timeout.close
24
+ end
25
+
26
+ it "should ping IPv4" do
27
+ pingger = ::UV::Ping.new('127.0.0.1')
28
+ result = nil
29
+
30
+ @reactor.run { |reactor|
31
+ pingger.ping
32
+ @timeout.stop
33
+ }
34
+
35
+ expect(@general_failure).to eq([])
36
+ expect(pingger.pingable).to eq(true)
37
+ expect(pingger.exception).to eq(nil)
38
+ expect(pingger.warning).to eq(nil)
39
+ expect(pingger.duration).to be > 0
40
+ end
41
+
42
+ it "should ping IPv6", travis_skip: true do
43
+ pingger = ::UV::Ping.new('::1')
44
+ result = nil
45
+
46
+ @reactor.run { |reactor|
47
+ pingger.ping
48
+ @timeout.stop
49
+ }
50
+
51
+ expect(@general_failure).to eq([])
52
+ expect(pingger.pingable).to eq(true)
53
+ expect(pingger.exception).to eq(nil)
54
+ expect(pingger.warning).to eq(nil)
55
+ expect(pingger.duration).to be > 0
56
+ end
57
+
58
+ it "should ping localhost after resolving using DNS" do
59
+ pingger = ::UV::Ping.new('localhost')
60
+ result = nil
61
+
62
+ @reactor.run { |reactor|
63
+ pingger.ping
64
+ @timeout.stop
65
+ }
66
+
67
+ expect(@general_failure).to eq([])
68
+ expect(pingger.pingable).to eq(true)
69
+ expect(pingger.exception).to eq(nil)
70
+ expect(pingger.warning).to eq(nil)
71
+ expect(pingger.duration).to be > 0
72
+ end
73
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uv-rays
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.2
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen von Takach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-18 00:00:00.000000000 Z
11
+ date: 2017-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: libuv
@@ -248,6 +248,7 @@ files:
248
248
  - lib/uv-rays/http/parser.rb
249
249
  - lib/uv-rays/http/request.rb
250
250
  - lib/uv-rays/http_endpoint.rb
251
+ - lib/uv-rays/ping.rb
251
252
  - lib/uv-rays/scheduler.rb
252
253
  - lib/uv-rays/scheduler/time.rb
253
254
  - lib/uv-rays/tcp_server.rb
@@ -256,6 +257,7 @@ files:
256
257
  - spec/buffered_tokenizer_spec.rb
257
258
  - spec/connection_spec.rb
258
259
  - spec/http_endpoint_spec.rb
260
+ - spec/ping_spec.rb
259
261
  - spec/scheduler_spec.rb
260
262
  - spec/scheduler_time_spec.rb
261
263
  - spec/zen_spec.rb
@@ -285,10 +287,11 @@ signing_key:
285
287
  specification_version: 4
286
288
  summary: Abstractions for working with Libuv
287
289
  test_files:
288
- - spec/abstract_tokenizer_spec.rb
289
- - spec/buffered_tokenizer_spec.rb
290
- - spec/connection_spec.rb
291
- - spec/http_endpoint_spec.rb
292
- - spec/scheduler_spec.rb
293
290
  - spec/scheduler_time_spec.rb
294
291
  - spec/zen_spec.rb
292
+ - spec/ping_spec.rb
293
+ - spec/http_endpoint_spec.rb
294
+ - spec/connection_spec.rb
295
+ - spec/abstract_tokenizer_spec.rb
296
+ - spec/scheduler_spec.rb
297
+ - spec/buffered_tokenizer_spec.rb