Class: IChannel

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

Constant Summary collapse

VERSION =
"5.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(serializer) ⇒ IChannel

Returns a new instance of IChannel.

Parameters:

  • serializer (#dump, #load)

    Any object that implements dump, & load.



11
12
13
14
# File 'lib/ichannel.rb', line 11

def initialize(serializer)
  @reader, @writer = UNIXSocket.pair Socket::SOCK_DGRAM
  @serializer = serializer
end

Instance Method Details

#closeBoolean

Close the channel.

Returns:

  • (Boolean)

    Returns true when the channel has been closed. Returns nil when the channel is already closed.



31
32
33
34
35
36
37
# File 'lib/ichannel.rb', line 31

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.



20
21
22
# File 'lib/ichannel.rb', line 20

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

#readable?Boolean

Returns true when the channel is readable.

Returns:

  • (Boolean)

    Returns true when the channel is readable.



136
137
138
139
140
141
142
143
# File 'lib/ichannel.rb', line 136

def readable?
  if @reader.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.



94
95
96
# File 'lib/ichannel.rb', line 94

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:

  • (IOError)

    When timeout seconds elapse & the channel remains unreadable.



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ichannel.rb', line 118

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
    @serializer.load msg
  else
    raise IOError, 'The channel cannot be read from.'
  end
end

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

Add an object to the channel.

Parameters:

  • object (Object)

    An object to add to the channel.

Raises:

  • (IOError)

    When the channel is closed.



48
49
50
# File 'lib/ichannel.rb', line 48

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.

Parameters:

  • timeout (Numeric) (defaults to: 0.1)

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

  • object (Object)

    An object to add to the channel.

Raises:

  • (IOError)

    When timeout seconds elapse & the channel remains unwritable.

  • (IOError)

    When the channel is closed.



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ichannel.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