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.


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

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


61
62
63
# File 'lib/dizby/stream/connection.rb', line 61

def shutdown_pipe
  @shutdown_pipe
end

Instance Method Details

#close


38
39
40
41
42
# File 'lib/dizby/stream/connection.rb', line 38

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


48
49
50
51
52
# File 'lib/dizby/stream/connection.rb', line 48

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

#recv_request


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

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)


32
33
34
35
36
# File 'lib/dizby/stream/connection.rb', line 32

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

#wait_for_stream (private)


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

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