Class: DRb::DRbSSHServerConn

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

Overview

DRbSSH protocol server per-connection class. Handles client-to-client communications by utilizing thread-safe Queue’s for the input and output, and by adding a small ‘rep’ or ‘req’ packet before sending, so we can have two-way DRb duplexed over a single pair of filedescriptors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, config, client) ⇒ DRbSSHServerConn

Create a new server-connection for the specified client.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/drbssh.rb', line 208

def initialize(uri, config, client)
  @uri = uri
  @client = client
  @srv_requestq = Queue.new

  msg = DRbMessage.new(config)

  # Read-thread
  Thread.new do
    # Read from client, and delegate request/reply to the correct place.
    begin
      loop do
        type = msg.load(client.read_fd)
        if type == 'req'
          @srv_requestq.push(msg.recv_request(client.read_fd))
        else
          client.receiveq.push(msg.recv_reply(client.read_fd))
        end
      end
    rescue
      client.receiveq.push($!)
      @srv_requestq.push($!)
    end
  end

  # Write-thread
  Thread.new do
    # Wait for outgoing data on send queue, and add header-packet before
    # writing.
    begin
      loop do
        type, data = client.sendq.pop

        client.write_fd.write(msg.dump(type))

        if type == 'req'
          msg.send_request(client.write_fd, *data)
        else
          msg.send_reply(client.write_fd, *data)
        end
      end
    rescue
      client.receiveq.push($!)
      @srv_requestq.push($!)
    end
  end
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



205
206
207
# File 'lib/drbssh.rb', line 205

def uri
  @uri
end

Instance Method Details

#closeObject

Delegate shutdown to client.



257
258
259
260
261
262
263
264
265
# File 'lib/drbssh.rb', line 257

def close
  return unless @client.alive?

  Timeout::timeout(15) do
    sleep 0.1 until @client.sendq.empty?
  end rescue nil

  @client.close
end

#recv_requestObject

Wait for a request to appear on the request-queue



268
269
270
271
272
273
274
275
276
# File 'lib/drbssh.rb', line 268

def recv_request
  reply = @srv_requestq.pop
  if reply.is_a? Exception
    self.close
    raise reply
  else
    reply
  end
end

#send_reply(succ, result) ⇒ Object

Queue client-reply



279
280
281
# File 'lib/drbssh.rb', line 279

def send_reply(succ, result)
  @client.sendq.push(['rep', [succ, result]])
end