00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
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
00129 logger.debug("Sending heart-beat");
00130 this.acquaintances.heartBeat();
00131 logger.debug("Heart-beat sent");
00132
00133
00134 if (this.groupOfAcquaintances.size() < NOA) {
00135
00136 logger.debug("NOA is " + NOA +
00137 " - Size of P2PAcquaintanceManager is " +
00138 this.groupOfAcquaintances.size());
00139
00140
00141 this.acquaintances.exploring(TTL, null, this.localService);
00142 logger.debug("Explorating message sent");
00143 }
00144 }
00145
00146
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 }