def delete(self):
        """
        {
            file_name:
            replicate: 
        }
        """
        data = request.json
        file_name = data['file_name']
        replicate = data['replicate']

        if does_file_exist(file_name, FILE_SERVER_PATH):
            try:
                delete_file(file_name, FILE_SERVER_PATH)
                print('File deleted from ', SERVER_PORT)
                #Alert dir server
                if replicate == True:
                    dir_port = get_port('dir_server')
                    req = format_file_req(dir_port)
                    data = {
                        'file_name': file_name,
                        'file_server_port': str(SERVER_PORT)
                    }
                    requests.delete(req,
                                    data=json.dumps(data),
                                    headers=cf.JSON_HEADER)
            except:
                print('Unable to delete file')
                abort(409)
 def delete(self):
     """
     Used to delete a file from the database
     {
         'file_name':
         'file_server_port':
     }
     """
     data = request.json
     file_name = data['file_name']
     file_server_port = data['file_server_port']
     db_f = file_map.find_one({'file_name': file_name})
     ports = db_f['ports']
     data = {'file_name': file_name, 'replicate': False}
     for p in ports:
         if str(p) != str(file_server_port):
             req = format_file_req(p)
             requests.delete(req,
                             data=json.dumps(data),
                             headers=cf.JSON_HEADER)
             db_s = active_nodes.find_one({'port': p})
             file_names = db_s['file_names']
             file_names.remove(file_name)
             file_map.update_one({'port': p},
                                 {'$set': {
                                     'file_names': file_names
                                 }})
     file_map.delete_one({'file_name': file_name})
Exemplo n.º 3
0
def get_file_port(file_name, dir_server_port):
    req = format_file_req(dir_server_port)
    data = {
        'file_name': file_name
    }
    resp = json.loads(requests.get(req, data=json.dumps(data), headers=cf.JSON_HEADER).content.decode())
    port = resp['file_server_port']
    return port
Exemplo n.º 4
0
def create_file(file_name):
    """
    Creates file
    """
    file_server = get_port('file_server')
    check_port(file_server)
    data = {
        "file_name": file_name,
        'file_content': " ",
        'replicate': True,
        'new_file': True
    }
    req = format_file_req(file_server)
    requests.post(req, data=json.dumps(data), headers=cf.JSON_HEADER)
Exemplo n.º 5
0
def delete_file(file_name):
    """
    Deletes file
    """
    dir_server_port = get_port('dir_server')
    check_port(dir_server_port)
    try:
        file_port = get_file_port(file_name, dir_server_port)
    except:
        print('File doesnt exist')
        return
    req = format_file_req(file_port)
    data = {'file_name': file_name, 'replicate': True}
    try:
        requests.delete(req, data=json.dumps(data), headers=cf.JSON_HEADER)
    except:
        print('unable to delete file')

dir_port = 5002
data1 = make_data('D1', D1_files)
data2 = make_data('D2', D2_files)
#Set up 4 file servers
req_1 = format_node_req(6001, dir_port)
req_2 = format_node_req(6002, dir_port)
req_3 = format_node_req(6003, dir_port)
req_4 = format_node_req(6004, dir_port)

print(
    requests.post(req_1, data=data1, headers=cf.JSON_HEADER).content.decode())
print(
    requests.post(req_2, data=data1, headers=cf.JSON_HEADER).content.decode())
print(
    requests.post(req_3, data=data2, headers=cf.JSON_HEADER).content.decode())
print(
    requests.post(req_4, data=data2, headers=cf.JSON_HEADER).content.decode())

#Request 2 files from two different directories
req_5 = format_file_req('1.1', dir_port)
req_6 = format_file_req('1.2', dir_port)
req_7 = format_file_req('2.1', dir_port)
req_8 = format_file_req('2.2', dir_port)

print(requests.get(req_5).content.decode())
print(requests.get(req_6).content.decode())
print(requests.get(req_7).content.decode())
print(requests.get(req_8).content.decode())
Exemplo n.º 7
0
import requests
import sys
sys.path.insert(0, '/home/lavelld/Documents/SS/Internet_Apps/DFS/src')
from format import format_file_req, format_registry_req
import config as cf
import json

file_name = 'D1\1.1'
file_content = 'Test generated content for file ' + file_name
dir_port_url = format_registry_req('dir_server', cf.REGISTRY_SERVER_PORT)
response = json.loads(requests.get(dir_port_url).content.decode())
dir_server_port = response['dir_port']

url = format_file_req(file_name, dir_server_port)
response = json.loads(requests.get(url).content.decode())
file_server_port = response['file_server_port']

