sip2 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sip2.rb +7 -0
- data/lib/sip2/connection.rb +2 -2
- data/lib/sip2/non_blocking_socket.rb +26 -4
- data/lib/sip2/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0383747c254aba1493539ca5465e870b7d06ddb3'
|
4
|
+
data.tar.gz: 3553ea22a5b3e1aea055a36cefb13ffa983e54c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2e284505e8d319abe25635185f7e22c1686f52657d9deddeaa0e2c616fa4b0a0e54865f1402c95fa3437ad9c73109ad0d92316edcfd970bbfaa004b28da7309a
|
7
|
+
data.tar.gz: 9b4d682b6b91bf12236cb8349fccdc6e3cca155d18218b74a93c0a2738dbf36d03a193f0f78d85adfce53c523b7fa25f51fd20c8f3bdd0e5405e4997c2c19c5b
|
data/lib/sip2.rb
CHANGED
@@ -5,6 +5,13 @@ require 'sip2/patron_information'
|
|
5
5
|
require 'sip2/messages/login'
|
6
6
|
require 'sip2/messages/patron_information'
|
7
7
|
|
8
|
+
module Sip2
|
9
|
+
class TimeoutError < StandardError; end
|
10
|
+
class ConnectionTimeout < TimeoutError; end
|
11
|
+
class WriteTimeout < TimeoutError; end
|
12
|
+
class ReadTimeout < TimeoutError; end
|
13
|
+
end
|
14
|
+
|
8
15
|
require 'sip2/non_blocking_socket'
|
9
16
|
require 'sip2/connection'
|
10
17
|
require 'sip2/client'
|
data/lib/sip2/connection.rb
CHANGED
@@ -1,20 +1,38 @@
|
|
1
1
|
require 'socket'
|
2
|
+
require 'timeout'
|
2
3
|
|
3
4
|
module Sip2
|
4
5
|
#
|
5
6
|
# Sip2 Non-blocking socket
|
6
7
|
# From https://spin.atomicobject.com/2013/09/30/socket-connection-timeout-ruby/
|
7
8
|
#
|
8
|
-
class NonBlockingSocket
|
9
|
+
class NonBlockingSocket < Socket
|
10
|
+
DEFAULT_TIMEOUT = 5
|
11
|
+
SEPARATOR = "\r".freeze
|
12
|
+
|
13
|
+
def send_with_timeout(message, separator = SEPARATOR)
|
14
|
+
::Timeout::timeout (connection_timeout || DEFAULT_TIMEOUT), WriteTimeout do
|
15
|
+
send message + separator, 0
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def gets_with_timeout(separator = SEPARATOR)
|
20
|
+
::Timeout::timeout (connection_timeout || DEFAULT_TIMEOUT), ReadTimeout do
|
21
|
+
gets separator
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
9
25
|
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
10
|
-
def self.connect(host, port, timeout =
|
26
|
+
def self.connect(host, port, timeout = DEFAULT_TIMEOUT)
|
11
27
|
# Convert the passed host into structures the non-blocking calls can deal with
|
12
28
|
addr = Socket.getaddrinfo(host, nil)
|
13
29
|
sockaddr = Socket.pack_sockaddr_in(port, addr[0][3])
|
14
30
|
|
15
|
-
|
31
|
+
NonBlockingSocket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0).tap do |socket|
|
16
32
|
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
17
33
|
|
34
|
+
socket.connection_timeout = timeout
|
35
|
+
|
18
36
|
begin
|
19
37
|
# Initiate the socket connection in the background. If it doesn't fail
|
20
38
|
# immediately it will raise an IO::WaitWritable (Errno::EINPROGRESS)
|
@@ -38,11 +56,15 @@ module Sip2
|
|
38
56
|
# IO.select returns nil when the socket is not ready before timeout
|
39
57
|
# seconds have elapsed
|
40
58
|
socket.close
|
41
|
-
raise
|
59
|
+
raise ConnectionTimeout
|
42
60
|
end
|
43
61
|
end
|
44
62
|
end
|
45
63
|
end
|
46
64
|
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
attr_accessor :connection_timeout
|
47
69
|
end
|
48
70
|
end
|
data/lib/sip2/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sip2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- abrom
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-06-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|