Ruby Thread with "watchdog"

Posted by Sergio Campamá on Stack Overflow See other posts from Stack Overflow or by Sergio Campamá
Published on 2012-04-06T05:03:17Z Indexed on 2012/04/07 17:29 UTC
Read the original article Hit count: 281

Filed under:
|
|
|

I'm implementing a ruby server for handling sockets being created from GPRS modules. The thing is that when the module powers down, there's no indication that the socket closed.

I'm doing threads to handle multiple sockets with the same server. What I'm asking is this: Is there a way to use a timer inside a thread, reset it after every socket input, and that if it hits the timeout, closes the thread? Where can I find more information about this?

EDIT: Code example that doesn't detect the socket closing

require 'socket'

server = TCPServer.open(41000)
loop do
    Thread.start(server.accept) do |client|
        puts "Client connected"

        begin
            loop do
                line = client.readline

                open('log.txt', 'a') { |f|
                    f.puts line.strip
                }
            end
        rescue
            puts "Client disconnected"
        end
    end
end 

© Stack Overflow or respective owner

Related posts about ruby

Related posts about multithreading