This repository has been archived by the owner on Aug 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
130 lines (114 loc) · 3.34 KB
/
main.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
import socket
from peer import Peer
from flask import Flask, request
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
IP = s.getsockname()[0]
PORT = 2000
BUFFER = 4096
MAX_BITS = 10
api = Flask(__name__)
peer = Peer(IP, PORT, BUFFER, MAX_BITS)
peer.start()
@api.route("/server/rest/DHT/addNode")
def anadirNodo():
"""
Se encarga de agregar el nodo existente a la red que se le pase por argumento
"""
global peer, IP
ip = request.args.get("ip")
if ip:
if ip == "127.0.0.1" or ip == "localhost":
return "La IP no es válida. Sólo se acepta la IP en red del equipo."
peer.unirseRed(ip, PORT)
return (
"Se ha unido el nodo ("
+ str(IP)
+ ":"
+ str(PORT)
+ ") con ID "
+ str(peer.id)
+ " a la red "
+ str(ip)
)
else:
return "No se ha podido unir al nodo a la red. Falta la IP."
@api.route("/server/rest/DHT/removeNode")
def apagarNodo():
"""
Se encarga de abandonar la red.
"""
global peer
if peer:
peer.abandonarRed()
text = "Cerrando nodo con id: " + str(peer.id)
return text
else:
return "El nodo ya se encuentra apagado."
@api.route("/server/rest/DHT/uploadContent")
def subirArchivo():
"""
Se encarga de subir un archivo a la red.
"""
global peer
if peer:
filename = request.args.get("filename")
data = request.args.get("data")
if filename and data:
fileID = peer.hashFichero(filename)
recvIPport = peer.sucesorDHT(peer.sucesor, fileID)
peer.subirFichero(filename, data, recvIPport, True)
return (
"Se ha subido el archivo " + str(filename) + " con el id " + str(fileID)
)
else:
return "Faltan parámetros de URL: filename, data."
else:
return "No se ha iniciado el nodo."
@api.route("/server/rest/DHT/downloadContent")
def descargarArchivo():
"""
Se encarga de descargar un archivo de la red.
"""
global peer
if peer:
filename = request.args.get("filename")
if filename:
peer.descargarFichero(filename)
text = "Archivo " + filename + " descargado.<br>"
with open(filename, "r") as f:
data = f.readlines()
for line in data:
text += line + "<br>"
return text
else:
return "Faltan párametros de URL: filename."
else:
return "No se ha iniciado el nodo."
@api.route("/server/rest/DHT/imprimirFingerTable")
def imprimirFingerTable():
"""
Se encarga de imprimir la lista de hashes.
"""
global peer
if peer:
return peer.mostrarFingerTable()
else:
return "No se ha iniciado el nodo."
@api.route("/server/rest/DHT/imprimirSucPred")
def imprimirSucPred():
"""
Se encarga de imprimir el sucesor y el predecesor.
"""
global peer
if peer:
text = "ID: " + str(peer.id) + "<br>"
text = text + "Suc: " + str(peer.sucesorID) + "<br>"
text = text + "Pred: " + str(peer.predecesorID) + "<br>"
return text
else:
return "No se ha iniciado el nodo."
try:
api.run(host="0.0.0.0", port=8080)
except Exception as e:
peer.socketListener.close()