From fa616645f414e2491ff939baf5a116c37439224b Mon Sep 17 00:00:00 2001 From: Anton Johansson Date: Mon, 22 Oct 2007 19:31:44 +0200 Subject: [PATCH] copy paste seems like everything works? Have to check though --- Node.java | 30 +++++++++--- TCPNode.java | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 171 insertions(+), 16 deletions(-) diff --git a/Node.java b/Node.java index 9872bb4..d04a154 100644 --- a/Node.java +++ b/Node.java @@ -1,6 +1,6 @@ /* * @(#)Node.java - * Time-stamp: "2007-10-18 15:58:45 spoof" + * Time-stamp: "2007-10-22 19:10:41 anton" */ import java.util.Random; @@ -99,16 +99,16 @@ public abstract class Node implements NodeInterface { */ public abstract boolean receivePacket(DatagramPacket receivePacket); + /** - * Constructs a DatagramPacket with an InetAddress, a port, - * a message type and a message. The method asserts that the - * message is not of greater length than the DatagramPacket's - * maximum length. + * Constructs a byte-array with first the messageType in the first + * byte and the message in following * - * @return DatagramPacket The packet constructed by this method. + * @param messageType the type of the message + * @param message the message + * @return a byte-array first byte is messageType rest is message */ - public DatagramPacket constructPacket(int messageType, String message) { - + public byte[] constructByteArray(int messageType, String message) { if (message == null) { message = ""; } @@ -120,12 +120,26 @@ public abstract class Node implements NodeInterface { for (int i=0; i " + messageType); + + // acquire the message's length + int messageLength = receivedData[1]; + System.out.println("MessageLength ->" + messageLength); + + // acquire the message... + String message = + new String(receivedData).substring(2, messageLength + 2); + System.out.println("message -> " + message + " @ port:"+localPort); + + // the largest ID that this packet has seen so far, i.e. the + // ID of the node with the largest ID so far in the ring. + int receivedID = Integer.parseInt(message); + + // keeping track of which phase we're in + int phase = FINDMASTER; + switch (messageType) { + // if messageType == 1 + case FINDMASTER: + + // if we left phase 1 but messages + // are still circling the ring: + if (phase != FINDMASTER) { + // don't do anything. + break; + } + // if this node will surely not be the master node + if (receivedID > ID) { + System.out.println("receivedID > ID" + " @ port:"+localPort); + + nextNodeOutput.write(receivedData); + } + // if this node has a larger ID than the one of the sending node + else if (receivedID < ID) { + // Send own ID again since it could have been lost + System.out.println("receivedID < ID" + " @ port:"+localPort); + //sendPacket(constructPacket(FINDMASTER, new Integer(ID).toString())); + nextNodeOutput.write(constructByteArray(FINDMASTER, new Integer(ID).toString())); + } + // if this node should be the master node + else if (receivedID == ID) { + phase = INITRING; + isMasterNode = true; + System.out.println("receivedID == ID"); + System.out.println("I am master, ID:"+ID+" Port:"+localPort); + + // if this node is a logging node, + if (isLogger) { + // set the starting time -- the time + // when phase two was initiated + startTime = System.nanoTime(); + + // increase the number of sent packets. + sentPackets++; + } + // send first packet in phase two + //sendPacket(constructPacket(INITRING, new Integer(ID).toString())); + nextNodeOutput.write(constructByteArray(INITRING, new Integer(ID).toString())); + } + break; + // if messageType == 2 + case INITRING: + // make sure everybody knows we're in the second phase + phase = INITRING; + + // if this node is a logger and the start time hasn't been set + if (isLogger && startTime == 0) { + // set the start time + startTime = System.nanoTime(); + } + // if this node is a logger: + if (isLogger) { + // increase the number of sent packets + sentPackets++; + // write to file + writeToLog(); + // if we've sent as many packets as we were to (and the + // number of packets to send is not zero (infinitely many)): + if (sentPackets == PACKETSTOSEND && PACKETSTOSEND != 0) { + // send the terminating packet to the next node + // (and, indirectly so, to all other nodes) + //sendPacket(constructPacket(TERMINATE, "3")); + nextNodeOutput.write(constructByteArray(TERMINATE, "3")); + break; + } + } + // if this node is the master node, make it print out + // what the logging node is writing to the log file, so + // that we can actally see things happening as well. + // NOTE: UNCOMMENT WHEN ALL DEBUGGING PRINTOUTS HAVE BEEN REMOVED + // SO THAT THE DEGUGGING PRINTOUTS AREN'T LOST COMPLETELY... + // if (isMasterNode) { + // System.out.println("Average lap time so far: " + + // (System.nanoTime() - + // startTime)/(double)sentPackets + + // " ns"); + // } + + // if we've not send as many packets as we are to: + // re-route packet (set its port and address) and send it + + // receivePacket.setPort(nextPort); + // receivePacket.setAddress(nextHost); + // sendPacket(receivePacket); + nextNodeOutput.write(receivedData); + break; + // if messageType == 3 (stop sending packets and ultimately quit + case TERMINATE: + runMainWhileLoop = false; + // if this node is a logger, log one final time before + // stopping the whole shebang and close the file + if (isLogger) { + System.out.println("Closing file..."); + writeToLog(); + closeLogFile(); + } + // send terminating packet (end the whole shebang) + sendPacket(constructPacket(TERMINATE, "3")); + nextNodeOutput.write(constructByteArray(TERMINATE, "3")); + break; + // if a bogus message came into the ring (SOMEHOW...) + default: + System.out.println("Message of unknown type -> " + messageType); + break; + } // end switch + } // end main while-loop + nextNode.close(); prevNode.close(); if (isLogger) { -- 2.11.4.GIT