ffi-rxs 1.1.0 → 1.2.0

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.
data/lib/ffi-rxs/util.rb~ DELETED
@@ -1,105 +0,0 @@
1
-
2
- module XS
3
-
4
- # These methods don't belong to any specific class. They get included
5
- # in the #Context, #Socket and #Poller classes.
6
- #
7
- module Util
8
-
9
- # Returns true when +rc+ is greater than or equal to 0, false otherwise.
10
- #
11
- # We use the >= test because xs_poll() returns the number of sockets
12
- # that had a read or write event triggered. So, a >= 0 result means
13
- # it succeeded.
14
- #
15
- def self.resultcode_ok? rc
16
- rc >= 0
17
- end
18
-
19
- # Returns the +errno+ as set by the libxs library.
20
- #
21
- def self.errno
22
- LibXS.xs_errno
23
- end
24
-
25
- # Returns a string corresponding to the currently set #errno. These
26
- # error strings are defined by libxs.
27
- #
28
- def self.error_string
29
- LibXS.xs_strerror(errno).read_string
30
- end
31
-
32
- # Returns an array of the form [major, minor, patch] to represent the
33
- # version of libzmq.
34
- #
35
- # Class method! Invoke as: XS::Util.version
36
- #
37
- def self.version
38
- major = FFI::MemoryPointer.new :int
39
- minor = FFI::MemoryPointer.new :int
40
- patch = FFI::MemoryPointer.new :int
41
- LibXS.xs_version major, minor, patch
42
- [major.read_int, minor.read_int, patch.read_int]
43
- end
44
-
45
- # Attempts to bind to a random tcp port on +host+ up to +max_tries+
46
- # times. Returns the port number upon success or nil upon failure.
47
- #
48
- def self.bind_to_random_tcp_port host = '127.0.0.1', max_tries = 500
49
- tries = 0
50
- rc = -1
51
-
52
- while !resultcode_ok?(rc) && tries < max_tries
53
- tries += 1
54
- random = random_port
55
- rc = socket.bind "tcp://#{host}:#{random}"
56
- end
57
-
58
- resultcode_ok?(rc) ? random : nil
59
- end
60
-
61
-
62
- private
63
-
64
- # generate a random port between 10_000 and 65534
65
- def self.random_port
66
- rand(55534) + 10_000
67
- end
68
-
69
- # :doc:
70
- # Called by most library methods to verify there were no errors during
71
- # operation. If any are found, raise the appropriate #XSError.
72
- #
73
- # When no error is found, this method returns +true+ which is behavior
74
- # used internally by #send and #recv.
75
- #
76
- def error_check source, result_code
77
- if -1 == result_code
78
- raise_error source, result_code
79
- end
80
-
81
- # used by Socket::send/recv, ignored by others
82
- true
83
- end
84
-
85
- def raise_error source, result_code
86
- if 'xs_init' == source || 'xs_socket' == source
87
- raise ContextError.new source, result_code, XS::Util.errno, XS::Util.error_string
88
-
89
- elsif ['xs_msg_init', 'xs_msg_init_data', 'xs_msg_copy', 'xs_msg_move'].include?(source)
90
- raise MessageError.new source, result_code, XS::Util.errno, XS::Util.error_string
91
-
92
- else
93
- puts "else"
94
- raise XSError.new source, result_code, -1,
95
- "Source [#{source}] does not match any xs_* strings, rc [#{result_code}], errno [#{XS::Util.errno}], error_string [#{XS::Util.error_string}]"
96
- end
97
- end
98
-
99
- def eagain?
100
- EAGAIN == XS::Util.errno
101
- end
102
-
103
- end # module Util
104
-
105
- end # module XS
@@ -1,3 +0,0 @@
1
- module XS
2
- VERSION = "0.1.0"
3
- end