Hello i have a problem in my UDP implementation in java. I supose to make the program read two files which is itc-1 and itc-2 but my program only read the itc-1 and function right actully but when i run it and read itc-2 file it crached i don't know where is the problem!! here is my code below:
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class Client {
public static final String MESSAGETONODE = "message to node";
public static final String CHECK = "checking message";
public static final String ACK = "ACK message";
public static final String CHECKMESSAGE = "Are you alive?";
public static final String ACKMESSAGE = "I'm okay";
public static final String DEATHNOTE = "Death note";
public static final String BIRTHCERT = "Birth cert";
public static ArrayList<Node> linkNode = new ArrayList<Node>();
private static int sourceNode;
static int destPort;
static int destNode;
private int neighborCounts;
private static int sourcePort;
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static BlockingQueue<QueueMessage> blockingQueue;
private static ArrayList<BlockingQueue<QueueMessage>> ACKQueue;
private static NetworkLayer networkLayer;
public Client(int nodeID, String itcFile) throws IOException {
blockingQueue = new ArrayBlockingQueue<>(100);
ACKQueue = new ArrayList<>();
assignFromFile(itcFile);
networkLayer = new NetworkLayer(itcFile, nodeID, linkNode);
neighborCounts = networkLayer.getNeighborsCount();
for (int i = 0; i < neighborCounts; ++i) ACKQueue.add(new ArrayBlockingQueue<>(100));
// networkLayer.displayRoutingTable();
System.out.println(nodeID + " " + itcFile);
sourceNode = nodeID;
// Gets the port number of the node you initially set
for (Node n : linkNode) {
if (n.getNodeID() == nodeID)
sourcePort = n.getPortNumber();
}
startServer(sourcePort);
startSender();
messagePrompt(reader, blockingQueue);
for (int i = 0; i < neighborCounts; ++i)
networkLayer.checkNeighbors(blockingQueue, ACKQueue.get(i), i);
}
public void messagePrompt(BufferedReader reader, BlockingQueue<QueueMessage> blockingQueue) {
(new Thread(() -> {
while (true) {
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter message: ");
String message = null;
try {
message = inFromUser.readLine();
} catch (IOException e) {
e.printStackTrace();
}
//prompts for dest node to set for later
System.out.print("Enter destination node: ");
int destNode = 0;
try {
destNode = Integer.parseInt(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
// TODO this is supposed to be wrapped in headers to avoid what I'm doing in this next line
message = MESSAGETONODE + " " + destNode + " " + sourceNode + " " + message;
blockingQueue.add(new QueueMessage(Client.MESSAGETONODE, message, destNode, sourceNode, getTimeInMS()));
}
})).start();
}
public static void assignFromFile(String itcFile) throws FileNotFoundException {
// reads from the file you initially set in the running prompt
Scanner docScan = new Scanner(new File(itcFile + ".txt"));
String data;
// final Node NODE;
// reads each line of the text files
while (docScan.hasNextLine()) {
data = docScan.nextLine();
// System.out.println(data);
String[] word = data.split(" ");
// sets each variable of the documents to a Node
final Node NODE = new Node(Integer.parseInt(word[0]), word[1], Integer.parseInt(word[2]),
Integer.parseInt(word[3]), Integer.parseInt(word[4]), Integer.parseInt(word[5]));
linkNode.add(NODE);
}
}
public static void startSender() {
(new Thread() {
@Override
public void run() {
try {
// Gets input from user
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
// Sets a socket that connects to the port of the nodeID you set in the beginning prompt
// Socket s = new Socket("localhost", sourcePort);
for (Node n : linkNode) {
if (destNode == n.getNodeID())
destPort = n.getPortNumber();
}
DatagramSocket socket = new DatagramSocket();
// System.out.println("PORT = " + socket.getLocalPort());
// linkNode.get(sourceNode).setSenderPortNumber(socket.getPort());
// System.out.println(destPort);
//creates a writer that writes out to the socket that you set
//BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
// gets input from user and sends it to the buffered reader
// System.out.print("Enter message: ");
// String message = inFromUser.readLine();
//prompts for dest node to set for later
// System.out.print("Enter destination node: ");
// int destNode = Integer.parseInt(reader.readLine());
// int destPort = 0;
// for (Node n : linkNode) {
// if (destNode == n.getNodeID()) {
// destPort = n.getPortNumber();
// }
// }
// System.out.println("PORT " + destPort);
DatagramPacket msg = new DatagramPacket(new byte[0], 0, InetAddress.getByName("localhost"), destPort);
// int length;
// message = message + "\n";
// length = message.length();
// byte[] bbuf = message.getBytes();
// msg.setData(bbuf);
// msg.setLength(length);
// try {
// socket.send(msg);
// } catch (IOException ioe) {
// System.err.println("send() failed");
// return;
// }
while (true) {
/// takes the next available message from blockingQueue
QueueMessage queueMessage = blockingQueue.take();
String type = queueMessage.getMessageType();
// System.out.println("TYPE TO BE SENT IS " + type);
// System.out.println(type + " " + queueMessage.getMessage());
// System.out.println("DEBUG");
// for (Node n : linkNode) System.out.println(n.getSenderPortNumber());
// TODO: this is the method that executes the function of transport layer, supposed to
// return bbuf array
// byte[] bbuf = transportLayer.process(queueMessage);
byte[] bbuf;
if (type != null) {
// String message = queueMessage.getMessage();
// System.out.println(message);
int destNode = queueMessage.getDestinationNID();
// System.out.println(destNode);
int destPort = networkLayer.routeMessage(queueMessage);
if (destPort == NetworkLayer.INF) continue;
// don't need the following piece of code since the above is present
// for (Node n : linkNode) {
// if (destNode == n.getNodeID()) {
// destPort = n.getPortNumber();
// }
// }
if (queueMessage.getMessage() == null) break;
// message = message + "\n";
// int length = message.length();
TransportLayer transportLayer = new TransportLayer(queueMessage.getSourceNID(),
networkLayer.getNodePort(queueMessage.getSourceNID()),
queueMessage.getDestinationNID(), destPort, queueMessage.getMessage());
// System.out.println("GET");
// System.out.println("BEFORE " + queueMessage.getMessage());
String message = transportLayer.sendData(queueMessage.getDestinationNID(),
queueMessage.getMessage());
// System.out.println("AFTER " + message);
transportLayer.setMessage(message);
bbuf = message.getBytes();
msg.setData(bbuf);
// System.out.println("HERE IS THE MESSAGE " + queueMessage.getMessage());
msg.setLength(message.length());
msg.setPort(destPort);
try {
socket.send(msg);
} catch (IOException ioe) {
// System.out.println(queueMessage.getMessage());
// System.out.println(queueMessage.getMessage().length());
// System.out.println("DESTPORT" + destPort);
// System.err.println(ioe);
// ioe.printStackTrace();
System.err.println("send() failed");
return;
}
}
// try {
// System.out.print("Enter message: ");
// message = inFromUser.readLine();
//
// //prompts for dest node to set for later
// System.out.print("Enter destination node: ");
// destNode = Integer.parseInt(reader.readLine());
//
// destPort = 0;
//
// for (Node n : linkNode) {
// if (destNode == n.getNodeID()) {
// destPort = n.getPortNumber();
// }
// }
// } catch (IOException ioe) {
// System.err.println("readline() failed");
// }
//
//
// if (message == null) break;
//
// message = message + "\n";
// length = message.length();
// bbuf = message.getBytes();
//
// msg.setData(bbuf);
// msg.setLength(length);
//
// try {
// socket.send(msg);
// } catch (IOException ioe) {
// System.err.println("send() failed");
// return;
// }
}
socket.close();
/*while (true) {
// Will write out your message and wait for input
out.write(message);
out.newLine();
out.flush();
Thread.sleep(200);
}*/
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
public static void startServer(int port) {
// creates a server on the port of the nodeID you set in the beginning prompt
//ss = new ServerSocket(port);
// System.out.println("MESSAGE RECEIVED IS " + str);
// TODO: Implement the function getSenderIDFromMsg that takes the DatagramaPakcet msg as an
// argument and gives back the node ID of the sender
// System.out.println("port = " + msg.getPort());
// System.out.println("ACKMESSAGE");
// System.out.println("STR LENGTH = " + str.length());
// System.out.println("RECEIVED ACKMESSAGE FROM NODE + " + senderNode + " INDEX " + neighborIndex);
// System.out.println("MESSAGE RECEIVED IS " + str);
// System.out.println("RECEIVED DEATHNOTE " + nodeID);
// networkLayer.displayRoutingTable();
// System.out.println("RECEIVED BIRTHCERT " + nodeID);
// networkLayer.displayRoutingTable();
// accepts that socket port and runs the server
//Socket s = ss.accept();
// gets input from the client
/*BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
// Reads the input from the client
line = in.readLine();
//displays the client and the local IP/port for debugging purposes
System.out.println(line);
System.out.println(s.getLocalSocketAddress());
} catch (IOException e) {
e.printStackTrace();
}*/
new Thread() {
@Override
public void run() {
ServerSocket ss;
ServerSocket destSocket;
DatagramSocket dSocket;
int bufSize = 512;
try {
// creates a server on the port of the nodeID you set in the beginning prompt
//ss = new ServerSocket(port);
dSocket = new DatagramSocket(port);
} catch (SocketException se) {
System.err.println("cannot create socket with port: " + port);
return;
}
try {
dSocket.setSoTimeout(15000);
} catch (SocketException se) {
System.err.println("Socekt exception: timeout not set");
}
DatagramPacket msg = new DatagramPacket(new byte[bufSize], bufSize);
while (true) {
try {
msg.setLength(bufSize);
dSocket.receive(msg);
} catch (SocketTimeoutException ste) {
System.err.println("response timed out");
continue;
} catch (IOException ioe) {
System.err.println("Bad receive");
break;
}
String str = new String(msg.getData(), 0, msg.getLength());
// System.out.println("MESSAGE RECEIVED IS " + str);
// System.out.println(str);
TransportLayer transportLayer = new TransportLayer();
transportLayer.setMessage(str);
int checksum = transportLayer.receiveChecksum();
transportLayer.receiveHeaders();
str = transportLayer.getMessage();
// System.out.println("STr IS " + str);
if (str.startsWith(CHECKMESSAGE)) {
// TODO: Implement the function getSenderIDFromMsg that takes the DatagramaPakcet msg as an
// argument and gives back the node ID of the sender
int senderNode = Integer.parseInt(str.substring(CHECKMESSAGE.length() + 1));
blockingQueue.add(new QueueMessage(ACK, ACKMESSAGE + " " + sourceNode, senderNode,
sourceNode, getTimeInMS()));
// System.out.println("port = " + msg.getPort());
} else if (str.startsWith(ACKMESSAGE)) {
// System.out.println("ACKMESSAGE");
// System.out.println("STR LENGTH = " + str.length());
int senderNode = Integer.parseInt(str.substring(ACKMESSAGE.length() + 1));
int neighborIndex = networkLayer.getNeighborIndex(senderNode);
// System.out.println("RECEIVED ACKMESSAGE FROM NODE + " + senderNode + " INDEX " + neighborIndex);
ACKQueue.get(neighborIndex).add(new QueueMessage(ACK, ACKMESSAGE, 0, sourceNode,
getTimeInMS()));
} else {
if (str.startsWith(MESSAGETONODE)) {
Scanner stringstream = new Scanner(str);
String type, actualMessage;
int dest, src;
// System.out.println("MESSAGE RECEIVED IS " + str);
type = stringstream.next();
type = stringstream.next();
type = stringstream.next();
dest = stringstream.nextInt();
src = stringstream.nextInt();
actualMessage = stringstream.next();
if (dest != sourceNode)
blockingQueue.add(new QueueMessage(MESSAGETONODE, str, dest, src, getTimeInMS()));
else {
System.err.println("message from <" + msg.getAddress().getHostAddress() + ","
+ msg.getPort() + ">");
System.out.println(actualMessage);
System.out.println("Checksum received = " + checksum);
}
} else {
Scanner stringstream = new Scanner(str);
// System.out.println("STRING IS " + str);
String type = stringstream.next();
type = stringstream.next();
int nodeID = stringstream.nextInt();
int src = stringstream.nextInt();
int TTL = stringstream.nextInt();
int neighborIndex = networkLayer.getNeighborIndex(src);
if (str.startsWith(DEATHNOTE)) {
System.out.println("Node " + nodeID + " is dead");
// System.out.println("RECEIVED DEATHNOTE " + nodeID);
networkLayer.setNodeDead(nodeID);
// networkLayer.displayRoutingTable();
if (TTL > 1) {
for (int i = 0; i < networkLayer.getNeighborsCount(); ++i)
if (i != neighborIndex)
blockingQueue.add(new QueueMessage(DEATHNOTE, DEATHNOTE + " " +
nodeID + " " + src + " " + Integer.toString(TTL - 1),
networkLayer.getNeighborNID(i), sourceNode, getTimeInMS()));
}
} else {
System.out.println("Node " + nodeID + " is alive");
// System.out.println("RECEIVED BIRTHCERT " + nodeID);
networkLayer.setNodeAlive(nodeID);
// networkLayer.displayRoutingTable();
if (TTL > 1) {
for (int i = 0; i < networkLayer.getNeighborsCount(); ++i)
if (i != neighborIndex)
blockingQueue.add(new QueueMessage(BIRTHCERT, BIRTHCERT + " " +
nodeID + " " + src + " " + Integer.toString(TTL - 1),
networkLayer.getNeighborNID(i), sourceNode, getTimeInMS()));
}
}
}
}
}
// accepts that socket port and runs the server
//Socket s = ss.accept();
// gets input from the client
/*BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
// Reads the input from the client
line = in.readLine();
//displays the client and the local IP/port for debugging purposes
System.out.println(line);
System.out.println(s.getLocalSocketAddress());
} catch (IOException e) {
e.printStackTrace();
}*/
}
}.start();
}
public static long getTimeInMS() {
return System.currentTimeMillis();
}
}
public class IPLayer {
String message;
int neighborOne, neighborTwo;
public IPLayer(String message, int neighborOne, int neighborTwo) {
super();
this.message = message;
this.neighborOne = neighborOne;
this.neighborTwo = neighborTwo;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getNeighborOne() {
return neighborOne;
}
public void setNeighborOne(int neighborOne) {
this.neighborOne = neighborOne;
}
public int getNeighborTwo() {
return neighborTwo;
}
public void setNeighborTwo(int neighborTwo) {
this.neighborTwo = neighborTwo;
}
// Need to rework these to add to a DatagramPacket to create for a UDP application
public void sendData(int destNode, int neighborOne, int neighborTwo, String message) {
message = neighborOne + " " + neighborTwo + " " + message;
System.out.println(message);
}
}
import java.io.*;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
Client client;
if(args.length != 2)
System.out.println("Usage: java Client host port");
else
client = new Client(Integer.parseInt(args[0]), args[1]); // run as java Main (nodeID) (itc-[number])
//java Main 1 itc-1
}
}
public class Neighbor {
private int ID;
private long lastCheckTime;
private boolean isAlive, sentMsg;
public Neighbor(int ID) {
this.ID = ID;
this.lastCheckTime = getTimeInMS();
this.isAlive = true;
this.sentMsg = false;
}
public long getTimeInMS() {
return System.currentTimeMillis();
}
public int getID() {
return ID;
}
public long getLastCheckTime() {
return lastCheckTime;
}
public void setLastCheckTime(long lastCheckTime) {
this.lastCheckTime = lastCheckTime;
}
public boolean getIsAlive() {
return isAlive;
}
public void setIsAlive(boolean isAlive) {
this.isAlive = isAlive;
}
public boolean getSentMsg() {
return sentMsg;
}
public void setSentMsg(boolean sentMsg) {
this.sentMsg = sentMsg;
}
}
import java.io.*;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class NetworkLayer {
private final int threshold = 5000;
private final int waitingTime = 2000;
public static final int INF = (int) 1e9;
private String itcFile;
private int nNodes;
private int nodeID;
private ArrayList<Node> linkNode;
private int[] nextHop;
private int[] shortestPath;
private List<List<Neighbor>> adjList;
private int aliveMsk = 4095;
private int tmpMsk = 4095;
public NetworkLayer(String itcFile, int nodeID, ArrayList<Node> linkNode) throws FileNotFoundException {
this.itcFile = itcFile;
this.nNodes = 0;
this.nodeID = nodeID;
this.linkNode = linkNode;
init();
nextHop = new int[nNodes + 1];
shortestPath = new int[nNodes + 1];
setupRoutingTable();
}
private void init() throws FileNotFoundException {
this.nNodes = 0;
Scanner scanner = new Scanner(new File(itcFile + ".txt"));
while (scanner.hasNextInt()) {
this.nNodes++;
int ID = scanner.nextInt();
String name = scanner.next();
int port = scanner.nextInt();
int n1 = scanner.nextInt();
int n2 = scanner.nextInt();
int mtu = scanner.nextInt();
}
linkNode.add(new Node(0, "", 0, 0, 0, 0));
Collections.sort(linkNode, (lhs, rhs) -> lhs.getNodeID() < rhs.getNodeID() ? -1 : 1);
fillAdjList();
}
private boolean notPresentInAdjList(int x, int y) {
for (Neighbor n : adjList.get(x)) if (n.getID() == y) return false;
return true;
}
private void addToAdjList(int x, int y) {
if (notPresentInAdjList(x, y)) adjList.get(x).add(new Neighbor(y));
}
private void fillAdjList() {
adjList = new ArrayList<>();
for (int i = 0; i < nNodes + 5; ++i) adjList.add(new ArrayList<>());
// System.out.println(adjList.size());
for (Node n : linkNode) {
addToAdjList(n.getNodeID(), n.getNodeOne());
addToAdjList(n.getNodeOne(), n.getNodeID());
addToAdjList(n.getNodeID(), n.getNodeTwo());
addToAdjList(n.getNodeTwo(), n.getNodeID());
}
// printAdjList();
}
private void printAdjList() {
System.out.println("\nSTARTED PRINTING");
for (Neighbor i : adjList.get(nodeID)) System.out.print(i.getID() + " ");
System.out.println("\nENDED PRINTING");
}
public void setupRoutingTable() {
Arrays.fill(nextHop, INF);
Arrays.fill(shortestPath, INF);
dijkstra(nodeID);
// System.out.println("BACK ");
displayRoutingTable();
}
private void dijkstra(int src) {
PriorityQueue<pqNode> pq = new PriorityQueue<>(1, Comparator.comparingInt(pqNode::getCost));
nextHop[src] = src;
shortestPath[src] = 0;
pq.add(new pqNode(0, src));
while (!pq.isEmpty()) {
int top = pq.peek().getCurNode();
pq.poll();
for (Neighbor n : adjList.get(top)) {
int node = n.getID();
if (isNodeAlive(node) && shortestPath[top] + 1 < shortestPath[node]) {
shortestPath[node] = shortestPath[top] + 1;
if (nextHop[top] == src) nextHop[node] = node;
else nextHop[node] = nextHop[top];
pq.add(new pqNode(shortestPath[node], node));
}
}
}
}
public void displayRoutingTable() {
for (int x : shortestPath) System.out.print(x + " ");
System.out.println();
System.out.println("\n----------------\n");
for (int x : nextHop) System.out.print(x + " ");
System.out.println();
System.out.println();
}
public void checkNeighbors(BlockingQueue<QueueMessage> blockingQueue, BlockingQueue<QueueMessage> ACKQueue,
int neighborIndex) {
(new Thread(() -> {
Neighbor neighbor = adjList.get(nodeID).get(neighborIndex);
while (true) {
// boolean changes = false;
long curTime = getTimeInMS();
if (neighbor.getSentMsg()) {
if (curTime - neighbor.getLastCheckTime() > waitingTime) {
neighbor.setSentMsg(false);
// sentMsg1 = false;
if (neighbor.getIsAlive()) {
System.out.println("Node " + neighbor.getID() + " is dead");
neighbor.setIsAlive(false);
// changes = true;
setNodeDead(neighbor.getID());
// eg: Death note 2(node ID) 4(TTL)
for (Neighbor n : adjList.get(nodeID))
if (neighbor.getID() != n.getID()) {
blockingQueue.add(new QueueMessage(Client.DEATHNOTE, Client.DEATHNOTE + " "
+ neighbor.getID() + " " + nodeID + " 4", n.getID(), nodeID,
getTimeInMS()));
}
}
} else {
try {
QueueMessage message = ACKQueue.poll(100, TimeUnit.MILLISECONDS);
if (message != null) {
if (message.getOriginatingTime() < neighbor.getLastCheckTime()) continue;
neighbor.setSentMsg(false);
if (!neighbor.getIsAlive()) {
System.out.println("Node " + neighbor.getID() + " is alive");
neighbor.setIsAlive(true);
// changes = true;
setNodeAlive(neighbor.getID());
// eg: Birth cert 2(node ID) 4(TTL)
for (Neighbor n : adjList.get(nodeID))
if (neighbor.getID() != n.getID()) {
blockingQueue.add(new QueueMessage(Client.BIRTHCERT,
Client.BIRTHCERT + " " + neighbor.getID() + " " + nodeID
+ " 4", n.getID(), nodeID, getTimeInMS()));
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} else {
if (curTime - neighbor.getLastCheckTime() > threshold) {
neighbor.setSentMsg(true);
neighbor.setLastCheckTime(curTime);
blockingQueue.add(new QueueMessage(Client.CHECK, Client.CHECKMESSAGE + " " + nodeID,
neighbor.getID(), nodeID, getTimeInMS()));
}
}
}
})).start();
}
public void setNodeAlive(int NID) {
this.tmpMsk |= (1 << NID);
}
public void setNodeDead(int NID) {
this.tmpMsk &= ~(1 << NID);
}
public int getNeighborNID(int neighbor) {
return adjList.get(nodeID).get(neighbor).getID();
}
public long getTimeInMS() {
return System.currentTimeMillis();
}
public boolean isNodeAlive(int NID) {
return ((tmpMsk >>> NID) & 1) == 1;
}
public int routeMessage(QueueMessage queueMessage) {
if (aliveMsk != tmpMsk) {
// System.out.println("ALIVE MSK " + aliveMsk);
// System.out.println("TMP MSK " + tmpMsk);
aliveMsk = tmpMsk;
setupRoutingTable();
}
int destNode = queueMessage.getDestinationNID();
if (queueMessage.getMessageType() == Client.MESSAGETONODE) {
if (nextHop[destNode] == INF) return INF;
return getPort(nextHop[destNode]);
}
// System.out.println(destNode + " " + getPort(destNode));
return getPort(destNode);
}
private int getPort(int i) {
return linkNode.get(i).getPortNumber();
}
public int getNeighborsCount() {
return adjList.get(nodeID).size();
}
public int getNeighborIndex(int senderNode) {
for (int i = 0; i < getNeighborsCount(); i++)
if (adjList.get(nodeID).get(i).getID() == senderNode) return i;
return 0;
}
public int getNodePort(int sourceNID) {
return linkNode.get(sourceNID).getPortNumber();
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Node {
private String hostName;
private int nodeID, portNumber, nodeOne, nodeTwo, mtuBytes, senderPortNumber;
public Node() {
}
public Node(int nodeID, String hostName, int portNumber, int nodeOne, int nodeTwo, int mtuBytes) {
super();
this.nodeID = nodeID;
this.hostName = hostName;
this.portNumber = portNumber;
this.nodeOne = nodeOne;
this.nodeTwo = nodeTwo;
this.mtuBytes = mtuBytes;
}
public int getNodeID() {
return nodeID;
}
public void setNodeID(int nodeID) {
this.nodeID = nodeID;
}
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public int getPortNumber() {
return portNumber;
}
public void setPortNumber(int portNumber) {
this.portNumber = portNumber;
}
public int getNodeOne() {
return nodeOne;
}
public void setNodeOne(int nodeOne) {
this.nodeOne = nodeOne;
}
public int getNodeTwo() {
return nodeTwo;
}
public void setNodeTwo(int nodeTwo) {
this.nodeTwo = nodeTwo;
}
public int getMtuBytes() {
return mtuBytes;
}
public void setMtuBytes(int mtuBytes) {
this.mtuBytes = mtuBytes;
}
@Override
public String toString() {
return "ReadData [nodeID=" + nodeID + ", hostName=" + hostName + ", portNumber=" + portNumber + ", nodeOne=" + nodeOne
+ ", nodeTwo=" + nodeTwo + ", mtuBytes=" + mtuBytes + "]";
}
}
public class pqNode {
private int cost;
private int curNode;
public pqNode(int _cost, int _curNode) {
cost = _cost;
curNode = _curNode;
}
public int getCost() {
return this.cost;
}
public int getCurNode() {
return this.curNode;
}
}
public class QueueMessage {
private String messageType, message;
private int destinationNID, sourceNID;
private long originatingTime;
public QueueMessage(String messageType, String message, int destinationNID, int sourceNID, long originatingTime) {
this.messageType = messageType;
this.message = message;
this.destinationNID = destinationNID;
this.sourceNID = sourceNID;
this.originatingTime = originatingTime;
}
public String getMessageType() {
return messageType;
}
public String getMessage() {
return message;
}
public int getDestinationNID() {
return destinationNID;
}
public int getSourceNID() {
return sourceNID;
}
public long getOriginatingTime() {
return originatingTime;
}
}
import java.io.*;
import java.net.InetAddress;
import java.util.Scanner;
import java.util.zip.Adler32;
public class TransportLayer {
int destPort, sourcePort, sourceNode, destNode, transmissionBytes;
String message;
public TransportLayer() {
}
public TransportLayer(int sourceNode, int sourcePort, int destNode, int destPort, String message) {
super();
this.sourceNode = sourceNode;
this.sourcePort = sourcePort;
this.destNode = destNode;
this.destPort = destPort;
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
private String createHeader(String message) throws FileNotFoundException {
byte srcPort;
byte dPort;
byte[] ports = new byte[2];
srcPort = (byte) sourcePort;
dPort = (byte) destPort;
ports[0] = srcPort;
ports[1] = dPort;
message = ports[0] + " " + ports[1] + " " + message;
// calls the method to create the checksum
message = createChecksum(message);
return message;
}
// Need to rework these to add to a DatagramPacket to create for a UDP application
public String sendData(int destNode, String message) throws IOException {
// Create header for transport layer: sourceport, destport, length, checksum, message
int length;
length = message.length();
return createHeader(message);
}
public String createChecksum(String message) throws FileNotFoundException {
int maxSize = 0;
int length = message.length();
// set the maximum size of the data with the MTU's from the file
for (Node node : Client.linkNode) {
if (sourceNode == node.getNodeID()) {
maxSize = node.getMtuBytes();
transmissionBytes = maxSize;
}
}
// array to hold the max size for the data being sent
int data[] = new int[maxSize];
// array to hold the max size for the compliment of the data
int complimentData[] = new int[maxSize];
int numBits = 0;
int checkSum = 0, i;
// creating the checksum for the length of data in the message
for (i = 0; i < length; i++) {
numBits = (int) (Math.floor(Math.log(data[i]) / Math.log(2))) + 1;
// compliments the checksum
complimentData[i] = ((1 << numBits) - 1) ^ data[i];
// adding the complimented data
checkSum += complimentData[i];
}
// creates the sum of the checksum
data[i] = checkSum;
length += 1;
// adds the checksum to the "header"
message = checkSum + " " + message;
// NetworkLayer nl = new NetworkLayer(sourceNode, destNode, transmissionBytes, message);
// nl.routingTable();
return message;
}
public int receiveChecksum() {
Scanner stringstream = new Scanner(message);
int checksum = stringstream.nextInt();
String tmp = "";
boolean b = false;
while (stringstream.hasNext()) {
if (b) tmp += " ";
tmp += stringstream.next();
b = true;
}
message = tmp;
// System.out.println(message);
return checksum;
}
public byte[] receiveHeaders() {
byte[] ports = new byte[2];
Scanner stringstream = new Scanner(message);
for (int i = 0; i < 2; ++i) ports[i] = (byte) stringstream.nextInt();
String tmp = "";
boolean b = false;
while (stringstream.hasNext()) {
if (b) tmp += " ";
tmp += stringstream.next();
b = true;
}
message = tmp;
return ports;
}
}
and here is the two files:
itc-1
1 u-02.sm201.iuk.edu 17870 2 4 600
2 u-03.sm201.iuk.edu 17871 1 3 1000
3 u-03.sm201.iuk.edu 17872 2 4 500
4 u-05.sm201.iuk.edu 17873 1 3 700
itc-2
1 u-01.sm201.iuk.edu 17870 2 3 500
2 u-02.sm201.iuk.edu 17871 1 3 1500
3 u-03.sm201.iuk.edu 17872 1 2 300
4 u-04.sm201.iuk.edu 17873 1 5 1000
5 u-05.sm201.iuk.edu 17874 1 4 500
6 u-06.sm201.iuk.edu 17875 2 7 700
7 u-07.sm201.iuk.edu 17876 2 6 500
8 u-08.sm201.iuk.edu 17877 3 9 500
9 u-09.sm201.iuk.edu 17878 3 8 400
as i said the program read itc-1 and function right the way that i want but it is not reading itc-2 can someone please help me fix my problem?!