3 * Time-stamp: "2007-10-16 15:49:22 anton"
6 import java
.io
.IOException
;
7 import java
.util
.Random
;
8 import java
.net
.InetAddress
;
9 import java
.net
.DatagramPacket
;
12 * Node – a node in a packet ring.
14 * @author "Anton Johansson" <anton.johansson@gmail.com>
15 * @author "Victor Zamanian" <victor.zamanian@gmail.com>
17 public class Node
implements NodeInterface
{
19 // general Node information
20 protected final int localPort
;
21 protected final InetAddress nextHost
;
22 protected final int nextPort
;
23 protected final int ID
;
24 protected LogThread log
;
25 protected long startTime
= 0;
27 // keeps track of whether this Node
28 // is a master node (sends the first message)
29 // and/or a logger (logs lap times to a file)
30 protected boolean isMasterNode
= false;
31 protected boolean isLogger
= false;
33 // different message types
34 protected final int FINDMASTER
= 1;
35 protected final int INITRING
= 2;
36 protected final int TERMINATE
= 3;
37 // message datagram size
38 protected final int DATAGRAMSIZE
= 100;
39 // ID range (from 1024..IDRANGE)
40 private final int IDRANGE
= 8192;
42 // number of packets to send in the ring
43 protected final int PACKETSTOSEND
= 50;
44 // needed to calculate lap times
45 protected int sentPackets
= 0;
48 * Constructs a new object instance of the Node class.
50 public Node(int localPort
,
56 this.ID
= 1024 + (new Random().nextInt(IDRANGE
));
57 this.localPort
= localPort
;
58 this.nextHost
= nextHost
;
59 this.nextPort
= nextPort
;
60 this.isLogger
= logger
;
61 // print some information about this node
62 System
.out
.println("\n---------(ID = " + ID
+ ") -----------------------\n" +
63 "Node listens on port no.: " + localPort
+
64 "\nNext host in ring is: " + nextHost
+
65 "\nNext host listens on port no.: " + nextPort
);
66 // if this node is assigned the task of
67 // logging lap times to a log file
69 this.log
= new LogThread(this);
70 System
.out
.println("This node (with ID: " + ID
+
71 ") will log this session.");
76 * Returns the amount of packets that this Node has sent.
78 * @return int The number of packets send by this Node.
80 public int getSentPackets() {
81 return this.sentPackets
;
85 * Returns the time when this Node started sending packets.
87 * @return long The number of milli-/nanoseconds that
88 * have passed since this Node entered Phase Two.
90 public long getStartTime() {
91 return this.startTime
;
95 * Sends a DatagramPacket through this Node's socket.
96 * NOTE: Since the Node class is really just a container
97 * for methods and fields for the subclasses of the Node class,
98 * this methods does nothing but return 'false' because the
99 * method of sending packets differ between UDPNode and TCPNode.
101 * @return boolean Whether or not the sending was successful.
103 public boolean sendPacket(DatagramPacket sendPacket
){
108 * Receives a DatagramPacket from this Node's socket.
109 * NOTE: This method does nothing but return 'false',
110 * because: see note at sendPacket(DatagramPacket sendPacket).
112 * @return boolean Whether or not the receiving was successful.
114 public boolean receivePacket(DatagramPacket receivePacket
) {
119 * Constructs a DatagramPacket with an InetAddress, a port,
120 * a message type and a message. The method asserts that the
121 * message is not of greater length than the DatagramPacket's
124 * @return DatagramPacket The packet constructed by this method.
126 public DatagramPacket
constructPacket(int messageType
, String message
) {
128 if (message
== null) {
132 byte[] sendBytes
= new byte[DATAGRAMSIZE
];
133 sendBytes
[0] = (byte) messageType
;
134 sendBytes
[1] = message
.length() < (DATAGRAMSIZE
-2) ?
(byte) message
.length() : (byte) (DATAGRAMSIZE
-2);
136 for (int i
=0; i
<sendBytes
[1]; i
++) {
137 sendBytes
[2 + i
] = (byte) message
.charAt(i
);
139 return new DatagramPacket(sendBytes
,