-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientMain.java
148 lines (121 loc) · 4.8 KB
/
ClientMain.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
import java.io.File;
import java.io.IOException;
import java.util.Random;
public class ClientMain {
public static void main(String[] args) throws Exception{
final int cport = Integer.parseInt(args[0]);
int timeout = Integer.parseInt(args[1]);
// this client expects a 'downloads' folder in the current directory; all files loaded from the store will be stored in this folder
File downloadFolder = new File("downloads");
if (!downloadFolder.exists())
if (!downloadFolder.mkdir()) throw new RuntimeException("Cannot create download folder (folder absolute path: " + downloadFolder.getAbsolutePath() + ")");
// this client expects a 'to_store' folder in the current directory; all files to be stored in the store will be collected from this folder
File uploadFolder = new File("to_store");
if (!uploadFolder.exists())
throw new RuntimeException("to_store folder does not exist");
// launch a single client
// testClient(cport, timeout, downloadFolder, uploadFolder);
// launch a number of concurrent clients, each doing the same operations
for (int i = 0; i < 10; i++) {
new Thread() {
public void run() {
test2Client(cport, timeout, downloadFolder, uploadFolder);
}
}.start();
}
}
public static void test2Client(int cport, int timeout, File downloadFolder, File uploadFolder) {
Client client = null;
try {
client = new Client(cport, timeout, Logger.LoggingType.ON_FILE_AND_TERMINAL);
client.connect();
Random random = new Random(System.currentTimeMillis() * System.nanoTime());
File fileList[] = uploadFolder.listFiles();
for (int i=0; i<fileList.length/2; i++) {
File fileToStore = fileList[random.nextInt(fileList.length)];
try {
client.store(fileToStore);
Thread.sleep(50);
} catch (Exception e) {
System.out.println("Error storing file " + fileToStore);
e.printStackTrace();
}
}
String list[] = null;
try { list = list(client); } catch(IOException e) { e.printStackTrace(); }
System.out.println("REMOVING>>>>>>>>>>>>>>>>");
for (int i = 0; i < list.length/4; i++) {
String fileToRemove = list[random.nextInt(list.length)];
try {
client.remove(fileToRemove);
} catch (Exception e) {
System.out.println("Error remove file " + fileToRemove);
e.printStackTrace();
}
}
try { list = list(client); } catch(IOException e) { e.printStackTrace(); }
} catch(IOException e) {
e.printStackTrace();
} finally {
if (client != null)
try { client.disconnect(); } catch(Exception e) { e.printStackTrace(); }
}
}
public static void testClient(int cport, int timeout, File downloadFolder, File uploadFolder) {
Client client = null;
try {
client = new Client(cport, timeout, Logger.LoggingType.ON_FILE_AND_TERMINAL);
try { client.connect(); } catch(IOException e) { e.printStackTrace(); return; }
try { list(client); } catch(IOException e) { e.printStackTrace(); }
// store first file in the to_store folder twice, then store second file in the to_store folder once
File fileList[] = uploadFolder.listFiles();
for(File f : fileList) {
System.out.println(">>>>>>>"+f.getName());
try {
client.store(f);
} catch (IOException e) {
e.printStackTrace();
}
}
// if (fileList.length > 0) {
// try { client.store(fileList[0]); } catch(IOException e) { e.printStackTrace(); }
// try { client.store(fileList[0]); } catch(IOException e) { e.printStackTrace(); }
// }
// if (fileList.length > 1) {
// try { client.store(fileList[1]); } catch(IOException e) { e.printStackTrace(); }
// }
String list[] = null;
try { list = list(client); } catch(IOException e) { e.printStackTrace(); }
if (list != null)
System.out.println(">>>>>>>>>>>>>>>>>>>>>>");
for (String filename : list) {
try {
client.load(filename, downloadFolder);
Thread.sleep(20);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (list != null)
for (String filename : list)
try { client.remove(filename); } catch(IOException e) { e.printStackTrace(); }
if (list != null && list.length > 0)
try { client.remove(list[0]); } catch(IOException e) { e.printStackTrace(); }
try { list(client); } catch(IOException e) { e.printStackTrace(); }
} finally {
if (client != null)
try { client.disconnect(); } catch(Exception e) { e.printStackTrace(); }
}
}
public static String[] list(Client client) throws IOException, NotEnoughDstoresException {
System.out.println("Retrieving list of files...");
String list[] = client.list();
System.out.println("Ok, " + list.length + " files:");
int i = 0;
for (String filename : list)
System.out.println("[" + i++ + "] " + filename);
return list;
}
}