vet inte
[packetlaptime.git] / Node.java
blob40910aa98e2d341e36b4623233844aed8596a09e
1 /*
2 * @(#)Node.java
3 * Time-stamp: "2007-10-16 13:20:44 spoof"
4 */
6 import java.io.IOException;
7 import java.util.Random;
8 import java.net.InetAddress;
9 import java.net.DatagramPacket;
11 /**
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 const InetAddress nextHost;
22 protected final int nextPort;
23 protected final int ID;
24 protected final long startTime;
26 // keeps track of whether this Node
27 // is a master node (sends the first message)
28 // and/or a logger (logs lap times to a file)
29 protected final boolean isMasterNode;
30 protected final boolean isLogger;
32 // different message types
33 protected final int FINDMASTER = 1;
34 protected final int INITRING = 2;
35 protected final int TERMINATE = 3;
36 // message datagram size
37 protected final int DATAGRAMSIZE = 100;
38 // ID range (from 0 to...)
39 private final int IDRANGE = 8192;
41 // number of packets to send in the ring
42 protected final int PACKETSTOSEND = 500;
43 // needed to calculate lap times
44 protected int sentPackets = 0;
46 /**
47 * Constructs a new object instance of the Node class.
49 public Node(int localPort,
50 InetAddress nextHost,
51 int nextPort,
52 boolean logger) {
54 // set some fields
55 this.ID = 1024 + (new Random().nextInt(IDRANGE));
56 this.localPort = localPort;
57 this.nextHost = nextHost;
58 this.nextPort = nextPort;
59 this.isLogger = logger;
61 // print some information about this node
62 System.out.println("Node listens on port no.: " + localPort +
63 "\nNext host in ring is: " + nextHost +
64 "\nNext host listens on port no.: " + nextPort);
65 // if this node is assigned the task of
66 // logging lap times to a log file
67 if (isLogger) {
68 System.out.println("This node (with ID: " + ID +
69 ") will log this session.");
73 /**
74 * Returns the amount of packets that this Node has sent.
76 * @return int The number of packets send by this Node.
78 public int getSentPackets() {
79 return this.sentPackets;
82 /**
83 * Returns the time when this Node started sending packets.
85 * @return long The number of milli-/nanoseconds that
86 * have passed since this Node entered Phase Two.
88 public long getStartTime() {
89 return this.startTime;
92 /**
93 * Sends a DatagramPacket through this Node's socket.
94 * NOTE: Since the Node class is really just a container
95 * for methods and fields for the subclasses of the Node class,
96 * this methods does nothing but return 'false' because the
97 * method of sending packets differ between UDPNode and TCPNode.
99 * @return boolean Whether or not the sending was successful.
101 public boolean sendPacket(DatagramPacket sendPacket){
102 return false;
106 * Receives a DatagramPacket from this Node's socket.
107 * NOTE: This method does nothing but return 'false',
108 * because: see note at sendPacket(DatagramPacket sendPacket).
110 * @return boolean Whether or not the receiving was successful.
112 public boolean receivePacket(DatagramPacket receivePacket) {
113 return false;
119 * @return DatagramPacket The packet constructed by this method.
121 public DatagramPacket constructPacket(int messageType, String message) {
123 if (message == null) {
124 message = "";
127 byte[] sendBytes = new byte[100];
128 sendBytes[0] = (byte) messageType;
129 sendBytes[1] = message.length() < 98 ? (byte) message.length() : (byte) 98;
131 for (int i=0; i<sendBytes[1]; i++) {
132 sendBytes[2 + i] = (byte) message.charAt(i);
134 return new DatagramPacket(sendBytes,
135 sendBytes.length,
136 nextHost,
137 nextPort);