Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
37c6e6628569528734d310970d10651ad0e3b340
[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 java.util.Hashtable;
16
17 /**
18  * The handler used to parse the deployment file which contains 
19  * the description of the application (simulation).
20  *
21  * @author  Abdelmalek Cherier
22  * @author  Martin Quinson
23  * @version 1.00, 07/05/01
24  * @see Host
25  * @see Process
26  * @see Simulation
27  * @since SimGrid 3.3
28  * @since JDK1.5011 
29  */
30 public final class ApplicationHandler{
31
32   /* 
33    * This class is used to create the processes descibed in the deployment file.
34    */
35   static class ProcessFactory {
36                 /**
37                  * The vector which contains the arguments of the main function 
38                  * of the process object.
39                  */
40     public Vector args;
41     
42     public Hashtable properties;
43
44                 /**
45                  * The name of the host of the process.
46                  */
47     private String hostName;
48
49                 /**
50                  * The function of the process.
51                  */
52     private String function;
53     
54
55                 /**
56                  * Default constructor.
57                  */
58     public ProcessFactory() {
59       this.args = new Vector();
60       this.properties = new Hashtable();
61       this.hostName = null;
62       this.function = null;
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       if(!properties.isEmpty())
82         properties.clear();
83     }
84                 /**
85                  * This method is called by the startElement() handler.
86                  * It stores the argument of the function of the next
87                  * process to create in the vector of arguments.
88                  *
89                  * @arg                                 The argument to add.
90                  *
91                  */ public void registerProcessArg(String arg) {
92       args.add(arg);
93     }
94     
95     public void setProperty(String id, String value)
96     {
97         properties.put(id,value);       
98     }
99     
100     public String getHostName()
101     {
102         return hostName;
103     }
104
105     public void createProcess() {
106       try {
107
108         Class cls = Class.forName(this.function);
109
110         simgrid.msg.Process process = (simgrid.msg.Process) cls.newInstance();
111         process.name = /*process.getName();*/       this.function;
112         process.id = simgrid.msg.Process.nextProcessId++;
113         Host host = Host.getByName(this.hostName);
114
115         MsgNative.processCreate(process, host);
116         Vector args = processFactory.args;
117         int size = args.size();
118
119         for (int index = 0; index < size; index++)
120           process.args.add(args.get(index));
121           
122         process.properties = this.properties;
123         this.properties = new Hashtable();
124
125       } catch(JniException e) {
126         System.out.println(e.toString());
127         e.printStackTrace();
128
129       } catch(NativeException e) {
130         System.out.println(e.toString());
131         e.printStackTrace();
132
133       } catch(HostNotFoundException e) {
134         System.out.println(e.toString());
135         e.printStackTrace();
136
137       } catch(ClassNotFoundException e) {
138         System.out.println(this.function +
139                            " 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)");
140         e.printStackTrace();
141
142       } catch(InstantiationException e) {
143         System.out.println("instantiation exception");
144         e.printStackTrace();
145       } catch(IllegalAccessException e) {
146         System.out.println("illegal access exception");
147         e.printStackTrace();
148       } catch(IllegalArgumentException e) {
149         System.out.println("illegal argument exception");
150         e.printStackTrace();
151       }
152
153     }
154   }
155
156   /* 
157    * the ProcessFactory object used to create the processes.
158    */
159   public static ProcessFactory processFactory;
160
161   public ApplicationHandler() {
162     super();
163   }
164     /**
165      * instanciates the process factory 
166      */
167   public static void onStartDocument() {
168     processFactory = new ProcessFactory();
169   }
170   
171    public static void onBeginProcess(String hostName, String function)
172    {
173          processFactory.setProcessIdentity(hostName, function);
174 }
175         public static void onProperty(String id, String value) {
176     processFactory.setProperty(id, value);
177   }
178   
179    public static void onProcessArg(String arg) {
180     processFactory.registerProcessArg(arg);
181   }
182   
183   public static void onEndProcess() {
184     
185       processFactory.createProcess();
186   }        
187
188   public static void onEndDocument() {
189   }  // NOTHING TODO
190 }