Class: Maze::Servlets::IdempotentCommandServlet

Inherits:
BaseServlet
  • Object
show all
Defined in:
lib/maze/servlets/idempotent_command_servlet.rb

Overview

Allows clients to queue up “commands”, in the form of Ruby hashes, using Maze::Server.commands.add. GET requests made to the /command endpoint will then respond with each queued command in turn.

Constant Summary collapse

NOOP_COMMAND =
'{"action": "noop", "message": "No commands queued"}'
RESET_COMMAND =
'{"action": "reset_uuid", "message": "The UUID given was unknown - client must reset its last known UUID"}'

Instance Method Summary collapse

Instance Method Details

#command_after(uuid, response) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/maze/servlets/idempotent_command_servlet.rb', line 45

def command_after(uuid, response)
  commands = Maze::Server.commands
  if uuid.empty?
    # Return the first command in the list, if there is one
    index = -1
  else
    # Find the matching command
    index = commands.all.find_index {|command| command[:uuid] == uuid }
  end

  if index.nil?
    # No matching command - client must reset its UUID
    response.body = RESET_COMMAND
    response.status = 200
  else
    if index + 1 < commands.size_all
      # Respond with the next command in the queue
      command = commands.get(index + 1)
      command_json = JSON.pretty_generate(command)
      response.body = command_json
      response.status = 200
    else
      # The UUID given was for the last command in the list
      response.body = NOOP_COMMAND
      response.status = 200
    end
  end
end

#do_GET(request, response) ⇒ Object

Serves the next command, if there is one.

Parameters:

  • request (HTTPRequest)

    The incoming GET request

  • response (HTTPResponse)

    The response to return



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

def do_GET(request, response)
  after_uuid = request.query['after']
  if after_uuid.nil?
    response.body = "The 'after' query parameter must be provided, but may be an empty string"
    response.status = 400
  else
    command_after(after_uuid, response)
  end

  response.header['Access-Control-Allow-Origin'] = '*'
end

#do_OPTIONS(request, response) ⇒ Object

Logs and returns a set of valid headers for this servlet.

Parameters:

  • request (HTTPRequest)

    The incoming GET request

  • response (HTTPResponse)

    The response to return



78
79
80
81
82
83
# File 'lib/maze/servlets/idempotent_command_servlet.rb', line 78

def do_OPTIONS(request, response)
  super

  response.header['Access-Control-Allow-Methods'] = 'POST, OPTIONS'
  response.status = Server.status_code('OPTIONS')
end

#send_current_command(response) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/maze/servlets/idempotent_command_servlet.rb', line 31

def send_current_command(response)
  commands = Maze::Server.commands

  if commands.size_remaining == 0
    response.body = NOOP_COMMAND
    response.status = 200
  else
    command = commands.current
    response.body = JSON.pretty_generate(command)
    response.status = 200
    commands.next
  end
end