req = format_file_req(file_name, file_server_port)
data = {
    'file_name': file_name,
    'file_content': file_content,
    'replicate': True
}
response = requests.post(req, data=json.dumps(data), headers=cf.JSON_HEADER)
print(response)
    def post(self):
        '''
        Endpoint for creating and updating file
                'file_name': ,
                'file_content': ,
                'file_server_port': ,
                'new_file': 
        '''
        data = request.json
        file_name = data['file_name']
        file_server_port = data['file_server_port']
        new_file = data['new_file']
        file_content = data['file_content']
        file_version = 0
        #Create file
        if new_file:
            file_version = 1
            #Handle db synchroniation
            db_node = active_nodes.find_one({'port': file_server_port})
            if not db_node:
                print('Server isnt registered: ', file_server_port)
                return

            #Add file to map if it hasnt been created, add it as a supported file in a node if it hasnt before
            db_file = file_map.find_one({'file_name': file_name})
            if db_file != None:
                print('file ', file_name, ' already exists, updating db')
                ports = db_file["ports"]
                if not file_server_port in ports:
                    ports.append(file_server_port)
                    file_map.update_one({'file_name': file_name}, {
                        '$inc': {
                            'num_nodes': 1
                        },
                        '$set': {
                            'ports': ports
                        }
                    })
            else:

                new_file = {
                    'file_name': file_name,
                    'ports': [file_server_port],
                    'num_nodes': 1,
                    'file_version': 1
                }
                print('inserting new file: ', file_name)
                file_map.insert_one(new_file)

            file_list = db_node['file_names']
            if not file_name in file_list:
                file_list.append(file_name)
                active_nodes.update_one({'port': file_server_port},
                                        {'$set': {
                                            'file_names': file_list
                                        }})
        #Update file
        else:
            d = file_map.find_one({'file_name': file_name})
            file_version = d['file_version']
            file_version += 1
            #increment file version
            file_map.update_one({'file_name': file_name},
                                {'$set': {
                                    'file_version': file_version
                                }})
            #Replicate update across other nodes
            ports = d['ports']
            data = {
                'file_name': file_name,
                'file_content': file_content,
                'replicate': False,
                'new_file': new_file
            }
            print('request to replicate received from ', file_server_port)
            for port in ports:
                if str(port) != str(file_server_port):
                    print('sending request to ', port)
                    req = format_file_req(port)
                    requests.post(req,
                                  data=json.dumps(data),
                                  headers=cf.JSON_HEADER)
                    print('request sent to, ', port)
            print('finished replication')

        return jsonify({'file_version': file_version})
Exemplo n.º 9
0
def write_file(file_name):
    """Allows user to write to file of a particular name"""
    file_name_data = {'file_name': file_name}
    #Get server ports from registry
    cache_port = get_port('cache_server')
    check_port(cache_port)

    dir_port = get_port('dir_server')
    check_port(dir_port)

    lock_port = get_port('lock_server')
    check_port(lock_port)

    #Get lock
    lock_url = format_lock_req('0', lock_port)
    response = json.loads(
        requests.get(lock_url,
                     data=json.dumps(file_name_data),
                     headers=cf.JSON_HEADER).content.decode())

    lock_acquired = response['lock_acquired']
    client_id = response['client_id']
    lock_url = format_lock_req(client_id, lock_port)
    while (not lock_acquired):
        time.sleep(1)
        response = json.loads(
            requests.get(lock_url,
                         data=json.dumps(file_name_data),
                         headers=cf.JSON_HEADER).content.decode())
        lock_acquired = response['lock_acquired']

    #Read File in
    read_file(file_name)

    input('Press enter to write file back to server')

    #Get file server port from directory server
    url = format_file_req(dir_port)
    response = json.loads(
        requests.get(url,
                     data=json.dumps(file_name_data),
                     headers=cf.JSON_HEADER).content.decode())
    file_server_port = response['file_server_port']

    #post to file server
    url = format_file_req(file_server_port)
    script_dir = os.path.dirname(__file__)
    abs_file_path = script_dir + '/temp'
    file_content = get_file_read(file_name, abs_file_path)

    data = {
        "file_name": file_name,
        "file_content": file_content,
        "replicate": True,
        "new_file": False
    }
    headers = cf.JSON_HEADER
    response = json.loads(
        requests.post(url, data=json.dumps(data),
                      headers=headers).content.decode())
    file_version = response['file_version']

    #release lock
    requests.post(lock_url,
                  data=json.dumps(file_name_data),
                  headers=cf.JSON_HEADER)

    data = {
        'file_name': file_name,
        'file_content': file_content,
        'file_version': file_version
    }
    #update cache
    url = format_file_req(cache_port)
    response = requests.post(url, json.dumps(data), headers=headers)
