📄 sensoranalyzer.java
字号:
// $Id: SensorAnalyzer.java,v 1.2.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 *///*********************************************************//*********************************************************//This file is a template that shows the conventions on how to write a //PacketAnalyzer subclass. It shows you how to do the following things// 1. recieve and process a new data packet// 2. recieve and process new node or edge clicks in the interface // 3. recieve and process the creation or deletion of nodes or edges// 4. Display a panel on the edge or node properties panels when they are clicked// 5. Add graphical output to the node, edge or the screen// 6. Display a properties panel to edit parameters of this PacketAnalyzer// 7. run this process in the background as a seperate thread//*********************************************************//*********************************************************//If you want to write a new class to do packet analysis, you should//not change any files besides this one, the MainFrame class (where you add menus)//And the MainClass (where this class is instantiated). This modularity//will help maintain future compatibilty with other people's analyzers.//*********************************************************//*********************************************************//The main thing to understand here is that the class is highly multithreaded.// 1. It can spawn a thread in the constructor to run in the background (as shown below)// 2. It can spawn new threads every time an event is recieved (to free the eventGenerating thread)// 3. Every "EventRecieved()" function is initially run on the thread of the object that generated the event// In this case, the following functions are initially run on the threads of the following objects// a. PacketRecieved() --> PacketListener// b. NodeCreated/Deleted(), EdgeCreated/Deleted() --> ObjectMaintainer// c. NodeClicked(), EdgeClicked() --> DisplayManager// d. PaintNode(), PaintEdge(), PaintScreen() --> DisplayManager// e. GetProrietaryNode/EdgeDisplayPanel() --> DisplayManager// 4. All Get/Set functions may be called by the GUI thread// 5. The constructor is called on the main() thread of the entire program//*********************************************************//*********************************************************//As a general rule for avoiding synchronization problems:// 1. All functions should be synchronized (except run() and the constructor) to eliminate problems with member // variables (i.e. only one thread can be running functions of this object at a time)// 2. All threads in the constructor should be started at the end of the constructor to eliminate problems between that thread and the code in the constructor// 3. Be careful about synchronizing over methods that call functions in other classes// It could cause problems if, for example, I grab resource A, somebody else grabs resource B, I grab resource B, somebody else wants resource A. We both end up waiting forever. // 4. Try not to call more than one synchronized method within the same call stack. (don't have one thread synchronized on more than one object at a time)//*********************************************************//*********************************************************package net.tinyos.tinydb.topology.PacketAnalyzer;//make sure you put this class in the net.tinyos.tinydb.topology/PacketAnalyzer folderimport net.tinyos.tinydb.topology.*;import net.tinyos.tinydb.topology.event.*;import net.tinyos.tinydb.topology.util.*;import java.util.*;import java.lang.*;import javax.swing.*;import net.tinyos.tinydb.topology.Dialog.*;import net.tinyos.tinydb.topology.Packet.*;import java.awt.*; public class SensorAnalyzer extends PacketAnalyzer //implements java.lang.Runnable { //Define your member variables (try not to have publics) protected static Hashtable proprietaryNodeInfo; protected static TwoKeyHashtable proprietaryEdgeInfo;// protected static Thread thread; //------------------------------------------------------------------------ //*****---CONSTRUCTOR---******// //the constructor should be called by the MainClass constructor when //it instantiates all the packetAnalyzers that we want //Make sure you edit that constructor to do so public SensorAnalyzer() { //initialize your variables //create new hashtables for your proprietary data proprietaryNodeInfo = new Hashtable(); proprietaryEdgeInfo = new TwoKeyHashtable(); //register to be notified of nodes and edges being created or deleted MainClass.objectMaintainer.AddEdgeEventListener(this);//listen to node events MainClass.objectMaintainer.AddNodeEventListener(this);//listen to edge event AnalyzerDisplayEnable(); } //------------------------------------------------------------------------ //EXAMPLE FUNCTION //*****---An example function with example code---******// //naming convention is as shown here. //Be sure to synchronize this function if it is being called from more than //one of the following threads // 1. your own thread (the run() function) // 2. PacketReciever threads (packetRecieved() function) // 3. ObjectMaintainer thread (NodeCreated/Deleted() or EdgeCreated/Deleted() functions) // 4. GUI thread (Node/Edge Clicked(), GetNode/EdgePanel(), PaintNode/Edge/Screen() functions) // 5. Any Get/Set function or other function called by the GUI thread //If you do not want to synchronize the entire method (or any method in this class), //you have to figure out which parts may need to be synchronized over which //variables, and do it manually/* public synchronized void DummyFunction(Integer pNodeNumber) { NodeInfo currentNodeInfo; //Always synchronize over MainClass.nodes or MainClass.edges if you care that somebody else might add or delete nodes/edges if(proprietaryNodeInfo.contains(pNodeNumber)==true) //e.g the node exists for the if() statement but is deleted and you get a null pointer exception when you try to paint it (In this case you should just use: currentNode = MainClass.nodes.get(Number) and afterward check if currentNode == null. Remember to synchronize over currentNode). { currentNodeInfo = ((NodeInfo)proprietaryNodeInfo.get(pNodeNumber)); } else { currentNodeInfo = new NodeInfo(pNodeNumber); } }*/ //*****---An example function with example code---******// //------------------------------------------------------------------------ //------------------------------------------------------------------------ //*****---Packet Recieved event handler---******// //this function will be called by the thread running the packetReciever //everytime a new packet is recieved //make sure it is synchronized if it modifies any of your data public synchronized void PacketReceived(PacketEvent e) { //this function defines what you do when a new packet is heard by the system (recall that the parent class (PacketAnalyzer) already registered you to listen for new packets automatically) //if this is a long function, you should call it in a seperate thread to allow the PacketReciever thread to continue recieving packets Packet packet = e.GetPacket(); Vector node_list = packet.CreateRoutePathArray(); for(int i = 0; i < node_list.size() - 1; i ++){ Integer currentNodeNumber = (Integer)node_list.elementAt(i); NodeInfo currentNodeInfo; if( (currentNodeInfo = (NodeInfo)proprietaryNodeInfo.get(currentNodeNumber)) != null) { currentNodeInfo.SetValue(packet.getValue()); } } } //It is called by net.tinyos.tinydb.topology.PacketAnalyzer.ObjectMainter public synchronized void NodeCreated(NodeEvent e) { Integer newNodeNumber = e.GetNodeNumber();//you probably want to create a new info pbject to track the data of this new node proprietaryNodeInfo.put(newNodeNumber, new NodeInfo(newNodeNumber)); } //*****---Node Created---******// //------------------------------------------------------------------------ //------------------------------------------------------------------------ //*****---Node Deleted---******// //this function defines what you do when a new node is deleted //It is called by net.tinyos.tinydb.topology.PacketAnalyzer.ObjectMainter public synchronized void NodeDeleted(NodeEvent e) { Integer deletedNodeNumber = e.GetNodeNumber();//you probably want to delete the info pbject to track the data of this new node proprietaryNodeInfo.remove(deletedNodeNumber); } //*****---Node Deleted---******// //------------------------------------------------------------------------ //------------------------------------------------------------------------ //*****---Edge Created---******// //this function defines what you do when a new edge is created //It is called by net.tinyos.tinydb.topology.PacketAnalyzer.ObjectMainter/* public synchronized 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)); }*/ //*****---Edge Created---******// //------------------------------------------------------------------------ //------------------------------------------------------------------------ //*****---Edge Deleted---******// //this function defines what you do when a new edge is deleted //It is called by net.tinyos.tinydb.topology.PacketAnalyzer.ObjectMainter/* public synchronized 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); }*/ //*****---EdgeDeleted---******// //------------------------------------------------------------------------ //************************************************************************ //************************************************************************ //the following two functions correspond to the //NodeClickedEventListener, EdgeClickedEventListener interfaces and will //only work if you register as a listener for these events //************************************************************************ //************************************************************************ //------------------------------------------------------------------------ //*****---Node Clicked---******// //this function defines what you do when a node is clicked //It is called by net.tinyos.tinydb.topology.DisplayManager/* public synchronized void NodeClicked(NodeClickedEvent e) { Integer nodeClicked = e.GetNodeNumber(); //and maybe do some other processing }*/ //*****---Node Clicked---******// //------------------------------------------------------------------------ //------------------------------------------------------------------------ //*****---Edge Clicked---******//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -