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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 66c5b3c4ac6909954ad5e3c0b11577cb97d17e91
4
- data.tar.gz: 47985f7e545caec1d2d71e7fdeda15dbf099af7a
3
+ metadata.gz: '0383747c254aba1493539ca5465e870b7d06ddb3'
4
+ data.tar.gz: 3553ea22a5b3e1aea055a36cefb13ffa983e54c5
5
5
  SHA512:
6
- metadata.gz: c59b28ec3bfb1c1e4604e0dc8e5196c91b994c0015c11a5badfc54e6cce2a4f45a4e4bbcae42fea24e0ee28a73f609353c609eaeb53a7109ed54d91f7b567c0c
7
- data.tar.gz: 6cab90f0952d40cc71664db95b746014ad4f996049523789be36d663539f44ddcae6f36420f496befc0c8f297bebbdccc483b1f6b98b9b4936fd025458282de2
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'
@@ -45,8 +45,8 @@ module Sip2
45
45
  end
46
46
 
47
47
  def send_message(message)
48
- @socket.send(message + "\r", 0)
49
- @socket.gets "\r"
48
+ @socket.send_with_timeout message
49
+ @socket.gets_with_timeout
50
50
  end
51
51
 
52
52
  def with_error_detection(message)
@@ -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 = 5)
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
- Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0).tap do |socket|
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 'Connection timeout'
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
@@ -1,3 +1,3 @@
1
1
  module Sip2
2
- VERSION = '0.0.9'.freeze
2
+ VERSION = '0.0.10'.freeze
3
3
  end
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.9
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-05-11 00:00:00.000000000 Z
11
+ date: 2018-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler