Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a3e2acb6725ffdff16a1a8286d8cee6d255a9ea9
[simgrid.git] / src / java / simgrid / msg / ApplicationHandler.java
1 /*
2  * simgrid.msg.ApplicationHandler.java  1.00 07/05/01
3  *
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier           
5  * All right reserved. 
6  *
7  * This program is free software; you can redistribute 
8  * it and/or modify it under the terms of the license 
9  *(GNU LGPL) which comes with this package. 
10  */
11
12 package simgrid.msg;
13
14 import java.util.Vector;
15 import org.xml.sax.*;
16 import org.xml.sax.helpers.*;
17 import java.lang.reflect.*;
18
19 /**
20  * The handler used to parse the deployment file which contains 
21  * the description of the application (simulation).
22  *
23  * @author  Abdelmalek Cherier
24  * @author  Martin Quinson
25  * @version 1.00, 07/05/01
26  * @see Host
27  * @see Process
28  * @see Simulation
29  * @since SimGrid 3.3
30  * @since JDK1.5011 
31  */
32 public final class ApplicationHandler extends DefaultHandler
33 {
34         
35         /* 
36          * This class is used to create the processes descibed in the deployment file.
37          */
38         class ProcessFactory
39         {
40                 /**
41                  * The vector which contains the arguments of the main function 
42                  * of the process object.
43                  */
44                 public Vector<String> args;
45                 
46                 /**
47                  * The name of the host of the process.
48                  */     
49                 private String hostName;
50                 
51                 /**
52                  * The function of the process.
53                  */
54                 private String function;
55                 
56                 /**
57                  * Default constructor.
58                  */
59                 public ProcessFactory(){
60                         this.args = new Vector<String>();
61                         this.hostName = null;
62                         this.function = null;
63                 }
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                 
83                 /**
84                  * This method is called by the startElement() handler.
85                  * It stores the argument of the function of the next
86                  * process to create in the vector of arguments.
87                  *
88                  * @arg                                 The argument to add.
89                  *
90                  */
91                 public void registerProcessArg(String arg){
92                         this.args.add(arg);     
93                 }
94         
95                 public void createProcess(){
96                         try {
97                                 
98                                 System.out.println("Create process " + function + " on the host " + hostName);
99                         Class cls = Class.forName(this.function);
100                         simgrid.msg.Process process = (simgrid.msg.Process)cls.newInstance();
101                         process.name = process.getName(); //this.function;
102                         process.id = simgrid.msg.Process.nextProcessId++;
103                         Host host = Host.getByName(this.hostName);
104                         Msg.processCreate(process,host);
105                         
106                         Vector<String> args = processFactory.args;
107                         int size = args.size();
108                         
109                         for(int index = 0; index < size; index++)
110                                 process.addArg(args.get(index));
111                                 
112                 } catch(JniException e)
113                 {
114                         System.out.println(e.toString());
115                         e.printStackTrace();    
116                  
117                 } catch(NativeException e)
118                 {
119                         System.out.println(e.toString());
120                         e.printStackTrace();
121                 
122                 } catch(HostNotFoundException e) {
123                         System.out.println(e.toString());
124                         e.printStackTrace();
125                         
126                 } catch(ClassNotFoundException e) {
127                         System.out.println(this.function + " 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)");
128                         e.printStackTrace();
129
130                 } catch(InstantiationException e) {
131                          System.out.println("instantiation exception");
132                          e.printStackTrace();
133                         } catch (IllegalAccessException e) {
134                         System.out.println("illegal access exception");
135                         e.printStackTrace();
136                 } catch (IllegalArgumentException e) {  
137                          System.out.println("illegal argument exception");
138                          e.printStackTrace();
139                 }
140
141         }
142         }
143         
144         /* 
145          * the ProcessFactory object used to create the processes.
146          */
147         private ProcessFactory processFactory;
148     
149     public ApplicationHandler() {
150         super();
151     }
152     /**
153      * instanciates the process factory 
154      */
155     public void startDocument(){
156         this.processFactory = new ProcessFactory();     
157     } 
158     
159     public void characters(char[] caracteres, int debut, int longueur) {} // NOTHING TODO
160     
161    /**
162     * element handlers
163     */
164     public void startElement(String nameSpace, String localName,String qName,Attributes attr) {
165         if(localName.equals("process"))
166             onProcessIdentity(attr);   
167                 else if(localName.equals("argument"))
168             onProcessArg(attr);
169     }
170     
171      /**
172      * process attributs handler.
173      */
174     public void onProcessIdentity(Attributes attr) {
175         processFactory.setProcessIdentity(attr.getValue(0),attr.getValue(1));
176     }
177         
178         /**
179      * process arguments handler.
180      */
181     public void onProcessArg(Attributes attr) {
182         processFactory.registerProcessArg(attr.getValue(0));      
183     }
184     
185     /**
186      * creates the process
187      */
188     public void endElement(String nameSpace, String localName,String qName)  {
189         if(localName.equals("process"))
190 {
191                 processFactory.createProcess();
192         }      
193     }
194      
195     public void endDocument() {} // NOTHING TODO
196 }