2 * Copyright (c) 2003-2005 The BISON Project
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.
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.
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.
19 package peersim.dynamics;
21 import java.lang.reflect.*;
22 import peersim.config.*;
23 import peersim.core.*;
24 import java.util.ArrayList;
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
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}.
38 public class MethodInvoker implements Control, NodeInitializer {
41 // --------------------------------------------------------------------------
43 // --------------------------------------------------------------------------
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.
51 private static final String PAR_PROT = "protocol";
54 * The method to be invoked. It must return void and should not have any
55 * parameters specified.
58 private static final String PAR_METHOD = "method";
61 // --------------------------------------------------------------------------
63 // --------------------------------------------------------------------------
65 /** Identifiers of the protocols to be initialized */
66 private final int pid[];
69 private final String methodName;
71 /** Methods corresponding to the protocols in {@link #pid}. */
72 private final Method method[];
75 // --------------------------------------------------------------------------
77 // --------------------------------------------------------------------------
80 * Standard constructor that reads the configuration parameters.
81 * Invoked by the simulation engine.
82 * @param prefix the configuration prefix for this class
84 public MethodInvoker(String prefix) {
86 methodName = Configuration.getString(prefix+"."+PAR_METHOD);
87 if(!Configuration.contains(prefix+"."+PAR_PROT))
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)
97 m = MethodInvoker.getMethod(
98 Network.prototype.getProtocol(i).getClass(),
101 catch(NoSuchMethodException e) {}
112 throw new IllegalParameterException(prefix + "." +
114 "No protocols found that implement 'void "+
118 pid=new int[pids.size()];
120 for(int i: pids) pid[j++]=i;
121 method=methods.toArray(new Method[methods.size()]);
126 pid[0] = Configuration.getPid(prefix+"."+PAR_PROT);
129 method = new Method[1];
130 method[0]=MethodInvoker.getMethod(
131 Network.prototype.getProtocol(pid[0]).getClass(),
134 catch (NoSuchMethodException e)
136 throw new IllegalParameterException(prefix + "." +
142 // --------------------------------------------------------------------------
144 // --------------------------------------------------------------------------
146 private static Method getMethod(Class cl, String methodName)
147 throws NoSuchMethodException {
149 Method[] methods = cl.getMethods();
150 ArrayList<Method> list = new ArrayList<Method>();
151 for(Method m: methods)
153 if(m.getName().equals(methodName))
155 Class[] pars = m.getParameterTypes();
156 Class ret = m.getReturnType();
157 if( pars.length == 0 && ret==void.class )
164 throw new NoSuchMethodException("Method "
165 + methodName + " in class " + cl.getName());
171 //--------------------------------------------------------------------------
173 /** Invokes method on all the nodes. */
174 public boolean execute() {
176 for(int i=0; i<Network.size(); ++i)
178 initialize(Network.get(i));
184 //--------------------------------------------------------------------------
186 /** Invokes method on given node. */
187 public void initialize(Node n) {
191 for(int i=0; i<pid.length; ++i)
193 CommonState.setNode(n);
194 CommonState.setPid(pid[i]);
195 method[i].invoke(n.getProtocol(pid[i]));