Class: Vedeu::Distributed::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/distributed/client.rb

Overview

A class for the client side of the DRb server/client relationship.

Examples:

client = Vedeu::Distributed::Client.
           connect("druby://localhost:21420")
client.input('a')
client.output # => 'some content...'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Vedeu::Distributed::Client

Returns a new instance of Vedeu::Distributed::Client.

Parameters:



29
30
31
# File 'lib/vedeu/distributed/client.rb', line 29

def initialize(uri)
  @uri = uri.to_s
end

Instance Attribute Details

#uriString (readonly, protected)

Returns:

  • (String)


100
101
102
# File 'lib/vedeu/distributed/client.rb', line 100

def uri
  @uri
end

Class Method Details

.connect(uri) ⇒ Object

Parameters:



21
22
23
# File 'lib/vedeu/distributed/client.rb', line 21

def self.connect(uri)
  new(uri).connect
end

Instance Method Details

#connectSymbol

Simulate connecting to the DRb server by requesting its status.

Returns:

  • (Symbol)


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vedeu/distributed/client.rb', line 37

def connect
  server.status

rescue DRb::DRbConnError
  drb_connection_error

rescue DRb::DRbBadURI
  Vedeu.log_stdout(message: 'Could not connect to DRb server, URI may ' \
                            'be bad.')

  :drb_bad_uri
end

#drb_connection_errorSymbol (private)

Returns:

  • (Symbol)


110
111
112
113
114
# File 'lib/vedeu/distributed/client.rb', line 110

def drb_connection_error
  Vedeu.log_stdout(message: 'Could not connect to DRb server.')

  :drb_connection_error
end

#input(data) ⇒ void|Symbol Also known as: read

Send input to the DRb server.

Parameters:

  • data (String|Symbol)

Returns:

  • (void|Symbol)


54
55
56
57
58
59
# File 'lib/vedeu/distributed/client.rb', line 54

def input(data)
  server.input(data)

rescue DRb::DRbConnError
  drb_connection_error
end

#outputvoid|Symbol Also known as: write

Fetch output from the DRb server.

Returns:

  • (void|Symbol)


65
66
67
68
69
70
# File 'lib/vedeu/distributed/client.rb', line 65

def output
  server.output

rescue DRb::DRbConnError
  drb_connection_error
end

#servervoid (private)

This method returns an undefined value.



105
106
107
# File 'lib/vedeu/distributed/client.rb', line 105

def server
  @server ||= DRbObject.new_with_uri(uri)
end

#shutdownvoid|Symbol

Note:

Runtime::Application will raise StopIteration when its ‘.stop` method is called. Here we rescue that to give a clean client exit.

Shutdown the DRb server and the client application.

Returns:

  • (void|Symbol)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vedeu/distributed/client.rb', line 81

def shutdown
  server.shutdown

  Process.kill('KILL', server.pid)

rescue DRb::DRbConnError
  drb_connection_error

rescue Interrupt
  Vedeu.log_stdout(message: 'Client application exited.')

ensure
  :shutdown
end