-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientTest.java
152 lines (133 loc) · 5.57 KB
/
ClientTest.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
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Random;
class ClientTest {
public static Socket socket;
public static BufferedReader in;
public static PrintWriter out;
public static String line;
public static void main(String[] args) {
try {
socket = new Socket(InetAddress.getLocalHost(),6000);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// storing
File downloadFolder = new File("UploadFiles");
File[] downloadFiles = downloadFolder.listFiles();
if (downloadFiles != null) {
for (File downloadFile : downloadFiles) {
testStore(downloadFile);
}
}
// loading
if (downloadFiles != null) {
for (File downloadFile : downloadFiles) {
testLoad(downloadFile, "LOAD ");
}
}
// listing
testList();
// removing
if (downloadFiles != null) {
for (File downloadFile : downloadFiles) {
testRemove(downloadFile);
}
}
socket.close();
}
catch (Exception e) {
System.out.println("Error: " + e);
}
}
public static void testStore(File file) throws Exception {
System.out.println("STORE " + file.getName() + " " + file.length());
out.println("STORE " + file.getName() + " " + file.length());
while ((line = in.readLine()) != null) {
if (line.startsWith("STORE_TO ")) {
ArrayList<String> ports = new ArrayList<>(Arrays.asList(line.split(" ")));
ports.remove(0);
for (String port : ports) {
Socket dstoreSocket = new Socket(InetAddress.getLocalHost(), Integer.parseInt(port));
BufferedReader clientIn = new BufferedReader(new InputStreamReader(dstoreSocket.getInputStream()));
PrintWriter clientOut = new PrintWriter(dstoreSocket.getOutputStream(), true);
String clientLine;
clientOut.println("STORE " + file.getName() + " " + file.length());
while ((clientLine = clientIn.readLine()) != null) {
if (clientLine.equals("ACK")) {
dstoreSocket.getOutputStream().write(Files.readAllBytes(file.toPath()));
break;
}
}
dstoreSocket.close();
}
while ((line = in.readLine()) != null) {
if (line.equals("STORE_COMPLETE")) {
return;
}
}
} else if (line.equals("ERROR_FILE_ALREADY_EXISTS")) {
System.out.println(line);
return;
} else if (line.equals("ERROR_NOT_ENOUGH_DSTORES")) {
System.out.println(line);
return;
}
}
}
public static void testLoad(File file, String load) throws Exception {
System.out.println(load + file.getName());
out.println(load + file.getName());
while ((line = in.readLine()) != null) {
if (line.startsWith("LOAD_FROM ")) {
try {
String port = line.split(" ")[1];
String fileSize = line.split(" ")[2];
Socket dataSocket = new Socket(InetAddress.getLocalHost(), Integer.parseInt(port));
PrintWriter dataOut = new PrintWriter(dataSocket.getOutputStream(), true);
dataOut.println("LOAD_DATA " + file.getName());
byte[] contents = dataSocket.getInputStream().readNBytes(Integer.parseInt(fileSize));
if ("ERROR_FILE_DOES_NOT_EXIST".contains(new String(contents, StandardCharsets.UTF_8))) {
testLoad(file, "RELOAD ");
break;
}
Random rand = new Random();
File newFile = new File("DownloadFiles/test" + (rand.nextInt(999) + 6) + ".txt");
Files.write(newFile.toPath(), Arrays.copyOfRange(contents, 0, Integer.parseInt(fileSize)));
dataSocket.close();
break;
} catch (Exception e) {
System.out.println("Log: Malformed load message from the client");
break;
}
} else if (line.equals("ERROR_FILE_DOES_NOT_EXIST")) {
System.out.println(line);
return;
} else if (line.equals("ERROR_LOAD")) {
System.out.println(line);
return;
}
}
}
public static void testRemove(File file) throws Exception {
System.out.println("REMOVE " + file.getName());
out.println("REMOVE " + file.getName());
while ((line = in.readLine()) != null) {
if (line.equals("REMOVE_COMPLETE")) {
return;
}
}
}
public static void testList() throws Exception {
out.println("LIST");
System.out.println(in.readLine());
}
}