net-ntp-next 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.yardopts +1 -0
- data/Gemfile +11 -0
- data/README.md +36 -0
- data/Rakefile +10 -0
- data/UNLICENSE +24 -0
- data/lib/net/ntp/next.rb +75 -0
- data/lib/net/ntp/next/version.rb +8 -0
- data/net-ntp-next.gemspec +21 -0
- data/spec/net-ntp-next_spec.rb +38 -0
- data/spec/spec_helper.rb +7 -0
- metadata +72 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 51bdca86fb861a1b327f790a67ded6f7a841485e
|
4
|
+
data.tar.gz: 57762ad7b2b19673496e27bb1d48b9744120a7b5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 747c4c9256d8634eee28cd3f89400fac74fd9f3e53d333986fdf3161660b6648b7439fbc5f1931ad59f07c839b7c28e27764710cdc763470a41aa9d11b4d07f8
|
7
|
+
data.tar.gz: 08452fc810d8a7b1820490181846b9666e10414451ceea6acbfa87866969c96899bbc45bcc84275ef9aee1c609375cdc63478ce9e491f17c4693fa6746ab4c4f
|
data/.gitignore
ADDED
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup markdown
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Net::NTP::Next
|
2
|
+
|
3
|
+
NTP client library with improved functionality.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Simply
|
8
|
+
|
9
|
+
`gem install net-ntp-next`
|
10
|
+
|
11
|
+
### Dependecies
|
12
|
+
|
13
|
+
Depends on `net-ntp` gem
|
14
|
+
|
15
|
+
## Authors
|
16
|
+
|
17
|
+
Currently everything (all functions, files, text) in this repository are made by me @davispuh and I've dedicated this repository to public domain.
|
18
|
+
|
19
|
+
|
20
|
+
## Unlicense
|
21
|
+
|
22
|
+
All text, documentation, code and files in this repository are in public domain (including this text, README).
|
23
|
+
It means you can copy, modify, distribute and include in your own work/code, even for commercial purposes, all without asking permission.
|
24
|
+
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
|
28
|
+
Feel free to improve anything what you see is improvable.
|
29
|
+
|
30
|
+
|
31
|
+
**Warning**: By sending pull request to this repository you dedicate any and all copyright interest in pull request (code files and all other) to the public domain. (files will be in public domain even if pull request doesn't get merged)
|
32
|
+
|
33
|
+
Also before sending pull request you acknowledge that you own all copyrights or have authorization to dedicate them to public domain.
|
34
|
+
|
35
|
+
If you don't want to dedicate code to public domain or if you're not allowed to (eg. you don't own required copyrights) then DON'T send pull request.
|
36
|
+
|
data/Rakefile
ADDED
data/UNLICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
2
|
+
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
4
|
+
distribute this software, either in source code form or as a compiled
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
6
|
+
means.
|
7
|
+
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
9
|
+
of this software dedicate any and all copyright interest in the
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
11
|
+
of the public at large and to the detriment of our heirs and
|
12
|
+
successors. We intend this dedication to be an overt act of
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
14
|
+
software under copyright law.
|
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 NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
For more information, please refer to <http://unlicense.org/>
|
data/lib/net/ntp/next.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'net/ntp'
|
2
|
+
require 'net/ntp/next/version'
|
3
|
+
|
4
|
+
|
5
|
+
# Networking module
|
6
|
+
module Net
|
7
|
+
# NTP module
|
8
|
+
#
|
9
|
+
# ```ruby
|
10
|
+
# r = Net::NTP::get # returns Response class
|
11
|
+
# r.time # time
|
12
|
+
# ```
|
13
|
+
module NTP
|
14
|
+
# Get time information from NTP server
|
15
|
+
# @param host [String] NTP server hostname or IP
|
16
|
+
# @param port [String, Fixnum] NTP server port
|
17
|
+
# @param timeout [Fixnum]
|
18
|
+
# @return [Response]
|
19
|
+
def self.get(host='pool.ntp.org', port='ntp', timeout=TIMEOUT)
|
20
|
+
sock = UDPSocket.new
|
21
|
+
sock.connect(host, port)
|
22
|
+
|
23
|
+
client_time_send = Time.new.to_i
|
24
|
+
client_localtime = client_time_send
|
25
|
+
client_adj_localtime = client_localtime + NTP_ADJ
|
26
|
+
client_frac_localtime = frac2bin(client_adj_localtime)
|
27
|
+
|
28
|
+
ntp_msg = (['00011011']+Array.new(12, 0)+[client_localtime, client_frac_localtime.to_s]).pack('B8 C3 N10 B32')
|
29
|
+
|
30
|
+
startTime = Time.new.to_f
|
31
|
+
|
32
|
+
sock.print ntp_msg
|
33
|
+
sock.flush
|
34
|
+
|
35
|
+
data = nil
|
36
|
+
Timeout::timeout(timeout) do |t|
|
37
|
+
data = sock.recvfrom(960)[0]
|
38
|
+
end
|
39
|
+
|
40
|
+
endTime = Time.new.to_f
|
41
|
+
|
42
|
+
Response.new(data, startTime, endTime)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Time information Response class
|
46
|
+
# Will be returned from `Net::NTP::get`
|
47
|
+
class Response
|
48
|
+
def initialize(raw_data, startTime=0, endTime=0)
|
49
|
+
@raw_data = raw_data
|
50
|
+
@client_time_receive = Time.new.to_i
|
51
|
+
@packet_data_by_field = nil
|
52
|
+
@startTime = startTime
|
53
|
+
@endTime = endTime
|
54
|
+
end
|
55
|
+
|
56
|
+
# Latency
|
57
|
+
# @return [Float]
|
58
|
+
def latency
|
59
|
+
@endTime-@startTime
|
60
|
+
end
|
61
|
+
|
62
|
+
# Difference between current time and real time
|
63
|
+
# @return [Float]
|
64
|
+
def timeDifference
|
65
|
+
receive_timestamp-@startTime
|
66
|
+
end
|
67
|
+
|
68
|
+
# Time difference without latency
|
69
|
+
# @return [Float]
|
70
|
+
def realDifference
|
71
|
+
timeDifference-(latency/2)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'net/ntp/next/version'
|
5
|
+
|
6
|
+
|
7
|
+
Gem::Specification.new do |gem|
|
8
|
+
gem.name = 'net-ntp-next'
|
9
|
+
gem.version = Net::NTP::Next::VERSION
|
10
|
+
gem.authors = ['Dāvis']
|
11
|
+
gem.email = ['davispuh@gmail.com']
|
12
|
+
gem.description = 'NTP client library with improved functionality.'
|
13
|
+
gem.summary = 'Improved NTP client library.'
|
14
|
+
gem.license = 'UNLICENSE'
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ['lib']
|
20
|
+
gem.add_dependency('net-ntp', '>= 2.1.1')
|
21
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe Net::NTP do
|
5
|
+
|
6
|
+
describe 'get' do
|
7
|
+
it 'should return Response' do
|
8
|
+
result = Net::NTP.get('pool.ntp.org', 'ntp', 10)
|
9
|
+
result.should be_kind_of(Net::NTP::Response)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe Net::NTP::Response do
|
14
|
+
let(:startTime) { 1362766284.080817 }
|
15
|
+
let(:realTime) { 1362766283.018052 }
|
16
|
+
let(:endTime) { 1362766284.3670762 }
|
17
|
+
let(:response) { Net::NTP::Response.new("\x1C\x03\0\xE9\0\0\0;\0\0\n\xDB\x80 \xCE7\xD4\xE4\xA5\xD2\x1D\xFF\xF6\xDDQ:)\xCC\xDD\0\0\0\xD4\xE4\xA8K\x04\x9F\rw\xD4\xE4\xA8K\x04\xA2\xBC\v", startTime, endTime) }
|
18
|
+
describe '#latency' do
|
19
|
+
it 'should return latency' do
|
20
|
+
response.latency.should eq(endTime-startTime)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#timeDifference' do
|
25
|
+
it 'should return differnece between times' do
|
26
|
+
response.timeDifference.should eq(realTime-startTime)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#realDifference' do
|
31
|
+
it 'should return time difference without latency' do
|
32
|
+
response.realDifference.should eq(realTime-(endTime-startTime)/2-startTime)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: net-ntp-next
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dāvis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-03-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: net-ntp
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.1.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.1.1
|
27
|
+
description: NTP client library with improved functionality.
|
28
|
+
email:
|
29
|
+
- davispuh@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- .gitignore
|
35
|
+
- .yardopts
|
36
|
+
- Gemfile
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- UNLICENSE
|
40
|
+
- lib/net/ntp/next.rb
|
41
|
+
- lib/net/ntp/next/version.rb
|
42
|
+
- net-ntp-next.gemspec
|
43
|
+
- spec/net-ntp-next_spec.rb
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
homepage:
|
46
|
+
licenses:
|
47
|
+
- UNLICENSE
|
48
|
+
metadata: {}
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 2.0.0
|
66
|
+
signing_key:
|
67
|
+
specification_version: 4
|
68
|
+
summary: Improved NTP client library.
|
69
|
+
test_files:
|
70
|
+
- spec/net-ntp-next_spec.rb
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
has_rdoc:
|