org/objectweb/proactive/p2p/service/P2PAcquaintanceManager.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.p2p.service;
00032 
00033 import java.io.Serializable;
00034 import java.util.Random;
00035 import java.util.Vector;
00036 
00037 import org.apache.log4j.Logger;
00038 import org.objectweb.proactive.ActiveObjectCreationException;
00039 import org.objectweb.proactive.Body;
00040 import org.objectweb.proactive.InitActive;
00041 import org.objectweb.proactive.ProActive;
00042 import org.objectweb.proactive.ProActiveInternalObject;
00043 import org.objectweb.proactive.RunActive;
00044 import org.objectweb.proactive.Service;
00045 import org.objectweb.proactive.core.ProActiveRuntimeException;
00046 import org.objectweb.proactive.core.exceptions.proxy.FailedGroupRendezVousException;
00047 import org.objectweb.proactive.core.group.Group;
00048 import org.objectweb.proactive.core.group.ProActiveGroup;
00049 import org.objectweb.proactive.core.mop.ClassNotReifiableException;
00050 import org.objectweb.proactive.core.node.NodeException;
00051 import org.objectweb.proactive.core.util.log.Loggers;
00052 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00053 import org.objectweb.proactive.core.util.wrapper.BooleanMutableWrapper;
00054 import org.objectweb.proactive.core.util.wrapper.IntMutableWrapper;
00055 import org.objectweb.proactive.p2p.service.util.P2PConstants;
00056 
00057 
00064 public class P2PAcquaintanceManager implements InitActive, RunActive,
00065     Serializable, P2PConstants, ProActiveInternalObject {
00066     private final static Logger logger = ProActiveLogger.getLogger(Loggers.P2P_ACQUAINTANCES);
00067     private P2PService localService = null;
00068     private P2PService acquaintances = null;
00069     private P2PService acquaintancesActived = null;
00070     private Group<P2PService> groupOfAcquaintances = null;
00071     private static final long TTU = Long.parseLong(System.getProperty(
00072                 P2PConstants.PROPERTY_TTU));
00073     private static final int NOA = Integer.parseInt(System.getProperty(
00074                 P2PConstants.PROPERTY_NOA));
00075     private static final int TTL = Integer.parseInt(System.getProperty(
00076                 P2PConstants.PROPERTY_TTL));
00077 
00081     public P2PAcquaintanceManager() {
00082         // empty constructor
00083     }
00084 
00089     public P2PAcquaintanceManager(P2PService localService) {
00090         this.localService = localService;
00091     }
00092 
00096     public void initActivity(Body body) {
00097         String nodeUrl = body.getNodeURL();
00098 
00099         // Create exportAcquaintances group
00100         try {
00101             this.acquaintances = (P2PService) ProActiveGroup.newGroup(P2PService.class.getName());
00102             ProActive.addNFEListenerOnGroup(this.acquaintances,
00103                 FailedGroupRendezVousException.AUTO_GROUP_PURGE);
00104             this.groupOfAcquaintances = ProActiveGroup.getGroup(acquaintances);
00105             this.acquaintancesActived = (P2PService) ProActiveGroup.turnActiveGroup(acquaintances,
00106                     nodeUrl);
00107         } catch (ClassNotReifiableException e) {
00108             logger.fatal("Couldn't create the group of exportAcquaintances", e);
00109         } catch (ClassNotFoundException e) {
00110             logger.fatal("Couldn't create the group of exportAcquaintances", e);
00111         } catch (ActiveObjectCreationException e) {
00112             logger.fatal("Couldn't create the group of exportAcquaintances", e);
00113         } catch (NodeException e) {
00114             logger.fatal("Couldn't create the group of exportAcquaintances", e);
00115         }
00116 
00117         logger.debug("Group of exportAcquaintances succefuly created");
00118     }
00119 
00123     public void runActivity(Body body) {
00124         Service service = new Service(body);
00125 
00126         while (body.isActive()) {
00127             if (this.groupOfAcquaintances.size() > 0) {
00128                 // Register the local P2P service in all exportAcquaintances
00129                 logger.debug("Sending heart-beat");
00130                 this.acquaintances.heartBeat();
00131                 logger.debug("Heart-beat sent");
00132 
00133                 // How many peers ?
00134                 if (this.groupOfAcquaintances.size() < NOA) {
00135                     // Looking for new peers
00136                     logger.debug("NOA is " + NOA +
00137                         " - Size of P2PAcquaintanceManager is " +
00138                         this.groupOfAcquaintances.size());
00139 
00140                     // Sending exploring message
00141                     this.acquaintances.exploring(TTL, null, this.localService);
00142                     logger.debug("Explorating message sent");
00143                 }
00144             }
00145 
00146             // Waiting TTU & serving requests
00147             logger.debug("Waiting for " + TTU + "ms");
00148             long endTime = System.currentTimeMillis() + TTU;
00149             service.blockingServeOldest(TTU);
00150             while (System.currentTimeMillis() < endTime) {
00151                 try {
00152                     service.blockingServeOldest(endTime -
00153                         System.currentTimeMillis());
00154                 } catch (ProActiveRuntimeException e) {
00155                     logger.debug("Certainly because the body is not active", e);
00156                 }
00157             }
00158             logger.debug("End waiting");
00159         }
00160     }
00161 
00165     public P2PService getActiveGroup() {
00166         return this.acquaintancesActived;
00167     }
00168 
00173     public void add(P2PService peer) {
00174         try {
00175             if (!this.groupOfAcquaintances.contains(peer)) {
00176                 String peerUrl = ProActive.getActiveObjectNodeUrl(peer);
00177                 if (!peerUrl.matches(".*cannot contact the body.*")) {
00178                     boolean result = this.groupOfAcquaintances.add(peer);
00179                     logger.info("Acquaintance " + peerUrl + " " + result +
00180                         " added");
00181                 }
00182             }
00183         } catch (Exception e) {
00184             this.groupOfAcquaintances.remove(peer);
00185             logger.debug("Problem while adding peer", e);
00186         }
00187     }
00188 
00189     public void remove(P2PService peer) {
00190         boolean result = this.groupOfAcquaintances.remove(peer);
00191         if (result) {
00192             logger.info("Peer successfully removed");
00193         } else {
00194             logger.debug("Peer not removed");
00195         }
00196     }
00197 
00203     public IntMutableWrapper size() {
00204         return new IntMutableWrapper(this.groupOfAcquaintances.size());
00205     }
00206 
00217     public BooleanMutableWrapper contains(P2PService service) {
00218         return new BooleanMutableWrapper(this.groupOfAcquaintances.contains(
00219                 service));
00220     }
00221     private Random randomizer = new Random();
00222 
00226     public P2PService randomPeer() {
00227         int random = this.randomizer.nextInt(this.groupOfAcquaintances.size());
00228         return this.groupOfAcquaintances.get(random);
00229     }
00230 
00234     public Vector<P2PService> getAcquaintanceList() {
00235         return new Vector<P2PService>(this.groupOfAcquaintances);
00236     }
00237 }

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