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

📄 objectmaintainer.java

📁 用于传感器网络的节点操作系统 TinyOS 结构设计非常有意思
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* "Copyright (c) 2001 and 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 and the following 
* two paragraphs 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." 
* 
* Authors:   Kamin Whitehouse <kamin@cs.berkeley.edu>
* History:   created 7/22/2001 
* Authors:   Wei Hong, modified for tinydb
*/

//**************************************************************************
//**************************************************************************
//this class reads packets and updates all nodes and paths
//that are in each packet with timestamps, creating
//those nodes or paths that do not exist.
//It also runs the "run" method as a seperate thread,
//which constantly checks the nodes and paths for having expired.
//**************************************************************************
//**************************************************************************

package net.tinyos.tinydb.topology.PacketAnalyzer;

import net.tinyos.tinydb.topology.*;
import net.tinyos.tinydb.topology.event.*;
import net.tinyos.tinydb.topology.util.*;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import net.tinyos.tinydb.topology.Dialog.*;
import net.tinyos.tinydb.topology.Packet.*;

 
public class ObjectMaintainer extends PacketAnalyzer implements Runnable
{
	          //these variables define when to eliminate nodes that are no longer in the network
	protected static long nodeExpireTime;
	protected static long edgeExpireTime;
	protected static long nodeInitialPersistance;
	protected static long edgeInitialPersistance;
	protected static long nodeInitialDormancy;
	protected static long edgeInitialDormancy;
//	protected static long edgeStability;
	//protected static long networkSpeed;
	protected static long expirationCheckRate;
	public static Hashtable nodeInfo;
	public static TwoKeyHashtable edgeInfo;
	protected static Thread oldObjectDeleterThread;
	protected static Vector nodeListeners;
	protected static Vector edgeListeners;

	public ObjectMaintainer()
	{
				//register myself to recieve NodeClickedEvents and EdgeClickedEvents
		// MainClass.displayManager.AddNodeDialogContributor(this);
		MainClass.displayManager.AddEdgeDialogContributor(this);
		
		      //all values are in milliseconds
		nodeExpireTime = 50000;
		edgeExpireTime = 20000;
		nodeInitialPersistance = 2500;
		edgeInitialPersistance = 2500;
		nodeInitialDormancy = 7500;
		edgeInitialDormancy = 2500;
		expirationCheckRate = 1500;//in milliseconds
		
		nodeInfo = new Hashtable();
		edgeInfo = new TwoKeyHashtable();
		nodeListeners = new Vector();
		edgeListeners = new Vector();
		oldObjectDeleterThread = new Thread(this);
		try{
			oldObjectDeleterThread.setPriority(Thread.MIN_PRIORITY);
			oldObjectDeleterThread.start(); //recall that start() calls the run() method defined in this class
		}
		catch(Exception e){e.printStackTrace();}

	}
              //-----------------------------------------------------------------------
	          //*****---Packet Recieved event handler---******//
    	      //when a new packet is recieved
    	      //look at the list and order of nodes in the hop-path
    	      //update the node times, create nodes that do not exist
    	      //update the edge times, create edges that do not exist
    public  void PacketReceived(PacketEvent e)
    {
    	Integer currentNodeNumber;
    	Integer previousNodeNumber=new Integer(-1);
    	NodeInfo currentNodeInfo;
    	NodeInfo previousNodeInfo=null;
    	EdgeInfo currentEdgeInfo;
    	Packet packet = e.GetPacket();

		Date eventTime = e.GetTime();
		Vector routePath = packet.CreateRoutePathArray();//vector of "Integer" objects
		
    	      //look at the list and order of nodes in the hop-path
		for(int count=0; count < routePath.size(); count++)
		{
			currentNodeNumber = (Integer)routePath.elementAt(count);
			// System.out.println("checking: " + currentNodeNumber);
			if((currentNodeInfo = (NodeInfo)nodeInfo.get(currentNodeNumber)) !=null){
				//System.out.println(currentNodeInfo.GetCreated());
				//System.out.println(NodeHasBeenSeenRecently(currentNodeInfo.GetTimeLastSeen(), eventTime));
				if( (currentNodeInfo.GetCreated()==false) && NodeHasBeenSeenRecently(currentNodeInfo.GetTimeLastSeen(), eventTime)){
					currentNodeInfo.SetCreated(true);
					currentNodeInfo.SetTimeLastSeen(eventTime);
					TriggerNodeCreatedEvent(new NodeEvent(this, currentNodeNumber, Calendar.getInstance().getTime()));	
                		} else {
                			currentNodeInfo.SetTimeLastSeen(eventTime);
				}
            		} else {
				//System.out.println("added: " + currentNodeNumber);
				currentNodeInfo = new NodeInfo(currentNodeNumber, eventTime);
				nodeInfo.put(currentNodeNumber, currentNodeInfo);

				currentNodeInfo.SetCreated(true);
				TriggerNodeCreatedEvent(new NodeEvent(this, currentNodeNumber, Calendar.getInstance().getTime()));
				 
            		}

			if(count != 0) {
				if( (currentEdgeInfo = (EdgeInfo)edgeInfo.get(currentNodeNumber,previousNodeNumber)) != null) {
					currentEdgeInfo.SetTimeLastSeen(eventTime);
				} else if((currentNodeInfo!=null) && (previousNodeInfo!=null)&&(currentNodeInfo.GetCreated() && previousNodeInfo.GetCreated())) {
					edgeInfo.put(previousNodeNumber, currentNodeNumber, new EdgeInfo(previousNodeNumber, currentNodeNumber, eventTime));
					TriggerEdgeCreatedEvent(new EdgeEvent(this, previousNodeNumber, currentNodeNumber, Calendar.getInstance().getTime()));
				}
				if(previousNodeInfo != null) previousNodeInfo.SetParent(currentNodeNumber);
			}
			previousNodeNumber = currentNodeNumber;
			previousNodeInfo = currentNodeInfo;
		}     //end for loop through hop-list
	}
	
