Class: Pantry::Communication::Server

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

Overview

The communication layer of a Pantry::Server 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) ⇒ Server

listener must respond to the #receive_message method



12
13
14
15
# File 'lib/pantry/communication/server.rb', line 12

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

Instance Method Details

#create_clientObject

Ask Security to generate a new set of credentials as necessary for a new Client to connect to this Server



48
49
50
# File 'lib/pantry/communication/server.rb', line 48

def create_client
  @security.create_client
end

#forward_message(message) ⇒ Object

Send a message to all connected subscribers without modifying the package. Used when handling requests meant for other clients (say from the CLI). The source is untouched so the Client(s) handling know how to respond.



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

def forward_message(message)
  message.forwarded!
  publish_message(message)
end

#handle_message(message) ⇒ Object

Listener callback from ReceiveSocket. See if we need to match this response with a previous request or if it’s a new message entirely.



76
77
78
79
80
81
82
83
84
# File 'lib/pantry/communication/server.rb', line 76

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

#publish_message(message) ⇒ Object

Send a message to all clients who match the given filter.



69
70
71
72
# File 'lib/pantry/communication/server.rb', line 69

def publish_message(message)
  message.from ||= @listener
  @publish_socket.send_message(message)
end

#receive_file(file_size, file_checksum) ⇒ Object



86
87
88
# File 'lib/pantry/communication/server.rb', line 86

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 client communication.



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

def run
  @security = Communication::Security.new_server
  @security.link_to(self)

  @publish_socket = Communication::PublishSocket.new_link(
    Pantry.config.server_host,
    Pantry.config.pub_sub_port,
    @security
  )
  @publish_socket.open

  @receive_socket = Communication::ReceiveSocket.new_link(
    Pantry.config.server_host,
    Pantry.config.receive_port,
    @security
  )
  @receive_socket.add_listener(self)
  @receive_socket.open

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

#send_file(file_path, receiver_uuid, file_uuid) ⇒ Object



90
91
92
# File 'lib/pantry/communication/server.rb', line 90

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

#send_request(message) ⇒ Object

Send a request to all clients, expecting a result. Returns a Future which can be queried later for the client response.



54
55
56
57
58
# File 'lib/pantry/communication/server.rb', line 54

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