def on_message(client, userdata, message):
    with lock:
        logger.debug(((('rcvd: ' + message.topic) + '/') + str(message.payload.decode('utf-8'))))
        if (message.topic == 'addToPool'):
            logger.info(f'storing payload')
            db_opaque.insert({'entry': str(message.payload.decode('utf-8'))})
        if (message.topic == 'requestPool'):
            asking = str(message.payload.decode('utf-8'))
            logger.info(f'received pool request from {asking}')
            completePool = db_opaque.all()
            db_opaque.truncate()
            if len(completePool):
                to_send = []
                if (len(completePool) > 10):
                    for i in range(10):
                        to_send.append(completePool.pop(0))
                else:
                    to_send = completePool.copy()
                    completePool.clear()
                for left in completePool:
                    db_opaque.insert(left)
                table_json = json.dumps(to_send)
                logger.info(f'publishing table to getPool{asking}, len={len(table_json)}, n={len(to_send)}')
                client.publish(f'getPool{asking}', table_json, qos=1)
        if (message.topic == 'measures'):
            j = json.loads(message.payload.decode('utf-8'))
            logger.info(f'm: {message.payload}')
            db_measures.insert({'entry': message.payload.decode('utf-8')})
            logger.info(f"received measure {j['MUID']}")
def on_message(client, userdata, message):
    with lock:
        logger.debug(((('rcvd: ' + message.topic) + '/') + str(message.payload.decode('utf-8'))))
        if (message.topic == 'addToPool'):
            logger.info(f'storing payload')
            db_opaque.insert({'entry': str(message.payload.decode('utf-8'))})
        if (message.topic == 'requestPool'):
            asking = str(message.payload.decode('utf-8'))
            logger.info(f'received pool request from {asking}')
            completePool = db_opaque.all()
            db_opaque.truncate()
            if len(completePool):
                to_send = []
                if (len(completePool) > 10):
                    for i in range(10):
                        to_send.append(completePool.pop(0))
                else:
                    to_send = completePool.copy()
                    completePool.clear()
                for left in completePool:
                    db_opaque.insert(left)
                table_json = json.dumps(to_send)
                logger.info(f'publishing table to getPool{asking}, len={len(table_json)}, n={len(to_send)}')
                client.publish(f'getPool{asking}', table_json, qos=1)
        if (message.topic == 'measures'):
            j = json.loads(message.payload.decode('utf-8'))
            logger.info(f'm: {message.payload}')
            db_measures.insert({'entry': message.payload.decode('utf-8')})
            logger.info(f"received measure {j['MUID']}")