	static Integer getParent(Integer pNode){
			NodeInfo currentNodeInfo = (NodeInfo)nodeInfo.get(pNode);
			if(currentNodeInfo == null) return new Integer(-1);
			else return currentNodeInfo.GetParent();
	}
					
	          //*****---Packet Recieved event handler---******//
	public static boolean isBase(Integer pNode) {
		return pNode.intValue() == 0;
	}
					
	  //this function constantly deletes old nodes and edges
    public void run()
    {
		while(true)
    	{
    		EliminateExpiredNodes();
    		EliminateExpiredEdges();
			try
			{
	    		oldObjectDeleterThread.sleep(expirationCheckRate);
	    	}
			catch(Exception e){e.printStackTrace();}
		}
		
    }

  //*****---NodeHasBeenSeenRecently---******//
    public boolean NodeHasBeenSeenRecently(Date TimeLastSeen, Date currentTime)
    {
		if((currentTime.getTime() - nodeInitialDormancy)  <= TimeLastSeen.getTime())//if it has been seen in time less than nodeInitialDormancy
		{
			return true;
		}
		return false;
    }

	  //*****---Eliminiate Nodes---******//
     void EliminateExpiredNodes()
    {
		Integer currentNodeNumber;
		NodeInfo currentNodeInfo;
		
		      //for all nodes in the network
		long currentTime;
		for(Enumeration nodes = nodeInfo.elements();nodes.hasMoreElements();) 
		{
			currentNodeInfo = (NodeInfo)nodes.nextElement();
			currentNodeNumber = currentNodeInfo.GetNodeNumber();
			if(currentNodeNumber.intValue() != 0){;
			
				 //if node has expired, eliminate it
				currentTime = Calendar.getInstance().getTime().getTime();//milliseconds since January 1, 1970
				if( (currentTime - currentNodeInfo.GetTimeLastSeen().getTime() > nodeExpireTime) &&
					(currentTime - currentNodeInfo.GetTimeCreated().getTime() > nodeInitialPersistance))
				{
					nodeInfo.remove(currentNodeNumber);
					TriggerNodeDeletedEvent(new NodeEvent(this, currentNodeNumber, Calendar.getInstance().getTime()));
				}
			}
		}
    }
    
     void EliminateExpiredEdges()
    {
		
		Integer sourceNumber;
		Integer destinationNumber;
		Integer currentEdgeNumber;
		EdgeInfo currentEdgeInfo;
		
		      //for all edges in the network
		long currentTime; 
		for(Enumeration edges = edgeInfo.elements();edges.hasMoreElements();) 
		{
			currentEdgeInfo = (EdgeInfo)edges.nextElement();
			sourceNumber = currentEdgeInfo.GetSourceNodeNumber();//final vairables that don't need to be synchronized
			destinationNumber = currentEdgeInfo.GetDestinationNodeNumber();//final vairables that don't need to be synchronized

						 //if edge has expired, eliminate it
			currentTime = Calendar.getInstance().getTime().getTime();//milliseconds since January 1, 1970
			if( (currentTime - currentEdgeInfo.GetTimeLastSeen().getTime() > edgeExpireTime) &&
				(currentTime - currentEdgeInfo.GetTimeCreated().getTime() > edgeInitialPersistance) )
			{
				edgeInfo.remove(sourceNumber, destinationNumber);
				TriggerEdgeDeletedEvent(new EdgeEvent(this, sourceNumber, destinationNumber, Calendar.getInstance().getTime()));
			}
		}
    }
    
	          //*****---ADD EVENT LISTENER---******//
	public static void AddNodeEventListener(NodeEventListener pListener)
	{
		nodeListeners.add(pListener);
	}
	
	public static void RemoveNodeEventListener(NodeEventListener pListener)
	{
		nodeListeners.remove(pListener);
	}
	
	public static void AddEdgeEventListener(EdgeEventListener pListener)
	{
		edgeListeners.add(pListener);
	}

	public static void RemoveEdgeEventListener(EdgeEventListener pListener)
	{
		edgeListeners.remove(pListener);
	}
	          //*****---TRIGGER EVENTS---******//
	protected void TriggerNodeCreatedEvent(NodeEvent e)
	{
		      //for each listener
		NodeEventListener currentListener;
		for(Enumeration list = nodeListeners.elements(); list.hasMoreElements();)
		{
			currentListener = (NodeEventListener)list.nextElement();
			currentListener.NodeCreated(e);//send the listener an event
        }			
	}

	protected void TriggerNodeDeletedEvent(NodeEvent e)
	{
		      //for each listener
		NodeEventListener currentListener;
		for(Enumeration list = nodeListeners.elements(); list.hasMoreElements();)
		{
			currentListener = (NodeEventListener)list.nextElement();
			currentListener.NodeDeleted(e);//send the listener an event
        }			

⌨️ 快捷键说明

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