⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 locationanalyzer.java

📁 nesC写的heed算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// $Id: LocationAnalyzer.java,v 1.1.14.5 2003/08/22 16:57:50 idgay Exp $/*									tab:4 * "Copyright (c) 2000-2003 The Regents of the University  of California.   * All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement is * hereby granted, provided that the above copyright notice, the following * two paragraphs and the author appear in all copies of this software. *  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * * Copyright (c) 2002-2003 Intel Corporation * All rights reserved. * * This file is distributed under the terms in the attached INTEL-LICENSE      * file. If you do not find these files, copies can be found by writing to * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA,  * 94704.  Attention:  Intel License Inquiry. *//** * @author Wei Hong */package net.tinyos.tinydb.topology.PacketAnalyzer;import net.tinyos.tinydb.topology.*;import net.tinyos.tinydb.topology.event.*;import net.tinyos.tinydb.topology.util.*;import net.tinyos.tinydb.topology.Dialog.*;import java.util.*;import java.lang.*;import javax.swing.*;import java.io.*;import java.awt.*;import net.tinyos.tinydb.topology.Packet.*;              //this class performs two functions:              // 1.  estimate distances between nodes              // 2.  estimate the locations of the nodes              //It is currently configured to be able to do these functions              //using interchangable distance and location estimators                            //Basically, it estimates distance every time a new packet is recieved              //and a seperate thread runs in the background, constantly updating               //the location estimatespublic class LocationAnalyzer extends PacketAnalyzer implements java.lang.Runnable{	Thread estimateLocationThread;	protected static Hashtable proprietaryNodeInfo;	protected static TwoKeyHashtable proprietaryEdgeInfo;		public LocationAnalyzer()	{		      //define hashtables to hold my proprietary information		proprietaryNodeInfo = new Hashtable();		proprietaryEdgeInfo = new TwoKeyHashtable();				//register to hear new node and edge events		MainClass.objectMaintainer.AddNodeEventListener(this);		MainClass.objectMaintainer.AddEdgeEventListener(this);		//register myself to recieve NodeClickedEvents and EdgeClickedEvents		// MainClass.displayManager.AddNodeDialogContributor(this);		// MainClass.displayManager.AddEdgeDialogContributor(this);		      //register to be a node and edge painter		// MainClass.displayManager.AddNodePainter(this);		MainClass.displayManager.AddEdgePainter(this);	}		      //*****---PACKETRECIEVED EVENT HANDLER---*****//	public  void PacketReceived(PacketEvent e)	{		      //this function will read the packet and update the lengths		      //of the edges that correspond to the data in the packet.  		Packet packet = e.GetPacket();		double distance;		EdgeInfo edgeInfo;		Integer sourceNodeNumber, destinationNodeNumber;				sourceNodeNumber = packet.getNodeId();		destinationNodeNumber = packet.getParent();		distance = -1;		edgeInfo = (EdgeInfo)proprietaryEdgeInfo.get(sourceNodeNumber, destinationNodeNumber);		if( (!Double.isNaN(distance)) && (edgeInfo!=null) )		{				edgeInfo.SetDistance(distance);			}	}		      //*****---PACKETRECIEVED EVENT HANDLER---*****//	          //----------------------------------------------------------------------		      		          //----------------------------------------------------------------------		      //*****---run---*****//	public void run() 	{		      //this function will do all background work for location estimation		      //in general, this would include mass springs or boltzman machine		      //type activity		while(true)		{				try			{				estimateLocationThread.sleep(100);			}			catch(Exception e){e.printStackTrace();}			// just randomly assign locations			NodeInfo currentNode;			for(Enumeration nodes = GetNodeInfo(); nodes.hasMoreElements();) 			{				currentNode = (NodeInfo)nodes.nextElement();				synchronized(currentNode)				{					if(currentNode.GetFixed() == false)					{							currentNode.SetX(Math.random());						currentNode.SetY(Math.random());						currentNode.SetFixed(true);					}				}			}		}	}			  //*****---run---*****//	          //----------------------------------------------------------------------	public  void NodeCreated(NodeEvent e)    {		Integer nodeNumber = e.GetNodeNumber();    	proprietaryNodeInfo.put(nodeNumber, new NodeInfo(nodeNumber));    }    public  void NodeDeleted(NodeEvent e)    {		Integer nodeNumber = e.GetNodeNumber();    	proprietaryNodeInfo.remove(nodeNumber);    }		public  void EdgeCreated(EdgeEvent e)    {		Integer sourceNodeNumber = e.GetSourceNodeNumber();    	Integer destinationNodeNumber = e.GetDestinationNodeNumber();//you probably want to create a new info pbject to track the data of this new node    	proprietaryEdgeInfo.put(sourceNodeNumber, destinationNodeNumber, new EdgeInfo(sourceNodeNumber, destinationNodeNumber));    }     public  void EdgeDeleted(EdgeEvent e)    {		Integer sourceNodeNumber = e.GetSourceNodeNumber();    	Integer destinationNodeNumber = e.GetDestinationNodeNumber();//you probably want to create a new info pbject to track the data of this new node    	proprietaryEdgeInfo.remove(sourceNodeNumber, destinationNodeNumber);    }    public  void NodeClicked(NodeClickedEvent e)    {    }    public  void EdgeClicked(EdgeClickedEvent e)    {    }	public void PaintNode(Integer pNodeNumber, int x1, int y1, int x2, int y2, Graphics g)	{		NodeInfo nodeInfo = (NodeInfo)proprietaryNodeInfo.get(pNodeNumber);		if(nodeInfo==null) return;				if(nodeInfo.GetDisplayCoords() == true)		{			String temp = String.valueOf(nodeInfo.GetX());			String text = temp.substring(0,Math.min(4, temp.length()));			text = text.concat(",");			temp = String.valueOf(nodeInfo.GetY());			text = text.concat(temp.substring(0,Math.min(4, temp.length())));			g.setColor(Color.black);			g.drawString(text,  x1, y1);		}	}		public void PaintEdge(Integer pSourceNodeNumber, Integer pDestinationNodeNumber, int screenX1, int screenY1, int screenX2, int screenY2, Graphics g)	{		EdgeInfo edgeInfo = (EdgeInfo)proprietaryEdgeInfo.get(pSourceNodeNumber,pDestinationNodeNumber);		if(edgeInfo == null) return;			if(edgeInfo.GetRoutingPath()){			g.setColor(Color.green);			drawLine(g, screenX1, screenY1, screenX2, screenY2, 5);		}			if(edgeInfo.GetDisplayLength() == true)		{			String temp= String.valueOf(this.GetDistance(pSourceNodeNumber,pDestinationNodeNumber));			String text = temp.substring(0,Math.min(3, temp.length()));			text = text.concat(",");			double x1 = GetX(pSourceNodeNumber);			double y1 = GetY(pSourceNodeNumber);			double x2 = GetX(pDestinationNodeNumber);			double y2 = GetY(pDestinationNodeNumber);			temp = String.valueOf(Math.sqrt(Math.pow(x1-x2, 2)+ Math.pow(y1-y2, 2)));			text = text.concat(temp.substring(0,Math.min(3, temp.length())));//put both estimated and real distances			g.setColor(Color.black);			g.drawString(text, (screenX2+screenX1)/2, (screenY2+screenY1)/2);		}	}		public ActivePanel GetProprietaryNodeInfoPanel(Integer pNodeNumber)	{		return new ProprietaryNodeInfoPanel((NodeInfo)proprietaryNodeInfo.get(pNodeNumber));	}	public ActivePanel GetProprietaryEdgeInfoPanel(Integer pSourceNodeNumber, Integer pDestinationNodeNumber)	{		return new ProprietaryEdgeInfoPanel((EdgeInfo)proprietaryEdgeInfo.get(pSourceNodeNumber,pDestinationNodeNumber));	}              //----------------------------------------------------------------              //GET/SET    public double GetDistance(Integer sourceNodeNumber, Integer destinationNodeNumber )    {    	EdgeInfo edgeInfo = (EdgeInfo)proprietaryEdgeInfo.get(sourceNodeNumber, destinationNodeNumber);    	if(edgeInfo != null)    	{    		return edgeInfo.GetDistance();    	}    	else    	{    		return Double.NaN;    	}    }        public double GetX(Integer nodeNumber )    {    	NodeInfo nodeInfo = (NodeInfo)proprietaryNodeInfo.get(nodeNumber);    	if(nodeInfo !=null)    	{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -