Class: Pantry::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Celluloid
Defined in:
lib/pantry/client.rb

Overview

The Pantry Client. The Client runs on any server that needs provisioning, and communicates to the Server through various channels. Clients can be further configured to manage an application, for a given environment, and across any number of roles.

Direct Known Subclasses

CLI

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application: nil, environment: nil, roles: [], identity: nil, network_stack_class: Communication::Client) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pantry/client.rb', line 19

def initialize(application: nil, environment: nil, roles: [], identity: nil, network_stack_class: Communication::Client)
  @info      = Pantry::ClientInfo.new(
    application: application,
    environment: environment,
    roles:       roles       || [],
    identity:    identity    || current_hostname
  )

  @commands   = CommandHandler.new(self, Pantry.client_commands)
  @networking = network_stack_class.new_link(self)
end

Instance Attribute Details

#last_received_messageObject (readonly)

For testing / debugging purposes, keep hold of the last message this client received



17
18
19
# File 'lib/pantry/client.rb', line 17

def last_received_message
  @last_received_message
end

Instance Method Details

#receive_file(file_size, file_checksum) ⇒ Object

See Pantry::Server#receive_file



82
83
84
# File 'lib/pantry/client.rb', line 82

def receive_file(file_size, file_checksum)
  @networking.receive_file(file_size, file_checksum)
end

#receive_message(message) ⇒ Object

Callback from Networking when a message is received



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pantry/client.rb', line 48

def receive_message(message)
  Pantry.logger.debug("[#{identity}] Received message #{message.inspect}")

  if message_meant_for_us?(message)
    @last_received_message = message
    results = @commands.process(message)

    if message.requires_response?
      Pantry.logger.debug("[#{identity}] Responding with #{results.inspect}")
      send_results_back_to_requester(message, results)
    end
  else
    Pantry.logger.debug("[#{identity}] Message discarded, not for us")
  end
end

#runObject

Start up the Client. This sets up the appropriate communication channels to the server, sends a registration message so the Server knows who just connected, and then waits for information to come.



35
36
37
38
39
40
# File 'lib/pantry/client.rb', line 35

def run
  Pantry.set_proc_title("pantry client #{Pantry::VERSION} :: #{identity}")
  @networking.run
  send_registration_message
  Pantry.logger.info("[#{identity}] Client registered and waiting for commands")
end

#send_file(file_path, receiver_uuid, file_uuid) ⇒ Object

See Pantry::Server#send_file



87
88
89
# File 'lib/pantry/client.rb', line 87

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

#send_message(message) ⇒ Object

Send a Pantry::Message directly to its intended recipient. For a Client this is almost always the Server.



66
67
68
# File 'lib/pantry/client.rb', line 66

def send_message(message)
  @networking.send_message(message)
end

#send_request(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.



73
74
75
76
77
78
79
# File 'lib/pantry/client.rb', line 73

def send_request(message)
  message.requires_response!

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

  @networking.send_request(message)
end

#shutdownObject



42
43
44
45
# File 'lib/pantry/client.rb', line 42

def shutdown
  Pantry.logger.info("[#{identity}] Client Shutting down")
  @registration_timer.cancel if @registration_timer
end