2 * Copyright (c) 2003-2005 The BISON Project
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.
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.
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.
19 package peersim.dynamics;
21 import peersim.core.*;
22 import peersim.config.Configuration;
25 * Initializes a node's neighbor list with a fixed center.
27 public class StarNI implements NodeInitializer {
30 // ========================= fields =================================
31 // ==================================================================
35 * The protocol to operate on.
38 private static final String PAR_PROT = "protocol";
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.
46 private static final String PAR_PACK = "pack";
49 * The protocol we want to wire
51 protected final int pid;
53 /** If true, method pack() is invoked on the initialized protocol */
54 protected final boolean pack;
57 * Used as center: nodes will link to this node.
59 protected Node center = null;
62 // ==================== initialization ==============================
63 //===================================================================
66 public StarNI(String prefix) {
68 pid = Configuration.getPid(prefix+"."+PAR_PROT);
69 pack = Configuration.contains(prefix+"."+PAR_PACK);
73 // ===================== public methods ==============================
74 // ===================================================================
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.
88 public void initialize(Node n) {
90 if( Network.size() == 0 ) return;
92 for(int i=0; (center==null || !center.isUp()) && i<Network.size(); ++i)
93 center=Network.get(i);
95 ((Linkable)n.getProtocol(pid)).addNeighbor(center);
99 ((Linkable)n.getProtocol(pid)).pack();