////////////////////////////////////////////////////////////////////// // Node.java // // --Container for Node IP and structure information-- // // // // Supervised Peer-to-peer Overlay Network (SPON) Simulator // // // // // // Computer Science M.S.E. Project // // Programmed by: Joey Chau // // Copyright: February, 2003 // // // // Research Advisor: Christian Scheideler // // Computer Science // // Johns Hopkins University // ////////////////////////////////////////////////////////////////////// import java.io.*; import java.net.*; import java.util.*; /** Node used to hold and manage parent, child, and next node information */ class Node { private String mKey; private int mHeight; private String mLeftChild; private String mRightChild; private String mNext; /** Default node constructor */ public Node() { mKey = " "; mHeight = -1; mLeftChild = " "; mRightChild = " "; mNext = " "; } /** Node constructor which declares the source node IP @param iaddy - String representation of the source node IP */ public Node( String iaddy ) { mKey = iaddy; mHeight = -1; mLeftChild = " "; mRightChild = " "; mNext = " "; } /** Copy constructor which duplicates the values of another node @param other - Node which will be duplicated */ public Node( Node other ) { mKey = other.mKey; mHeight = other.mHeight; mLeftChild = other.mLeftChild; mRightChild = other.mRightChild; mNext = other.mNext; } /** Modify the default key of this node @param iaddy - String key */ public void setKey( String iaddy ) { mKey = iaddy; } /** Access the the key of the node @return String - the key */ public String getKey() { return mKey; } /** Modify the height of this node @param h - int height */ public void setHeight( int h ) { mHeight = h; } /** Access the height of this node @return int - height */ public int getHeight() { return mHeight; } /** Access the right child of this node @return String - the key of the right child */ public String getRightChild() { return mRightChild; } /** Modify the right child of this node @param rc - String key of the new right child */ public void setRightChild( String rc ) { mRightChild = rc; } /** Access the left child of this node @return String - the key of the left child */ public String getLeftChild() { return mLeftChild; } /** Modify the left child of this node @param lc - String key of the new left child */ public void setLeftChild( String lc ) { mLeftChild = lc; } /** Access the next node @return String - the key of the next node */ public String getNext() { return mNext; } /** Modify the next node @param nx - String key of the next node */ public void setNext( String nx ) { mNext = nx; } /** Determines whether the node has not been set @return boolean - true if node is empty or undelcared or unset */ public boolean isEmpty() { return (mHeight == -1); } /** String representation of the node used for communication and information maintenance @return String - the String representation of the information in Node */ public String toString() { return mKey + ":" + mHeight + ":" + mLeftChild + ":" + mRightChild + ":" + mNext; } /** Given a formated Node string, it attempts to parse the String and set the attributes of the node @param pStr - String containing Node information @boolean - true if parse was successful for the given parameter */ public boolean parseString( String pStr ) { StringTokenizer stok = new StringTokenizer( pStr, ":" ); int count; count = stok.countTokens(); if( count != 5 ) { return false; } mKey = stok.nextToken(); try { mHeight = Integer.parseInt( stok.nextToken() ); } catch( NumberFormatException e ) { return false; } mLeftChild = stok.nextToken(); mRightChild = stok.nextToken(); mNext = stok.nextToken(); return true; } }