Class: Scriptroute::ScriptrouteConnection

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

Overview

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

Constant Summary collapse

Client_name =
"%s(%d)" % [  defined?(Etc) ? Etc.getlogin : ENV['USER'], Process.uid ]
VERSION =

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

'0.4.14'

Instance Method Summary collapse

Constructor Details

#initializeScriptrouteConnection

Returns a new instance of ScriptrouteConnection.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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

Instance Method Details

#get_configHash

Returns the configuration of the scriptroute daemon.

Returns:

  • (Hash)

    the configuration of the scriptroute daemon



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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_replyString?

Returns A one-line reply, or nil if the connection terminated.

Returns:

  • (String, nil)

    A one-line reply, or nil if the connection terminated.



88
89
90
91
92
93
94
# File 'lib/scriptroute.rb', line 88

def get_reply
  begin
    @s.readline
  rescue EOFError
    nil
  end
end

#issue_command(t) ⇒ Object

Parameters:

  • t (String)

    the command to issue, newline optional



79
80
81
82
83
84
85
86
# 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) ⇒ String?

Returns the one-line reply, or nil if the connection timed out or was terminated.

Parameters:

  • t (String)

    the command to issue, newline optional

Returns:

  • (String, nil)

    the one-line reply, or nil if the connection timed out or was terminated.



45
46
47
48
49
50
51
52
53
54
55
# 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