Class: Pantry::Communication::Client

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/pantry/communication/client.rb

Overview

The communication layer of a Pantry::Client This class manages all of the ZeroMQ sockets and underlying communication systems, handling the sending and receiving of messages.

Instance Method Summary collapse

Constructor Details

#initialize(listener) ⇒ Client

Returns a new instance of Client.



10
11
12
13
# File 'lib/pantry/communication/client.rb', line 10

def initialize(listener)
  @listener = listener
  @response_wait_list = Communication::WaitList.new
end

Instance Method Details

#handle_message(message) ⇒ Object

Callback from the SubscribeSocket when a message is received.



45
46
47
48
49
50
51
# File 'lib/pantry/communication/client.rb', line 45

def handle_message(message)
  if @response_wait_list.waiting_for?(message)
    @response_wait_list.received(message)
  else
    @listener.receive_message(message)
  end
end

#receive_file(file_size, file_checksum) ⇒ Object



64
65
66
# File 'lib/pantry/communication/client.rb', line 64

def receive_file(file_size, file_checksum)
  @file_service.receive_file(file_size, file_checksum)
end

#runObject

Start up the networking layer, opening up sockets and getting ready for communication.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pantry/communication/client.rb', line 17

def run
  @security = Communication::Security.new_client

  @subscribe_socket = Communication::SubscribeSocket.new_link(
    Pantry.config.server_host,
    Pantry.config.pub_sub_port,
    @security
  )
  @subscribe_socket.add_listener(self)
  @subscribe_socket.filter_on(@listener.filter)
  @subscribe_socket.open

  @send_socket = Communication::SendSocket.new_link(
    Pantry.config.server_host,
    Pantry.config.receive_port,
    @security
  )
  @send_socket.open

  @file_service = Communication::FileService.new_link(
    Pantry.config.server_host,
    Pantry.config.file_service_port,
    @security
  )
  @file_service.start_client
end

#send_file(file_path, receiver_uuid, file_uuid) ⇒ Object



68
69
70
# File 'lib/pantry/communication/client.rb', line 68

def send_file(file_path, receiver_uuid, file_uuid)
  @file_service.send_file(file_path, receiver_uuid, file_uuid)
end

#send_message(message) ⇒ Object



59
60
61
62
# File 'lib/pantry/communication/client.rb', line 59

def send_message(message)
  message.from = @listener
  @send_socket.send_message(message)
end

#send_request(message) ⇒ Object



53
54
55
56
57
# File 'lib/pantry/communication/client.rb', line 53

def send_request(message)
  @response_wait_list.wait_for(message).tap do
    send_message(message)
  end
end