6 # A connection to an ICS server
8 class Connection < Qt::Object
11 signals :hostFound, :established,
12 'receivedLine(QString, int)',
13 'receivedText(QString, int)'
15 def initialize(host, port)
18 @create_socket = lambda do
19 puts "connecting to #{host}:#{port}"
20 s = Qt::TcpSocket.new(self)
21 connect(s, SIGNAL(:hostFound), self, SIGNAL(:hostFound))
22 s.connect(s, SIGNAL(:connected), self, SIGNAL(:established))
23 s.on(:readyRead) { process_line }
24 s.connect_to_host(host, port)
30 puts "processing line"
36 while @socket.can_read_line
37 line = @socket.read_line.to_s
38 line = @buffer + line.gsub("\r", '')
39 emit receivedLine(line, @buffer.size)
43 if (size = @socket.bytes_available) > 0
44 data = @socket.read_all
46 @buffer += data.to_s.gsub("\r", '')
47 emit receivedText(@buffer, offset)
53 @socket = @create_socket.call
62 puts "> #{text}" if @debug
64 @unsent_text += @text + "\n"
74 os = Qt::TextStream(@socket)
78 def on_received_line(&blk)
79 connect(self, SIGNAL('receivedLine(QString, int)'), &blk)
82 def on_received_text(&blk)
83 connect(self, SIGNAL('receivedText(QString, int)'), &blk)