Class: Gossiperl::Client::Transport::Udp

Inherits:
Resolution
  • Object
show all
Defined in:
lib/gossiperl_client/headers.rb,
lib/gossiperl_client/transport/udp.rb

Constant Summary

Constants inherited from Resolution

Resolution::UNDEFINED_VALUE

Instance Method Summary collapse

Methods inherited from Resolution

field

Constructor Details

#initialize(worker) ⇒ Udp

Returns a new instance of Udp.



15
16
17
18
19
# File 'lib/gossiperl_client/transport/udp.rb', line 15

def initialize worker
  self.worker = worker
  self.serializer = Gossiperl::Client::Serialization::Serializer.new
  self.encryption = Gossiperl::Client::Encryption::Aes256.new( self.worker.options[:symkey].to_s )
end

Instance Method Details

#handle(&block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gossiperl_client/transport/udp.rb', line 21

def handle &block
  worker = Thread.new ({ :proto => self, :block => block }) do |args|
    begin
      args[:proto].socket = UDPSocket.new
      args[:proto].socket.bind '127.0.0.1', args[:proto].worker.options[:client_port]
      while args[:proto].worker.working
        begin
          data, address = args[:proto].socket.recvfrom args[:proto].recv_buf_size
          decrypted = args[:proto].encryption.decrypt(data)
          deserialized = args[:proto].serializer.deserialize(decrypted)
          args[:block].call deserialized
        rescue Exception => ex
          args[:block].call({ :error => ex })
        end
      end
      self.socket.close unless self.socket.nil?
      args[:proto].worker.logger.debug("Stopping UDP services for client #{args[:proto].worker.options[:client_name]}.")
    rescue Exception => e
      args[:proto].worker.logger.error("Could not bind UDP service for client #{args[:proto].worker.options[:client_name]}.")
    end
  end
end

#send(digest) ⇒ Object



44
45
46
47
48
# File 'lib/gossiperl_client/transport/udp.rb', line 44

def send digest
  serialized = self.serializer.serialize digest
  encrypted  = self.encryption.encrypt serialized
  self.socket.send encrypted, 0, '127.0.0.1', self.worker.options[:overlay_port]
end