class VirtP2V::Connection

Public Class Methods

new(hostname, username, password) click to toggle source
# File lib/virt-p2v/connection.rb, line 37
def initialize(hostname, username, password)
    @mutex = Mutex.new
    @connection_listeners = []

    @hostname = hostname
    @username = username
    @password = password

    @buffer = ""

    @session = nil
    @channel = nil
end

Public Instance Methods

close() click to toggle source
# File lib/virt-p2v/connection.rb, line 110
def close
    unless @channel.nil?
        @channel.close
        @channel = nil
    end
    unless @session.nil?
        @session.close
        @session = nil
    end

    @buffer = ''
end
connect(&cb) click to toggle source
# File lib/virt-p2v/connection.rb, line 51
def connect(&cb)
    run(cb) {
        begin
            @session = Libssh2.connect(@hostname, @username, @password)                      if @session.nil?

            @channel = @session.exec('LANG=C virt-p2v-server')                      if @channel.nil?
        #rescue Libssh2::Session::InvalidHostnameError
        #    raise InvalidHostnameError
        #rescue Libssh2::Session::InvalidCredentialsError
        #    raise InvalidCredentialsError
        #rescue => ex
        #    raise RemoteError.new(ex.message)
        end

        begin
            line = @channel.read(64)

            if line !~ %r^VIRT_P2V_SERVER /
                raise RemoteError.new("Unexpected response: #{line}")
            end
        rescue Libssh2::Channel::ApplicationError => ex
            if ex.message =~ %rcommand not found/
                raise RemoteError.new(
                    "virt-p2v-server is not installed on #{@hostname}")
            else
                raise RemoteError.new(ex.message)
            end
        end

        begin
            i = 0;
            listener_result = lambda { |result|
                if result.kind_of?(Exception)
                    cb.call(result)
                else
                    i += 1
                    if i == @connection_listeners.length
                        cb.call(true)
                    else
                        Gtk.queue {
                            @connection_listeners[i].call(listener_result)
                        }
                    end
                end
            }
            Gtk.queue { @connection_listeners[0].call(listener_result) }
        rescue => ex
            @channel.close unless @channel.nil?
            raise ex
        end
    }
end
connected?() click to toggle source
# File lib/virt-p2v/connection.rb, line 106
def connected?
    return !@channel.nil?
end
container(type, &cb) click to toggle source
# File lib/virt-p2v/connection.rb, line 191
def container(type, &cb)
    raise NotConnectedError if @channel.nil?

    run(cb) {
        @channel.write("CONTAINER #{type}\n")
        result = parse_return

        Gtk.queue { cb.call(result) }
    }
end
convert(&cb) click to toggle source
# File lib/virt-p2v/connection.rb, line 158
def convert(&cb)
    raise NotConnectedError if @channel.nil?

    run(cb) {
        @channel.write("CONVERT\n")
        result = parse_return

        Gtk.queue { cb.call(result) }
    }
end
lang(lang, &cb) click to toggle source
# File lib/virt-p2v/connection.rb, line 123
def lang(lang, &cb)
    raise NotConnectedError if @channel.nil?

    run(cb) {
        @channel.write("LANG #{lang}\n")
        result = parse_return

        Gtk.queue { cb.call(result) }
    }
end
list_profiles(&cb) click to toggle source
# File lib/virt-p2v/connection.rb, line 169
def list_profiles(&cb)
    raise NotConnectedError if @channel.nil?

    run(cb) {
        @channel.write("LIST_PROFILES\n")
        result = parse_return

        Gtk.queue { cb.call(result) }
    }
end
metadata(meta, &cb) click to toggle source
# File lib/virt-p2v/connection.rb, line 134
def metadata(meta, &cb)
    raise NotConnectedError if @channel.nil?

    run(cb) {
        payload = YAML::dump(meta)
        @channel.write("METADATA #{payload.length}\n");
        @channel.write(payload)
        result = parse_return

        Gtk.queue { cb.call(result) }
    }
end
on_connect(&cb) click to toggle source
# File lib/virt-p2v/connection.rb, line 33
def on_connect(&cb)
    @connection_listeners << cb
end
path(length, path, &cb) click to toggle source
# File lib/virt-p2v/connection.rb, line 147
def path(length, path, &cb)
    raise NotConnectedError if @channel.nil?

    run(cb) {
        @channel.write("PATH #{length} #{path}\n")
        result = parse_return

        Gtk.queue { cb.call(result) }
    }
end
send_data(io, length, progress, &completion) click to toggle source
# File lib/virt-p2v/connection.rb, line 202
def send_data(io, length, progress, &completion)
    raise NotConnectedError if @channel.nil?

    run(completion) {
        @channel.write("DATA #{length}\n")
        @channel.send_data(io) { |total|
            Gtk.queue { progress.call(total) }
        }

        result = parse_return

        Gtk.queue { progress.call(length); completion.call(result) }
    }
end
set_profile(profile, &cb) click to toggle source
# File lib/virt-p2v/connection.rb, line 180
def set_profile(profile, &cb)
    raise NotConnectedError if @channel.nil?

    run(cb) {
        @channel.write("SET_PROFILE #{profile}\n")
        result = parse_return

        Gtk.queue { cb.call(result) }
    }
end