7 public class LogThread
extends Thread
{
10 private final Node node
;
11 private final String logFileName
;
12 private boolean keepLogging
= true;
13 private final int SLEEPTIME
= 50;
14 private FileWriter file
= null;
15 private BufferedWriter out
= null;
16 private long startTime
;
19 * Constructs a new object of the LogThread class.
20 * This class writes out lap times of packets that
21 * circle the ring of UDP/TCP nodes.
23 public LogThread(Node node
) {
25 this.logFileName
= "laptimes.log";
27 this.file
= new FileWriter(logFileName
);
28 this.out
= new BufferedWriter(file
);
30 catch (IOException e
) {
31 System
.out
.println(e
);
40 catch (IOException e
) {
41 System
.err
.println(e
);
46 * Sets the startTime fields of this Node.
47 * The startTime field is used by the logging
48 * Node's LogThread to calculate average lap times.
50 * @param long The startTime of the Node.
52 public void setStartTime(long startTime
) {
53 this.startTime
= startTime
;
58 out
.write("Average time for a packet to circle the ring: " +
60 startTime
)/(double)node
.getSentPackets() +
63 catch (IOException e
) {
64 System
.out
.println(e
);
71 out
.write("Average time for a packet to circle the ring: " +
72 (System
.nanoTime() - startTime
)/(double)node
.getSentPackets() + " ms\n");
74 catch (IOException e
) {
75 System
.out
.println(e
);
78 // sleep for a bit before writing
79 // the next average lap time (so not
80 // to fill the log file too much)
81 this.sleep(SLEEPTIME
);
83 catch (InterruptedException e
) {
84 System
.out
.println(e
);
90 catch (IOException e
) {
91 System
.out
.println(e
);