tinydbnetwork.java

来自「用于传感器网络的节点操作系统 TinyOS 结构设计非常有意思」· Java 代码 · 共 518 行 · 第 1/2 页

JAVA
518
字号
	    try {		if (getBaseBcastInterval() == Integer.MAX_VALUE) {		    Thread.currentThread().sleep(1000);		    continue;		} else		    Thread.currentThread().sleep(getBaseBcastInterval()); 				if (knownQids.size() != 0) {		  neighborhoodResetCount--;		  		  if (neighborhoodResetCount <= 0) {		    neighborhoodResetCount = ROUNDS_TO_NEIGHBORHOOD_RESET;		    		    nwSizeEstimate = (short)(2 * (double)neighborhood.size());		    if (nwSizeEstimate == 0) nwSizeEstimate = 16; //default instead of 0!		    if (TinyDBMain.debug) System.out.println("New neighborhood size = " + nwSizeEstimate);		    neighborhood = new Hashtable();		    		  }				  int cnt = 0;		  boolean isTopo = false;		  do {		      if (knownQids.size() > 127) System.out.println("WARNING : TOO MANY QUERIES!");		      if (++curQuery >= knownQids.size()) {			  curQuery = 0;		      }		      qid = (byte)((Integer)knownQids.elementAt(curQuery)).intValue();		      if (queries.elementAt(qid) != null) {			  			  if (((TinyDBQuery)queries.elementAt(qid)).active()) break; //advertise for the first "active" query		      }		      cnt++;		  } while ( cnt <= knownQids.size());		  idx++;		  if (idx == 256) idx = 0;		  		  short epochNo;		  try {		      Object lastEpoch = lastEpochs.elementAt(qid);		      if (lastEpoch == null)			  epochNo = 0;		      else			  epochNo = (short)(((Integer)lastEpoch).intValue() + 1);		  } catch (ArrayIndexOutOfBoundsException e) {		      epochNo = 0;		  }		  m =	QueryResult.generateDataMessage(qid,							(char)idx,							epochNo,							(byte)(nwSizeEstimate & 0x00FF),							(byte)255);		  if (!sendingQuery) {		      if (TinyDBMain.debug) System.out.println("Sending hearbeat for query : " + qid);		      mif.send(MoteIF.TOS_BCAST_ADDR,m);	    		  } else		      if (TinyDBMain.debug) System.out.println("Sending query.");		}	    } catch (Exception e) {		e.printStackTrace();	    }	}    }    //QueryListener methods    /** A new query has begun running.  Use this to track the queries that we need to	send data messages for (see run() above.)    */    public void addQuery(TinyDBQuery q) {	knownQids.addElement(new Integer(q.getId()));	if (queries.size() < ( q.getId() + 1))	    queries.setSize(q.getId() + 1);	queries.setElementAt(q, q.getId());	/* Set the rate at which we broadcast data packets to be the	   epoch duration of the fastest query */	if (q.epochDur < baseBcastInterval) 	    baseBcastInterval = Math.max(q.epochDur,MIN_EPOCH_DUR);    }    /** A query has stopped running */    public void removeQuery(TinyDBQuery q) {	Integer qid = new Integer(q.getId());	try {	    knownQids.remove(qid);	    processedListeners.remove(qid);	    qidListeners.remove(qid);	    queries.setElementAt(null,q.getId());	    //this was the fastest running query?  if so, choose a new epoch duration	    if (q.epochDur == baseBcastInterval) {		baseBcastInterval = Integer.MAX_VALUE;		for (int i = 0; i < queries.size(); i++) {		    TinyDBQuery tmpq = (TinyDBQuery)queries.elementAt(i);		    if (tmpq != null)			if (tmpq.epochDur < baseBcastInterval)			    baseBcastInterval = tmpq.epochDur;		}	    }	} catch (NoSuchElementException e) {	    System.out.println("Error removing query.");	} catch (ArrayIndexOutOfBoundsException e) {	}    }    /** Send the specified query out over the radio */    public void sendQuery(TinyDBQuery q) throws java.io.IOException{	sendingQuery = true;	Iterator it = q.messageIterator(); //generate messages for this query	try {	    if (TinyDBMain.debug) System.out.println("Sending query.");	    while (it.hasNext()) {		Message msg = (Message)it.next();		mif.send(MoteIF.TOS_BCAST_ADDR,msg);		try {		    Thread.currentThread().sleep(1000);		} catch (InterruptedException e) {		}	    }	} catch (java.io.IOException e) {	    throw e;	}	sendingQuery = false;    }    /** Send message to abort the specified query out over the radio */    public void abortQuery(TinyDBQuery query)    {	for (int i = 0; i < 5; i++)	    {		try		    {			mif.send(MoteIF.TOS_BCAST_ADDR,query.abortMessage());			Thread.currentThread().sleep(200);		    }		catch (Exception e)		    {			e.printStackTrace();		    }	    }	//HACK -- use this as a hint to start heartbeating about another query!	query.setActive(false);    }    /** Return baseBcastInterval, which controls how often data messages are 	sent out from the base station so that nodes can see	the root.    */	    public static int getBaseBcastInterval()    {  	return baseBcastInterval;    }    /** Set the base station data message broadcast interval */    public static void setBaseBcastInterval(int interval)    {	baseBcastInterval = interval;    }    static final short BCAST_ADDR = (short)-1;    /*    public boolean postProcessKeyEvent(KeyEvent e) {	if (e.getID() == KeyEvent.KEY_RELEASED) {	    switch (e.getKeyCode()) {	    case KeyEvent.VK_F3: //F3 == Reset		try {		    Thread.currentThread().sleep(200);		    aif.sendAM(CommandMsgs.resetCmd(BCAST_ADDR), CommandMsgs.CMD_MSG_TYPE, (short)-1);		} catch (Exception ex) {		    System.out.println("Reset failed: ");		    ex.printStackTrace();		}		System.out.println("Reset!");		break;	    case KeyEvent.VK_F4:  //F4 == reset the mote in the parallel port		try {		    Runtime.getRuntime().exec("uisp -dprog=dapa -dno-poll");		    Thread.currentThread().sleep(500);		} catch (Exception ex) {		    System.out.println("Reset failed: ");		    ex.printStackTrace();		}		System.out.println("Reset!");		break;	    case KeyEvent.VK_F9: //F9 == Set Signal Strength to 0		try {		    aif.sendAM(CommandMsgs.setPot(BCAST_ADDR,(char)0), CommandMsgs.CMD_MSG_TYPE, (short)-1);		} catch (Exception ex) {		    System.out.println("Set Pot failed: ");		    ex.printStackTrace();		}		System.out.println("Set pot to 0!");		break;	    case KeyEvent.VK_F10: //F10 == Set Signal Strength to 10		try {		    aif.sendAM(CommandMsgs.setPot(BCAST_ADDR, (char)10), CommandMsgs.CMD_MSG_TYPE, (short)-1);		} catch (Exception ex) {		    System.out.println("Set Pot Failed: ");		    ex.printStackTrace();		}		System.out.println("Set Pot to 10!");		break;	    case KeyEvent.VK_F11: //F10 == Set Signal Strength to 50		try {		    aif.sendAM(CommandMsgs.setPot(BCAST_ADDR, (char)50), CommandMsgs.CMD_MSG_TYPE, (short)-1);		} catch (Exception ex) {		    System.out.println("Set Pot Failed: ");		    ex.printStackTrace();		}		System.out.println("Set Pot to 50!");		break;	    case KeyEvent.VK_F12: //F10 == Set Signal Strength to 100		try {		    aif.sendAM(CommandMsgs.setPot(BCAST_ADDR, (char)100), CommandMsgs.CMD_MSG_TYPE, (short)-1);		} catch (Exception ex) {		    System.out.println("Set Pot Failed: ");		    ex.printStackTrace();		}		System.out.println("Set Pot to 100!");		break;	    }	    if (e.getKeyCode() >= '0' && e.getKeyCode() <= '9') {		int qid = e.getKeyCode() - KeyEvent.VK_0;		if (queries.size() <= qid) return true;		TinyDBQuery q = (TinyDBQuery)queries.elementAt(qid);		if (q != null) {		    if ((e.getModifiersEx() & KeyEvent.SHIFT_DOWN_MASK) > 0) {			System.out.println("STOPPING QUERY ID : " + qid);			abortQuery(q);			//stop the query		    } else {			System.out.println("SENDING QUERY ID : " + qid);			sendQuery(q);			//resend the query		    }		}	    }	}	    	return true;    }    */}

⌨️ 快捷键说明

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