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.rangesim;
24 import peersim.config.*;
25 import peersim.core.*;
28 * This OutputStream uses an underlying stream to output
29 * data. Each line (terminated with `\n`) is augmented
30 * with a tag character. This is used to discriminate
31 * among standard error and standard output. This
32 * feature is needed for launching new JVMs; it should
33 * not be used for other purposes.
35 * @author Alberto Montresor
36 * @version $Revision: 1.5 $
38 public class TaggedOutputStream extends PrintStream
41 //--------------------------------------------------------------------------
43 //--------------------------------------------------------------------------
46 * This character is appended at the end of each line.
48 public static final int TAG = 1;
50 //--------------------------------------------------------------------------
52 //--------------------------------------------------------------------------
55 * This parameter contains the string that should be printed on each
56 * line, containing the values of the range parameters for the experiment
57 * which is being run. The full name of this configuration string is
58 * prefixed by {@value peersim.Simulator#PAR_REDIRECT}.
61 public static final String PAR_RANGES = "ranges";
64 * This parameter contains the list of observers for which the string
65 * contained in parameter {@value #PAR_RANGES} should be augmented with
66 * a "TIME <t>" specification regarding current time. Observers are
67 * separated by one of this characters: ' ' - ',' - ';'.
70 public static final String PAR_TIME = "simulation.timed-observers";
73 //--------------------------------------------------------------------------
75 //--------------------------------------------------------------------------
77 /** Variable used to save the original System.out to simplify printing */
78 private PrintStream stdout;
80 /** Buffer used to store a single line; it can grow */
81 private byte[] buffer = new byte[1024];
83 /** Current size of the buffer */
86 /** The value of the PAR_RANGES parameter */
87 private final String ranges;
89 /** The value of the PAR_TIME parameter */
90 private final ArrayList<String> obstime;
92 //--------------------------------------------------------------------------
94 //--------------------------------------------------------------------------
98 * Creates a tagged output stream that prints the tagged
99 * output on the specified stream.
101 public TaggedOutputStream(String prefix)
105 obstime = new ArrayList<String>();
106 String[] obs = Configuration.getString(PAR_TIME, "").split("[ :,]");
107 for (int i=0; i < obs.length; i++) {
108 obstime.add("control." + obs[i]);
110 ranges = Configuration.getString(prefix + "." + PAR_RANGES, "");
115 //--------------------------------------------------------------------------
117 //--------------------------------------------------------------------------
119 // Comment inherited from interface
121 public synchronized void write(byte[] b, int off, int len)
123 if (size+len > buffer.length) {
124 byte[] tmp = new byte[Math.max(buffer.length*2, size+len)];
125 System.arraycopy(buffer, 0, tmp, 0, size);
129 for (int i=off; i < last; i++) {
131 buffer[size++] = TAG;
132 buffer[size++] = b[i];
135 buffer[size++] = b[i];
140 // Comment inherited from interface
142 public synchronized void write(int b)
144 if (size == buffer.length) {
145 byte[] tmp = new byte[buffer.length*2];
146 System.arraycopy(buffer, 0, tmp, 0, size);
150 buffer[size++] = TAG;
151 buffer[size++] = (byte) b;
154 buffer[size++] = (byte) b;
159 * Actually prints a line, inserting ranges and time
162 private void printLine()
164 String line = new String(buffer, 0, size);
165 String[] parts = line.split(":");
166 if (parts.length == 2) {
167 stdout.print(parts[0]);
169 stdout.print(ranges);
170 if (obstime.contains(parts[0]))
171 stdout.print(" TIME " + CommonState.getTime() + " ");
172 stdout.print(parts[1]);