Sha256: aac78eb1ad2d86928252a6125d912777bb2eacd543fc8273a7645bb70287b481

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

# Copyright © 2007 OpenRain, LLC. All rights reserved.
#
# Preston Lee <preston.lee@openrain.com>

require 'socket'

module Journeta
  
  # Accepts inbound connections from other peers using TCP.
  # After the peer finishes sending data, the connection is terminated.
  # No data is returned to the sender.
  class PeerListener < Journeta::Asynchronous
    
    def go
      begin
        # Grab configuration information from the injected object.
        port = @engine.peer_port
        socket = TCPServer.new(port) 
        putsd "Listening on port #{port}"
        
        begin
          loop do             
            session = socket.accept
            # We'll put the actual handling of the new session in the background so we 
            # can continue listening for new connections as soon as possible.
            Thread.new do 
              data = ''
              # Read every last bit from the socket before passing off to the handler.
              while more = session.gets
                data += more
              end
              begin
                msg     = YAML::load(data)
                h = @engine.peer_handler
                h.call msg              
              rescue
                putsd "YAML could not be deserialized! The data will not be passed up to the application."
              end
            end
          end
        rescue
          putsd "Session closed."
        end 
      ensure 
        putsd "Closing peer listener socket."
        # session.close
        # socket.close
      end
    end
    
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
journeta-0.1.2 lib/journeta/peer_listener.rb
journeta-0.1.3 lib/journeta/peer_listener.rb