Class: DCase::Local

Inherits:
Object
  • Object
show all
Includes:
Celluloid::IO
Defined in:
lib/dcase/local.rb

Constant Summary collapse

MAX_PACKET_SIZE =
512

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addr, port, crypto, config) ⇒ Local

Returns a new instance of Local.



9
10
11
12
13
14
15
16
17
# File 'lib/dcase/local.rb', line 9

def initialize(addr, port, crypto, config)
  @socket = UDPSocket.new
  @socket.to_io.setsockopt(:SOCKET, :REUSEADDR, 1)
  @socket.bind(addr, port)
  @crypto = crypto
  @config = config

  async.run
end

Instance Attribute Details

#cryptoObject

Returns the value of attribute crypto.



7
8
9
# File 'lib/dcase/local.rb', line 7

def crypto
  @crypto
end

#socketObject

Returns the value of attribute socket.



7
8
9
# File 'lib/dcase/local.rb', line 7

def socket
  @socket
end

Instance Method Details

#handle_data(data, port, addr) ⇒ Object



26
27
28
29
30
31
# File 'lib/dcase/local.rb', line 26

def handle_data(data, port, addr)
  connector = UDPSocket.new
  connector.send crypto.encrypt(data), 0, @config.server, @config.port

  async.request(connector, port, addr)
end

#request(connector, port, addr) ⇒ Object



33
34
35
36
37
# File 'lib/dcase/local.rb', line 33

def request(connector, port, addr)
  data, (_, _port, _addr) = connector.recvfrom(16384)
  @socket.send crypto.decrypt(data), 0, addr, port
  connector.close unless connector.closed?
end

#runObject



19
20
21
22
23
24
# File 'lib/dcase/local.rb', line 19

def run
  loop do
    data, (_, port, addr) = @socket.recvfrom(MAX_PACKET_SIZE)
    handle_data(data, port, addr)
  end
end