1 # Copyright 2017 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 import collections 6 7 8 # Message types. 9 ACK = 'ack' 10 ECHO = 'echo' 11 GET = 'get' 12 SHUTDOWN = 'shutdown' 13 STATUS = 'status' 14 15 # Message type for container pool communication. 16 Message = collections.namedtuple('Message', ['type', 'args']) 17 18 19 def ack(): 20 """Creates a message of type ACK. 21 22 ACK messages are returned by the server to acknowledge receipt and confirm 23 that requested operations have taken place. 24 """ 25 return Message(ACK, {}) 26 27 28 def echo(msg): 29 """Creates an echo message. 30 31 ECHO messages are mainly for testing. They verify that the service is up 32 and running. 33 34 @param msg: An optional string that can be attached to the message. 35 """ 36 return Message(ECHO, {'msg': msg}) 37 38 39 def shutdown(): 40 """Creates a service shutdown message. 41 42 SHUTDOWN messages cause the service to shut down. See Service.stop(). 43 """ 44 return Message(SHUTDOWN, {}) 45 46 47 def status(): 48 """Creates a status request message. 49 50 STATUS messages cause the service to return a dictionary describing the 51 current state of the container pool. 52 """ 53 return Message(STATUS, {}) 54 55 56 def get(id, timeout=0): 57 """Creates a get container message. 58 59 GET messages retrieve a running container from the container pool. 60 61 @param id: A ContainerId to be assigned to the container. 62 @param timeout: An optional timeout to wait for the container. 63 """ 64 return Message(GET, {'id': id, 'timeout': timeout}) 65