Exemplo n.º 1
0
def HammingCodeDriver():
    mode = input("Enter mode: ")
    total_bits, num_data_bits = (15, 11) if mode == "H1511" else (7, 4)

    message = np.array(helper.generate_random_message(num_data_bits))
    print("Message          :", message)

    x = helper.encodeMessage(G11, message) if total_bits == 15 else helper.encodeMessage(G4, message)
    print("Send Vector      :", x)

    # Create error
    helper.generate_error(x, total_bits)

    z = helper.generateParityCheckMatrix(H11, x) if total_bits == 15 else helper.generateParityCheckMatrix(H4, x)
    print("Received Message :", x)

    print("Parity Check     :", z)

    # Convert to Big endian
    z = z[::-1]

    if sum(z) == 0:
        print("Decoded Message  :", helper.decodeMessage(R11, x)) if total_bits == 15 else print("Decoded Message  :", helper.decodeMessage(R4, x))
    else:
        x = helper.errorCorrection(x, z)
        print("Corrected Message:", x)
        print("Decoded Message  :", helper.decodeMessage(R11, x)) if total_bits == 15 else print("Decoded Message  :", helper.decodeMessage(R4, x))
Exemplo n.º 2
0
    def get(self, account_id, region, server_id):
        token = tasks.check_auth_token(account_id,
                                       helper.check_for_token(request))
        if not token:
            return helper.generate_error(
                'No authentication token provided, '
                'or authentication was unsuccessful', 401)

        server_data = g.db.accounts.find_one(
            {
                'account_number': account_id,
                'region': region,
                'servers.id': server_id
            }, {'servers.$': 1})
        if not server_data:
            return helper.generate_error('Server was not found', 404)

        check_host = server_data.get('servers')[0].get('host_id')
        host_servers = helper.generate_servers_on_same_host(
            account_id, region, check_host)
        duplicate = False
        if host_servers is not None and len(host_servers) > 1:
            duplicate = True

        return jsonify(duplicate=duplicate, host_servers=host_servers)
Exemplo n.º 3
0
    def put(self, account_id, region, server_id):
        """
            Update the account data with the new server, and return True
            or False as to whether the server is by itself on the hypervisor
            Save the data into cache as either way it will still reside on a
            host that has another server or by itself. The answer will be
            correct either way
        """
        token = tasks.check_auth_token(account_id,
                                       helper.check_for_token(request))
        if not token:
            return helper.generate_error(
                'No authentication token provided, '
                'or authentication was unsuccessful', 401)

        account_data = g.db.accounts.find_one({
            'account_number': account_id,
            'region': region,
            'cache_expiration': {
                '$gte': helper.get_timestamp()
            }
        })
        if not account_data:
            return helper.generate_error(
                'You must initialize before checking a server', 400)

        check_server = g.db.accounts.find_one({'servers.id': server_id})
        if check_server:
            return helper.generate_error('Server has been catalogued already',
                                         400)

        response = tasks.check_add_server_to_cache(token, region, account_id,
                                                   server_id, account_data)
        return jsonify(duplicate=response)
Exemplo n.º 4
0
def create_announcement(event, context):
    try:
        item = helper.generate_announcement(event['body'])
        helper.dynamodb_put_item(item)
        response = helper.generate_response(status_code=201, body=item)
    except ValueError as e:
        error_message = "Received malformed announcement."
        logging.error(e)
        response = helper.generate_error(status_code=400,
                                         message=error_message)
    except Exception as e:
        logging.error(e)
        response = helper.generate_error(status_code=500,
                                         message=helper.UNKNOWN_ERROR_MESSAGE)
    return response
Exemplo n.º 5
0
def list_announcements(event, context):
    try:
        items = helper.dynamodb_list_items()
        response = helper.generate_response(status_code=200, body=items)
    except Exception as e:
        logging.error(e)
        response = helper.generate_error(status_code=500,
                                         message=helper.UNKNOWN_ERROR_MESSAGE)
    return response
Exemplo n.º 6
0
 def delete(self, account_id, region):
     try:
         g.db.accounts.remove({
             'account_number': account_id,
             'region': region
         })
         return 'Request was successful', 204
     except:
         return helper.generate_error(
             'An error occured that prevented the delete to complete', 500)
Exemplo n.º 7
0
    def post(self, account_id, region):
        token = tasks.check_auth_token(account_id,
                                       helper.check_for_token(request))
        if not token:
            return helper.generate_error(
                'No authentication token provided, '
                'or authentication was unsuccessful', 401)

        task_id = tasks.generate_account_object_list.delay(
            account_id, token, region, 'host_server')
        return jsonify(task_id=str(task_id))
Exemplo n.º 8
0
    def get(self, account_id, region):
        token = tasks.check_auth_token(account_id,
                                       helper.check_for_token(request))
        if not token:
            return helper.generate_error(
                'No authentication token provided, '
                'or authentication was unsuccessful', 401)

        account_data = g.db.accounts.find_one(
            {
                'account_number': account_id,
                'region': region,
                'cache_expiration': {
                    '$gte': helper.get_timestamp()
                }
            }, {
                '_id': 0,
                'servers': 1
            })
        return jsonify(data=account_data)