Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://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 FILENAME1 = "/doc/simgrid/examples/platforms/g5k.xml";
30   private static String FILENAME2 = "\\Windows\\setupact.log";
31   private static String FILENAME3 = "/doc/simgrid/examples/platforms/g5k_cabinets.xml";
32   private static String FILENAME4 = "/doc/simgrid/examples/platforms/nancy.xml";
33
34   protected int number;
35
36   public Node(Host host, int number) throws HostNotFoundException {
37     super(host, Integer.toString(number), null);
38     this.number = number;
39   }
40
41   public void main(String[] args) throws MsgException {
42     String mount;
43     String filename;
44     switch (number) {
45       case 0:
46         mount = "/home";
47         filename = mount + FILENAME1;
48       break;
49       case 1:
50         mount = "c:";
51         filename = mount + FILENAME2;
52       break;
53       case 2:
54         mount = "/home";
55         filename = mount + FILENAME3;
56       break;
57       case 3:
58         mount = "/home";
59         filename = mount + FILENAME4;
60       break;
61       default:
62         mount = "/home";
63         filename = mount + FILENAME1;
64     }
65
66     Msg.info("Open file " + filename);
67     File file = new File(filename);
68
69     long read = file.read(10000,1);
70     Msg.info("Having read " + read + " on " + filename);
71
72     long write = file.write(100000,1);
73     Msg.info("Having write " + write + " on " + filename);
74
75     Msg.info("Seek back to the beginning of " + filename);
76     file.seek(0,file.SEEK_SET);
77
78     read = file.read(150000,1);
79     Msg.info("Having read " + read + " on " + filename);  
80   }
81 }