Sha256: 54f2783077b1b1b28fbae6a4a4fdbc11508b3db68ef240c0803c0663cc8b73b1

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

require "socket"

module VncTools
  class Server

    class Error < StandardError
    end

    class << self
      def displays
        Dir[File.expand_path("~/.vnc/*.pid")].map { |e| e[/(\d+)\.pid/, 1] }.compact
      end

      def all
        displays.map { |display| new ":#{display}" }
      end

      attr_writer :executable

      def executable
        @executable ||= "tightvncserver"
      end
    end

    attr_reader :display

    def initialize(display = nil)
      @display = display
    end

    def start
      if display
        server display
      else
        output = server
        @display = output[/desktop is #{host}(\S+)/, 1]
      end
    end

    def stop
      server "-kill", display.to_s
    end

    private

    def server(*args)
      cmd = [self.class.executable, args, '2>&1'].flatten.compact
      out = `#{cmd.join ' '}`

      unless last_status.success?
        raise Error, "could not run #{self.class.executable}: #{out.inspect}"
      end

      out
    end

    def last_status
      $?
    end

    def host
      @host ||= Socket.gethostname
    end
  end # VncServer
end # CukeForker



Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vnctools-0.0.3 lib/vnctools/server.rb
vnctools-0.0.2 lib/vnctools/server.rb
vnctools-0.0.1 lib/vnctools/server.rb