Skip to content

Commit 7b18259

Browse files
committed
add comments [final Project 1 submission]
1 parent a6afd17 commit 7b18259

File tree

10 files changed

+280
-200
lines changed

10 files changed

+280
-200
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
out/
2+
idea/
3+
*iml
4+
5+
# for docker created files (?)
6+
.bash_history
7+
.viminfo

.idea/workspace.xml

Lines changed: 92 additions & 123 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Dockerfile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#
2+
# Ubuntu Dockerfile
3+
#
4+
# https://github.com/dockerfile/ubuntu
5+
#
6+
7+
# Pull base image.
8+
FROM ubuntu:18.04
9+
10+
# Install.
11+
RUN \
12+
sed -i 's/# \(.*multiverse$\)/\1/g' /etc/apt/sources.list && \
13+
apt-get update && \
14+
apt-get -y upgrade && \
15+
apt-get install -y build-essential && \
16+
apt-get install -y software-properties-common && \
17+
apt-get install -y byobu curl git htop man unzip vim wget && \
18+
apt install -y default-jre && \
19+
apt install -y iputils-ping && \
20+
apt install -y tcpdump && \
21+
apt install -y default-jdk && \
22+
apt install -y iptables && \
23+
rm -rf /var/lib/apt/lists/*
24+
25+
# Set environment variables.
26+
ENV HOME /root
27+
28+
# Define working directory.
29+
WORKDIR /root
30+
31+
# Define default command.
32+
CMD ["bash"]

log.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
docker run -it -v /home/aneeshj/RIT/Sem2/FCN/RIP2:/root/ fcn_docker /bin/bash
2+
3+
docker run -it -v /home/aneeshj/RIT/Sem2/FCN/RIP2:/root/ fcn_docker /bin/bash

src/ArgumentParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class ArgumentParser {
5858
}
5959
}
6060

61+
/**
62+
* Displays the help in case an incorrect argument list is passed
63+
*/
6164
void displayHelp(){
6265
System.out.println("DESCRIPTION :\n Initializes a Rover on a given multicast ip, port and with a given id\n" +
6366
"USAGE :\n " +

src/RIPPacketUtil.java

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ static byte[] getRIPPacket(byte command, byte roverId, Map<InetAddress, RoutingT
3232
int packetOffset = 8;
3333
for (RoutingTableEntry entry : entries.values()) {
3434
// Add IP entry
35-
packetOffset = addIpAddress(ripPacket, packetOffset, entry.ipAddress, entry);
35+
packetOffset = addIpAddress(ripPacket, packetOffset, entry.ipAddress);
3636
packetOffset += 3; // skip the first 3 bytes since they will be
3737
// 0 and the subnet mask will at max be 32
3838

3939
ripPacket[packetOffset] = entry.subnetMask;
4040
packetOffset += 1;
4141

42-
packetOffset = addIpAddress(ripPacket, packetOffset, entry.nextHop, entry);
42+
packetOffset = addIpAddress(ripPacket, packetOffset, entry.nextHop);
4343

4444
packetOffset += 3; // skip the first 3 bytes since they will be
4545
// 0 and the metric will at max be 15
@@ -49,7 +49,15 @@ static byte[] getRIPPacket(byte command, byte roverId, Map<InetAddress, RoutingT
4949
return ripPacket;
5050
}
5151

52-
public static List<RoutingTableEntry> decodeRIPPacket(byte[] packet, int packetLength) throws UnknownHostException{
52+
/**
53+
* Decode the byte representation of a string into a list of routing table entries
54+
*
55+
* @param packet the byte array representing the packet
56+
* @param packetLength the length of the payload in the packet
57+
* @return a list of routing table entries
58+
* @throws UnknownHostException
59+
*/
60+
public static List<RoutingTableEntry> decodeRIPPacket(byte[] packet, int packetLength) throws UnknownHostException {
5361
int totalEntries = (packetLength - 8) / 16;
5462
List<RoutingTableEntry> list = new ArrayList<>();
5563
RoutingTableEntry entry;
@@ -72,8 +80,14 @@ public static List<RoutingTableEntry> decodeRIPPacket(byte[] packet, int packetL
7280
return list;
7381
}
7482

75-
76-
private static InetAddress getIpFromPacket(byte[] packet, int offset) throws UnknownHostException{
83+
/**
84+
* Returns the IP address as extracted from the packet
85+
* @param packet the byte array representing the packet
86+
* @param offset the offset from which to check
87+
* @return the IP starting from the given offset
88+
* @throws UnknownHostException
89+
*/
90+
private static InetAddress getIpFromPacket(byte[] packet, int offset) throws UnknownHostException {
7791
StringBuilder res = new StringBuilder();
7892
for (int i = 0; i < 4; i++) {
7993
res.append(getNextNBytes(packet, offset + i, 1) + (i == 3 ? "" : "."));
@@ -97,15 +111,36 @@ private static String getNextNBytes(byte[] packet, int offset, int N) {
97111
return "" + res;
98112
}
99113

100-
101-
private static int addIpAddress(byte[] ripPacket, int packetOffset, InetAddress ipToAdd, RoutingTableEntry entry) {
114+
/**
115+
* Adds the ip to the packet
116+
* @param ripPacket the rip packet
117+
* @param packetOffset the offset from which to start filling
118+
* @param ipToAdd the ip which has to be added
119+
* @return
120+
*/
121+
private static int addIpAddress(byte[] ripPacket, int packetOffset, InetAddress ipToAdd) {
102122
for (byte ipSubPart : ipToAdd.getAddress()) {
103123
ripPacket[packetOffset] = ipSubPart;
104124
packetOffset += 1;
105125
}
106126
return packetOffset;
107127
}
108128

129+
/**
130+
* Prints the hex dump of the packet
131+
* @param ripByteRepresentation the byte representation of the rip packet
132+
*/
133+
private static void printPacket(byte[] ripByteRepresentation) {
134+
for (int i = 0; i < ripByteRepresentation.length; i += 1) {
135+
System.out.printf("%02x" + ((i + 1) % 4 == 0 ? "\n" : " -- "), ripByteRepresentation[i]);
136+
}
137+
}
138+
139+
/**
140+
* Main function which tests the utility
141+
* @param args arguments passed to the main function
142+
* @throws Exception
143+
*/
109144
public static void main(String[] args) throws Exception {
110145
// Test for RIP packet util
111146
RoutingTableEntry packet = new RoutingTableEntry(InetAddress.getByName("255.255.255.255"),
@@ -117,16 +152,11 @@ public static void main(String[] args) throws Exception {
117152
Map<InetAddress, RoutingTableEntry> routingTableEntries = new HashMap<>();
118153
routingTableEntries.put(InetAddress.getByName("255.255.255.255"), packet);
119154
routingTableEntries.put(InetAddress.getByName("123.221.1.55"), packet1);
120-
byte[] ripByteRepresentation = getRIPPacket((byte) 1, (byte)12, routingTableEntries);
155+
byte[] ripByteRepresentation = getRIPPacket((byte) 1, (byte) 12, routingTableEntries);
121156

122157
printPacket(ripByteRepresentation);
123158

124159
System.out.println(decodeRIPPacket(ripByteRepresentation, ripByteRepresentation.length));
125160
}
126161

127-
private static void printPacket(byte[] ripByteRepresentation) {
128-
for (int i = 0; i < ripByteRepresentation.length; i += 1) {
129-
System.out.printf("%02x" + ((i + 1) % 4 == 0 ? "\n" : " -- "), ripByteRepresentation[i]);
130-
}
131-
}
132162
}

src/RouterDeathTimerTask.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,26 @@
22
import java.net.InetAddress;
33
import java.util.TimerTask;
44

5+
/**
6+
* A timer task for when a Rover goes down
7+
*/
58
public class RouterDeathTimerTask extends TimerTask {
69
InetAddress routerIp;
710
Rover rover;
811

12+
/**
13+
* Constructs a timer task for rover death
14+
* @param rover the Rover object running the timer
15+
* @param routerIp the ip of the rover which is being checked
16+
*/
917
RouterDeathTimerTask(Rover rover, InetAddress routerIp){
1018
this.routerIp = routerIp;
1119
this.rover = rover;
1220
}
1321

22+
/**
23+
* Task which is run on the timer running out
24+
*/
1425
@Override
1526
public void run() {
1627
try {

src/RoutingTableEntry.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,33 @@ public class RoutingTableEntry{
2828
}
2929

3030
/**
31-
* Empty constructor which allows manual setting of member varaibles.
31+
* Empty constructor which allows manual setting of member variables.
3232
*/
3333
RoutingTableEntry(){
3434

3535
}
3636

37+
/**
38+
* Returns a string representation of the routing table entry
39+
* @return string representation of the routing table entry
40+
*/
3741
@Override
3842
public String toString(){
3943

4044
StringBuilder res = new StringBuilder();
41-
res.append("\n");
42-
res.append(ipAddress).append("/").append(subnetMask).append(" \t");
43-
res.append(nextHop + " \t");
45+
res.append(ipAddress.toString().substring(1)).append("/").append(subnetMask).append(" \t");
46+
res.append(nextHop.toString().substring(1) + " \t");
4447
res.append(metric + " \t");
45-
// res.append("IP Address : " + ipAddress + "\t");
46-
// res.append("Subnet Mask : " + subnetMask + "\t");
47-
// res.append("Next Hop : " + nextHop + "\t");
48-
// res.append("Metric : " + metric + "\t");
48+
4949

5050
return res.toString();
5151
}
5252

53+
/**
54+
* Checks whether 2 entries are the same.
55+
* @param otherObject the object to check equality with
56+
* @return true if the other object is equal to this one, false otherwise
57+
*/
5358
@Override
5459
public boolean equals(Object otherObject) {
5560
if(!(otherObject instanceof RoutingTableEntry)){

0 commit comments

Comments
 (0)