katcp 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -139,12 +139,15 @@ module KATCP
139
139
  #
140
140
  # Supported keys for the +opts+ Hash are:
141
141
  #
142
- # :remote_host Specifies hostname of KATCP server
143
- # :remote_port Specifies port used by KATCP server (default 7147)
144
- # :local_host Specifies local interface to bind to (default nil)
145
- # :local_port Specifies local port to bind to (default nil)
146
- # :typemap Provides a default device typemap (default {}).
147
- # See #device_typemap for details.
142
+ # :remote_host Specifies hostname of KATCP server
143
+ # :remote_port Specifies port used by KATCP server
144
+ # (default ENV['KATCP_PORT'] || 7147)
145
+ # :local_host Specifies local interface to bind to (default nil)
146
+ # :local_port Specifies local port to bind to (default nil)
147
+ # :socket_timeout Specifies timeout for socket operations
148
+ # (default DEFAULT_SOCKET_TIMEOUT)
149
+ # :typemap Provides a default device typemap (default {}).
150
+ # See #device_typemap for details.
148
151
  def initialize(*args)
149
152
  # List of all devices
150
153
  @devices = [];
data/lib/katcp/client.rb CHANGED
@@ -2,6 +2,7 @@ require 'rubygems'
2
2
  require 'thread'
3
3
  require 'monitor'
4
4
  require 'socket'
5
+ require 'timeout'
5
6
 
6
7
  require 'katcp/response'
7
8
  require 'katcp/util'
@@ -25,10 +26,13 @@ module KATCP
25
26
  #
26
27
  # Supported keys for the +opts+ Hash are:
27
28
  #
28
- # :remote_host Specifies hostname of KATCP server
29
- # :remote_port Specifies port used by KATCP server (default 7147)
30
- # :local_host Specifies local interface to bind to (default nil)
31
- # :local_port Specifies local port to bind to (default nil)
29
+ # :remote_host Specifies hostname of KATCP server
30
+ # :remote_port Specifies port used by KATCP server
31
+ # (default ENV['KATCP_PORT'] || 7147)
32
+ # :local_host Specifies local interface to bind to (default nil)
33
+ # :local_port Specifies local port to bind to (default nil)
34
+ # :socket_timeout Specifies timeout for socket operations
35
+ # (default DEFAULT_SOCKET_TIMEOUT)
32
36
  def initialize(*args)
33
37
  # If final arg is a Hash, pop it off
34
38
  @opts = (Hash === args[-1]) ? args.pop : {}
@@ -36,10 +40,17 @@ module KATCP
36
40
  # Save parameters
37
41
  remote_host, remote_port, local_host, local_port = args
38
42
  @remote_host = remote_host ? remote_host.to_s : @opts[:remote_host].to_s
39
- @remote_port = remote_port || @opts[:remote_port] || 7147
43
+ @remote_port = remote_port || @opts[:remote_port] || ENV['KATCP_PORT'] || 7147
40
44
  @local_host = local_host || @opts[:local_host]
41
45
  @local_port = local_port || @opts[:local_port]
42
46
 
47
+ # Make sure @remote_port is Integer, if not use default of 7147
48
+ @remote_port = Integer(@remote_port) rescue 7147
49
+
50
+ # Create sockaddr from remote host and port. This can raise
51
+ # "SocketError: getaddrinfo: Name or service not known".
52
+ @sockaddr = Socket.sockaddr_in(@remote_port, @remote_host)
53
+
43
54
  # Init attribute(s)
44
55
  @informs = []
45
56
 
@@ -57,7 +68,7 @@ module KATCP
57
68
  @rxq = Queue.new
58
69
 
59
70
  # Timeout value for socket operations
60
- @socket_timeout = DEFAULT_SOCKET_TIMEOUT
71
+ @socket_timeout = @opts[:socket_timeout] || DEFAULT_SOCKET_TIMEOUT
61
72
 
62
73
  # No socket yet
63
74
  @socket = nil
@@ -76,33 +87,22 @@ module KATCP
76
87
 
77
88
  # Create new socket.
78
89
  @socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
79
- sockaddr = Socket.sockaddr_in(@remote_port, @remote_host)
80
90
 
81
- # Do non-blocking connect
91
+ # Do connect in timeout block
82
92
  begin
83
- @socket.connect_nonblock(sockaddr)
84
- rescue Errno::EINPROGRESS
85
- # Wait with timeout for @socket to become writable.
86
- # Is DEFAULT_SOCKET_TIMEOUT seconds an OK timeout for connect?
87
- rd_wr_err = select([], [@socket], [], DEFAULT_SOCKET_TIMEOUT)
88
- if rd_wr_err.nil?
89
- # Timeout during connect, close socket and raise TimeoutError
90
- @socket.close
91
- raise TimeoutError.new(
92
- 'connection timed out in %.3f seconds' % DEFAULT_SOCKET_TIMEOUT)
93
+ Timeout::timeout(@socket_timeout) do
94
+ @socket.connect(@sockaddr)
93
95
  end
94
-
95
- # Verify that we're connected
96
- begin
97
- @socket.connect_nonblock(sockaddr)
98
- rescue Errno::EISCONN # expected
99
- # ignore
100
- rescue # anything else, unexpected
101
- # Close socket and re-raise
102
- @socket.close
103
- raise
96
+ rescue => e
97
+ # Close socket
98
+ @socket.close
99
+ # Change e to TimeoutError instead of Timeout::Error
100
+ if Timeout::Error === e
101
+ e = TimeoutError.new(
102
+ 'connection timed out in %.3f seconds' % @socket_timeout)
104
103
  end
105
- end # non-blocking connect
104
+ raise e
105
+ end
106
106
 
107
107
  # Start thread that reads data from server.
108
108
  Thread.new do
data/lib/katcp/version.rb CHANGED
@@ -3,5 +3,5 @@
3
3
  #++
4
4
 
5
5
  module KATCP
6
- VERSION = "0.1.4"
6
+ VERSION = "0.1.6"
7
7
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katcp
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 4
10
- version: 0.1.4
9
+ - 6
10
+ version: 0.1.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - David MacMahon
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-03-04 00:00:00 Z
18
+ date: 2013-04-25 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: narray
@@ -59,7 +59,7 @@ rdoc_options:
59
59
  - -m
60
60
  - README
61
61
  - --title
62
- - Ruby/KATCP 0.1.4 Documentation
62
+ - Ruby/KATCP 0.1.6 Documentation
63
63
  require_paths:
64
64
  - lib
65
65
  required_ruby_version: !ruby/object:Gem::Requirement