]> git.aero2k.de Git - sirius.git/commitdiff
Use dict-as-argument trick to store global data for next command id.
authorTom Hunger <tehunger@gmail.com>
Mon, 26 Jan 2015 17:39:10 +0000 (17:39 +0000)
committerTom Hunger <tehunger@gmail.com>
Mon, 26 Jan 2015 17:39:10 +0000 (17:39 +0000)
sirius/protocol/protocol_loop.py
sirius/web/printer_print.py

index 8c5fd6245a44e9286da2f7fb1906c1a3171dab17..8037a0717a389bb9a895b20e85aa5539f067cba5 100644 (file)
@@ -34,8 +34,6 @@ last_seen_by_address = dict()
 PrinterDevice = collections.namedtuple(
     'PrinterDevice', 'address last_heartbeat_timestamp')
 
-next_command_id = None
-
 
 class BridgeState(object):
     "Ephemeral state of a bridge. Lives as long as the websocket."
@@ -48,19 +46,21 @@ class BridgeState(object):
         self.connected_devices = set()
 
 
-def _get_next_command_id():
-    """
+def _get_next_command_id(local_data={}):
+    """Return the next usable command id. Initialized lazily from the
+    database.
+
     :returns: An int that can be used as the next command id.
     """
     # Lazy-initialize global next_command_id
-    global next_command_id
-    if next_command_id is None:
+    if 'next_command_id' not in local_data:
         # NB 0 is an invalid command id (AKA file-id) so we start at
         # whatever the latest message id is to avoid collisions.
         next_command_id = model_messages.Message.get_next_command_id()
         logger.info("Initialized next_command_id as %s", next_command_id)
-    next_command_id += 1
-    return next_command_id
+        local_data['next_command_id'] = next_command_id
+    local_data['next_command_id'] += 1
+    return local_data['next_command_id']
 
 
 def send_message(device_address, message):
index 56e4d21ffa4463691bdbf0c72a714badc394a008..942c2ad0288ec17c8e31067316e0c48f5b62ac45 100644 (file)
@@ -78,13 +78,15 @@ def printer_print(user_id, username, printer_id):
                         'error')
             stats.inc('printer.print.offline')
 
-        # Store the same message in the model.
+        # Store the same message in the database.
         model_message = model_messages.Message(
             print_id=next_print_id,
             pixels=bytearray(pixels),
             sender_id=login.current_user.id,
             target_printer=printer,
         )
+
+        # We know immediately if the printer wasn't online.
         if not success:
             model_message.failure_message = 'Printer offline'
             model_message.response_timestamp = datetime.datetime.utcnow()