org/objectweb/proactive/loadbalancing/LoadBalancing.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.loadbalancing;
00032 
00033 import java.util.Iterator;
00034 import java.util.ArrayList;
00035 
00036 import org.objectweb.proactive.ActiveObjectCreationException;
00037 import org.objectweb.proactive.ProActive;
00038 import org.objectweb.proactive.core.node.Node;
00039 import org.objectweb.proactive.core.node.NodeException;
00040 import org.objectweb.proactive.loadbalancing.metrics.MetricFactory;
00041 
00042 public class LoadBalancing {
00043         private static ArrayList<LoadBalancer> loadBalancers = new ArrayList<LoadBalancer>();
00044         private static MetricFactory mf;
00045         private static boolean activated = false;
00046         private static InformationRecover ir;
00047         
00048         public static void activateOn(Node[] nodes, MetricFactory mf) {
00049                 LoadBalancing.mf = mf;
00050                 activated = true;
00051                 LoadBalancer lb = null;
00052 
00053                 try {
00054                         ir = (InformationRecover) ProActive.newActive(
00055                                         InformationRecover.class.getName(), null);
00056 
00057                         for (int i = 0; i < nodes.length; i++) {
00058                                 lb = (LoadBalancer) ProActive.newActive(LoadBalancer.class
00059                                                 .getName(), new Object[]{mf}, nodes[i]);
00060                                 loadBalancers.add(lb);
00061 
00062                                 lb = null;
00063                         }
00064 
00065                         Iterator<LoadBalancer> it = loadBalancers.iterator();
00066                         while (it.hasNext()) {
00067                                 lb = it.next();
00068                                 lb.init(loadBalancers,ir);
00069                                 lb = null;
00070 
00071                         }
00072                 } 
00073                 catch (ActiveObjectCreationException e)         {e.printStackTrace();}
00074                 catch (NodeException e)                                         {e.printStackTrace();}
00075 
00076         }
00077         
00078         public static void activate(MetricFactory mf) {
00079                 LoadBalancing.mf = mf;
00080                 activated = true;
00081 
00082                 try {
00083                         ir = (InformationRecover) ProActive.newActive(
00084                                         InformationRecover.class.getName(), null);
00085 
00086                         
00087                 } 
00088                 catch (ActiveObjectCreationException e)         {e.printStackTrace();}
00089                 catch (NodeException e)                                         {e.printStackTrace();}
00090 
00091         }
00092         
00093         public static void kill() {
00094                 activated = false;
00095 
00096                 ProActive.terminateActiveObject(ir,true);
00097                 LoadBalancer lb;
00098                 Iterator<LoadBalancer> it = loadBalancers.iterator();
00099                 while (it.hasNext()) {
00100                         lb = it.next();
00101                         ProActive.terminateActiveObject(lb,true);
00102                 }
00103         }
00104         
00105         public static void addNode(Node node){
00106                 if(! activated)
00107                         return; // TO DO : it's better to throw an exception
00108                 
00109                 try {
00110                         LoadBalancer lb = (LoadBalancer) ProActive.newActive(LoadBalancer.class
00111                                         .getName(), new Object[]{mf}, node);
00112                         loadBalancers.add(lb);
00113                         lb.init(loadBalancers,ir);
00114                         lb.notifyLoadBalancers();
00115                         
00116                 }
00117                 catch (ActiveObjectCreationException e)         {e.printStackTrace();}
00118                 catch (NodeException e)                                         {e.printStackTrace();}
00119                 
00120         }
00121 }

Generated on Mon Jan 22 15:16:11 2007 for ProActive by  doxygen 1.5.1