Class: Zwiebel::Control
- Inherits:
-
Object
- Object
- Zwiebel::Control
- Defined in:
- lib/zwiebel/control.rb
Instance Attribute Summary collapse
-
#cookie ⇒ Object
Returns the value of attribute cookie.
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#socket ⇒ Object
Returns the value of attribute socket.
Instance Method Summary collapse
- #authenticate ⇒ Object
- #authenticated? ⇒ Boolean
- #close ⇒ Object
- #connect ⇒ Object
- #connected? ⇒ Boolean
-
#initialize(host: "127.0.0.1", port: 9051, cookie: nil) ⇒ Control
constructor
A new instance of Control.
- #quit ⇒ Object
- #read_reply ⇒ Object
- #send_command(command, *args) ⇒ Object
- #send_line(line) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(host: "127.0.0.1", port: 9051, cookie: nil) ⇒ Control
Returns a new instance of Control.
20 21 22 23 24 25 |
# File 'lib/zwiebel/control.rb', line 20 def initialize(host: "127.0.0.1", port: 9051, cookie: nil) @host = host @port = port @cookie = connect end |
Instance Attribute Details
#cookie ⇒ Object
Returns the value of attribute cookie.
18 19 20 |
# File 'lib/zwiebel/control.rb', line 18 def @cookie end |
#host ⇒ Object
Returns the value of attribute host.
18 19 20 |
# File 'lib/zwiebel/control.rb', line 18 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
18 19 20 |
# File 'lib/zwiebel/control.rb', line 18 def port @port end |
#socket ⇒ Object
Returns the value of attribute socket.
18 19 20 |
# File 'lib/zwiebel/control.rb', line 18 def socket @socket end |
Instance Method Details
#authenticate ⇒ Object
50 51 52 53 54 |
# File 'lib/zwiebel/control.rb', line 50 def authenticate send_line(@cookie ? "AUTHENTICATE #{}" : "AUTHENTICATE") reply = read_reply @authenticated = reply == "250 OK" end |
#authenticated? ⇒ Boolean
56 57 58 |
# File 'lib/zwiebel/control.rb', line 56 def authenticated? !!@authenticated end |
#close ⇒ Object
37 38 39 40 41 |
# File 'lib/zwiebel/control.rb', line 37 def close @socket.close unless @socket.nil? @authenticated = false @socket = nil end |
#connect ⇒ Object
27 28 29 30 31 |
# File 'lib/zwiebel/control.rb', line 27 def connect close @socket = TCPSocket.new(host, port) @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true) end |
#connected? ⇒ Boolean
33 34 35 |
# File 'lib/zwiebel/control.rb', line 33 def connected? !@socket.nil? end |
#quit ⇒ Object
43 44 45 46 47 48 |
# File 'lib/zwiebel/control.rb', line 43 def quit send_line("QUIT") reply = read_reply close reply end |
#read_reply ⇒ Object
77 78 79 |
# File 'lib/zwiebel/control.rb', line 77 def read_reply @socket.readline.chomp end |
#send_command(command, *args) ⇒ Object
67 68 69 70 |
# File 'lib/zwiebel/control.rb', line 67 def send_command(command, *args) authenticate unless authenticated? send_line([command, *args].join(" ")) end |
#send_line(line) ⇒ Object
72 73 74 75 |
# File 'lib/zwiebel/control.rb', line 72 def send_line(line) @socket.write("#{line}\r\n") @socket.flush end |
#version ⇒ Object
60 61 62 63 64 65 |
# File 'lib/zwiebel/control.rb', line 60 def version send_command("GETINFO", "version") reply = read_reply.split("=").last read_reply reply end |