Logo AND Algorithmique Numérique Distribuée

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