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 the neighbor list of a node with random links.
27 public class RandNI implements NodeInitializer {
30 //--------------------------------------------------------------------------
32 //--------------------------------------------------------------------------
35 * The protocol to operate on.
38 private static final String PAR_PROT = "protocol";
41 * The number of samples (with replacement) to draw to initialize the
42 * neighbor list of the node.
45 private static final String PAR_DEGREE = "k";
48 * If this config property is defined, method {@link Linkable#pack()} is
49 * invoked on the specified protocol at the end of the wiring phase.
53 private static final String PAR_PACK = "pack";
55 //--------------------------------------------------------------------------
57 //--------------------------------------------------------------------------
60 * The protocol we want to wire
62 private final int pid;
65 * The degree of the regular graph
70 * If true, method pack() is invoked on the initialized protocol
72 private final boolean pack;
75 //--------------------------------------------------------------------------
77 //--------------------------------------------------------------------------
80 * Standard constructor that reads the configuration parameters. Invoked by the
82 * @param prefix the configuration prefix for this class
84 public RandNI(String prefix)
86 pid = Configuration.getPid(prefix + "." + PAR_PROT);
87 k = Configuration.getInt(prefix + "." + PAR_DEGREE);
88 pack = Configuration.contains(prefix + "." + PAR_PACK);
91 //--------------------------------------------------------------------------
93 //--------------------------------------------------------------------------
96 * Takes {@value #PAR_DEGREE} random samples with replacement from the nodes of
97 * the overlay network. No loop edges are added.
99 public void initialize(Node n)
101 if (Network.size() == 0) return;
103 Linkable linkable = (Linkable) n.getProtocol(pid);
104 for (int j = 0; j < k; ++j)
106 int r = CommonState.r.nextInt(Network.size());
107 linkable.addNeighbor(Network.get(r));
110 if (pack) linkable.pack();