Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
work around issues by passing the hostname instead of the host to processCreate()
[simgrid.git] / org / simgrid / msg / ApplicationHandler.java
1 /*
2  * These are the upcalls used by the FleXML parser for application files
3  *
4  * Copyright 2006,2007,2010,2011 The SimGrid team.           
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 org.simgrid.msg;
13
14 import java.util.Hashtable;
15 import java.util.Vector;
16
17 public final class ApplicationHandler {
18
19
20                 /**
21                  * The vector which contains the arguments of the main function 
22                  * of the process object.
23                  */
24                 public  static Vector<String> args;
25
26         /**
27          *
28          */
29         public  static Hashtable<String,String> properties;
30
31                 /**
32                  * The name of the host of the process.
33                  */
34                 private  static String hostName;
35
36                 /**
37                  * The function of the process.
38                  */
39                 private  static String function;
40                 
41                 /**
42                  * This method is called by the start element handler.
43                  * It sets the host and the function of the process to create,
44                  * and clear the vector containing the arguments of the 
45                  * previouse process function if needed.
46                  *
47          * @param hostName_
48          * @param function_
49          * @host                                The host of the process to create.
50                  * @function                    The function of the process to create.
51                  *
52                  */
53                 public  static void setProcessIdentity(String hostName_, String function_) {
54                         hostName = hostName_;    
55                         function = function_;
56
57                         if (!args.isEmpty())
58                                 args.clear();
59
60                         if(!properties.isEmpty())
61                                 properties.clear();
62                 }
63                 /**
64                  * This method is called by the startElement() handler.
65                  * It stores the argument of the function of the next
66                  * process to create in the vector of arguments.
67                  *
68          * @param arg
69          * @arg                                 The argument to add.
70                  *
71                  */ public  static void registerProcessArg(String arg) {
72                          args.add(arg);
73                  }
74
75          /**
76           *
77           * @param id
78           * @param value
79           */
80          public  static void setProperty(String id, String value)
81                  {
82                          properties.put(id,value);      
83                  }
84
85          /**
86           *
87           * @return
88           */
89          public  static String getHostName()
90                  {
91                          return hostName;
92                  }
93
94          /**
95           *
96           */
97          @SuppressWarnings("unchecked")
98                  public  static void createProcess() {
99                          try {
100                                  Class<Process> cls = (Class<Process>) Class.forName(function);
101
102                                  Process process = cls.newInstance();
103                                  process.name = function;
104                                  process.id = org.simgrid.msg.Process.nextProcessId++;
105
106                                  MsgNative.processCreate(process, hostName);
107                                  Vector<String> args_ = args;
108                                  int size = args_.size();
109
110                                  for (int index = 0; index < size; index++)
111                                          process.args.add(args_.get(index));
112
113                                  process.properties = properties;
114                                  properties = new Hashtable<String,String>();
115
116                          } catch(HostNotFoundException e) {
117                                  System.out.println(e.toString());
118                                  e.printStackTrace();
119
120                          } catch(ClassNotFoundException e) {
121                                  System.out.println(function +
122                                  " 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)");
123                                  e.printStackTrace();
124
125                          } catch(InstantiationException e) {
126                                  System.out.println("Unable to create the process. I got an instantiation exception");
127                                  e.printStackTrace();
128                          } catch(IllegalAccessException e) {
129                                  System.out.println("Unable to create the process. I got an illegal access exception");
130                                  e.printStackTrace();
131                          } 
132
133                  }
134         
135
136          /**
137           *
138           */
139          public  static void onStartDocument() {
140                         args = new Vector<String>();
141                         properties = new Hashtable<String,String>();
142                         hostName = null;
143                         function = null;
144         }
145
146          /**
147           *
148           * @param hostName
149           * @param function
150           */
151          public  static void onBeginProcess(String hostName, String function) {
152                 setProcessIdentity(hostName, function);
153                 
154         }
155     /**
156      *
157      * @param id
158      * @param value
159      */
160     public  static void onProperty(String id, String value) {
161                 setProperty(id, value);
162         }
163
164     /**
165      *
166      * @param arg
167      */
168     public  static void onProcessArg(String arg) {
169                 registerProcessArg(arg);
170         }
171
172     /**
173      *
174      */
175     public  static void onEndProcess() {
176                 createProcess();
177         }        
178
179     /**
180      *
181      */
182     public static void onEndDocument() {
183         }  
184 }