class Gossiperl::Client::Transport::Udp

Public Class Methods

new(worker) click to toggle source
# 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

Public Instance Methods

handle(&block) click to toggle source
# 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) click to toggle source
# 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