-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.java
More file actions
132 lines (116 loc) · 5.09 KB
/
Copy pathCommand.java
File metadata and controls
132 lines (116 loc) · 5.09 KB
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
import java.net.*;
import java.io.*;
public class Command {
// The static field containing the current user input
private static String[] param;
/**
* The static method that parses the current command input by the user
* @param command the command in the form of a string
*/
public static void parseCommand(String command) {
parseArguments(command);
switch (param[0]) {
// returns back to the prompt
case "":
return;
// list the available sockets and connected sockets based on the config ports
case "List":
case "list":
System.out.println("Available Ports:");
for (TcpSocket socket : TcpSocketController.availableTcpSockets) {
System.out.println(socket.getLocalPort());
}
System.out.println("Connected Ports:");
for (TcpSocket socket : TcpSocketController.connectedTcpSockets) {
System.out.println(socket.getLocalPort() + " - connected to " + socket.getConnectionSocket().getRemoteSocketAddress());
}
break;
// connect to a peer with the provided IP address and port
case "Connect":
case "connect":
try {
connect(param[1], param[2]);
}
catch (IndexOutOfBoundsException | NumberFormatException e) {
help(true);
}
break;
// send to all neighboring peers a query of the filename
case "Get":
case "get":
if (param.length > 1)
getFile(param[1]);
else
help(true);
break;
// disconnects all connection with peers
case "Leave":
case "leave":
TcpSocketController.disconnectAllSockets();
System.out.println("Closed all active TCP connection");
break;
// disconnects all connection with peers and exit the program
case "Exit":
case "exit":
TcpSocketController.disconnectAllSockets();
System.out.println("Have a great day. Bye!");
System.exit(0);
// prints the help section
case "Help":
case "help":
help(false);
break;
// when a command is not found
default:
System.out.println("Error: " + command + ": command not found");
}
}
/**
* The static method that processes the arguments and stores them into an array of strings
* @param args the user input to be split into an array
*/
public static void parseArguments(String args) {
param = args.split(" ");
}
/**
* The static method that prints out the help section
* @param versionHelp the boolean parameter that indicates whether to print the version information
*/
public static void help(boolean versionHelp) {
System.out.println();
if (!versionHelp) {
System.out.println("P2P Network - version 0.1");
System.out.println("Developed by Steven Chen Hao Nyeo");
System.out.println("Case Western Reserve University - 2019\n");
}
System.out.println("List of commands: ");
System.out.println("Connect [ip-address] [port] - connect to the peer with the designated IP address and port number");
System.out.println("Get [filename] - send queries to peer to request for designated file");
System.out.println("List - list the status of the sockets and the active connections");
System.out.println("Leave - close all TCP connections with neighboring peers");
System.out.println("Help - print this help menu");
System.out.println("Exit - close all TCP connections and terminates the program\n");
}
/**
* The static method that connects an available socket to the peer socket with the provided IP address and port
* @param ipAddrStr the IP address in the format of a string
* @param portStr the port number in the format of a string
*/
public static void connect(String ipAddrStr, String portStr) {
System.out.println("Connecting to peer...");
TcpSocket peerSocket = TcpSocketController.getNextAvailableSocket();
peerSocket.connectToPeer(ipAddrStr, Integer.parseInt(portStr));
}
/**
* The static method that handles with sending queries for file transfer
* @param filename a string representing the filename of the requested file
*/
public static void getFile(String filename) {
for (TcpSocket activeSocket : TcpSocketController.connectedTcpSockets) {
// Initiate a new ID for the query
final int newId = TcpSocketController.getNewId();
System.out.println("Sent query to port " + activeSocket.getLocalPort() + ". Query ID = " + newId);
activeSocket.writeToPeer("Q:" + newId + ";" + filename + '\n');
}
}
}