-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnodeServer.py
146 lines (116 loc) · 5.2 KB
/
nodeServer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os, requests, json, sys, pathlib, serverConfig
from flask import Flask, render_template, jsonify, url_for, request, session, flash, redirect, send_from_directory
from flask.ext.pymongo import PyMongo
from werkzeug.utils import secure_filename
from serverConfig import *
from time import sleep
#returns the Id of this current node
def getNodeID():
if(len(sys.argv) > 0):
return int(sys.argv[1])
return 1
##CONFIGURATIONS##
nodeID = getNodeID()
app = Flask(__name__)
app.config['SECRET_KEY'] = 'thisShouldBeSecret'
app.config['UPLOAD_FOLDER'] = '/home/donal-tuohy/Documents/SS_year/DFS/NODE_' + str(nodeID)
#######################
### LOCAL FUNCTIONS ###
#######################
#returns the address of this node
def getAddress():
nodeID = getNodeID()
portNum = 5000 + nodeID
return ("http://127.0.0.1:" + str(portNum) + "/")
#returns a dict of all the files on the node
def getDictOfFiles(nodeAddress):
filesOnNode = {}
fileList = os.listdir(app.config['UPLOAD_FOLDER'])
for fileName in fileList:
filesOnNode[fileName] = nodeAddress
return filesOnNode
#Returns boolean of whether the file is there or not
def checkForFile(filename):
return os.path.isfile("./NODE_" + str(nodeID) +"/" + filename)
#################
### ENDPOINTS ###
#################
#Server checks if the file exists
@app.route('/servercheck/<filename>', methods=['GET'])
def serverCheck(filename):
fileExists = checkForFile(filename)
#Server looking for file
print('Checking for file')
if fileExists:
return jsonify({'message': 'File on node.'})
return jsonify({'message': 'File does not exist.'})
#Server uses this endpoint to tell the node to delete a file from the server
@app.route('/removefile', methods=['POST'])
def removeFile():
message = request.get_json()
filename = message['fileToDelete']
os.remove(app.config['UPLOAD_FOLDER'] + '/' + filename)
print("<" + filename + "> has been deleted from this node.")
return jsonify({'message' : 'File deleted.'})
#Returns the file at this enpoint
@app.route('/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
#Recieves a file from the client to be uploaded to this node
#Once uploaded, sends a notification to the server that a file has been uploaded.
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
#Check if the post request has the file part
if 'file' not in request.files:
print('No file part')
return "No file part" #redirect(request.url)
file = request.files['file']
#if user does not select file
if file.filename =='':
print('No selected file')
return "NO selected file" #redirect(request.url)""
if file:
filename = file.filename
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
currentFiles[filename] = ""
dataToNotify = {'nodeAddress': getAddress(), 'fileName': {filename: [getAddress()]}, 'fileType': "upload" }
notifyServer = requests.post("http://127.0.0.1:5000/newfile", json=dataToNotify)
print("<" + filename + "> has been uploaded to this node.")
return filename + " uploaded to node " + str(getNodeID())
@app.route('/backup/<filename>', methods=['GET','POST'])
def backup(filename):
clientId = {'clientID' : -9999}
filecheck = requests.get("http://127.0.0.1:5000/download/" + filename, json = clientId)
if (filecheck.ok):
serverJsonResponse = filecheck.json()
print(serverJsonResponse)
if (serverJsonResponse['message'] == "File exists."):
print("Address to get file from = ", serverJsonResponse['address'])
fileRequest = requests.get(serverJsonResponse['address'])
print("Request sent to:", serverJsonResponse['address'])
with open('/home/donal-tuohy/Documents/SS_year/DFS/NODE_' + str(nodeID) + "/" + filename, 'wb') as handler:
handler.write(fileRequest.content)
del fileRequest
dataToNotify = {'nodeAddress': getAddress(), 'fileName': {filename: [getAddress()]} ,'fileType': "backup"}
updateServer = requests.post("http://127.0.0.1:5000/newfile", json=dataToNotify)
if updateServer.ok:
print(filename + " backed up on this node.")
else:
print("Could not get file to backup")
else:
print("File backup unsuccessful")
return "Hello"
if __name__ == "__main__":
from flask import jsonify
##First node will run on port 5001 and increment with each node
nodeID = getNodeID()
portNum = 5000 + nodeID
address = ("http://127.0.0.1:" + str(portNum) + "/")
#Create folder if not already created for this node
pathlib.Path('./NODE_' + str(nodeID)).mkdir(parents=True, exist_ok=True)
currentFiles = getDictOfFiles(address)
mainServerUrl = "http://127.0.0.1:5000/"
joinJSON = {'message': 'I am a new node.', 'nodeID': nodeID, 'address': address, 'currentFiles': currentFiles}
sendFlag = requests.post(mainServerUrl + "newnode", json=joinJSON)
app.run(debug=True,port=portNum)