Class: Nailgun::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/nailgun/client.rb,
lib/nailgun/client/chunk.rb,
lib/nailgun/client/chunk_header.rb

Defined Under Namespace

Classes: Chunk, ChunkHeader, ExitStatus

Constant Summary collapse

LOGGER =
Logger.new('log')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Public: Initialize a Client.

opts = {} - a Hash of options to override the defaults in Nailgun::DEFAULTS



28
29
30
31
32
# File 'lib/nailgun/client.rb', line 28

def initialize(opts = {})
  @opts = Nailgun::DEFAULTS.merge(opts)
  @socket = TCPSocket.new(*@opts.values_at(:hostname, :port))
  debug "Opened new #{@socket.inspect}"
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



12
13
14
# File 'lib/nailgun/client.rb', line 12

def opts
  @opts
end

#socketObject (readonly)

Returns the value of attribute socket.



12
13
14
# File 'lib/nailgun/client.rb', line 12

def socket
  @socket
end

Class Method Details

.run(command, args, opts = {}) ⇒ Object

Public: Convinience method to instantiate and run the command

command - see #run args - see #run opts = {} - see #initialize

Returns the duplicated String.



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

def self.run(command, args, opts = {})
  self.new(opts).run(command, args)
end

Instance Method Details

#receive_loopObject

Public: Start the receiver loop Thread, memoize it, and return the Thread

Returns the Thread object, whose value will eventually be the exit status from the Nailgun server



55
56
57
58
59
60
61
# File 'lib/nailgun/client.rb', line 55

def receive_loop
  @loop ||= Thread.new {
    catch(:exit) do
      loop { receive_chunk }
    end
  }
end

#run(command, *args) ⇒ Object

Public: Run a command on the Client instance

command - the command string *args - any arguments to send



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

def run(command, *args)
  receive_loop # start the loop

  send_args     args.flatten
  send_env      opts[:env]
  send_dir      opts[:dir]
  send_command  command
  send_stdin    opts[:stdin]

  receive_loop.join
  return nil
end