Class: Hokaido::ConnectionHandler

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Celluloid::Notifications
Defined in:
lib/hokaido/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ ConnectionHandler

Returns a new instance of ConnectionHandler.



10
11
12
13
14
# File 'lib/hokaido/server.rb', line 10

def initialize(connection)
  @connection = connection

  async.run
end

Instance Method Details

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hokaido/server.rb', line 16

def run
  _, port, host = @connection.peeraddr

  puts "#{host}:#{port} connected"

  case @connection.gets.chomp
  when 'broadcast'
    @connection.puts ':)'

    while chunk = @connection.readpartial(4096)
      publish 'broadcast', chunk
    end
  when 'watch'
    @connection.puts '=)'

    Watcher.new(@connection).link Actor.current

    Kernel.sleep
  else
    @connection.puts ':('
  end
rescue EOFError, Errno::EIO, Errno::ECONNRESET
  # do nothing
ensure
  puts "#{host}:#{port} disconnected"

  @connection.close
end