-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
155 lines (143 loc) · 4.74 KB
/
Client.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
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
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.HashMap;
import java.util.Scanner;
import java.util.function.Consumer;
public class Client {
private static Socket socket;
private static HashMap<String, Consumer<String[]>> command;
private static Utils util;
private static DataInputStream input;
public static void initilzate() throws IOException {
util = new Utils();
//IP address
String serverAddress = util.getIp();
// Port number
int port = util.getPort();
// Create a connection to Port at the IP address
socket = new Socket(serverAddress, port);
command = new HashMap<String, Consumer<String[]>>();
input = new DataInputStream(socket.getInputStream());
command.put(
"cd",
cmd -> {
try {
// Receive the confirmation that an existent directory was given
if (input.readBoolean()) {
// Receive the name of the new directory
String directory = input.readUTF();
System.out.println("The server is at: " + directory);
} else {
System.out.println("This directory doesn't exist");
}
} catch (IOException e) {
System.out.println("An error occured while executing this command!");
}
}
);
command.put(
"ls",
cmd -> {
try {
// Read the output of the command
String lsOutput = input.readUTF();
System.out.println(lsOutput);
} catch (IOException e) {
System.out.println("An error occured while executing this command");
}
}
);
command.put("mkdir", cmd -> {
try {
// Return the confirmation that a new directory has been created
System.out.println(input.readUTF());
} catch (IOException e) {
// Auto-generated catch block
e.printStackTrace();
}
});
command.put(
"upload",
cmd -> {
try {
// Make sure that the command has a file name
if (cmd.length == 2) {
//Send file to User
util.sendfile(socket, System.getProperty("user.dir"), cmd[1]);
System.out.println("The file " + cmd[1] + " has been uploaded successfully!");
} else {
System.out.println("A file name is required!");
}
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println("An error occured while uploading");
}
}
);
// Download protocol
command.put(
"download",
cmd -> {
try {
// Receive file from user
if (cmd.length == 2) {
util.getfile(socket, System.getProperty("user.dir"));
System.out.println("The file " + cmd[1] + " has been downloaded successfully!");
} else {
System.out.println("A file name is required!");
}
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println("An error occured while downloading");
} finally {
}
}
);
// End of session
command.put(
"exit",
cmd -> {
System.out.println("End of session, you have been disconnected!");
}
);
}
public static void main(String[] args) throws Exception {
try {
System.out.println("Client is running");
initilzate();
// Assign a reader
String input = "";
Scanner myObj;
do {
System.out.print("Enter command line : ");
DataOutput out = new DataOutputStream(socket.getOutputStream());
//Create stream to read inputs
myObj = new Scanner(System.in);
// Read input from the user
input = myObj.nextLine();
String[] key = util.getkey(input);
if (command.containsKey(key[0])) {
// Create stream to send information
out = new DataOutputStream(socket.getOutputStream());
// Send Command line to server
out.writeUTF(input);
// Depending on key we use the require step to send or receive information from server
command.get(key[0]).accept(key);
} else {
System.out.println("This command doesn't exist!");
}
// If input equal 0, then exit
} while (input.compareTo("exit") != 0);
myObj.close();
socket.close();
} catch (Exception e) {
System.out.println(e);
System.out.println("Error while exiting");
}
}
}