org/objectweb/proactive/core/body/ft/servers/resource/ResourceServerImpl.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.core.body.ft.servers.resource;
00032 
00033 import java.rmi.RemoteException;
00034 import java.util.ArrayList;
00035 import java.util.Vector;
00036 
00037 import org.apache.log4j.Logger;
00038 import org.objectweb.proactive.core.ProActiveException;
00039 import org.objectweb.proactive.core.body.ft.servers.FTServer;
00040 import org.objectweb.proactive.core.node.Node;
00041 import org.objectweb.proactive.core.node.NodeException;
00042 import org.objectweb.proactive.core.util.log.Loggers;
00043 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00044 import org.objectweb.proactive.p2p.service.P2PService;
00045 import org.objectweb.proactive.p2p.service.StartP2PService;
00046 import org.objectweb.proactive.p2p.service.node.P2PNodeLookup;
00047 import org.objectweb.proactive.p2p.service.util.P2PConstants;
00048 
00049 
00054 public class ResourceServerImpl implements ResourceServer {
00055     //logger
00056     protected static Logger logger = ProActiveLogger.getLogger(Loggers.FAULT_TOLERANCE);
00057 
00058     // global server
00059     private FTServer server;
00060 
00061     // list of free ProActiveRuntime
00062     private ArrayList freeNodes;
00063 
00064     // OR use p2p infracstructure
00065     private P2PService serviceP2P;
00066 
00067     // number of returned free nodes
00068     private int nodeCounter;
00069 
00070     public ResourceServerImpl(FTServer server) {
00071         this.server = server;
00072         this.freeNodes = new ArrayList();
00073         this.nodeCounter = 0;
00074     }
00075 
00076     public ResourceServerImpl(FTServer server, String p2pServerURL) {
00077         this(server);
00078         try {
00079             Vector v = new Vector(1);
00080             v.add(p2pServerURL);
00081             StartP2PService startServiceP2P = new StartP2PService(v);
00082             System.setProperty(P2PConstants.PROPERTY_PORT, "2603");
00083             startServiceP2P.start();
00084             this.serviceP2P = startServiceP2P.getP2PService();
00085             logger.info("[RESOURCE] Running on p2p network");
00086         } catch (ProActiveException e) {
00087             logger.error("**ERROR** Unable to reach p2p network");
00088             e.printStackTrace();
00089         }
00090     }
00091 
00095     public void addFreeNode(Node n) throws RemoteException {
00096         logger.info("[RESSOURCE] A node is added : " +
00097             n.getNodeInformation().getURL());
00098         this.freeNodes.add(n);
00099     }
00100 
00104     public Node getFreeNode() throws RemoteException {
00105         this.nodeCounter++;
00106         Node n = null;
00107         if (this.freeNodes.isEmpty()) {
00108             // use p2p service if any
00109             if (this.serviceP2P != null) {
00110                 P2PNodeLookup p2pNodeLookup = this.serviceP2P.getNodes(1, "FT",
00111                         "1"); // SET JOB-ID
00112                 n = (Node) ((p2pNodeLookup.getNodes(30000)).firstElement());
00113             } else {
00114                 logger.error(
00115                     "[RESSOURCE] **ERROR** There is no resource nodes !");
00116                 return null;
00117             }
00118         } else {
00119             n = (Node) (this.freeNodes.get(nodeCounter % (this.freeNodes.size())));
00120         }
00121         try {
00122             // testing free node
00123             n.getNumberOfActiveObjects();
00124         } catch (NodeException e) {
00125             // free node is unreachable !
00126             logger.info("[RESSOURCE] An unreachable node is removed.");
00127             this.freeNodes.remove(n);
00128             this.nodeCounter = 0;
00129             n = getFreeNode();
00130         }
00131         logger.info("[RESSOURCE] Return a node : " +
00132             n.getNodeInformation().getURL());
00133         return n;
00134     }
00135 
00139     public void initialize() throws RemoteException {
00140         this.freeNodes = new ArrayList();
00141         this.nodeCounter = 0;
00142     }
00143 }

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