class Scriptroute::ScriptrouteConnection

A ScriptrouteConnection is an object that wraps the TCP socket used to connect to a scriptroute daemon. It likely need not be used directly, unless accessing low level scriptroute commands not wrapped by the general Scriptroute module

Constants

Client_name
VERSION

this version is designed to emulate the 0.4.14 version of srinterpreter.

Public Class Methods

new() click to toggle source
# File lib/scriptroute.rb, line 26
def initialize
  begin
    timeout(5) do 
      @s = TCPSocket.new 'localhost', 3356
    end
    reply = ""
    reply = one_line_command( "interpret v%s %s\n" % [ VERSION, Client_name ]  )
    if reply =~ /proceed( Hz=(\d+))/ then
      # puts "negotiated."
    else
      puts "failed to negotiate: %s" % reply
    end
  rescue Errno::ECONNREFUSED => e
    $stderr.puts "Connection refused connecting to scriptrouted, ensure that it is running"
    raise e
  end
end

Public Instance Methods

get_config() click to toggle source

@return [Hash] the configuration of the scriptroute daemon

# File lib/scriptroute.rb, line 57
def get_config
  ret = Hash.new
  timeout(5) do 
    issue_command "showconfig\n"
    l = "do/while"
    # showconfig ends reply with an empty line.
    while l !~ /^#done/  && l !~ /^$/ 
      l = @s.readline
      if l =~ /^(\S+)\s+=\s+(.+)$/ then
        ret[$1] = $2
      elsif l =~ /^(\S+)\s+=\s*$/ then
        ret[$1] = nil
      elsif l =~ /^$/ then
        # end.
      else
        puts "unparsed config: %s" % l 
      end
    end 
  end
  ret
end
get_reply() click to toggle source

@return [String,nil] A one-line reply, or nil if the connection terminated.

# File lib/scriptroute.rb, line 88
def get_reply
  begin
    @s.readline
  rescue EOFError
    nil
  end
end
issue_command(t) click to toggle source

@param t [String] the command to issue, newline optional

# File lib/scriptroute.rb, line 79
def issue_command(t)
  # $stderr.puts "writing %s" % t
  if t[-1] == "\n" then
    @s.write t
  else
    @s.write "%s\n" % [ t ]
  end
end
one_line_command(t) click to toggle source

@param t [String] the command to issue, newline optional @return [String, nil] the one-line reply, or nil if the connection timed out or was terminated.

# File lib/scriptroute.rb, line 45
def one_line_command(t)
  begin
    reply = nil
    timeout(2) do 
      issue_command t
      reply = @s.readline
    end
  rescue EOFError
  end
  return reply
end