/************************************************************************** * $Id: Node.java,v 1.1.1.1 2000/03/29 22:58:14 kkeys Exp $ * * File: Node.java Class: Node * Goal: A self contained class that will mantain its own * way of being draw, size, color, selected color, * location x,y, name * * Written: Bradley Huffaker (12/17/97) * * Modified: Jaeyeon Jung (2/13/93) * root_x, root_y: for topological layout * geo_x, geo_y: for geographical layout * getDomain: ipaddress which doesn't have domain name will * be returned as unknown * * For:Cooperative Association for Internet Data Analysis *************************************************************************** *************************************************************************** By accessing this software, NODE, you are duly informed of and agree to be bound by the conditions described below in this notice: This software product, NODE, is developed by Bradley Huffaker, and Jaeyeon Jung copyrighted(C) 1998 by the University of California, San Diego (UCSD), with all rights reserved. UCSD administers the NLANR Cache grants, NCR-9796082 and NCR-9616602, under which most of this code was developed. There is no charge for NODE software. You can redistribute it and/or modify it under the terms of the GNU General Public License, v. 2 dated June 1991 which is incorporated by reference herein. NODE is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS, OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that the use of it will not infringe on any third party's intellectual property rights. You should have received a copy of the GNU GPL along with the NODE program. Copies can also be obtained from http://www.gnu.org/copyleft/gpl.html or by writing to University of California, San Diego SDSC/CAIDA/NLANR 9500 Gilman Dr., MS-0505 La Jolla, CA 92093 - 0505 USA Or contact INFO@NLANR.NET **************************************************************************/ package bhuffake.plankton; import java.awt.*; import java.util.Random; public class Node extends DisplayObject { String name; String domain; int x,y; int root_x, root_y; int geo_x, geo_y; static final Color NODE_DEFAULT_COLOR = Color.white; Color outline_color = Color.black; Color root_color = Color.red; Color name_color = Color.green; boolean selected = false; boolean highlighted = false; // Drawing Name Stuff // Will have it draw it's name boolean show_name = false; static final int NORTH_EAST = 0; static final int NORTH = 2; static final int NORTH_WEST = 3; static final int WEST = 4; static final int SOUTH_WEST = 5; static final int SOUTH = 6; static final int SOUTH_EAST = 7; static final int EAST = 8; int name_location = SOUTH; DList links = new DList(); Node[] children; // Keeps track of weather it is a root or not boolean root = false; boolean setXY = false; boolean set_to_root_xy = false; boolean set_to_geo_xy = false; // This is needed for animation. A Group of nodes is compressed // into a single node across files. Node animatorNode; public Node(String name_input, int x_input, int y_input) { size = 10; little_size = 4; default_color = NODE_DEFAULT_COLOR; color = default_color; name = name_input; x = x_input; y = y_input; } /********************************************************* * Purpose: To have a contructor what works with IOHandler * to have a node create that has a file assotionated with it **********************************************************/ public Node(String name_input, int x_input, int y_input, int fileIndex, int num_files_input) { this(name_input, x_input,y_input); num_files = num_files_input; setUpFiles(); setExists(fileIndex); } public boolean isSetToRootXY() { return set_to_root_xy; } public void setToRootXY(boolean flag) { set_to_root_xy = flag; } public void setToRootXY() { setXY(root_x, root_y); setToRootXY(true); } public void setToRootXYNoFlag() { setXYNoFlag(root_x, root_y); setToRootXY(true); } public void setRootXY(int x_input, int y_input) { root_x = x_input; root_y = y_input; } public boolean isSetToGeoXY() { return set_to_geo_xy; } public void setToGeoXY(boolean flag) { set_to_geo_xy = flag; } public void setToGeoXY() { setXY(geo_x, geo_y); setToGeoXY(true); } public void setToGeoXYNoFlag() { setXYNoFlag(geo_x, geo_y); setToGeoXY(true); } public void setGeoXY(int x_input, int y_input) { geo_x = x_input; geo_y = y_input; } /**************************************************** * Purpose: To have all nodes in order of their names * and after all lines *****************************************************/ public boolean greater(DisplayObject object) { if (!(object instanceof Node)) return true; char[] mine = name.toCharArray(); char[] other= ((Node)object).name.toCharArray(); int shortest = mine.length; if (shortest > other.length) shortest = other.length; for (int index =0; index < shortest; index++) { if (mine[index] > other[index]) return true; else if (mine[index] < other[index]) return false; } if (mine.length != shortest) return true; return false; } /************************************************** * Purpose: To find the squared distance from the center of the * object to the point. I use the squared to avoid needless * math (ie faster) * * Returns : the distance or -1 if distance is greater then size*size ***************************************************/ public double near(int x_input, int y_input) { if (hidden) return -1; int delta_x = x_input - x; int delta_y = y_input - y; double Z = delta_x*delta_x + delta_y*delta_y; if (Z > size*size) return -1; return Z; } public void setAnimatorNode(Node node) { animatorNode = node; } public Node getAnimatorNode() { return animatorNode;} public void setName(String input) { name = input; domain = null; } public String getName() { return name; } public String getDomain() { if (domain == null) { char[] array = name.toCharArray(); int length = array.length; int index=length-1; // Checks to see it is part of a ip boolean ip = true; while (index > 0 && array[index] != '.') { if (!(array[index] >= '1' && array[index] <= '9')) ip = false; index--; } domain = new String(array,index,length-index); char[] temp = new char[domain.length()]; domain.getChars(0,domain.length()-1,temp,0); //System.err.println(domain + "chr0:"+ temp[1] + "\n"); if (ip) domain = "unknown"; else if (temp[1] >= '1' && temp[1] <= '9') domain = "unknown"; } return domain; } public void setHighlight(boolean input) { highlighted = input; } public boolean isHighlighted() { return highlighted; } public void setSelect(boolean input) { selected = input; } public boolean isSelected() { return selected; } public void selectTree(boolean input,DList dlist) { if (input) dlist.enqueue(this); else dlist.drop(this); selected = input; if (children != null) for (int index=0; index < children.length; index++) children[index].selectTree(input,dlist); } public void setRoot(boolean input) { root = input; } public boolean isRoot() { return root; } public void swapRoot() { root = !root; } // added by Braldey Huffaker 4/16/97 public void showName() { show_name = true; } public void hideName() { show_name = false; } public void setNameLocation(double degree) { while (degree < 0) degree += 2*Math.PI; degree = degree % (Math.PI*2); final double FOURTH = Math.PI/4; degree += FOURTH; if (degree <= FOURTH) name_location = EAST; else if (degree <= 2*FOURTH || degree > 7*FOURTH) name_location = NORTH_EAST; else if (degree <= 3*FOURTH) name_location = NORTH; else if (degree <= 4*FOURTH) name_location = NORTH_WEST; else if (degree <= 5*FOURTH) name_location = WEST; else if (degree <= 6*FOURTH) name_location = SOUTH_WEST; else name_location = SOUTH; } // end added by Braldey Huffaker 4/16/97 public void draw(Graphics g, Dimension dim, double[] latlon) { if (!exists || hidden) return; g.setColor(outline_color); if (show_name) drawName(g); if (this.isOnMap()) { if (!this.isSetGeo()) return; g.drawOval(x-size/2,y-size/2,size,size); if (selected || highlighted) g.setColor(selected_color); else if (root && !colored) g.setColor(root_color); else g.setColor(color); g.fillOval(x-size/2+1,y-size/2+1,size-1,size-1); } else { if (little && false) { if (root && !colored) g.setColor(root_color); if (selected || highlighted) g.setColor(selected_color); g.fillRect(x-little_size/2,y-little_size/2, little_size,little_size); } else { g.drawRect(x-size/2,y-size/2,size,size); if (selected || highlighted) g.setColor(selected_color); else if (root && !colored) g.setColor(root_color); else g.setColor(color); g.fillRect(x-size/2+1,y-size/2+1,size-1,size-1); } } } public void drawName(Graphics g) { int name_x = x; int name_y = y; FontMetrics fm = g.getFontMetrics(g.getFont()); int stringWidth = fm.stringWidth(name); int stringHeight = fm.getHeight(); int halfSize = size/2; switch(name_location) { case NORTH_EAST: name_x -= stringWidth+halfSize; name_y -= halfSize; break; case NORTH: name_x -= stringWidth/2; name_y -= halfSize; break; case NORTH_WEST: name_x += halfSize; name_y -= halfSize-stringHeight/2; break; case WEST: name_x += halfSize; name_y += stringHeight/2; break; case SOUTH_WEST: name_x += halfSize; name_y += halfSize+stringHeight/2; break; case SOUTH: name_x -= stringWidth/2; name_y += halfSize+stringHeight; break; case SOUTH_EAST: name_x -= stringWidth + halfSize; name_y += halfSize+ stringHeight/2; break; case EAST: name_x -= stringWidth + halfSize; name_y += stringHeight/2; break; } //g.setColor(name_color); g.drawString(name,name_x,name_y); } public DisplayObject[] remove() { DisplayObject[] objects = new DisplayObject[links.count()]; int index = 0; while (!links.empty()) objects[index++] = links.pop(); return objects; } public void removeObject(DisplayObject object) { links.drop(object); } public Node[] getChildren() { return children; } public void setChildren(Node[] input) { children = input; } public String getString() { String answer = name; if (root) answer = answer + " (root)" ; if (!isSetXY()) answer = answer + " !setXY" ; if (isSetToHTTPColor()) answer = answer + " " + getHTTPRequest()+" requests"; if (isSetToICPColor()) answer = answer + " " + getICPRequest()+" requests"; return answer; } public void unSetXY() { setXY = false; } public boolean isSetXY() { return setXY; } public void setXY(int x_input, int y_input) { x = x_input; y = y_input; setXY = true; } public void setXYNoFlag(int x_input, int y_input) { x = x_input; y = y_input; } public void setXY(int[] xy) { x = xy[0]; y = xy[1]; setXY = true; } public int[] getXY() { int[] xy = new int[2]; xy[0] = x; xy[1] = y; return xy; } public int[] getRootXY() { int[] xy = new int[2]; xy[0] = root_x; xy[1] = root_y; return xy; } public void changeXY(int delta_x, int delta_y) { x += delta_x; y += delta_y; } public void changeRootXY(int delta_x, int delta_y) { root_x += delta_x; root_y += delta_y; } public void changeChildXY(int delta_x, int delta_y) { x += delta_x; y += delta_y; if (children != null) for (int index=0; index < children.length; index++) children[index].changeChildXY(delta_x,delta_y); } public void scale(double scaler) { x *= scaler; y *= scaler; } public void scaleRootXY(double scaler) { root_x *= scaler; root_y *= scaler; } public void addLink(DisplayObject link) { links.push(link); } public void removeLink(DisplayObject link) { links.drop(link); } public int countLink() { return links.count(); } public DList getLink() {return links;} public DisplayObject getParent() { return new Node("",0,0); } public DisplayObject getChild() { return new Node("",0,0); } public Node[] getNeighbors() { Node[] neighbors = new Node[links.count()]; links.reset(); int index =0; while(!links.end()) { DisplayObject link = links.next(); neighbors[index] = (Node) link.getParent(); if (neighbors[index] == this) neighbors[index] = (Node) link.getChild(); index++; } return neighbors; } double dx = 0; double dy = 0; public void stablize() { dx = dy = 0; } public void change() { x += dx; y += dy; dx = dy = 0; } Random random = new Random(); public void push(Node node) { int delta_x = x - node.x; int delta_y = y - node.y; double distance = Math.sqrt(delta_x*delta_x + delta_y*delta_y); double shift = 0; if (distance == 0) { shift = (random.nextDouble() - .5); } else shift = size/distance; if (delta_x < 0) shift *= -1; dx += shift; node.dx -= shift; if (delta_y < 0) shift *= -1; dy += shift; node.dy -= shift; } public void pull(Node node) { int delta_x = x - node.x; int delta_y = y - node.y; double distance = Math.sqrt(delta_x*delta_x + delta_y*delta_y); distance -= 3*size; if (distance < 0) distance *= -1; double shift; if (distance == 0) shift = (random.nextDouble() - .5); else shift = distance/(3*size); if (shift > .40) shift = .40; delta_x *= shift; dx -= delta_x; node.dx += delta_y; delta_y *= shift; dy -= delta_y; node.dy += delta_y; } }