Class: Cod::Bidir

Inherits:
Channel show all
Includes:
Callbacks
Defined in:
lib/cod/bidir.rb

Overview

A bidirectional pipe, also called a socket pair.

Defined Under Namespace

Classes: OtherEnd

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

#callbacks_enabled?, #register_callback, #using_callbacks

Methods inherited from Channel

#client, #interact, #service

Constructor Details

#initialize(serializer, path, socket, other) ⇒ Bidir

Initializes a Bidir channel given two or alternatively one end of a bidirectional pipe. (socketpair)

socket ---- other


37
38
39
40
41
# File 'lib/cod/bidir.rb', line 37

def initialize(serializer, path, socket, other)
  @serializer = serializer
  @socket, @other = socket, other
  @path = path
end

Instance Attribute Details

#otherObject (readonly)

The other side of the pipe.



16
17
18
# File 'lib/cod/bidir.rb', line 16

def other
  @other
end

#pathObject (readonly)

Path of the socket (.named), if based on a file system FIFO.



19
20
21
# File 'lib/cod/bidir.rb', line 19

def path
  @path
end

#serializerObject (readonly)

Serializer to use for messages on this transport.



10
11
12
# File 'lib/cod/bidir.rb', line 10

def serializer
  @serializer
end

#socketObject

This process’ end of the pipe, can be used for both reading and writing.



13
14
15
# File 'lib/cod/bidir.rb', line 13

def socket
  @socket
end

Class Method Details

._load(params) ⇒ Object

:nodoc:



89
90
91
92
93
94
# File 'lib/cod/bidir.rb', line 89

def self._load(params) # :nodoc:
  # Instead of a tcp client (no way to construct one at this point), we'll
  # insert a kind of marker in the object stream that will be replaced 
  # with a valid client later on. (hopefully)
  OtherEnd.new(Marshal.load(params))
end

.named(name, serializer = nil) ⇒ Object



21
22
23
24
25
# File 'lib/cod/bidir.rb', line 21

def self.named(name, serializer=nil)
  new(serializer || SimpleSerializer.new, 
    name, 
    UNIXSocket.new(name), nil)
end

.pair(serializer = nil) ⇒ Object



26
27
28
29
30
# File 'lib/cod/bidir.rb', line 26

def self.pair(serializer=nil)
  new(serializer || SimpleSerializer.new, 
    nil, 
    *UNIXSocket.pair)
end

Instance Method Details

#_dump(level) ⇒ Object

:nodoc:



82
83
84
85
86
87
88
# File 'lib/cod/bidir.rb', line 82

def _dump(level) # :nodoc:
  if !path && callbacks_enabled?
    register_callback { |conn| conn.send_io(other) }
  end
  
  Marshal.dump(path)
end

#closeObject



58
59
60
61
62
63
64
# File 'lib/cod/bidir.rb', line 58

def close
  socket.close; 
  other.close if other
rescue IOError
  # One code path through Cod::Process will close other prematurely. This
  # is to avoid an error. 
end

#getObject



50
51
52
53
54
55
56
# File 'lib/cod/bidir.rb', line 50

def get
  serializer.de(socket)
rescue EOFError, IOError
  raise Cod::ConnectionLost, 
    "All pipe ends seem to be closed. Reading from this pipe will not "+
    "return any data."
end

#put(obj) ⇒ Object



43
44
45
46
47
48
# File 'lib/cod/bidir.rb', line 43

def put(obj)
  using_callbacks(socket) do
    socket.write(
      serializer.en(obj))
  end
end

#swap!Object

Swaps the end of this pipe around.



68
69
70
# File 'lib/cod/bidir.rb', line 68

def swap!
  @socket, @other = @other, @socket
end

#to_read_fdsObject



98
99
100
# File 'lib/cod/bidir.rb', line 98

def to_read_fds
  [socket]
end