class Pantry::Server

The Pantry Server.

Attributes

client_registry[R]
identity[RW]

Public Class Methods

new(network_stack_class = Communication::Server) click to toggle source
# File lib/pantry/server.rb, line 12
def initialize(network_stack_class = Communication::Server)
  @commands = CommandHandler.new(self, Pantry.server_commands)
  @identity = current_hostname
  @clients  = []

  @client_registry = ClientRegistry.new

  @networking = network_stack_class.new_link(self)
end

Public Instance Methods

client_who_sent(message) click to toggle source

Return ClientInfo on which Client sent the given Message

# File lib/pantry/server.rb, line 48
def client_who_sent(message)
  @client_registry.find(message.from)
end
create_client() click to toggle source

Generate new authentication credentials for a client. Returns a Hash containing the credentials required for the client to connect and authenticate with this Server

# File lib/pantry/server.rb, line 43
def create_client
  @networking.create_client
end
publish_message(message, filter = Communication::ClientFilter.new) click to toggle source

Broadcast a message to all clients who match the given filter. By default all clients will be notified.

# File lib/pantry/server.rb, line 54
def publish_message(message, filter = Communication::ClientFilter.new)
  Pantry.logger.debug("[#{@identity}] Publishing #{message.inspect} to #{filter.stream.inspect}")
  message.to = filter.stream
  @networking.publish_message(message)
end
receive_file(file_size, file_checksum) click to toggle source

Set up a FileService::ReceiveFile worker to begin receiving a file with the given size and checksum. This returns an informational object with the new receiver’s identity and the file UUID so a SendFile worker knows who to send the file contents to.

# File lib/pantry/server.rb, line 105
def receive_file(file_size, file_checksum)
  @networking.receive_file(file_size, file_checksum)
end
receive_message(message) click to toggle source

Callback from the network when a message is received unsolicited from a client. If the message received is unhandleable by this Server, the message is forwarded on down to the clients who match the message’s to.

# File lib/pantry/server.rb, line 63
def receive_message(message)
  Pantry.logger.debug("[#{@identity}] Received message #{message.inspect}")
  if @commands.can_handle?(message)
    results = @commands.process(message)

    if message.requires_response?
      Pantry.logger.debug("[#{@identity}] Returning results #{results.inspect}")
      send_results_back_to_requester(message, results)
    end
  else
    matched_clients = @client_registry.all_matching(message.to).map(&:identity)

    Pantry.logger.debug("[#{@identity}] Forwarding message on. Expect response from #{matched_clients.inspect}")
    send_results_back_to_requester(message, matched_clients, true)
    forward_message(message)
  end
end
register_client(client) click to toggle source

Mark an authenticated client as checked-in

# File lib/pantry/server.rb, line 35
def register_client(client)
  Pantry.logger.info("[#{@identity}] Received client registration :: #{client.identity}")
  @client_registry.check_in(client)
end
run() click to toggle source

Start up the networking stack and start the server

# File lib/pantry/server.rb, line 23
def run
  Pantry.set_proc_title("pantry server #{Pantry::VERSION}")
  @networking.run
  Pantry.logger.info("[#{@identity}] Server running")
end
send_file(file_path, receiver_uuid, file_uuid) click to toggle source

Start a FileService::SendFile worker to upload the contents of the file at file_path to the equivalent ReceiveFile at receiver_uuid. Using this method requires asking the receiving end (Server or Client) to receive a file first, which will return the receiver_uuid and file_uuid to use here.

# File lib/pantry/server.rb, line 97
def send_file(file_path, receiver_uuid, file_uuid)
  @networking.send_file(file_path, receiver_uuid, file_uuid)
end
send_request(client, message) click to toggle source

Send a Pantry::Message but mark it as requiring a response. This will set up and return a Celluloid::Future that will contain the response once it is available.

# File lib/pantry/server.rb, line 84
def send_request(client, message)
  message.requires_response!
  message.to = client.identity

  Pantry.logger.debug("[#{@identity}] Sending request #{message.inspect}")

  @networking.send_request(message)
end
shutdown() click to toggle source

Close down networking and clean up resources

# File lib/pantry/server.rb, line 30
def shutdown
  Pantry.logger.info("[#{@identity}] Server Shutting Down")
end

Protected Instance Methods

current_hostname() click to toggle source
# File lib/pantry/server.rb, line 111
def current_hostname
  Socket.gethostname
end
forward_message(message) click to toggle source
# File lib/pantry/server.rb, line 127
def forward_message(message)
  @networking.forward_message(message)
end
send_results_back_to_requester(message, results, client_response_list = false) click to toggle source
# File lib/pantry/server.rb, line 115
def send_results_back_to_requester(message, results, client_response_list = false)
  response_message = message.build_response
  response_message.from = Pantry::SERVER_IDENTITY
  response_message[:client_response_list] = client_response_list

  [results].flatten(1).each do |entry|
    response_message << entry
  end

  @networking.publish_message(response_message)
end