Class: Pantry::Server
- Inherits:
-
Object
- Object
- Pantry::Server
- Includes:
- Celluloid
- Defined in:
- lib/pantry/server.rb
Overview
The Pantry Server.
Instance Attribute Summary collapse
-
#client_registry ⇒ Object
readonly
Returns the value of attribute client_registry.
-
#identity ⇒ Object
Returns the value of attribute identity.
Instance Method Summary collapse
-
#client_who_sent(message) ⇒ Object
Return ClientInfo on which Client sent the given Message.
-
#create_client ⇒ Object
Generate new authentication credentials for a client.
-
#initialize(network_stack_class = Communication::Server) ⇒ Server
constructor
A new instance of Server.
-
#publish_message(message, filter = Communication::ClientFilter.new) ⇒ Object
Broadcast a
messageto all clients who match the givenfilter. -
#receive_file(file_size, file_checksum) ⇒ Object
Set up a FileService::ReceiveFile worker to begin receiving a file with the given size and checksum.
-
#receive_message(message) ⇒ Object
Callback from the network when a message is received unsolicited from a client.
-
#register_client(client) ⇒ Object
Mark an authenticated client as checked-in.
-
#run ⇒ Object
Start up the networking stack and start the server.
-
#send_file(file_path, receiver_uuid, file_uuid) ⇒ Object
Start a FileService::SendFile worker to upload the contents of the file at
file_pathto the equivalent ReceiveFile atreceiver_uuid. -
#send_request(client, message) ⇒ Object
Send a Pantry::Message but mark it as requiring a response.
-
#shutdown ⇒ Object
Close down networking and clean up resources.
Constructor Details
#initialize(network_stack_class = Communication::Server) ⇒ Server
Returns a new instance of Server.
12 13 14 15 16 17 18 19 20 |
# 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 |
Instance Attribute Details
#client_registry ⇒ Object (readonly)
Returns the value of attribute client_registry.
10 11 12 |
# File 'lib/pantry/server.rb', line 10 def client_registry @client_registry end |
#identity ⇒ Object
Returns the value of attribute identity.
8 9 10 |
# File 'lib/pantry/server.rb', line 8 def identity @identity end |
Instance Method Details
#client_who_sent(message) ⇒ Object
Return ClientInfo on which Client sent the given Message
48 49 50 |
# File 'lib/pantry/server.rb', line 48 def client_who_sent() @client_registry.find(.from) end |
#create_client ⇒ Object
Generate new authentication credentials for a client. Returns a Hash containing the credentials required for the client to connect and authenticate with this Server
43 44 45 |
# File 'lib/pantry/server.rb', line 43 def create_client @networking.create_client end |
#publish_message(message, filter = Communication::ClientFilter.new) ⇒ Object
Broadcast a message to all clients who match the given filter. By default all clients will be notified.
54 55 56 57 58 |
# File 'lib/pantry/server.rb', line 54 def (, filter = Communication::ClientFilter.new) Pantry.logger.debug("[#{@identity}] Publishing #{.inspect} to #{filter.stream.inspect}") .to = filter.stream @networking.() end |
#receive_file(file_size, file_checksum) ⇒ Object
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.
105 106 107 |
# 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) ⇒ Object
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.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/pantry/server.rb', line 63 def () Pantry.logger.debug("[#{@identity}] Received message #{.inspect}") if @commands.can_handle?() results = @commands.process() if .requires_response? Pantry.logger.debug("[#{@identity}] Returning results #{results.inspect}") send_results_back_to_requester(, results) end else matched_clients = @client_registry.all_matching(.to).map(&:identity) Pantry.logger.debug("[#{@identity}] Forwarding message on. Expect response from #{matched_clients.inspect}") send_results_back_to_requester(, matched_clients, true) () end end |
#register_client(client) ⇒ Object
Mark an authenticated client as checked-in
35 36 37 38 |
# 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 ⇒ Object
Start up the networking stack and start the server
23 24 25 26 27 |
# 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) ⇒ Object
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.
97 98 99 |
# 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) ⇒ Object
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.
84 85 86 87 88 89 90 91 |
# File 'lib/pantry/server.rb', line 84 def send_request(client, ) .requires_response! .to = client.identity Pantry.logger.debug("[#{@identity}] Sending request #{.inspect}") @networking.send_request() end |