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;
22 import java.io.IOException;
23 import java.io.FileReader;
24 import java.io.LineNumberReader;
25 import java.util.StringTokenizer;
26 import peersim.graph.Graph;
27 import peersim.core.*;
28 import peersim.config.Configuration;
31 * Takes a {@link Linkable} protocol and adds connections that are stored in a
33 * connections are removed, they are only added. So it can be used in
34 * combination with other initializers.
35 * The format of the file is as follows. Each line begins with a node ID
36 * (IDs start from 0) followed by a list of neighbors, separated by whitespace.
37 * All node IDs larger than the actual network size will be discarded, but
38 * it does not trigger an error. Lines starting with a "#" character and
39 * empty lines are ignored.
41 public class WireFromFile extends WireGraph {
44 // ========================= fields =================================
45 // ==================================================================
49 * The filename to load links from.
52 private static final String PAR_FILE = "file";
55 * The number of neighbors to be read from the file. If unset, the default
56 * behavior is to read all links in the file. If set, then the first k
57 * neighbors will be read only.
60 private static final String PAR_K = "k";
62 private final String file;
66 // ==================== initialization ==============================
67 // ==================================================================
71 * Standard constructor that reads the configuration parameters.
72 * Invoked by the simulation engine.
73 * @param prefix the configuration prefix for this class
75 public WireFromFile(String prefix) {
78 file = Configuration.getString(prefix+"."+PAR_FILE);
79 k = Configuration.getInt(prefix + "." + PAR_K, Integer.MAX_VALUE);
83 // ===================== public methods ==============================
84 // ===================================================================
88 * Wires the graph from a file.
89 * The format of the file is as follows. Each line begins with a node ID
90 * (IDs start from 0) followed by a list of neighbors, separated by whitespace.
91 * All node IDs larger than the actual network size will be discarded, but
92 * it does not trigger an error. Lines starting with a "#" character and
93 * empty lines are ignored.
95 public void wire(Graph g) {
98 FileReader fr = new FileReader(file);
99 LineNumberReader lnr = new LineNumberReader(fr);
101 boolean wasOutOfRange=false;
102 while((line=lnr.readLine()) != null)
104 if( line.startsWith("#") ) continue;
105 StringTokenizer st = new StringTokenizer(line);
106 if(!st.hasMoreTokens()) continue;
108 final int from = Integer.parseInt(st.nextToken());
109 if( from < 0 || from >= Network.size() )
111 wasOutOfRange = true;
115 for(int i=0; i<k && st.hasMoreTokens(); ++i)
117 final int to = Integer.parseInt(st.nextToken());
118 if( to < 0 || to >= Network.size() )
119 wasOutOfRange = true;
126 System.err.println("WireFromFile warning: in "+file+" "+
127 "some nodes were out of range and so ignored.");
130 catch( IOException e )
132 throw new RuntimeException(e);