Removed LogThread.java, seemingly working, except nothing is being written to the...
[packetlaptime.git] / Node.java
blobd05839b4694e29c464b2ea59604f6027b5064555
1 /*
2 * @(#)Node.java
3 * Time-stamp: "2007-10-16 17:40:50 spoof"
4 */
6 import java.util.Random;
8 import java.io.FileWriter;
9 import java.io.BufferedWriter;
10 import java.io.IOException;
12 import java.net.InetAddress;
13 import java.net.DatagramPacket;
14 import java.net.SocketException;
16 /**
17 * Node – a node in a packet ring.
19 * @author "Anton Johansson" <anton.johansson@gmail.com>
20 * @author "Victor Zamanian" <victor.zamanian@gmail.com>
22 public class Node implements NodeInterface {
24 // general Node information
25 protected final int localPort;
26 protected final InetAddress nextHost;
27 protected final int nextPort;
28 protected final int ID;
29 protected LogThread log;
30 protected long startTime = 0;
32 // keeps track of whether this Node
33 // is a master node (sends the first message)
34 // and/or a logger (logs lap times to a file)
35 protected boolean isMasterNode = false;
36 protected boolean isLogger = false;
37 protected String logFileName = null;
38 protected FileWriter file = null;
39 protected BufferedWriter out = null;
42 // different message types
43 protected final int FINDMASTER = 1;
44 protected final int INITRING = 2;
45 protected final int TERMINATE = 3;
46 // message datagram size
47 protected final int DATAGRAMSIZE = 100;
48 // ID range (from 1024..IDRANGE)
49 private final int IDRANGE = 8192;
51 // number of packets to send in the ring
52 protected final int PACKETSTOSEND = 50;
53 // needed to calculate lap times
54 protected int sentPackets = 0;
56 /**
57 * Constructs a new object instance of the Node class.
59 public Node(int localPort,
60 InetAddress nextHost,
61 int nextPort,
62 boolean logger) {
64 // set some fields
65 this.ID = 1024 + (new Random().nextInt(IDRANGE));
66 this.localPort = localPort;
67 this.nextHost = nextHost;
68 this.nextPort = nextPort;
69 this.isLogger = logger;
70 // print some information about this node
71 System.out.println("\n---------(ID = " + ID + ") -----------------------\n" +
72 "Node listens on port no.: " + localPort +
73 "\nNext host in ring is: " + nextHost +
74 "\nNext host listens on port no.: " + nextPort);
77 /**
78 * Sends a DatagramPacket through this Node's socket.
79 * NOTE: Since the Node class is really just a container
80 * for methods and fields for the subclasses of the Node class,
81 * this methods does nothing but return 'false' because the
82 * method of sending packets differ between UDPNode and TCPNode.
84 * @return boolean Whether or not the sending was successful.
86 public boolean sendPacket(DatagramPacket sendPacket){
87 return false;
90 /**
91 * Receives a DatagramPacket from this Node's socket.
92 * NOTE: This method does nothing but return 'false',
93 * because: see note at sendPacket(DatagramPacket sendPacket).
95 * @return boolean Whether or not the receiving was successful.
97 public boolean receivePacket(DatagramPacket receivePacket) {
98 return false;
102 * Constructs a DatagramPacket with an InetAddress, a port,
103 * a message type and a message. The method asserts that the
104 * message is not of greater length than the DatagramPacket's
105 * maximum length.
107 * @return DatagramPacket The packet constructed by this method.
109 public DatagramPacket constructPacket(int messageType, String message) {
111 if (message == null) {
112 message = "";
115 byte[] sendBytes = new byte[DATAGRAMSIZE];
116 sendBytes[0] = (byte) messageType;
117 sendBytes[1] = message.length() < (DATAGRAMSIZE-2) ? (byte) message.length() : (byte) (DATAGRAMSIZE-2);
119 for (int i=0; i<sendBytes[1]; i++) {
120 sendBytes[2 + i] = (byte) message.charAt(i);
122 return new DatagramPacket(sendBytes,
123 sendBytes.length,
124 nextHost,
125 nextPort);