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
);
36 this.keepLogging
= false;
40 * Sets the startTime fields of this Node.
41 * The startTime field is used by the logging
42 * Node's LogThread to calculate average lap times.
44 * @param long The startTime of the Node.
46 public void setStartTime(long startTime
) {
47 this.startTime
= startTime
;
52 out
.write("Average time for a packet to circle the ring: " +
54 startTime
)/(double)node
.getSentPackets() +
57 catch (IOException e
) {
58 System
.out
.println(e
);
65 out
.write("Average time for a packet to circle the ring: " +
66 (System
.nanoTime() - startTime
)/(double)node
.getSentPackets() + " ms\n");
68 catch (IOException e
) {
69 System
.out
.println(e
);
72 // sleep for a bit before writing
73 // the next average lap time (so not
74 // to fill the log file too much)
75 this.sleep(SLEEPTIME
);
77 catch (InterruptedException e
) {
78 System
.out
.println(e
);
84 catch (IOException e
) {
85 System
.out
.println(e
);