Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c1622cd599fb29d82b5c854f10a95c9ab5cdfcfa
[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.HostNotFoundException;
25 import org.simgrid.msg.MsgException;
26
27 public class Node extends Process {
28   private static String file1 = "/doc/simgrid/examples/platforms/g5k.xml";
29   private static String file2 = "\\Windows\\setupact.log";
30   private static String file3 = "/doc/simgrid/examples/platforms/g5k_cabinets.xml";
31   private static String file4 = "/doc/simgrid/examples/platforms/nancy.xml";
32
33   protected int rank;
34
35   public Node(Host host, int number) {
36     super(host, Integer.toString(number), null);
37     this.rank = number;
38   }
39
40   public void main(String[] args) throws MsgException {
41     String mount = "/home";
42     String fileName;
43     switch (rank) {
44       case 4:
45         fileName = mount + file1;
46       break;
47       case 0:
48         mount = "c:";
49         fileName = mount + file2;
50       break;
51       case 2:
52         fileName = mount + file3;
53       break;
54       case 1:
55         fileName = mount + file4;
56       break;
57       default:
58         fileName = mount + file1;
59       break;
60     }
61
62     Msg.info("Open file " + fileName);
63     File file = new File(fileName);
64
65     long read = file.read(10000,1);
66     Msg.info("Having read " + read + " on " + fileName);
67
68     long write = file.write(100000,1);
69     Msg.info("Having write " + write + " on " + fileName);
70
71     Msg.info("Seek back to the beginning of " + fileName);
72     file.seek(0,File.SEEK_SET);
73
74     read = file.read(150000,1);
75     Msg.info("Having read " + read + " on " + fileName);  
76   }
77 }