net-empty_port 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .*.swp
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - 1.8.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in net-empty_port.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ryosuke IWANAGA
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Net::EmptyPort
2
+
3
+ [![Build Status](https://travis-ci.org/riywo/net-empty_port.png?branch=master)](https://travis-ci.org/riywo/net-empty_port)
4
+
5
+ This module can find, check and wait an empty TCP/UDP port.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'net-empty_port'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install net-empty_port
20
+
21
+ ## Usage
22
+
23
+ port = Net::EmptyPort.empty_port
24
+ TCPServer.new('127.0.0.1', port)
25
+
26
+ Net::EmptyPort.used?(port)
27
+
28
+ Net::EmptyPort.wait(port, 3)
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => [:spec]
4
+ begin
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.rspec_opts = %w[-cfs -r ./spec/spec_helper.rb]
8
+ end
9
+ rescue LoadError => e
10
+ end
@@ -0,0 +1,5 @@
1
+ module Net
2
+ module EmptyPort
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,81 @@
1
+ require "net/empty_port/version"
2
+ require "socket"
3
+
4
+ module Net
5
+ module EmptyPort
6
+ class << self
7
+ def empty_port(port = 0, proto = :tcp)
8
+ port = random_port unless valid_port?(port)
9
+
10
+ while (port += 1) < 60000
11
+ next if (proto == :tcp and used?(port))
12
+
13
+ begin
14
+ TCPServer.new('127.0.0.1', port).close
15
+ return port
16
+ rescue Errno::EADDRINUSE, Errno::EACCES
17
+ end
18
+ end
19
+ end
20
+
21
+ def used?(port, proto = :tcp)
22
+ if proto == :udp
23
+ udp_used?(port)
24
+ else
25
+ tcp_used?(port)
26
+ end
27
+ end
28
+
29
+ def wait(port, max_wait, proto = :tcp)
30
+ waiter = make_waiter(max_wait)
31
+ while waiter.call
32
+ return true if used?(port, proto)
33
+ end
34
+ return false
35
+ end
36
+
37
+ private
38
+
39
+ def valid_port?(port)
40
+ num = Integer(port)
41
+ if (num < 49152 or num > 65535)
42
+ false
43
+ else
44
+ true
45
+ end
46
+ rescue
47
+ false
48
+ end
49
+
50
+ def random_port
51
+ 50000 + (rand * 1000).to_i
52
+ end
53
+
54
+ def tcp_used?(port)
55
+ TCPSocket.new('127.0.0.1', port).close
56
+ true
57
+ rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT
58
+ false
59
+ end
60
+
61
+ def udp_used?(port)
62
+ UDPSocket.new().bind('127.0.0.1', port)
63
+ false
64
+ rescue Errno::EADDRINUSE, Errno::EACCES
65
+ true
66
+ end
67
+
68
+ def make_waiter(max_wait)
69
+ waited = 0
70
+ interval = 0.001
71
+ Proc.new {
72
+ next false if waited > max_wait
73
+ sleep interval
74
+ waited += interval
75
+ interval *= 2
76
+ next true
77
+ }
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'net/empty_port/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "net-empty_port"
8
+ spec.version = Net::EmptyPort::VERSION
9
+ spec.authors = ["Ryosuke IWANAGA"]
10
+ spec.email = ["riywo.jp@gmail.com"]
11
+ spec.description = %q{An empty port for TCP/UDP}
12
+ spec.summary = %q{This module can find, check and wait an empty TCP/UDP port.}
13
+ spec.homepage = "https://github.com/riywo/net-empty_port"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,29 @@
1
+ describe Net::EmptyPort do
2
+ describe "VERSION" do
3
+ subject { Net::EmptyPort::VERSION }
4
+ it { should be_a String }
5
+ end
6
+
7
+ describe "empty_port" do
8
+ context "random" do
9
+ let!(:port) { Net::EmptyPort.empty_port }
10
+ it "is between 49152 and 65535" do
11
+ expect(port).to be >= 49152
12
+ expect(port).to be <= 65535
13
+ end
14
+ end
15
+ end
16
+
17
+ describe "wait" do
18
+ let!(:port) { Net::EmptyPort.empty_port }
19
+ it "wait until the empty_port is ready" do
20
+ s = TCPServer.new('127.0.0.1', port)
21
+ expect(Net::EmptyPort.wait(port, 2)).to be(true)
22
+ s.close
23
+ end
24
+
25
+ it "fails because port is not read while the max_wait" do
26
+ expect(Net::EmptyPort.wait(port, 0.1)).to be(false)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,2 @@
1
+ require 'rspec'
2
+ require 'net/empty_port'
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: net-empty_port
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryosuke IWANAGA
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: An empty port for TCP/UDP
63
+ email:
64
+ - riywo.jp@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .travis.yml
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - lib/net/empty_port.rb
76
+ - lib/net/empty_port/version.rb
77
+ - net-empty_port.gemspec
78
+ - spec/net/empty_port_spec.rb
79
+ - spec/spec_helper.rb
80
+ homepage: https://github.com/riywo/net-empty_port
81
+ licenses:
82
+ - MIT
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.23
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: This module can find, check and wait an empty TCP/UDP port.
105
+ test_files:
106
+ - spec/net/empty_port_spec.rb
107
+ - spec/spec_helper.rb