Class: DRbFire::Protocol

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/drb/drbfire.rb

Overview

:nodoc:

Defined Under Namespace

Classes: ClientServer, ClientServerProxy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, delegate) ⇒ Protocol

Returns a new instance of Protocol.



247
248
249
250
251
252
# File 'lib/drb/drbfire.rb', line 247

def initialize(uri, delegate)
  super(delegate)
  @uri = uri
  @id = 0
  @id_mutex = Mutex.new
end

Instance Attribute Details

#signal_idObject (readonly)

Returns the value of attribute signal_id.



245
246
247
# File 'lib/drb/drbfire.rb', line 245

def signal_id
  @signal_id
end

#uriObject (readonly)

Returns the value of attribute uri.



245
246
247
# File 'lib/drb/drbfire.rb', line 245

def uri
  @uri
end

Class Method Details

.add_client_connection(id, connection) ⇒ Object



192
193
194
195
196
197
# File 'lib/drb/drbfire.rb', line 192

def add_client_connection(id, connection)
  if((c = @client_servers[id]))
    c.push(connection)
  else
  end
end

.add_client_server(id, server) ⇒ Object



199
200
201
# File 'lib/drb/drbfire.rb', line 199

def add_client_server(id, server)
  @client_servers[id] = server
end

.open(uri, config, type = INCOMING_CONN) ⇒ Object



182
183
184
185
186
187
188
189
190
# File 'lib/drb/drbfire.rb', line 182

def open(uri, config, type=INCOMING_CONN)
  unless(server?(config))
    connection = new(uri, delegate(config).open(uri, config))
    connection.stream.write(type)
    connection
  else
    @client_servers[parse_uri(uri).last.to_i].open
  end
end

.open_server(uri, config) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/drb/drbfire.rb', line 162

def open_server(uri, config)
  # make sure the scheme is ours
  parse_uri(uri)
  
  if(server?(config))
    @client_servers ||= {}
    
    sock = delegate(config).open_server(uri, config)
    
    # get the uri from the delegate, and replace the scheme with drbfire://
    # this allows randomly chosen ports (:0) to work
    scheme = sock.uri.match(/^(.*):\/\//)[1]
    drbfire_uri = sock.uri.sub(scheme, SCHEME)
    
    new(drbfire_uri, sock)
  else
    ClientServer.new(uri, config)
  end
end

.parse_uri(uri) ⇒ Object



203
204
205
206
207
208
209
210
# File 'lib/drb/drbfire.rb', line 203

def parse_uri(uri)
  if(%r{^#{SCHEME}://([^:]+):(\d+)(?:\?(.+))?$} =~ uri)
    [$1, $2.to_i, $3]
  else
    raise DRb::DRbBadScheme, uri unless(/^#{SCHEME}/ =~ uri)
    raise DRb::DRbBadURI, "Can't parse uri: #{uri}"
  end
end

.uri_option(uri, config) ⇒ Object



212
213
214
215
# File 'lib/drb/drbfire.rb', line 212

def uri_option(uri, config)
  host, port, option = parse_uri(uri)
  return "#{SCHEME}://#{host}:#{port}", option
end

Instance Method Details

#acceptObject



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/drb/drbfire.rb', line 254

def accept
  while(__getobj__.instance_eval{@socket})
    begin
      connection = self.class.new(nil, __getobj__.accept)
    rescue IOError
      return nil
    end
    begin
      type = connection.stream.read(1)
    rescue
      next
    end
    case type
    when INCOMING_CONN
      return connection
    when OUTGOING_CONN
      self.class.add_client_connection(connection.read_signal_id, connection)
      next
    when SIGNAL_CONN
      new_id = nil
      @id_mutex.synchronize do
        new_id = (@id += 1)
      end
      client_server = ClientServerProxy.new(connection, new_id)
      self.class.add_client_server(new_id, client_server)
      client_server.write_signal_id
      next
    else
      raise "Invalid type #{type}"
    end
  end
end

#read_signal_idObject



287
288
289
# File 'lib/drb/drbfire.rb', line 287

def read_signal_id
  stream.read(4).unpack(ID_FORMAT).first
end