Class: IChannel::UNIXSocket
- Inherits:
-
Object
- Object
- IChannel::UNIXSocket
- Defined in:
- lib/ichannel/unix_socket.rb
Instance Method Summary collapse
-
#close ⇒ Boolean
Close the channel.
-
#closed? ⇒ Boolean
Returns true when the channel is closed.
- #initialize(serializer = Marshal, adapter_options) ⇒ IChannel::UNIXSocket constructor
-
#last_msg ⇒ Object
Reads the last message written to the channel by reading until the channel is empty.
-
#readable? ⇒ Boolean
Returns true when the channel is readable.
-
#recv ⇒ Object
(also: #get)
Receive an object from the channel.
-
#recv!(timeout = 0.1) ⇒ Object
(also: #get!)
Receive an object from the channel.
-
#write(object) ⇒ Object
(also: #put)
Add an object to the channel.
-
#write!(object, timeout = 0.1) ⇒ Object
(also: #put!)
Add an object to the channel.
Constructor Details
#initialize(serializer = Marshal, adapter_options) ⇒ IChannel::UNIXSocket
15 16 17 18 19 |
# File 'lib/ichannel/unix_socket.rb', line 15 def initialize(serializer = Marshal, ) @serializer = serializer @last_msg = nil @reader, @writer = ::UNIXSocket.pair :STREAM end |
Instance Method Details
#close ⇒ Boolean
Close the channel.
35 36 37 38 39 40 41 42 |
# File 'lib/ichannel/unix_socket.rb', line 35 def close unless closed? @reader.close @writer.close @last_msg = nil true end end |
#closed? ⇒ Boolean
Returns true when the channel is closed.
25 26 27 |
# File 'lib/ichannel/unix_socket.rb', line 25 def closed? @reader.closed? && @writer.closed? end |
#last_msg ⇒ Object
Reads the last message written to the channel by reading until the channel is empty. The last message is cached and reset to nil on call to #close.
97 98 99 100 101 102 |
# File 'lib/ichannel/unix_socket.rb', line 97 def last_msg while readable? @last_msg = get end @last_msg end |
#readable? ⇒ Boolean
Returns true when the channel is readable.
155 156 157 158 159 160 161 162 |
# File 'lib/ichannel/unix_socket.rb', line 155 def readable? if closed? false else readable, _ = IO.select [@reader], nil, nil, 0 !! readable end end |
#recv ⇒ Object Also known as: get
Receive an object from the channel.
113 114 115 |
# File 'lib/ichannel/unix_socket.rb', line 113 def recv recv!(nil) end |
#recv!(timeout = 0.1) ⇒ Object Also known as: get!
Receive an object from the channel.
Unlike #recv, which waits indefinitely until the channel becomes readable, this method will raise an IOError when timeout seconds elapse and the channel remains unreadable.
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/ichannel/unix_socket.rb', line 137 def recv!(timeout = 0.1) if @reader.closed? raise IOError, 'The channel cannot be read from (closed).' end readable, _ = IO.select [@reader], nil, nil, timeout if readable msg = readable[0].readline(SEP).chomp SEP @last_msg = @serializer.load msg else raise Timeout::Error, 'Time out on read (waited for %s second(s))' % [timeout] end end |
#write(object) ⇒ Object Also known as: put
Add an object to the channel.
53 54 55 |
# File 'lib/ichannel/unix_socket.rb', line 53 def write(object) write!(object, nil) end |
#write!(object, timeout = 0.1) ⇒ Object Also known as: put!
Add an object to the channel.
Unlike #write, which waits indefinitely until the channel becomes writable, this method will raise an IOError when timeout seconds elapse and the channel remains unwritable.
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ichannel/unix_socket.rb', line 76 def write!(object, timeout = 0.1) if @writer.closed? raise IOError, 'The channel cannot be written to (closed).' end _, writable, _ = IO.select nil, [@writer], nil, timeout if writable msg = @serializer.dump(object) writable[0].syswrite "#{msg}#{SEP}" else raise IOError, 'The channel cannot be written to.' end end |