-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNode.java
86 lines (74 loc) · 2.45 KB
/
Node.java
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
import java.net.*;
import java.io.*;
/**
* Clase del servidor.
*/
public class Node {
private static int node_port;
private static String node_id;
private static String music_library_filepath;
private static String known_nodes_filepath;
//public static Thread mainThread;
/**
*
* @param args Argumentos de la línea de comandos.
*/
public static void main(String[] args){
//mainThread = Thread.currentThread();
//Parseo de parámetros
set_params(args);
try{
System.out.println("Servidor "+node_id+" estableciendo puerto de escucha");
ServerSocket node_socket = new ServerSocket(node_port);
Socket client_socket = null;
// Crear P2pProtocolHandler genérico
P2pProtocolHandler genericHandler =
new P2pProtocolHandler(known_nodes_filepath,
music_library_filepath, node_id);
System.out.println("Servidor "+node_id+" listo para recibir ordenes");
//Loop principal del servidor
while(true){
client_socket = node_socket.accept();
new ClientRequestThread(client_socket,
genericHandler).start();
}
}
catch(FileNotFoundException fnf) {
System.out.println("Error al abrir archivo "
+known_nodes_filepath+" :"+fnf);
}
catch(IOException e){
System.out.println("I/O Error: "+e);
}
}
/**
* Parsea los argumentos de la línea de comandos.
* @param args Argumentos de la línea de comandos.
*/
private static void set_params(String args[]){
char op = '\0';
int i = 0;
while( i < args.length ){
op = args[i].charAt(1);
switch(op){
case 'p':
node_port = Integer.parseInt(args[i+1]);
break;
case 'c':
known_nodes_filepath = args[i+1];
break;
case 'b':
music_library_filepath = args[i+1];
break;
case 'i':
node_id = args[i+1];
break;
default:
System.out.println("Opcion incorrecta");
System.exit(1);
break;
}
i += 2;
}
}
}