class Pantry::Communication::Server

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.

Public Class Methods

new(listener) click to toggle source

listener must respond to the receive_message method

# File lib/pantry/communication/server.rb, line 12
def initialize(listener)
  @listener           = listener
  @response_wait_list = Communication::WaitList.new
end

Public Instance Methods

create_client() click to toggle source

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

# File lib/pantry/communication/server.rb, line 48
def create_client
  @security.create_client
end
forward_message(message) click to toggle source

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.

# File lib/pantry/communication/server.rb, line 63
def forward_message(message)
  message.forwarded!
  publish_message(message)
end
handle_message(message) click to toggle source

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

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

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

# 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) click to toggle source
# File lib/pantry/communication/server.rb, line 86
def receive_file(file_size, file_checksum)
  @file_service.receive_file(file_size, file_checksum)
end
run() click to toggle source

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

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

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

# 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