Class: Nailgun::Client

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

Defined Under Namespace

Classes: Chunk, ChunkHeader

Constant Summary collapse

DEFAULTS =
{
  hostname:  'localhost',
  port:      2113,
  stdin:     nil,
  stdout:    STDOUT,
  stderr:    STDERR,
  env:       ENV,
  dir:       Dir.pwd
}.freeze
CHUNK_HEADER_LEN =
5
TIMEOUT =
5
TimeoutError =
Class.new(StandardError)
SocketFailedError =
Class.new(StandardError)
ConnectFailedError =
Class.new(StandardError)
UnexpectedChunktypeError =
Class.new(StandardError)
ServerExceptionError =
Class.new(StandardError)
ConnectionBrokenError =
Class.new(StandardError)
BadArgumentsError =
Class.new(StandardError)
OtherError =
Class.new(StandardError)
EXIT_CODE_EXCEPTIONS =
{
  999 => SocketFailedError,
  998 => ConnectFailedError,
  997 => UnexpectedChunktypeError,
  996 => ServerExceptionError,
  995 => ConnectionBrokenError,
  994 => BadArgumentsError
}.freeze
CHUNK_TYPES =
{
  stdin:     '0',
  stdout:    '1',
  stderr:    '2',
  stdin_eof: '.',
  arg:       'A',
  env:       'E',
  dir:       'D',
  cmd:       'C',
  exit:      'X'
}.freeze
VERSION =
'0.0.3'

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 DEFAULTS



69
70
71
72
73
74
75
76
77
78
# File 'lib/nailgun/client.rb', line 69

def initialize(opts = {})
  @opts = DEFAULTS.merge(opts)
  @socket = TCPSocket.new(*@opts.values_at(:hostname, :port))

  if block_given?
    yield self
    @socket.close
    return nil
  end
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



53
54
55
# File 'lib/nailgun/client.rb', line 53

def opts
  @opts
end

#socketObject (readonly)

Returns the value of attribute socket.



53
54
55
# File 'lib/nailgun/client.rb', line 53

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.



62
63
64
# File 'lib/nailgun/client.rb', line 62

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

Instance Method Details

#close!Object

Public: Explicitly close the TCPSocket



110
111
112
# File 'lib/nailgun/client.rb', line 110

def close!
  socket.close
end

#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



101
102
103
104
105
106
107
# File 'lib/nailgun/client.rb', line 101

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



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/nailgun/client.rb', line 84

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