Parent

Included Modules

Listen::TCP::Broadcaster

Attributes

server[R]
sockets[R]

Public Class Methods

new(host, port) click to toggle source

Initializes a Celluloid::IO-powered TCP-broadcaster

@param [String] host to broadcast on @param [String] port to broadcast on

Note: Listens on all addresses when host is nil

# File lib/listen/tcp/broadcaster.rb, line 19
def initialize(host, port)
  @server = TCPServer.new(host, port)
  @sockets = []
end

Public Instance Methods

broadcast(payload) click to toggle source

Broadcasts given payload to all connected sockets

# File lib/listen/tcp/broadcaster.rb, line 39
def broadcast(payload)
  @sockets.each do |socket|
    unicast(socket, payload)
  end
end
finalize() click to toggle source

Cleans up sockets and server

# File lib/listen/tcp/broadcaster.rb, line 30
def finalize
  if @server
    @sockets.clear
    @server.close
    @server = nil
  end
end
handle_connection(socket) click to toggle source

Handles incoming socket connection

# File lib/listen/tcp/broadcaster.rb, line 65
def handle_connection(socket)
  @sockets << socket
end
run() click to toggle source

Continuously accept and handle incoming connections

# File lib/listen/tcp/broadcaster.rb, line 58
def run
  while socket = @server.accept
    handle_connection(socket)
  end
end
start() click to toggle source

Asynchronously start accepting connections

# File lib/listen/tcp/broadcaster.rb, line 25
def start
  async.run
end
unicast(socket, payload) click to toggle source

Unicasts payload to given socket

@return [Boolean] whether writing to socket was succesful

# File lib/listen/tcp/broadcaster.rb, line 49
def unicast(socket, payload)
  socket.write(payload)
  true
rescue IOError, Errno::ECONNRESET, Errno::EPIPE
  @sockets.delete(socket)
  false
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.