Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cleanups in Java examples advised by Sonar
[simgrid.git] / examples / java / io / file / Node.java
1 /* Copyright (c) 2012-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /********************* Files and Storage handling ****************************
7  * This example implements all main file functions of the MSG API
8  *
9  * Scenario: Each host
10  * - opens a file
11  * - tries to read 10,000 bytes in this file
12  * - writes 100,000 bytes in the file
13  * - seeks back to the beginning of the file
14  * - tries to read 150,000 bytes in this file
15  *
16 ******************************************************************************/
17
18 package io.file;
19
20 import org.simgrid.msg.Msg;
21 import org.simgrid.msg.File;
22 import org.simgrid.msg.Host;
23 import org.simgrid.msg.Process;
24 import org.simgrid.msg.MsgException;
25
26 public class Node extends Process {
27   private static String file1 = "/doc/simgrid/examples/platforms/g5k.xml";
28   private static String file2 = "\\Windows\\setupact.log";
29   private static String file3 = "/doc/simgrid/examples/platforms/g5k_cabinets.xml";
30   private static String file4 = "/doc/simgrid/examples/platforms/nancy.xml";
31
32   protected int rank;
33
34   public Node(Host host, int number) {
35     super(host, Integer.toString(number), null);
36     this.rank = number;
37   }
38
39   public void main(String[] args) throws MsgException {
40     String mount = "/home";
41     String fileName;
42     switch (rank) {
43       case 4:
44         fileName = mount + file1;
45       break;
46       case 0:
47         mount = "c:";
48         fileName = mount + file2;
49       break;
50       case 2:
51         fileName = mount + file3;
52       break;
53       case 1:
54         fileName = mount + file4;
55       break;
56       default:
57         fileName = mount + file1;
58       break;
59     }
60
61     Msg.info("Open file " + fileName);
62     File file = new File(fileName);
63
64     long read = file.read(10000,1);
65     Msg.info("Having read " + read + " on " + fileName);
66
67     long write = file.write(100000,1);
68     Msg.info("Having write " + write + " on " + fileName);
69
70     Msg.info("Seek back to the beginning of " + fileName);
71     file.seek(0,File.SEEK_SET);
72
73     read = file.read(150000,1);
74     Msg.info("Having read " + read + " on " + fileName);  
75   }
76 }