class Knot::Protocol

Attributes

conf[R]
debug[RW]
sock[R]
zones[R]

Public Class Methods

new(path_or_sock = nil) click to toggle source
# File lib/knot/protocol.rb, line 91
def initialize path_or_sock = nil
        case path_or_sock
        when String, Pathname
                @sock = UNIXSocket.new path_or_sock.to_s
        when Socket
                @sock = path_or_sock
        when nil
                @sock = UNIXSocket.new '/run/knot/knot.sock'
        end
        @debug = false
        @conf = Knot::Conf.new self
        @zones = Hash.new {|h, zone| h[zone] = Knot::Zone.new zone, self }
end

Public Instance Methods

call(sock: nil, **data) click to toggle source
# File lib/knot/protocol.rb, line 168
def call sock: nil, **data
        snd sock: sock, **data
        rcv( sock: sock).each do |r| 
                if r[:error]
                        if e = Knot::Errors.err2exc[r[:error]]
                                raise e, r[:error]
                        end
                        raise Knot::Error, r[:error]
                end
        end
end
conf_set( **opts) click to toggle source
# File lib/knot/protocol.rb, line 182
def conf_set( **opts)   call opts.update( command: 'conf-set') end
conf_unset( **opts) click to toggle source
# File lib/knot/protocol.rb, line 183
def conf_unset( **opts) call opts.update( command: 'conf-set') end
rcv(sock: nil) click to toggle source
# File lib/knot/protocol.rb, line 144
def rcv sock: nil
        ret, r = [], nil
        sock = sock || @sock
        sock = RecordIO.new sock  if @debug
        loop do
                t = sock.unpack1 'c'
                case t
                when 0, 3
                        return ret
                when 1, 2
                        type = t
                        ret.push( r = {})
                else 
                        raise Knot::Errors::EINVAL, "Missing Type before: #{t}"  if ret.empty?
                        i = Idx::Idx[t - 0x10] or raise Knot::Errors::EINVAL, "Unknown index: #{t-0x10}"
                        l = sock.unpack1 'n'
                        r[i] = sock.read( l)
                end
        end
ensure
        STDERR.puts( {rcvd: ret, read: sock.str}.inspect)  if @debug
        ret
end
snd(sock: nil, **data) click to toggle source
# File lib/knot/protocol.rb, line 105
def snd sock: nil, **data
        rsock = sock || @sock
        s = ''
        sock = StringIO.new s
        sock.write [1].pack( 'c')
        data[:flags] ||= ''
        Idx::Idx.each_with_index do |n, i|
                v = data[n]&.to_s
                sock.write [0x10+i, v.size, v].pack( 'c na*')  if v
        end
        sock.write [3].pack( 'c')
        sock.flush
        STDERR.puts( {data: data, _: s}.inspect)  if @debug
        rsock.write s
        rsock.flush
end
zone( zone) click to toggle source
# File lib/knot/protocol.rb, line 180
def zone( zone) @zones[zone.to_s.to_sym] end
zone_get( **opts) click to toggle source
# File lib/knot/protocol.rb, line 187
def zone_get( **opts)   call opts.update( command: 'zone-get') end
zone_set( **opts) click to toggle source
# File lib/knot/protocol.rb, line 185
def zone_set( **opts)   call opts.update( command: 'zone-set') end
zone_unset( **opts) click to toggle source
# File lib/knot/protocol.rb, line 186
def zone_unset( **opts) call opts.update( command: 'zone-unset') end