Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
34803da7d4eab3621e1248502b37b32b336cb80e
[simgrid.git] / src / java / simgrid / msg / ApplicationHandler.java
1 /*
2  *
3  * Copyright 2006,2007 Martin Quinson, Malek Cherier           
4  * All right reserved. 
5  *
6  * This program is free software; you can redistribute 
7  * it and/or modify it under the terms of the license 
8  * (GNU LGPL) which comes with this package.
9  *
10  * These are the upcalls used by the FleXML parser for application files
11  */
12
13 package simgrid.msg;
14
15 import java.util.Vector;
16 import java.util.Hashtable;
17
18 /**
19  * The handler used to parse the deployment file which contains 
20  * the description of the application (simulation).
21  *
22  * @author  Abdelmalek Cherier
23  * @author  Martin Quinson
24  * @version 1.00, 07/05/01
25  * @see Host
26  * @see Process
27  * @see Simulation
28  * @since SimGrid 3.3
29  * @since JDK1.5011 
30  */
31 public final class ApplicationHandler {
32
33         /* 
34          * This class is used to create the processes described in the deployment file.
35          */
36         static class ProcessFactory {
37                 /**
38                  * The vector which contains the arguments of the main function 
39                  * of the process object.
40                  */
41                 public Vector<String> args;
42
43                 public Hashtable<String,String> properties;
44
45                 /**
46                  * The name of the host of the process.
47                  */
48                 private String hostName;
49
50                 /**
51                  * The function of the process.
52                  */
53                 private String function;
54
55
56                 /**
57                  * Default constructor.
58                  */
59                 public ProcessFactory() {
60                         this.args = new Vector<String>();
61                         this.properties = new Hashtable<String,String>();
62                         this.hostName = null;
63                         this.function = null;
64                 }
65                 /**
66                  * This method is called by the start element handler.
67                  * It sets the host and the function of the process to create,
68                  * and clear the vector containing the arguments of the 
69                  * previouse process function if needed.
70                  *
71                  * @host                                The host of the process to create.
72                  * @function                    The function of the process to create.
73                  *
74                  */
75                 public void setProcessIdentity(String hostName, String function) {
76                         this.hostName = hostName;
77                         this.function = function;
78
79                         if (!args.isEmpty())
80                                 args.clear();
81
82                         if(!properties.isEmpty())
83                                 properties.clear();
84                 }
85                 /**
86                  * This method is called by the startElement() handler.
87                  * It stores the argument of the function of the next
88                  * process to create in the vector of arguments.
89                  *
90                  * @arg                                 The argument to add.
91                  *
92                  */ public void registerProcessArg(String arg) {
93                          args.add(arg);
94                  }
95
96                  public void setProperty(String id, String value)
97                  {
98                          properties.put(id,value);      
99                  }
100
101                  public String getHostName()
102                  {
103                          return hostName;
104                  }
105
106                  @SuppressWarnings("unchecked")
107                  public void createProcess() {
108                          try {
109                                  Class<simgrid.msg.Process> cls = (Class<Process>) Class.forName(this.function);
110
111                                  simgrid.msg.Process process = cls.newInstance();
112                                  process.name = this.function;
113                                  process.id = simgrid.msg.Process.nextProcessId++;
114                                  Host host = Host.getByName(this.hostName);
115
116                                  MsgNative.processCreate(process, host);
117                                  Vector args = processFactory.args;
118                                  int size = args.size();
119
120                                  for (int index = 0; index < size; index++)
121                                          process.args.add(args.get(index));
122
123                                  process.properties = this.properties;
124                                  this.properties = new Hashtable();
125
126                          } catch(JniException e) {
127                                  System.out.println(e.toString());
128                                  e.printStackTrace();
129
130                          } catch(NativeException e) {
131                                  System.out.println(e.toString());
132                                  e.printStackTrace();
133
134                          } catch(HostNotFoundException e) {
135                                  System.out.println(e.toString());
136                                  e.printStackTrace();
137
138                          } catch(ClassNotFoundException e) {
139                                  System.out.println(this.function +
140                                  " class not found\n The attribut function of the element process  of your deployment file\n must correspond to the name of a Msg Proces class)");
141                                  e.printStackTrace();
142
143                          } catch(InstantiationException e) {
144                                  System.out.println("instantiation exception");
145                                  e.printStackTrace();
146                          } catch(IllegalAccessException e) {
147                                  System.out.println("illegal access exception");
148                                  e.printStackTrace();
149                          } catch(IllegalArgumentException e) {
150                                  System.out.println("illegal argument exception");
151                                  e.printStackTrace();
152                          }
153
154                  }
155         }
156
157         /* 
158          * the ProcessFactory object used to create the processes.
159          */
160         public static ProcessFactory processFactory;
161
162         /**
163          * instanciates the process factory 
164          */
165         public static void onStartDocument() {
166                 processFactory = new ProcessFactory();
167         }
168
169         public static void onBeginProcess(String hostName, String function) {
170                 processFactory.setProcessIdentity(hostName, function);
171         }
172         public static void onProperty(String id, String value) {
173                 processFactory.setProperty(id, value);
174         }
175
176         public static void onProcessArg(String arg) {
177                 processFactory.registerProcessArg(arg);
178         }
179
180         public static void onEndProcess() {
181                 processFactory.createProcess();
182         }        
183
184         public static void onEndDocument() {
185         }  
186 }