Exemplo n.º 10
0
def read_file(file_name):
    """
    Allows user to read file of a particular name
    Will involve sending a get request to the fileservers api
    """
    file_name_data = {'file_name': file_name}

    #Get server ports from registry
    cache_port = get_port('cache_server')
    check_port(cache_port)

    dir_port = get_port('dir_server')
    check_port(dir_port)

    #Check if cached
    url = format_file_req(cache_port)
    response = json.loads(
        requests.get(url,
                     data=json.dumps(file_name_data),
                     headers=cf.JSON_HEADER).content.decode())
    if response['cache_miss'] == False:
        file_content = get_file_read(file_name, cf.CACHE_FILE_PATH)
    else:
        #No lock needed for reading

        #Get file server port
        url = format_file_req(dir_port)
        try:
            response = json.loads(
                requests.get(url,
                             data=json.dumps(file_name_data),
                             headers=cf.JSON_HEADER).content.decode())
            file_server_port = response['file_server_port']

        except:
            print('file doesnt exist')
            return

        #Get file from file server
        url = format_file_req(file_server_port)
        response = json.loads(
            requests.get(url,
                         data=json.dumps(file_name_data),
                         headers=cf.JSON_HEADER).content.decode())
        file_content = response["file_content"]
        file_version = response['file_version']
        #post to the cache
        url = format_file_req(cache_port)
        data = {
            'file_name': file_name,
            'file_content': file_content,
            'file_version': file_version
        }
        response = requests.post(url,
                                 data=json.dumps(data),
                                 headers=cf.JSON_HEADER)

    script_dir = os.path.dirname(__file__)
    abs_file_path = script_dir + '/temp'
    f = add_and_get_file(file_name, abs_file_path)
    f.write(file_content)
    f.close()
    #The following line is specific to linux machines
    os.system('xdg-open ' + 'temp/' + file_name)
    return
import requests
import sys
sys.path.insert(0, '/home/lavelld/Documents/SS/Internet_Apps/DFS/src')
from format import format_file_req
import json
import config as cf

if __name__ == '__main__':
    cache_server_port = cf.CACHE_SERVER_PORT
    file_name = '1.9'
    data = {'file_name': file_name, 'file_content': 'hello there sir'}
    JSON_HEADER = {'content-type': 'application/json'}

    url = format_file_req(file_name, cache_server_port)
    #response = requests.post(url, data=json.dumps(data), headers = JSON_HEADER)
    response = requests.get(url)
    response = response.content
    print(response)
    def post(self):
        '''write to file
        request format:
        {
            file_name:
            file_content:
            replicate: (if replicate = true send request onto the replication server)
            new_file:
        }'''

        content = request.json
        file_name = content['file_name']
        new_file = content['new_file']
        file_version = 0
        file = get_file_write(file_name, FILE_SERVER_PATH)
        print("Received request for " + file_name)
        if (file == None and new_file == False):
            abort(404)
        else:

            file_content = content["file_content"]
            if new_file == True:
                print("Creating new file")
                try:
                    update_file(file_name, FILE_SERVER_PATH, file_content)
                    #Send alert to dir server
                    dir_port = get_port('dir_server')
                    req = format_file_req(dir_port)
                    data = {
                        'file_name': file_name,
                        'file_server_port': str(SERVER_PORT),
                        'new_file': new_file,
                        'file_content': file_content
                    }
                    response = json.loads(
                        requests.post(req,
                                      data=json.dumps(data),
                                      headers=cf.JSON_HEADER).content.decode())
                    file_version = response['file_version']

                except:
                    print('Creation failed')
                    return
            else:
                print("Performing write")
                try:
                    file.write(file_content)
                    if content['replicate'] == True:
                        print('replicating write')
                        #Find replication port
                        dir_server_port = get_port('dir_server')
                        #Send post onto replication server
                        req = format_file_req(dir_server_port)
                        data = {
                            'file_name': file_name,
                            'file_content': content['file_content'],
                            'file_server_port': str(SERVER_PORT),
                            'new_file': new_file
                        }
                        response = json.loads(
                            requests.post(
                                req,
                                data=json.dumps(data),
                                headers=cf.JSON_HEADER).content.decode())
                        file_version = response['file_version']

                except:
                    print('write failed')
                    return
            response = {"file_version": file_version}

            return response