locationanalyzer.java
来自「nesC写的heed算法」· Java 代码 · 共 511 行 · 第 1/2 页
JAVA
511 行
// $Id: LocationAnalyzer.java,v 1.2.4.4 2003/08/22 16:51:22 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.surge.PacketAnalyzer;
import net.tinyos.surge.*;
import net.tinyos.surge.event.*;
import net.tinyos.surge.util.*;
import net.tinyos.surge.Dialog.*;
import java.util.*;
import java.lang.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;
//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 estimates
public 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 to be a node and edge painter
// MainClass.displayManager.AddNodePainter(this);
MainClass.displayManager.AddEdgePainter(this);
}
//*****---PACKETRECIEVED EVENT HANDLER---*****//
public void PacketReceived(MultihopMsg /*SurgeMsg*/ msg) {
//this function will read the packet and update the lengths
//of the edges that correspond to the data in the packet.
double distance;
EdgeInfo edgeInfo;
Integer sourceNodeNumber, destinationNodeNumber;
SurgeMsg SMsg = new SurgeMsg(msg.dataGet(),msg.offset_data(0));
sourceNodeNumber = new Integer(msg.get_originaddr());
//destinationNodeNumber = new Integer(msg.get_parentaddr();
destinationNodeNumber = new Integer(SMsg.get_parentaddr());
distance = -1;
edgeInfo = (EdgeInfo)proprietaryEdgeInfo.get(sourceNodeNumber, destinationNodeNumber);
if( (!Double.isNaN(distance)) && (edgeInfo!=null) )
{
edgeInfo.SetDistance(distance);
//SurgeMsg SMsg = new SurgeMsg(msg.dataGet(),msg.offset_data(0));
if (SMsg.get_type() == 0) {
edgeInfo.SetLinkQuality(128 /*msg.get_args_reading_args_parent_link_quality()*/);
}
}
}
//*****---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(edgeInfo.GetEdgeColor());
drawLine(g, screenX1, screenY1, screenX2, screenY2, 3); // XXX MDW: 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)
{
return nodeInfo.GetX();
}
else
{
return Double.NaN;
}
}
public double GetY(Integer nodeNumber)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?