Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a5c13c01f23804c65faf1762e514f62341177bdf
[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
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 extends DefaultHandler
32 {
33         
34         /* 
35          * This class is used to create the processes descibed in the deployment file.
36          */
37         class ProcessFactory
38         {
39                 /**
40                  * The vector which contains the arguments of the main function 
41                  * of the process object.
42                  */
43                 public Vector args;
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                  * Default constructor.
57                  */
58                 public ProcessFactory(){
59                         this.args = new Vector();
60                         this.hostName = null;
61                         this.function = null;
62                 }
63                 
64                 /**
65                  * This method is called by the start element handler.
66                  * It sets the host and the function of the process to create,
67                  * and clear the vector containing the arguments of the 
68                  * previouse process function if needed.
69                  *
70                  * @host                                The host of the process to create.
71                  * @function                    The function of the process to create.
72                  *
73                  */
74                 public void setProcessIdentity(String hostName, String function){
75                         this.hostName = hostName;
76                         this.function = function;
77                 
78                         if(!args.isEmpty())
79                                 args.clear();
80                 }
81                 
82                 /**
83                  * This method is called by the startElement() handler.
84                  * It stores the argument of the function of the next
85                  * process to create in the vector of arguments.
86                  *
87                  * @arg                                 The argument to add.
88                  *
89                  */
90                 public void registerProcessArg(String arg){
91                         this.args.add(arg);     
92                 }
93         
94                 public void createProcess(){
95                         try {
96                                 
97                                 System.out.println("Create process " + function + " on the host " + hostName);
98                         Class cls = Class.forName(this.function);
99                         simgrid.msg.Process process = (simgrid.msg.Process)cls.newInstance();
100                         process.name = process.getName(); //this.function;
101                         process.id = simgrid.msg.Process.nextProcessId++;
102                         Host host = Host.getByName(this.hostName);
103                         Msg.processCreate(process,host);
104                         
105                         Vector args = processFactory.args;
106                         int size = args.size();
107                         
108                         for(int index = 0; index < size; index++)
109                                 process.args.add(args.get(index));
110                                 
111                 } catch(JniException e)
112                 {
113                         System.out.println(e.toString());
114                         e.printStackTrace();    
115                  
116                 } catch(NativeException e)
117                 {
118                         System.out.println(e.toString());
119                         e.printStackTrace();
120                 
121                 } catch(HostNotFoundException e) {
122                         System.out.println(e.toString());
123                         e.printStackTrace();
124                         
125                 } catch(ClassNotFoundException e) {
126                         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)");
127                         e.printStackTrace();
128
129                 } catch(InstantiationException e) {
130                          System.out.println("instantiation exception");
131                          e.printStackTrace();
132                         } catch (IllegalAccessException e) {
133                         System.out.println("illegal access exception");
134                         e.printStackTrace();
135                 } catch (IllegalArgumentException e) {  
136                          System.out.println("illegal argument exception");
137                          e.printStackTrace();
138                 }
139
140         }
141         }
142         
143         /* 
144          * the ProcessFactory object used to create the processes.
145          */
146         private ProcessFactory processFactory;
147     
148     public ApplicationHandler() {
149         super();
150     }
151     /**
152      * instanciates the process factory 
153      */
154     public void startDocument(){
155         this.processFactory = new ProcessFactory();     
156     } 
157     
158     public void characters(char[] caracteres, int debut, int longueur) {} // NOTHING TODO
159     
160    /**
161     * element handlers
162     */
163     public void startElement(String nameSpace, String localName,String qName,Attributes attr) {
164         if(localName.equals("process"))
165             onProcessIdentity(attr);   
166                 else if(localName.equals("argument"))
167             onProcessArg(attr);
168     }
169     
170      /**
171      * process attributs handler.
172      */
173     public void onProcessIdentity(Attributes attr) {
174         processFactory.setProcessIdentity(attr.getValue(0),attr.getValue(1));
175     }
176         
177         /**
178      * process arguments handler.
179      */
180     public void onProcessArg(Attributes attr) {
181         processFactory.registerProcessArg(attr.getValue(0));      
182     }
183     
184     /**
185      * creates the process
186      */
187     public void endElement(String nameSpace, String localName,String qName)  {
188         if(localName.equals("process"))
189 {
190                 processFactory.createProcess();
191         }      
192     }
193      
194     public void endDocument() {} // NOTHING TODO
195 }