org/objectweb/proactive/p2p/service/P2PFirstContact.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.Vector;
00035 
00036 import org.apache.log4j.Logger;
00037 import org.objectweb.proactive.Body;
00038 import org.objectweb.proactive.ProActiveInternalObject;
00039 import org.objectweb.proactive.RunActive;
00040 import org.objectweb.proactive.core.node.Node;
00041 import org.objectweb.proactive.core.node.NodeFactory;
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.util.P2PConstants;
00045 
00046 
00055 public class P2PFirstContact implements Serializable, RunActive, P2PConstants,
00056     ProActiveInternalObject {
00057     private static final Logger logger = ProActiveLogger.getLogger(Loggers.P2P_FIRST_CONTACT);
00058     private Vector peers;
00059     private P2PAcquaintanceManager acqGroup;
00060     private P2PService localP2pService;
00061 
00065     public P2PFirstContact() {
00066         // The empty constructor
00067     }
00068 
00075     public P2PFirstContact(Vector peers, P2PAcquaintanceManager acquaintances,
00076         P2PService local) {
00077         this.peers = peers;
00078         this.acqGroup = acquaintances;
00079         this.localP2pService = local;
00080     }
00081 
00087     public void runActivity(Body body) {
00088         this.peers = StartP2PService.checkingPeersUrl(this.peers);
00089         // First coontact
00090         connectingPeer();
00091         while (body.isAlive()) {
00092             if (this.peers.size() != 0) {
00093                 // We have some url and we know nobody
00094                 connectingPeer();
00095             }
00096             try {
00097                 Thread.sleep(Long.parseLong(System.getProperty(PROPERTY_TTU)));
00098             } catch (Exception e) {
00099             }
00100         }
00101     }
00102 
00106     private void connectingPeer() {
00107         for (int index = 0; index < this.peers.size(); index++) {
00108             String peerUrl = urlAdderP2PNodeName((String) this.peers.get(index));
00109             try {
00110                 Node distNode = NodeFactory.getNode(peerUrl);
00111                 P2PService peer = (P2PService) distNode.getActiveObjects(P2PService.class.getName())[0];
00112 
00113                 if (!peer.equals(this.localP2pService) &&
00114                         !this.acqGroup.contains(peer).booleanValue()) {
00115                     // Send a message to the remote peer to record me
00116                     peer.register(this.localP2pService);
00117                     // Add the peer in my group of acquaintances
00118                     this.acqGroup.add(peer);
00119                 }
00120             } catch (Exception e) {
00121                 logger.debug("The peer at " + peerUrl +
00122                     " couldn't be contacted", e);
00123             }
00124         }
00125     }
00126 
00132     private static String urlAdderP2PNodeName(String url) {
00133         if (url.endsWith("/")) {
00134             url += P2P_NODE_NAME;
00135         } else {
00136             url += ("/" + P2P_NODE_NAME);
00137         }
00138 
00139         return url;
00140     }
00141 }

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