Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Energy, onHostDestruction: ensured ptr existence
[simgrid.git] / contrib / psg / src / peersim / dynamics / StarNI.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  */
18                 
19 package peersim.dynamics;
20
21 import peersim.core.*;
22 import peersim.config.Configuration;
23
24 /**
25  * Initializes a node's neighbor list with a fixed center.
26  */
27 public class StarNI implements NodeInitializer {
28
29
30 // ========================= fields =================================
31 // ==================================================================
32
33
34 /**
35  * The protocol to operate on.
36  * @config
37  */
38 private static final String PAR_PROT = "protocol";
39
40 /**
41  * If this config property is defined, method {@link Linkable#pack()} is 
42  * invoked on the specified protocol at the end of the wiring phase. 
43  * Default to false.
44  * @config
45  */
46 private static final String PAR_PACK = "pack";
47
48 /**
49  * The protocol we want to wire
50  */
51 protected final int pid;
52
53 /** If true, method pack() is invoked on the initialized protocol */
54 protected final boolean pack;
55
56 /**
57  * Used as center: nodes will link to this node.
58  */
59 protected Node center = null;
60
61
62 // ==================== initialization ==============================
63 //===================================================================
64
65
66 public StarNI(String prefix) {
67
68         pid = Configuration.getPid(prefix+"."+PAR_PROT);
69         pack = Configuration.contains(prefix+"."+PAR_PACK);
70 }
71
72
73 // ===================== public methods ==============================
74 // ===================================================================
75
76
77 /**
78  * Adds a link to a fixed node, the center. This fixed node remains the same
79  * throughout consecutive calls to this method. If the center fails in the
80  * meantime, a new one is chosen so care should be taken. The center is the
81  * first node that is not down (starting from node 0, 1, etc)
82  * at the time of the first call to the function. When a new center needs to
83  * be chosen (the current one is down), the new center is again the first
84  * one which is up. If no nodes are up, the node with the last index is set
85  * as center, and selection of a new center is always attempted when this
86  * method is called again.
87  */
88 public void initialize(Node n) {
89         
90         if( Network.size() == 0 ) return;
91         
92         for(int i=0; (center==null || !center.isUp()) && i<Network.size(); ++i)
93                 center=Network.get(i);
94         
95         ((Linkable)n.getProtocol(pid)).addNeighbor(center);
96         
97         if(pack)
98         {
99                 ((Linkable)n.getProtocol(pid)).pack();
100         }
101 }
102
103 }
104