Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Energy, onHostDestruction: ensured ptr existence
[simgrid.git] / contrib / psg / src / peersim / dynamics / MethodInvoker.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  */
18
19 package peersim.dynamics;
20
21 import java.lang.reflect.*;
22 import peersim.config.*;
23 import peersim.core.*;
24 import java.util.ArrayList;
25
26 /**
27  * This {@link Control} invokes a specified method on a protocol.
28  * The method is defined by config parameter {@value #PAR_METHOD} and
29  * the protocol is by {@value #PAR_PROT}. The method must not have any
30  * parameters and must return void. If no protocol is specified, then the
31  * method will be invoked on all protocol in the protocol stack that define
32  * it.
33  * <p>
34  * Although the method cannot have any parameters, it can of course read
35  * {@link CommonState}. It is guaranteed that the state is up-to-date,
36  * inlcuding eg method {@link CommonState#getNode}.
37  */
38 public class MethodInvoker implements Control, NodeInitializer {
39
40
41 // --------------------------------------------------------------------------
42 // Parameter names
43 // --------------------------------------------------------------------------
44
45 /**
46  * The protocol to operate on.
47  * If not defined, the given method will be invoked on all protocols that
48  * define it. In all cases, at least one protocol has to define it.
49  * @config
50  */
51 private static final String PAR_PROT = "protocol";
52
53 /**
54  * The method to be invoked. It must return void and should not have any
55  * parameters specified.
56  * @config
57  */
58 private static final String PAR_METHOD = "method";
59
60
61 // --------------------------------------------------------------------------
62 // Fields
63 // --------------------------------------------------------------------------
64
65 /** Identifiers of the protocols to be initialized */
66 private final int pid[];
67
68 /** Method name */
69 private final String methodName;
70
71 /** Methods corresponding to the protocols in {@link #pid}. */
72 private final Method method[];
73
74
75 // --------------------------------------------------------------------------
76 // Initialization
77 // --------------------------------------------------------------------------
78
79 /**
80  * Standard constructor that reads the configuration parameters.
81  * Invoked by the simulation engine.
82  * @param prefix the configuration prefix for this class
83  */
84 public MethodInvoker(String prefix) {
85
86         methodName = Configuration.getString(prefix+"."+PAR_METHOD);
87         if(!Configuration.contains(prefix+"."+PAR_PROT))
88         {
89                 // find protocols that implement method
90                 ArrayList<Integer> pids = new ArrayList<Integer>();
91                 ArrayList<Method> methods = new ArrayList<Method>();
92                 for(int i=0; i<Network.prototype.protocolSize(); ++i)
93                 {
94                         Method m = null;
95                         try
96                         {
97                                 m = MethodInvoker.getMethod(
98                                   Network.prototype.getProtocol(i).getClass(),
99                                   methodName );
100                         }
101                         catch(NoSuchMethodException e) {}
102                         
103                         if(m!=null)
104                         {
105                                 pids.add(i);
106                                 methods.add(m);
107                         }
108                 }
109
110                 if( pids.isEmpty() )
111                 {
112                         throw new IllegalParameterException(prefix + "." +
113                         PAR_METHOD,
114                         "No protocols found that implement 'void "+
115                         methodName+"()'");
116                 }
117
118                 pid=new int[pids.size()];
119                 int j=0;
120                 for(int i: pids) pid[j++]=i;
121                 method=methods.toArray(new Method[methods.size()]);
122         }
123         else
124         {
125                 pid = new int[1];
126                 pid[0] = Configuration.getPid(prefix+"."+PAR_PROT);
127                 try
128                 {
129                         method = new Method[1];
130                         method[0]=MethodInvoker.getMethod(
131                           Network.prototype.getProtocol(pid[0]).getClass(),
132                           methodName );
133                 }
134                 catch (NoSuchMethodException e)
135                 {
136                         throw new IllegalParameterException(prefix + "." +
137                         PAR_METHOD, e+"");
138                 }
139         }
140 }
141
142 // --------------------------------------------------------------------------
143 // Methods
144 // --------------------------------------------------------------------------
145
146 private static Method getMethod(Class cl, String methodName)
147 throws NoSuchMethodException {
148
149         Method[] methods = cl.getMethods();
150         ArrayList<Method> list = new ArrayList<Method>();
151         for(Method m: methods)
152         {
153                 if(m.getName().equals(methodName))
154                 {
155                         Class[] pars = m.getParameterTypes();
156                         Class ret = m.getReturnType();
157                         if( pars.length == 0 && ret==void.class )
158                                 list.add(m);
159                 }
160         }
161         
162         if(list.size() == 0)
163         {
164                 throw new NoSuchMethodException("Method "
165                 + methodName + " in class " + cl.getName());
166         }
167         
168         return list.get(0);
169 }
170
171 //--------------------------------------------------------------------------
172
173 /** Invokes method on all the nodes. */
174 public boolean execute() {
175
176         for(int i=0; i<Network.size(); ++i)
177         {
178                 initialize(Network.get(i));
179         }
180
181         return false;
182 }
183
184 //--------------------------------------------------------------------------
185
186 /** Invokes method on given node. */
187 public void initialize(Node n) {
188         
189         try
190         {
191                 for(int i=0; i<pid.length; ++i)
192                 {
193                         CommonState.setNode(n);
194                         CommonState.setPid(pid[i]);
195                         method[i].invoke(n.getProtocol(pid[i]));
196                 }
197         }
198         catch(Exception e)
199         {
200                 e.printStackTrace();
201                 System.exit(1);
202         }
203 }
204 }