integrate new plugin into Splitter-NG-test and build.xml
[splitter-ng] / Splitter-ng-test / src / SplitterTest.java
1 import java.io.IOException;
2 import java.nio.file.DirectoryStream;
3 import java.nio.file.Files;
4 import java.nio.file.Path;
5 import java.nio.file.Paths;
6 import java.util.Collection;
7 import java.util.HashMap;
8 import java.util.LinkedList;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Map.Entry;
12 import java.util.Scanner;
13 import java.util.TreeMap;
14
15 import splitterng.CodingTask;
16 import splitterng.DecodingTask;
17 import splitterng.EncodingTask;
18 import splitterng.Splitter;
19 import splitterng.SplitterCallback;
20 import splitterng.plugin.CodecDescription;
21 import splitterng.plugin.PluginDescription;
22
23 public class SplitterTest implements SplitterCallback {
24
25         private Collection<Splitter> splitters = new LinkedList<Splitter>();
26         private List<CodingTask> finished_encoding_task = new LinkedList<CodingTask>();
27         private List<CodingTask> finished_decoding_task = new LinkedList<CodingTask>();
28
29         // in der Map werden die Metadaten-vorlage für jeden Codec gespeichert
30         private Map<Splitter, Map<String, Object>> splitter_metadata_map = new HashMap<Splitter, Map<String, Object>>();
31
32         public static void main(String[] args) throws InterruptedException {
33                 SplitterTest splitterTest = new SplitterTest();
34                 splitterTest.run();
35
36                 // Thread.sleep(50000);
37                 // System.exit(0);
38         }
39
40         public void run() {
41
42                 System.out.println("Splitter Plugins");
43                 // Optional: Anfrage, welche Plugins geladen sind
44                 for (PluginDescription p : Splitter.getPluginDescriptions()) {
45                         System.out.println("---------------------------------------------\n" + p.getPluginID() + "\t"
46                                         + p.getPluginName());
47                         // Optional: Aus jeder Plugindescription die Verfügbaren Codecs
48                         // auslesen
49                         for (CodecDescription c : p.getCodecDescriptions()) {
50                                 System.out.println("\t|---------------------------------------------\n\t|" + c.getCodecID() + "\t"
51                                                 + c.getCodecDescriptionText());
52                                 Map<String, Integer[]> codecParameterMinMax = c
53                                                 .getCodecParameterMinMax();
54                                 for (Entry<String, Integer[]> e : codecParameterMinMax
55                                                 .entrySet()) {
56                                         System.out.println("\t|\t" + e.getKey() + "\tmin:"
57                                                         + e.getValue()[0] + "  max:" + e.getValue()[1]);
58                                 }
59                                 // Wenn hier eine Konfiguration für eine plugin-codec
60                                 // Kombination vorhanden ist, wird der Splitter initialisiert.
61                                 switch (p.getPluginID() + "/" + c.getCodecID()) {
62 ///-----------------------------------------------------------------------------------------------
63 ///-----------------------------------------------------------------------------------------------
64 ///-----------------------------------------------------------------------------------------------
65 ///-----------------------------------------------------------------------------------------------
66                                 case "jerasure/JerasureCauchyGood":
67                                         // Codecparameter Bauen
68                                         Map<String, Integer> codecParameter = new TreeMap<String, Integer>();
69                                         codecParameter.put("k", 3);
70                                         codecParameter.put("m", 3);
71                                         codecParameter.put("w", 16);
72                                         codecParameter.put("packetsize", 64);
73                                         System.out.println("\t\tinitialisiere mit" + " k="
74                                                         + codecParameter.get("k") + " m="
75                                                         + codecParameter.get("m") + " w="
76                                                         + codecParameter.get("w") + " packetsize="
77                                                         + codecParameter.get("packetsize"));
78                                         // Splitter mit Plugin-ID CodecID und codecparametern
79                                         // initialisieren
80                                         Splitter splitterjerasure = new Splitter(p.getPluginID(), c.getCodecID(),
81                                                         codecParameter);
82                                         splitters.add(splitterjerasure);
83                                         
84                                         // Metadaten bauen vorlage
85                                         Map<String, Object> metadatajerasure = new TreeMap<String, Object>();
86                                         metadatajerasure.put("fragmentsize", (int) 4096);
87                                         // Beleiebige weitere Metadaten
88                                         
89                                         splitter_metadata_map.put(splitterjerasure, metadatajerasure);
90                                         break;
91 ///-----------------------------------------------------------------------------------------------
92                                 case "--jerasure/JerasureReedSolomon":
93                                         // Codecparameter Bauen
94                                         Map<String, Integer> codecParameter1 = new TreeMap<String, Integer>();
95                                         codecParameter1.put("k", 3);
96                                         codecParameter1.put("m", 3);
97                                         codecParameter1.put("w", 16);
98                                         codecParameter1.put("packetsize", 64);
99                                         System.out.println("\t\tinitialisiere mit" + " k="
100                                                         + codecParameter1.get("k") + " m="
101                                                         + codecParameter1.get("m") + " w="
102                                                         + codecParameter1.get("w") + " packetsize="
103                                                         + codecParameter1.get("packetsize"));
104                                         // Splitter mit Plugin-ID CodecID und codecparametern
105                                         // initialisieren
106                                         Splitter splitterjerasure1 = new Splitter(p.getPluginID(), c.getCodecID(),
107                                                         codecParameter1);
108                                         splitters.add(splitterjerasure1);
109                                         
110                                         // Metadaten bauen vorlage
111                                         Map<String, Object> metadatajerasure1 = new TreeMap<String, Object>();
112                                         metadatajerasure1.put("fragmentsize", (int) 40960);
113                                         // Beleiebige weitere Metadaten
114                                         
115                                         splitter_metadata_map.put(splitterjerasure1, metadatajerasure1);
116                                         break;
117 ///-----------------------------------------------------------------------------------------------
118                                 case "--jerasure/JerasureLiber8tion":
119                                         // Codecparameter Bauen
120                                         Map<String, Integer> codecParameter2 = new TreeMap<String, Integer>();
121                                         codecParameter2.put("k", 3);
122                                         codecParameter2.put("m", 2);
123                                         //codecParameter.put("w", 16);
124                                         codecParameter2.put("packetsize", 64);
125                                         System.out.println("\t\tinitialisiere mit" + " k="
126                                                         + codecParameter2.get("k") + " m="
127                                                         + codecParameter2.get("m") + " w="
128                                                         + codecParameter2.get("packetsize"));
129                                         // Splitter mit Plugin-ID CodecID und codecparametern
130                                         // initialisieren
131                                         Splitter splitterjerasure2 = new Splitter(p.getPluginID(), c.getCodecID(),
132                                                         codecParameter2);
133                                         splitters.add(splitterjerasure2);
134                                         
135                                         // Metadaten bauen vorlage
136                                         Map<String, Object> metadatajerasure2 = new TreeMap<String, Object>();
137                                         metadatajerasure2.put("fragmentsize", (int) 40960);
138                                         // Beleiebige weitere Metadaten
139                                         
140                                         splitter_metadata_map.put(splitterjerasure2, metadatajerasure2);
141                                         break;
142 ///-----------------------------------------------------------------------------------------------
143                                 case "Raid1Plugin/raid1codec":
144                                         // Codecparameter Bauen
145                                         Map<String, Integer> codecParameternoop = new TreeMap<String, Integer>();
146                                         codecParameternoop.put("k", 1);
147                                         codecParameternoop.put("m", 1);
148                                         // codecParameternoop.put("w", 4);
149                                         //codecParameternoop.put("packetsize", 16);
150                                         System.out.println("\t\tinitialisiere mit k="
151                                                         + codecParameternoop.get("k") + " m="
152                                                         + codecParameternoop.get("m"));
153                                         // Splitter mit Plugin-ID CodecID und codecparametern
154                                         // initialisieren
155                                         Splitter splitterraid1 = new Splitter(p.getPluginID(), c.getCodecID(),
156                                                         codecParameternoop);
157                                         splitters.add(splitterraid1);
158                                         
159                                         // Metadaten bauen vorlage
160                                         Map<String, Object> metadataraid1 = new TreeMap<String, Object>();
161                                         metadataraid1.put("fragmentsize", (int) 40960);
162                                         // Beleiebige weitere Metadaten
163                                         
164                                         splitter_metadata_map.put(splitterraid1, metadataraid1);
165                                         
166                                         break;
167 ///-----------------------------------------------------------------------------------------------
168                                 case "jsharing/shamir":
169                                         // Codecparameter Bauen
170                                         Map<String, Integer> codecParameterjsharing = new TreeMap<String, Integer>();
171                                         codecParameterjsharing.put("k", 3);
172                                         codecParameterjsharing.put("m", 3);
173                                         System.out.println("\t\tinitialisiere mit k="
174                                                         + codecParameterjsharing.get("k") + " m="
175                                                         + codecParameterjsharing.get("m"));
176
177                                         Splitter splittershamir= new Splitter(p.getPluginID(), c.getCodecID(),
178                                                         codecParameterjsharing);
179                                         splitters.add(splittershamir);
180                                         
181                                         // Metadaten bauen vorlage
182                                         Map<String, Object> metadatashamir = new TreeMap<String, Object>();
183                                         metadatashamir.put("fragmentsize", (int)2);
184                                         // Beleiebige weitere Metadaten
185                                         
186                                         splitter_metadata_map.put(splittershamir, metadatashamir);
187                                         
188                                         break;
189                                 case "jerasure-java/JerasureJavaCRS":
190                                         // Codecparameter Bauen
191                                         Map<String, Integer> codecParameter11 = new TreeMap<String, Integer>();
192                                         codecParameter11.put("k", 3);
193                                         codecParameter11.put("m", 3);
194                                         codecParameter11.put("w", 16);
195                                         codecParameter11.put("packetsize", 64);
196                                         System.out.println("\t\tinitialisiere mit" + " k="
197                                                         + codecParameter11.get("k") + " m="
198                                                         + codecParameter11.get("m") + " w="
199                                                         + codecParameter11.get("w") + " packetsize="
200                                                         + codecParameter11.get("packetsize"));
201                                         // Splitter mit Plugin-ID CodecID und codecparametern
202                                         // initialisieren
203                                         Splitter splitterjerasure11 = new Splitter(p.getPluginID(), c.getCodecID(),
204                                                         codecParameter11);
205                                         splitters.add(splitterjerasure11);
206                                         
207                                         // Metadaten bauen vorlage
208                                         Map<String, Object> metadatajerasure11 = new TreeMap<String, Object>();
209                                         metadatajerasure11.put("fragmentsize", (int) 4096);
210                                         // Beleiebige weitere Metadaten
211                                         
212                                         splitter_metadata_map.put(splitterjerasure11, metadatajerasure11);
213                                         break;
214 ///-----------------------------------------------------------------------------------------------
215
216 ///-----------------------------------------------------------------------------------------------
217 ///-----------------------------------------------------------------------------------------------
218 ///-----------------------------------------------------------------------------------------------
219 ///-----------------------------------------------------------------------------------------------
220 ///-----------------------------------------------------------------------------------------------
221                                 default:
222                                         System.out.println("\t\tNo Splitter created for: "
223                                                         + p.getPluginID() + "/" + c.getCodecID());
224                                         break;
225                                 }
226                         }
227                 }
228
229                 System.out.println("\nListe der Dateien");
230                 Path srcDir = Paths.get("srcDir");
231                 List<Path> srcFileList = getFileList(srcDir);
232                 for (Path entry : srcFileList) {
233                         System.out.println("- " + entry.toString());
234                 }
235
236                 // restoreDir anlegen bzw aufräumen
237                 Path splitDir = Paths.get("splitDir");
238                 checkDirectory(splitDir);
239                 for (Path entry : getFileList(splitDir)) {
240                         try {
241                                 Files.delete(entry);
242                         } catch (IOException e) {
243                                 // TODO Auto-generated catch block
244                                 e.printStackTrace();
245                         }
246                 }
247
248                 // restoreDir anlegen bzw aufräumen
249                 Path restoreDir = Paths.get("restoreDir");
250                 checkDirectory(restoreDir);
251                 for (Path entry : getFileList(restoreDir)) {
252                         try {
253                                 Files.delete(entry);
254                         } catch (IOException e) {
255                                 e.printStackTrace();
256                         }
257                 }
258                 // Jede Datei ...
259                 for (Path srcfile : srcFileList) {
260                         // ... mit jedem initialisierten Splitter bearbeiten
261                         for (Splitter splitter : splitters) {
262                                 System.out.println("Encoding: "
263                                                 + srcfile
264                                                 + "  with "
265                                                 + splitter.getCurrentPlugin().getPluginDescription()
266                                                                 .getPluginID() + "/"
267                                                 + splitter.getCurrentCodecDescription().getCodecID());
268                                 try {
269
270                                         Map<String, Object> metadata = new TreeMap<String, Object>();
271                                         
272                                         for (Entry<String, Object> e: splitter_metadata_map.get(splitter).entrySet()){
273                                                 metadata.put(e.getKey(), e.getValue());
274                                         }
275                                         
276                                         metadata.put("time_encoding_submitted",
277                                                         System.currentTimeMillis());
278                         
279
280                                         // Hier wird codiert. Der Encodingtask
281                                         splitter.encode(new EncodingTask(srcfile, Paths
282                                                         .get("splitDir"), this, metadata));
283                                 } catch (InterruptedException | IOException e) {
284                                         e.printStackTrace();
285                                 }
286
287                         }
288
289                 }
290
291                 System.out.println("-----");
292                 System.out.println("Alle Aufträge an die Splitter übermittelt");
293
294                 printUsage();
295
296                 
297                 // dateien zusammenbauen
298                 @SuppressWarnings("resource")
299                 Scanner in = new Scanner(System.in);
300                 while (true) {
301                         System.out.print(">>>");
302                         switch (in.next()) {
303                         case "q":
304                         case "quit":
305                         case "exit":
306                                         System.exit(0);
307                         case "h":
308                         case "help":
309                                 printUsage();
310                                 break;
311                         case "ls":
312                                 switch (in.next()){
313                                 case "enc":
314                                         for (CodingTask t : finished_encoding_task) {
315                                                 System.out.println(""
316                                                                 + finished_encoding_task.lastIndexOf(t) + "\t"
317                                                                 + t.getFilePath().getFileName());
318                                         }
319                                         break;
320                                 case "dec":
321                                         for (CodingTask t : finished_decoding_task) {
322                                                 System.out.println(""
323                                                                 + finished_decoding_task.lastIndexOf(t) + "\t"
324                                                                 + t.getFilePath().getFileName());
325                                         }
326                                         break;
327                                         
328                                 case "frag":
329                                         try {
330                                                 for (Entry<Integer, Path> e : finished_encoding_task
331                                                                 .get(Integer.parseInt(in.next()))
332                                                                 .getFragmentPaths().entrySet()) {
333                                                         System.out.format("\t %-5s %s %s\n", e.getKey(), e
334                                                                         .getValue().toString(), (Files.exists(e
335                                                                         .getValue()) ? "OK" : "Fehlt"));
336                                                         // System.out.println(++"\t"+e.getValue().toString());
337                                                 }
338                                         } catch (Exception e) {
339                                                 System.out.println("Index nicht gefunden");
340                                         }
341                                         break;
342                                         
343                                 case "src":
344                                         System.out.println("noch nicht implementiert");
345                                         break;
346                                         
347                                         
348                                 case "splitter":
349                                         System.out.println("noch nicht implementiert");
350                                         break;
351                                 
352                                 }
353                                 break;
354
355                         case "meta":
356                                 switch(in.next()){
357                                 case "enc":
358                                         try {
359                                                 for (Entry<String, Object> e : finished_encoding_task
360                                                                 .get(Integer.parseInt(in.next())).getMetadata()
361                                                                 .entrySet()) {
362                                                         System.out.format("\t %-25s %-25s %s\n", e.getKey(), e
363                                                                         .getValue().toString(), e.getValue().getClass()
364                                                                         .toString());
365                                                         // System.out.println(++"\t"+e.getValue().toString());
366                                                 }
367                                         } catch (Exception e) {
368                                                 System.out.println("Index nicht gefunden");
369                                         }
370                                         break;
371                                 
372                                 case "dec":
373                                         try {
374                                                 for (Entry<String, Object> e : finished_decoding_task
375                                                                 .get(Integer.parseInt(in.next())).getMetadata()
376                                                                 .entrySet()) {
377                                                         System.out.format("\t %-25s %s\n", e.getKey(), e
378                                                                         .getValue().toString());
379                                                         // System.out.println(++"\t"+e.getValue().toString());
380                                                 }
381                                         } catch (Exception e) {
382                                                 System.out.println("Index nicht gefunden");
383                                         }
384                                         break;
385                                 }
386                                 break;
387
388                         case "rm":
389                                 try {
390                                         int tasknr = Integer.parseInt(in.next());
391                                         int fragmentnr = Integer.parseInt(in.next());
392                                         Map<Integer, Path> fragmentPaths = finished_encoding_task
393                                                         .get(tasknr).getFragmentPaths();
394                                         Path path = fragmentPaths.get(fragmentnr);
395                                         try {
396                                                 Files.delete(path);
397                                         } catch (Exception e) {
398                                                 System.out.println("Konnte fragment nicht löschen "
399                                                                 + e.getMessage());
400
401                                         }
402
403                                 } catch (Exception e) {
404                                         System.out.println("Index nicht gefunden");
405                                 }
406                                 break;
407
408                         case "dec":
409                                 try {
410                                         int tasknr = Integer.parseInt(in.next());
411                                         CodingTask enctask = (CodingTask) finished_encoding_task
412                                                         .get(tasknr);
413                                         Path restorefile = restoreDir.resolve(""
414                                                         + System.currentTimeMillis()
415                                                         + enctask.getFilePath().getFileName());
416
417                                         Map<Integer, Path> fragmentPaths = enctask
418                                                         .getFragmentPaths();
419                                         for (int i = 0; i < fragmentPaths.size(); i++) {
420                                                 Path path = fragmentPaths.get(i);
421                                                 if (path == null || !Files.exists(path)) {
422                                                         fragmentPaths.put(i, null);
423                                                 }
424
425                                         }
426                                         DecodingTask dectask = new DecodingTask(fragmentPaths,
427                                                         restorefile, this, enctask.getMetadata());
428                                         for (Splitter spl : splitters) {
429                                                 if (spl.getCurrentPlugin().getPluginDescription()
430                                                                 .getPluginID() == (String) enctask
431                                                                 .getMetadata("pluginid")
432                                                                 && spl.getCurrentCodecDescription()
433                                                                                 .getCodecID() == (String) enctask
434                                                                                 .getMetadata("codecid")) {
435                                                         System.out.println("Auftrag angenommen..."
436                                                                         + restorefile);
437                                                         spl.decode(dectask);
438                                                 }
439                                         }
440
441                                 } catch (Exception e) {
442                                         e.printStackTrace();
443                                         System.out.println("Index nicht gefunden");
444                                 }
445                                 break;
446                         case "enc":
447                                 System.out.println("noch nicht implementiert");
448                                 break;
449                                 
450                         
451                         default:
452                                 printUsage();
453                         }
454
455                 }
456
457         }
458
459         private static List<Path> getFileList(Path path) {
460                 List<Path> files = new LinkedList<Path>();
461                 checkDirectory(path);
462                 try (DirectoryStream<Path> stream = Files.newDirectoryStream(path,
463                                 new DirectoryStream.Filter<Path>() {
464                                         @Override
465                                         public boolean accept(Path entry) throws IOException {
466                                                 return !Files.isDirectory(entry);
467                                         }
468                                 });) {
469                         for (Path entry : stream) {
470                                 files.add(entry);
471                         }
472                 } catch (IOException e) {
473                         e.printStackTrace();
474                 }
475                 return files;
476         }
477
478         private static void checkDirectory(Path path) {
479                 if (!Files.exists(path)) {
480                         try {
481                                 Files.createDirectory(path);
482                         } catch (IOException e) {
483                                 System.err.println("Verzeichnis kann nicht erstellt werden: "
484                                                 + path.toUri());
485                                 e.printStackTrace();
486                         }
487                 }
488         }
489
490         // private static void initSplitter(Splitter splitter) {
491         //
492         // System.out.println("Liste der Codecs:");
493         // Collection<String> codecids = splitter.getCodecIDs();
494         // Iterator<String> it = codecids.iterator();
495         // while (it.hasNext()){
496         // System.out.println("- " + it.next().toString());
497         // }
498         // if (codecids.contains("jerasureparity")){
499         // SplitterConfiguration config = new SplitterConfiguration();
500         // config.setCodecID("jerasureparity");
501         // splitter.setSplitterConfiguration(config);
502         // }
503         // System.out.println();
504         // }
505
506         @Override
507         public void codingFailed(CodingTask task, String reason) {
508                 synchronized (task) { // damit die Ausgabe nicht verwürfelt wird
509                         System.err.println("Coding Failed: "
510                                         + task.getFilePath().toString() + "  Reason:" + reason);
511                 }
512
513         }
514
515         @Override
516         public synchronized void codingSuccessfull(CodingTask task) {
517                 synchronized (task) { // damit die Ausgabe nicht verwürfelt wird
518                         long duration = 0;
519                         if (task instanceof EncodingTask) {
520                                 finished_encoding_task.add(task);
521                                 duration = (long) task.getMetadata("time_encode_end")
522                                                 - (long) task.getMetadata("time_encode_start");
523                         } else if (task instanceof DecodingTask) {
524                                 finished_decoding_task.add(task);
525                                 duration = (long) task.getMetadata("time_decode_end")
526                                                 - (long) task.getMetadata("time_decode_start");
527                         }
528
529                         System.err.println("Coding OK: " + task.getFilePath().toString()
530                                         + "  Codec: " + task.getMetadata("pluginid") + "/"
531                                         + task.getMetadata("codecid") + "  T(ms):" + duration);
532                 }
533         }
534
535         public void printUsage() {
536                 System.out.println("Bedienung:"
537                                 //+ "\nls splitter\tlistet alle initialisierten Splitter auf"
538                                 //+ "\nls src\t\tlistet alle Dateien in scrDir auf"
539                                 + "\nls enc\t\tlistet alle fertig encodierten Tasks auf"
540                                 + "\nls dec\t\tlistet alle fertig decodierten Tasks auf"
541                                 + "\nls frag <nr>\tlistet die Fragmentdateien zu Encoding-Task <nr> auf"
542                                 + "\nrm <nr> <frac_nr>\tlöscht aus Encoding-Task <nr> die Fragmentdatei <frac_nr>"
543                                 + "\ndec <nr>\twiederherstellen der Originaldatei aus Encoding-Task <nr> in das Verzeichnis restoreDir"
544                                 //+ "\nenc <scr_nr> <splitter_nr>\tencodieren von Datei <scr_nr> mit Splitter <split-ter_nr>"
545                                 + "\nmeta enc <nr>\tgibt metadaten zu encodingtask <nr> aus"
546                                 + "\nmeta dec <nr>\tgibt metadaten zu decodingtask <nr> aus"
547                                 + "\nq, quit oder exit\tbeenden das Programm"
548                                 //+" \nh oder help\tzeigt die Hilfe an"
549                                 + "");
550         }
551
552 }