Class: Dizby::BasicConnection

Inherits:
Messenger show all
Defined in:
lib/dizby/stream/connection.rb

Instance Attribute Summary collapse

Attributes inherited from Messenger

#remote_uri, #server

Instance Method Summary collapse

Methods inherited from Messenger

#closed?

Methods included from WritableStream

#dump_data, #dump_obj, #write

Methods included from ReadableStream

#check_packet_size, #load_obj, #load_packet, #load_size, #read

Constructor Details

#initialize(server, stream) ⇒ BasicConnection

Returns a new instance of BasicConnection.


9
10
11
12
13
14
15
16
17
# File 'lib/dizby/stream/connection.rb', line 9

def initialize(server, stream)
  super(server, stream)

  # get the uri that the client recognizes the server as
  @remote_uri = read

  @shutdown_pipe = SelfPipe.new(*IO.pipe)
  @object_space = []
end

Instance Attribute Details

#shutdown_pipe (readonly, private)

Returns the value of attribute shutdown_pipe


63
64
65
# File 'lib/dizby/stream/connection.rb', line 63

def shutdown_pipe
  @shutdown_pipe
end

Instance Method Details

#close


40
41
42
43
44
# File 'lib/dizby/stream/connection.rb', line 40

def close
  @object_space.clear
  shutdown_pipe.close_write if shutdown_pipe
  super
end

#make_distributed(_obj, _error) (private)

when a distributed object is made through a connection, store it so that it doesn't get consumed by the garbage collector


50
51
52
53
54
# File 'lib/dizby/stream/connection.rb', line 50

def make_distributed(_obj, _error)
  distributed = super
  @object_space << distributed
  distributed
end

#recv_request


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dizby/stream/connection.rb', line 19

def recv_request
  wait_for_stream

  ref, msg, argc = 3.times.map { read }

  @server.log.debug("called through proxy: #{ref} #{msg}")
  fail ConnectionError, 'too many arguments' if @server.argc_limit < argc

  argv = Array.new(argc) { read }
  block = read

  ro = @server.to_obj(ref)
  [ro, msg, argv, block]
end

#send_reply(succ, result)


34
35
36
37
38
# File 'lib/dizby/stream/connection.rb', line 34

def send_reply(succ, result)
  write(dump_data(succ) + dump_data(result, !succ))
rescue
  raise ConnectionError, $!.message, $!.backtrace
end

#wait_for_stream (private)


56
57
58
59
60
61
# File 'lib/dizby/stream/connection.rb', line 56

def wait_for_stream
  readable, = IO.select([@stream, shutdown_pipe.read])
  fail RemoteServerShutdown if readable.include?(shutdown_pipe.read)
rescue IOError
  raise RemoteServerShutdown
end