Class: Pantry::Server

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

Overview

The Pantry Server.

Instance Attribute Summary collapse

Instance Method Summary collapse

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_registryObject (readonly)

Returns the value of attribute client_registry.



10
11
12
# File 'lib/pantry/server.rb', line 10

def client_registry
  @client_registry
end

#identityObject

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(message)
  @client_registry.find(message.from)
end

#create_clientObject

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 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) ⇒ 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 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) ⇒ 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

#runObject

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, message)
  message.requires_response!
  message.to = client.identity

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

  @networking.send_request(message)
end

#shutdownObject

Close down networking and clean up resources



30
31
32
# File 'lib/pantry/server.rb', line 30

def shutdown
  Pantry.logger.info("[#{@identity}] Server Shutting Down")
end