Class: Netz::Broadcaster

Inherits:
Object
  • Object
show all
Defined in:
lib/netz/broadcaster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBroadcaster

Returns a new instance of Broadcaster.



5
6
7
# File 'lib/netz/broadcaster.rb', line 5

def initialize
  @remote_clients = []
end

Instance Attribute Details

#command_channelObject

Returns the value of attribute command_channel.



3
4
5
# File 'lib/netz/broadcaster.rb', line 3

def command_channel
  @command_channel
end

#remote_clientsObject

Returns the value of attribute remote_clients.



3
4
5
# File 'lib/netz/broadcaster.rb', line 3

def remote_clients
  @remote_clients
end

Instance Method Details

#push_to_peers(command) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/netz/broadcaster.rb', line 18

def push_to_peers(command)
  # TODO add command buffering?
  command.extend(CommandSerializer)

  # write to clients
  msg = command.serialize
  puts msg
  @remote_clients.each do |rc|
    rc.write [msg.length].pack("S")
    rc.write msg
  end
end

#runObject



9
10
11
12
13
14
15
16
# File 'lib/netz/broadcaster.rb', line 9

def run
  Thread.new do
    loop do
      command = @command_channel.pop
      push_to_peers command if command.local?
    end
  end
end