Class: NL::KndClient::EMKndCommand

Inherits:
Object
  • Object
show all
Includes:
EM::Deferrable
Defined in:
lib/nl/knd_client/em_knd_command.rb

Overview

Represents a deferred KND command run by the EventMachine-based client, EMKndClient. Callbacks will be called with the EMKndCommand object as their sole parameter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *args) ⇒ EMKndCommand

Initializes a deferred ‘name’ command, removing commas from arguments (KND command arguments cannot contain commas)



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/nl/knd_client/em_knd_command.rb', line 12

def initialize(name, *args)
  @name = name
  @args = args
  @argstring = (args.length > 0 && " #{@args.map {|s| s.to_s.gsub(',', '') if s != nil }.join(',')}") || ''
  @linecount = 0
  @lines = []
  @message = ""

  timeout 10

  log "Command #{@name} Initialized" if EMKndClient.debug_cmd?(@name)
end

Instance Attribute Details

#linecountObject (readonly)

Returns the value of attribute linecount.



8
9
10
# File 'lib/nl/knd_client/em_knd_command.rb', line 8

def linecount
  @linecount
end

#linesObject (readonly)

Returns the value of attribute lines.



8
9
10
# File 'lib/nl/knd_client/em_knd_command.rb', line 8

def lines
  @lines
end

#messageObject (readonly)

Returns the value of attribute message.



8
9
10
# File 'lib/nl/knd_client/em_knd_command.rb', line 8

def message
  @message
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/nl/knd_client/em_knd_command.rb', line 8

def name
  @name
end

Instance Method Details

#add_line(line) ⇒ Object

Called by EMKndClient to add a line Returns true when enough lines have been received



56
57
58
59
60
61
62
63
# File 'lib/nl/knd_client/em_knd_command.rb', line 56

def add_line(line)
  lines << line
  @linecount -= 1
  if @linecount == 0
    succeed self
  end
  return @linecount == 0
end

#err_line(message) ⇒ Object

Called by EMKndClient when an error line is received



46
47
48
49
50
51
52
# File 'lib/nl/knd_client/em_knd_command.rb', line 46

def err_line(message)
  @message = message

  log "Command #{@name} ERR - #{message}" if EMKndClient.debug_cmd?(@name)

  fail self
end

#ok_line(message) ⇒ Object

Called by EMKndClient when a success line is received Returns true if the command is done, false if lines are needed



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nl/knd_client/em_knd_command.rb', line 27

def ok_line(message)
  @message = message

  log "Command #{@name} OK - #{message}" if EMKndClient.debug_cmd?(@name)

  case @name
  when "zones", "help"
    @linecount = message.to_i
  end

  if @linecount == 0
    succeed self
    return true
  end

  return false
end

#to_sObject

Converts the command to a string formatted for sending to knd.



66
67
68
# File 'lib/nl/knd_client/em_knd_command.rb', line 66

def to_s
  "#{@name}#{@argstring}"
end