Class: OneDrb::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/onedrb.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: '127.0.0.1', port: nil) ⇒ Client

Returns a new instance of Client.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/onedrb.rb', line 96

def initialize(host: '127.0.0.1', port: nil)

  DRb.start_service

  puts 'no port supplied'.error unless port

  puts ('client connecting to port ' + port).info
  @obj = DRbObject.new_with_uri("druby://#{host}:#{port}")
  parent = self

  if @obj.respond_to? :services then

    @obj&.services.each do |name, methods|

      make_class(name, methods, parent)

    end

  end

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



175
176
177
178
179
180
181
182
183
# File 'lib/onedrb.rb', line 175

def method_missing(sym, *args)

  if args.last.is_a?(Hash) then
    @obj.send(sym, *args[0..-2], **args.last)
  else
    @obj.send(sym, *args)
  end

end

Class Method Details

.call(s, port: nil) ⇒ Object

Makes a remote call in 1 line of code using a URI Only available when using the ServiceMgr as the server object;

e.g. OneDrb::Client.call ‘odrb://clara.home/fun/go?arg=James&age=49’



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/onedrb.rb', line 123

def self.call(s, port: nil)

  r = s.match(/^odrb:\/\/([^\/]+)\/([^\/]+)\/([^\?]+)\??(.*)/).captures

  rawhostname, service, methodname, rawargs = r
  hostname, rawport = rawhostname.split(':',2)
  port ||= rawport

  rawargs2 = rawargs&.split('&').map {|x| x.split('=')}

  a1, a2 = rawargs2.partition do |field, value|
    field.downcase.to_sym == :arg
  end

  h = a2.map {|k,v| [k.to_sym, v]}.to_h
  a = a1.map(&:last)

  client = OneDrb::Client.new host: hostname, port: port

  if h.any? then

    if a.any? then
      client.call service.to_sym, methodname.to_sym, *a, **h
    else
      client.call service.to_sym, methodname.to_sym, **h
    end

  elsif a.any?

    client.call service.to_sym, methodname.to_sym, *a

  else

    client.call service.to_sym, methodname.to_sym

  end

end

Instance Method Details

#remoteObject



162
163
164
# File 'lib/onedrb.rb', line 162

def remote()
  @obj
end

#restart(service) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/onedrb.rb', line 166

def restart(service)

  return unless @obj.respond_to? :services

  @obj.restart(service)
  found = @obj.services.assoc(service)
  make_class(*found, self) if found.any?
end