forked from matiasrojo/cliente-p2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
230 lines (175 loc) · 6.5 KB
/
controller.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
const ClientP2P = require('./class/clientp2p.js')
const ServerP2P = require('./class/serverp2p.js')
const ClientCatalog = require('./class/clientCatalog.js')
const ClientBalancer = require('./class/clientBalancer.js')
mi_server_p2p = new ServerP2P();
list_client_p2p = [];
mi_client_balancer = new ClientBalancer(onConnectionBalancer,
onErrorConnectionBalancer,
onGetServerCatalog);
mi_client_catalog = new ClientCatalog(onConnectionCatalog, onErrorConnectionCatalog, onGetFileList, onGetPeerList);
function load() {
// Iniciamos el servidor de archivos
mi_server_p2p.listen();
// Iniciamos la conexión con el balanceador de cargas
mi_client_balancer.addIPPort('127.0.0.1', 3333);
//mi_client_balancer.addIPPort('192.168.0.35', 3333);
mi_client_balancer.connect();
// Solicitamos un servidor de catalogo al balanceador de cargas
mi_client_balancer.getCatalogServer();
}
function searchFiles(name) {
mi_client_catalog.getFilesList(name);
}
/*********************************************************/
/* EVENTOS DE LAS CLASES
/*********************************************************/
/******************* BALANCEADOR *************************/
/* Evento conexión con el balanceador */
function onConnectionBalancer() {
document.title = "Cliente P2P - Conectado al balanceador";
}
/* Evento recibe servidor catálogo */
function onGetServerCatalog(info) {
// Nos conectamos a un servidor de catálogo
mi_client_catalog.setIPPort(info.ip, info.port);
mi_client_catalog.connect();
}
/* Nos desconectamos del balanceador */
function onErrorConnectionBalancer() {
document.title = "Cliente P2P - Desconectado del balanceador";
}
/******************* CATÁLOGO **************************/
/* Evento conexión con el servidor de Catalogo */
function onConnectionCatalog(){
document.title = "Cliente P2P - Conectado al Catálogo";
$("#button-search").prop("disabled", false);
}
/* Nos desconectamos o no nos pudimos conectar al catalogo */
function onErrorConnectionCatalog() {
document.title = "Cliente P2P - Desconexión del Catálogo";
// Solicitamos un servidor de catalogo al balanceador de cargas
mi_client_balancer.getCatalogServer();
}
/* Evento recibe listado de archivos */
function onGetFileList(files) {
clearResultTableSearch();
$.each(files, function(i, file) {
addResultTableSearch(file.nombre, file.size, file.peers, file.id);
});
}
/* Evento recibe lista de pares */
function onGetPeerList(peers) {
var current_file = mi_client_catalog.getCurrentFileSelected();
var peers_amount = peers.length;
var client_p2p = new ClientP2P(onCompleteDownloadFilePeer,
onErrorConnectionClientP2P,
onConnectFilePeer,
onCompleteDownload);
client_p2p.setFile(current_file.id, current_file.nombre, current_file.hash,current_file.size);
$.each(peers, function(i, peer) {
client_p2p.addPeer(peer.ip, 6532);
});
addRowTableDownload(current_file.id, current_file.nombre, current_file.size, peers_amount, 'Descargando...');
// Agregamos la conexión a la lista
list_client_p2p[current_file.id] = client_p2p;
list_client_p2p[current_file.id].downloadFile();
}
/******************* CLIENTE P2P **************************/
/* No se puede conectar o se desconecta un par */
function onErrorConnectionClientP2P(lost_peer, file_id) {
}
/* Se conectar a un par */
function onConnectFilePeer(peer, file_hash, file_name, chunk) {
addRowTablePeer(file_name, file_hash, chunk.offset, peer.ip, chunk.offset + ' - ' + (chunk.offset + chunk.size), 'Conectando...');
}
/* Se completa la descarga del par */
function onCompleteDownloadFilePeer(peer, file_hash, chunk) {
editRowTablePeer(file_hash, chunk.offset, 'Completo');
}
/* Se completa la descarga */
function onCompleteDownload(file_id, file_name) {
editRowTableDownload(file_id, 'Completo');
mi_client_catalog.sendNewFile(file_name);
}
/*********************************************************/
/* EVENTOS DE LA VISTA
/*********************************************************/
// Click en el botón de buscar
$("#button-search").click(function() {
var file_name = $("#text-search").val();
searchFiles(file_name);
});
// Click en el bóton descargar
$('.container').on('click', 'button.download-button', function() {
var id_file = $(this).attr('idfile');
mi_client_catalog.getPeersList(id_file);
});
function clearResultTableSearch(){
$('#search-table tbody tr').remove();
}
function addResultTableSearch(name, size, peers, id) {
$('#search-table > tbody:last-child').append(`<tr>
<td class="p-name">
<b>` + name + `</b>
<br>
</td>
<td class="p-progress">
<span>` + size + ` b</span>
</td>
<td>
<span class="label label-primary">` + peers + `</span>
</td>
<td>
<button id="download-button" idfile="` + id + `" class="download-button btn btn-info btn-xs">
<i class="fa fa-pencil"></i>
Descargar
</button>
</td>
</tr>`);
}
function addRowTableDownload(id, name, size, peers, state) {
$('#download-table > tbody:last-child').append(`<tr id="download-` + id + `">
<td class="p-name">
<b>` + name + `</b>
<br>
</td>
<td class="p-progress">
<span>` + size + `</span>
</td>
<td>
<span class="label label-primary">` + peers + `</span>
</td>
<td class="p-state">
<span>` + state + `</span>
</td>
</tr>`);
}
function addRowTablePeer(file_name, file_hash, id, ip, fragment, state) {
$('#peers-table > tbody:last-child').append(`<tr id="peer-` + file_hash + id + `">
<td class="name">
<b>` + file_name + `</b>
<br>
</td>
<td class="ip">
<b>` + ip + `</b>
<br>
</td>
<td class="size">
<span>` + fragment + `</span>
</td>
<td class="p-state">
<span>` + state + `</span>
</td>
</tr>`);
}
function editRowTableDownload(peer_id, state) {
$('#download-' + peer_id + ' > .p-state').html(state);
}
function editRowTablePeer(file_hash, peer_id, state) {
$('#peer-' + file_hash + peer_id + ' > .p-state').html(state);
}
/*********************************************************/
/* INICIO
/*********************************************************/
load();