Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill another out of date script
[simgrid.git] / contrib / psg / src / peersim / dynamics / RandNI.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 the neighbor list of a node with random links.
26  */
27 public class RandNI implements NodeInitializer {
28
29
30 //--------------------------------------------------------------------------
31 //Parameters
32 //--------------------------------------------------------------------------
33
34 /**
35  * The protocol to operate on.
36  * @config
37  */
38 private static final String PAR_PROT = "protocol";
39
40 /**
41  * The number of samples (with replacement) to draw to initialize the
42  * neighbor list of the node.
43  * @config
44  */
45 private static final String PAR_DEGREE = "k";
46
47 /**
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. 
50  * Default to false.
51  * @config
52  */
53 private static final String PAR_PACK = "pack";
54
55 //--------------------------------------------------------------------------
56 //Fields
57 //--------------------------------------------------------------------------
58
59 /**
60  * The protocol we want to wire
61  */
62 private final int pid;
63
64 /**
65  * The degree of the regular graph
66  */
67 private final int k;
68
69 /**
70  * If true, method pack() is invoked on the initialized protocol
71  */
72 private final boolean pack;
73
74
75 //--------------------------------------------------------------------------
76 //Initialization
77 //--------------------------------------------------------------------------
78
79 /**
80  * Standard constructor that reads the configuration parameters. Invoked by the
81  * simulation engine.
82  * @param prefix the configuration prefix for this class
83  */
84 public RandNI(String prefix)
85 {
86         pid = Configuration.getPid(prefix + "." + PAR_PROT);
87         k = Configuration.getInt(prefix + "." + PAR_DEGREE);
88         pack = Configuration.contains(prefix + "." + PAR_PACK);
89 }
90
91 //--------------------------------------------------------------------------
92 //Methods
93 //--------------------------------------------------------------------------
94
95 /**
96  * Takes {@value #PAR_DEGREE} random samples with replacement from the nodes of
97  * the overlay network. No loop edges are added.
98  */
99 public void initialize(Node n)
100 {
101         if (Network.size() == 0) return;
102
103         Linkable linkable = (Linkable) n.getProtocol(pid);
104         for (int j = 0; j < k; ++j)
105         {
106                 int r = CommonState.r.nextInt(Network.size());
107                 linkable.addNeighbor(Network.get(r));
108         }
109
110         if (pack) linkable.pack();
111 }
112
113 }
114