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

📄 taskclient.java

📁 nesC写的heed算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// $Id: TASKClient.java,v 1.1.2.1 2003/09/10 20:59:39 whong 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. */package net.tinyos.task.taskapi;import java.sql.Timestamp;import java.util.*;import java.io.*;import java.net.*;import net.tinyos.task.tasksvr.*;/** * The TASKClient class is the main class to provide APIs  * for clients to access the TASKServer. * @author whong@intel-research.net */public class TASKClient{	/**	 * The TASKServer constructor establishes a connection to the	 * TASKServer, pre-fetches all the attribute and command information	 * (since they don't change), and create a new thread waiting for	 * results from the TASKServer and calling client callbacks.	 *	 * @param	serverIP	IP address of TASKServer.	 * @param	serverPort	port number of TASKServer.	 * @throws	IOException	 */	public TASKClient(String serverIP, int serverPort) throws IOException	{		this.serverIP = serverIP;		this.serverPort = serverPort;		sensorListenerHandler = new ListenerHandler();		healthListenerHandler = new ListenerHandler();		if (prefetchMetaData() != TASKError.SUCCESS)		{			throw new IOException("connection to TASKServer failed.");		}	};	private int prefetchMetaData()	{		Socket conn = null;		ObjectOutputStream outStream = null;		ObjectInputStream inStream = null;		int error = TASKError.SOCKET_IO_EXCEPTION;		try		{			conn = getConnection();			System.out.println("got connection");			outStream = new ObjectOutputStream(conn.getOutputStream());			System.out.println("got output stream");			inStream = new ObjectInputStream(conn.getInputStream());			System.out.println("got input stream");			System.out.println("sending PREFETCH_METADATA command");			outStream.writeShort(TASKServer.PREFETCH_METADATA);			outStream.flush();			System.out.println("PREFETCH_METADATA command sent");			attributeInfos = (Vector)inStream.readObject();			System.out.println("attributeinfos read done.");			commandInfos = (Vector)inStream.readObject();			System.out.println("commandinfos read done.");			aggregateInfos = (Vector)inStream.readObject();			System.out.println("aggregateinfos read done.");			error = inStream.readInt();			outStream.close();			inStream.close();			conn.close();			System.out.println("prefetching of metadata was successful.");		}		catch (Exception e)		{			try			{				if (conn != null)					conn.close();				if (outStream != null)					outStream.close();				if (inStream != null)					inStream.close();			}			catch (Exception ex)			{			}		}		return error;	}	/**	 * The TASKServer constructor with default port number	 *	 * @param	serverIP	IP address of TASKServer.	 * @throws	IOException	 */	public TASKClient(String serverIP) throws IOException	{		this(serverIP, TASKServer.DEFAULT_SERVER_PORT);	};	private TASKQuery getQuery(short whichQuery)	{		TASKQuery query = null;		Socket conn = null;		ObjectOutputStream outStream = null;		ObjectInputStream inStream = null;		try		{			conn = getConnection();			outStream = new ObjectOutputStream(conn.getOutputStream());			inStream = new ObjectInputStream(conn.getInputStream());			outStream.writeShort(TASKServer.GET_QUERY);			outStream.writeShort(whichQuery);			outStream.flush();			query = (TASKQuery)inStream.readObject();			outStream.close();			inStream.close();			conn.close();		}		catch (Exception e)		{			try			{				if (conn != null)					conn.close();				if (outStream != null)					outStream.close();				if (inStream != null)					inStream.close();			}			catch (Exception ex)			{			}			e.printStackTrace();		}		return query;	}	/**	 * Returns the current sensor query, null if no query has been submitted.	 * Note that other TASK clients can modify this query.  Therefore,	 * this query may not reflect the latest in the sensor network.	 */	public TASKQuery getSensorQuery() 	{		return getQuery(TASKServer.SENSOR_QUERY);	};	/**	 * Returns the current network health monitoring query,	 * null if no query has been submitted.	 * Note that other TASK clients can modify this query.  Therefore,	 * this query may not reflect the latest in the sensor network.	 */	public TASKQuery getHealthQuery() 	{		return getQuery(TASKServer.HEALTH_QUERY);	};    static final int SENSOR_COST_UJ = 90;	/**	 * Estimate a network life time (in seconds) 	 * given a sensor query and a health monitoring query	 * (using tehir associated sample periods (in milliseconds)	 *	 * @param	sensorQuery	a sensor query.	 * @param	healthQuery	a health monitoring query.	 * @return	estimated network life time in seconds.	 */	public long estimateLifeTime(TASKQuery sensorQuery, TASKQuery healthQuery)	{	    return EstimateLifetime.samplePeriodToLifetime((long)sensorQuery.getSamplePeriod(),			  (long)EstimateLifetime.maxVReading, // XXX assumes fully charged batteries			  SENSOR_COST_UJ,			  sensorQuery.getSelectEntries().size(),			  1 /* XXX Assume just one message per epoch ! */ );	}	/**	 * Given an expected network life time (in seconds), set the	 * appropriate sample period (in milliseconds) in the sensor	 * query and the health monitoring query.	 *	 * @param	lifeTime	expected network life time in seconds.	 * @param	sensorQuery	sensor query.  Its samplePeriod will be set.	 * @param healthQuery health monitoring query. Its samplePeriod will be set.  */	public void estimateSamplePeriods(long lifeTime, TASKQuery sensorQuery, TASKQuery healthQuery)	{	    long samplePeriod = EstimateLifetime.lifetimeToSamplePeriod(lifeTime,					(long)EstimateLifetime.maxVReading, // XXX assumes fully charged batteries					SENSOR_COST_UJ,					sensorQuery.getSelectEntries().size(),					1 /* XXX Assume just one message per epoch ! */ );	    sensorQuery.setSamplePeriod((int)samplePeriod);	    healthQuery.setSamplePeriod((int)samplePeriod);	}	/**	 * Returns the name of all the clientinfo's registered with the	 * TASKServer.  Clientinfo's are opaque client states (e.g., layout of 	 * sensor space) stored at the server.	 */	public String[] getClientInfos() 	{		Socket conn = null;		ObjectOutputStream outStream = null;		ObjectInputStream inStream = null;		Vector clientInfos = null;		String[] clientInfoNames;		try		{			conn = getConnection();			outStream = new ObjectOutputStream(conn.getOutputStream());			inStream = new ObjectInputStream(conn.getInputStream());			outStream.writeShort(TASKServer.GET_CLIENTINFOS);			outStream.flush();			clientInfos = (Vector)inStream.readObject();			outStream.close();			inStream.close();			conn.close();			System.out.println("Vector of ClientInfo names received");		}		catch (Exception e)		{			try			{				if (conn != null)					conn.close();				if (outStream != null)					outStream.close();				if (inStream != null)					inStream.close();			}			catch (Exception ex)			{			}			e.printStackTrace();		}		clientInfoNames = new String[clientInfos.size()];		for (int i = 0; i < clientInfos.size(); i++)			clientInfoNames[i] = (String)clientInfos.elementAt(i);		return clientInfoNames;	};	/**	 * Look up the cleintinfo by name.	 *	 * @param	name	name of clientinfo to look up.	 * @return	clientinfo corresponding to name.	 */	public TASKClientInfo getClientInfo(String name) 	{		Socket conn = null;		ObjectOutputStream outStream = null;		ObjectInputStream inStream = null;		TASKClientInfo clientInfo = null;		try		{			conn = getConnection();			outStream = new ObjectOutputStream(conn.getOutputStream());			inStream = new ObjectInputStream(conn.getInputStream());			outStream.writeShort(TASKServer.GET_CLIENTINFO);			outStream.writeObject(name);			outStream.flush();			clientInfo = (TASKClientInfo)inStream.readObject();			outStream.close();			inStream.close();			conn.close();		}		catch (Exception e)		{			try			{				if (conn != null)					conn.close();				if (outStream != null)					outStream.close();				if (inStream != null)					inStream.close();			}			catch (Exception ex)			{			}			e.printStackTrace();		}		return clientInfo;	};	/**	 * Create a new clientinfo with the TASKServer.	 *	 * @param	clientinfo	new clientinfo to be created by TASKServer.	 * @return	true if successful, false otherwise	 */	public int addClientInfo(TASKClientInfo clientInfo) 	{		Socket conn = null;		ObjectOutputStream outStream = null;		ObjectInputStream inStream = null;		int error = TASKError.SOCKET_IO_EXCEPTION;		try		{			conn = getConnection();			outStream = new ObjectOutputStream(conn.getOutputStream());			inStream = new ObjectInputStream(conn.getInputStream());			outStream.writeShort(TASKServer.ADD_CLIENTINFO);			outStream.writeObject(clientInfo);			outStream.flush();			error = inStream.readInt();			outStream.close();			inStream.close();			conn.close();		}		catch (Exception e)		{			try			{				if (conn != null)					conn.close();				if (outStream != null)					outStream.close();				if (inStream != null)					inStream.close();			}			catch (Exception ex)			{			}			e.printStackTrace();		}		return error;	};	/**	 * Delete a clientinfo and all its related moteinfo.	 *	 * @param	name 	name of clientinfo to be deleted.	 * @return	true if successful, false otherwise	 */	public int deleteClientInfo(String name) 	{		Socket conn = null;		ObjectOutputStream outStream = null;		ObjectInputStream inStream = null;		int error = TASKError.SOCKET_IO_EXCEPTION;		try		{			conn = getConnection();			outStream = new ObjectOutputStream(conn.getOutputStream());			inStream = new ObjectInputStream(conn.getInputStream());			outStream.writeShort(TASKServer.DELETE_CLIENTINFO);			outStream.writeObject(name);			outStream.flush();			error = inStream.readInt();			outStream.close();			inStream.close();			conn.close();		}		catch (Exception e)		{			try			{				if (conn != null)					conn.close();				if (outStream != null)					outStream.close();				if (inStream != null)					inStream.close();			}			catch (Exception ex)			{			}			e.printStackTrace();		}		return error;	};	/**	 * Get client information about a certain mote	 *	 * @param	moteId	id of the mote	 * @return	client information about the particular mote	 */	public TASKMoteClientInfo getMoteClientInfo(int moteId) 	{		Socket conn = null;		ObjectOutputStream outStream = null;		ObjectInputStream inStream = null;		TASKMoteClientInfo moteClientInfo = null;		try		{			conn = getConnection();			outStream = new ObjectOutputStream(conn.getOutputStream());			inStream = new ObjectInputStream(conn.getInputStream());			outStream.writeShort(TASKServer.GET_MOTECLIENTINFO);			outStream.writeInt(moteId);			outStream.flush();			moteClientInfo = (TASKMoteClientInfo)inStream.readObject();			outStream.close();			inStream.close();			conn.close();		}		catch (Exception e)		{			try			{				if (conn != null)					conn.close();				if (outStream != null)					outStream.close();				if (inStream != null)					inStream.close();			}			catch (Exception ex)			{			}			e.printStackTrace();		}		return moteClientInfo;	};	/**	 * Add client information about a new mote or overwrite information	 * about an existing mote.	 *	 * @param	moteClientInfo	mote client information	 * @return	true if successful, false otherwise	 */	public int addMote(TASKMoteClientInfo moteClientInfo) 	{		Socket conn = null;		ObjectOutputStream outStream = null;		ObjectInputStream inStream = null;		int error = TASKError.SOCKET_IO_EXCEPTION;		try		{			conn = getConnection();			outStream = new ObjectOutputStream(conn.getOutputStream());			inStream = new ObjectInputStream(conn.getInputStream());			outStream.writeShort(TASKServer.ADD_MOTE);			outStream.writeObject(moteClientInfo);			outStream.flush();			error = inStream.readInt();			outStream.close();			inStream.close();			conn.close();		}		catch (Exception e)		{			try			{				if (conn != null)					conn.close();				if (outStream != null)					outStream.close();				if (inStream != null)					inStream.close();			}			catch (Exception ex)			{			}			e.printStackTrace();		}		return error;	};	/**	 * Delete a mote, which deletes its clientinfo.	 *	 * @param	moteId	mote id.	 * @return	true if successful, false otherwise	 */	public int deleteMote(int moteId) 	{		Socket conn = null;		ObjectOutputStream outStream = null;		ObjectInputStream inStream = null;		int error = TASKError.SOCKET_IO_EXCEPTION;		try		{			conn = getConnection();			outStream = new ObjectOutputStream(conn.getOutputStream());			inStream = new ObjectInputStream(conn.getInputStream());			outStream.writeShort(TASKServer.DELETE_MOTE);			outStream.writeInt(moteId);			outStream.flush();			error = inStream.readInt();			outStream.close();			inStream.close();			conn.close();		}		catch (Exception e)		{			try			{				if (conn != null)					conn.close();				if (outStream != null)					outStream.close();				if (inStream != null)					inStream.close();			}			catch (Exception ex)			{			}			e.printStackTrace();		}		return error;	};	/**	 * Delete all motes associated with a clientinfo	 *	 * @param	clientInfoName	a clientinfo name	 * @return	an error code as defined in TASKError	 */	public int deleteMote(String clientInfoName)	{		Socket conn = null;		ObjectOutputStream outStream = null;		ObjectInputStream inStream = null;		int error = TASKError.SOCKET_IO_EXCEPTION;		try		{			conn = getConnection();			outStream = new ObjectOutputStream(conn.getOutputStream());			inStream = new ObjectInputStream(conn.getInputStream());			outStream.writeShort(TASKServer.DELETE_MOTES);			outStream.writeObject(clientInfoName);			outStream.flush();			error = inStream.readInt();			outStream.close();			inStream.close();			conn.close();		}		catch (Exception e)		{			try			{				if (conn != null)					conn.close();				if (outStream != null)					outStream.close();				if (inStream != null)					inStream.close();			}			catch (Exception ex)			{			}			e.printStackTrace();		}		return error;	};	/**	 * Get information about all motes in one shot.	 *	 * @param	clientinfoName	name of clientinfo.	 * @return	a Vector of TASKMoteClientInfo for all known motes.	 */	public Vector getAllMoteClientInfo(String clientinfoName) 	{		Socket conn = null;		ObjectOutputStream outStream = null;		ObjectInputStream inStream = null;		Vector moteClientInfos = null;		try		{			conn = getConnection();			outStream = new ObjectOutputStream(conn.getOutputStream());			inStream = new ObjectInputStream(conn.getInputStream());

⌨️ 快捷键说明

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