예제 #1
0
def get_port(server_name):
    '''Gets port of given server from registry server, returns none if that server doesnt exist'''
    url = format_registry_req(server_name, cf.REGISTRY_SERVER_PORT)
    response = json.loads(requests.get(url).content.decode())
    port = response['dir_port']
    if str(port) == str(-1):
        print('port returned -1')
        return None
    return port
class state_API(Resource):
    '''Api that allows calls to check the state and shutdown the server'''
    def get(self):
        response = {
            'state': 'running'
        }
        return response

    def delete(self):
        request.environ.get('werkzeug.server.shutdown')()
        response = {
            'state': 'shutting down'
        }
        return response

api.add_resource(state_API, '/api/state')
api.add_resource(Cache_API, '/api/file', endpoint= 'file')

if __name__ == '__main__':
    clear_cache()
    db.drop_collection('file_map')
    server_port = int(sys.argv[1])
    server_init_url = format_registry_req('cache_server', cf.REGISTRY_SERVER_PORT)
    data = {
        'dir_port': server_port
    }
    requests.post(server_init_url, data=json.dumps(data), headers=cf.JSON_HEADER)
    app.run(host= '0.0.0.0', port = server_port, debug = True)

import sys
sys.path.insert(0, '/home/lavelld/Documents/SS/Internet_Apps/DFS/src')
from format import format_registry_req
import requests
import config as cf
import json

url = format_registry_req('file_server', cf.REGISTRY_SERVER_PORT)
data = {'dir_port': 5001}
requests.post(url, data=json.dumps(data), headers=cf.JSON_HEADER)

url = format_registry_req('file_server', cf.REGISTRY_SERVER_PORT)
data = {'dir_port': 5002}
requests.post(url, data=json.dumps(data), headers=cf.JSON_HEADER)

url = format_registry_req('dir_server', cf.REGISTRY_SERVER_PORT)
data = {'dir_port': 5003}
requests.post(url, data=json.dumps(data), headers=cf.JSON_HEADER)

url = format_registry_req('lock_server', cf.REGISTRY_SERVER_PORT)
data = {'dir_port': 5004}
requests.post(url, data=json.dumps(data), headers=cf.JSON_HEADER)

url = format_registry_req('file_server', cf.REGISTRY_SERVER_PORT)
response = json.loads(requests.get(url).content.decode())
print('file server 1 response: ', response)

url = format_registry_req('file_server', cf.REGISTRY_SERVER_PORT)
response = json.loads(requests.get(url).content.decode())
print('file server 2 response: ', response)
예제 #4
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)