require 'Qt' require 'tempfile' # Implements a TCP server listening on port 8081 # This server will accept HTTP requests and deliver a HTML page containing an image # with the current snapshot. class MyServer < Qt::TcpServer slots 'connection()' # Initialize the server and put into listen mode (port is 8081) def initialize(parent = nil) super ha = Qt::HostAddress.new("0.0.0.0") listen(ha, 8081) connect(self, SIGNAL('newConnection()'), SLOT('connection()')); end # Signals an incoming connection def connection begin connection = nextPendingConnection url = nil while connection.isOpen if connection.canReadLine line = connection.readLine.to_s if line.chomp == "" break elsif line =~ /GET\s+(.*)\s+HTTP/ url = Qt::Url.new($1) end else connection.waitForReadyRead(100) end end if url && url.path == "/screenshot.png" # Deliver the image view = RBA::Application.instance.main_window.current_view if view tmp = Tempfile.new("klayout-screenshot") view.save_image(tmp.path, 400, 400) tmpin = Qt::File.new(tmp.path) tmpin.open(1) connection.write(tmpin.readAll) tmpin.close tmp.close end elsif url && url.path == "/screenshot.html" # Deliver the HTML page connection.write('') else connection.write("Invalid URL") end connection.disconnectFromHost() rescue puts "ERROR #{$!}" end end end # Start the screenshot server $server = MyServer.new # It is important to run the application in the script so that exceptions are caught RBA::Application.instance.exec