Class: IChannel::UNIXSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/ichannel/unix_socket.rb

Instance Method Summary collapse

Constructor Details

#initialize(serializer = Marshal, adapter_options) ⇒ IChannel::UNIXSocket

Parameters:

  • serializer (#dump, #load) (defaults to: Marshal)

    Any object that implements dump, & load.



14
15
16
17
18
# File 'lib/ichannel/unix_socket.rb', line 14

def initialize(serializer = Marshal, adapter_options)
  @serializer = serializer
  @last_msg = nil
  @reader, @writer = ::UNIXSocket.pair :STREAM
end

Instance Method Details

#closeBoolean

Close the channel.

Returns:

  • (Boolean)

    Returns true when the channel has been closed.



34
35
36
37
38
39
40
# File 'lib/ichannel/unix_socket.rb', line 34

def close
  unless closed?
    @reader.close
    @writer.close
    true
  end
end

#closed?Boolean

Returns true when the channel is closed.

Returns:

  • (Boolean)

    Returns true when the channel is closed.



24
25
26
# File 'lib/ichannel/unix_socket.rb', line 24

def closed?
  @reader.closed? && @writer.closed?
end

#last_msgObject

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.

Returns:

  • (Object)

    Returns the last message to be written to the channel.



92
93
94
95
96
97
# File 'lib/ichannel/unix_socket.rb', line 92

def last_msg
  while readable?
    @last_msg = get
  end
  @last_msg
end

#readable?Boolean

Returns true when the channel is readable.

Returns:

  • (Boolean)

    Returns true when the channel is readable.



150
151
152
153
154
155
156
157
# File 'lib/ichannel/unix_socket.rb', line 150

def readable?
  if closed?
    false
  else
    readable, _ = IO.select [@reader], nil, nil, 0
    !! readable
  end
end

#recvObject Also known as: get

Receive an object from the channel.

Returns:

  • (Object)

    The object read from the channel.

Raises:

  • (IOError)

    When the channel is closed.



108
109
110
# File 'lib/ichannel/unix_socket.rb', line 108

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.

Parameters:

  • timeout (Numeric) (defaults to: 0.1)

    The number of seconds to wait for the channel to become readable.

Returns:

  • (Object)

    The object read from the channel.

Raises:



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ichannel/unix_socket.rb', line 132

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 IChannel::TimeoutError, 'Time out on read (waited for %s second(s))' % [timeout]
  end
end

#redis?Boolean

Returns false.

Returns:

  • (Boolean)

    Returns false.

See Also:



175
176
177
# File 'lib/ichannel/unix_socket.rb', line 175

def redis?
  false
end

#unix_socket?Boolean

Returns true.

Returns:

  • (Boolean)

    Returns true.

See Also:



165
166
167
# File 'lib/ichannel/unix_socket.rb', line 165

def unix_socket?
  true
end

#write(object) ⇒ Object Also known as: put

Add an object to the channel.

Parameters:

  • object (Object)

    An object to add.

Raises:

  • (IOError)

    When the channel is closed.



51
52
53
# File 'lib/ichannel/unix_socket.rb', line 51

def write(object)
  write!(object, nil)
end

#write!(object, timeout = 0.1) ⇒ Object Also known as: put!

Add an object to the channel.

Parameters:

  • timeout (Numeric) (defaults to: 0.1)

    The number of seconds to wait for the channel to become writable.

Raises:

  • (IOError)

    When timeout seconds elapse and the channel cannot be written to.



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ichannel/unix_socket.rb', line 71

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