[Tinyos-commits] CVS: tinyos-1.x/tools/python/pytos/util MessageSnooper.py, NONE, 1.1

Kamin Whitehouse kaminw at users.sourceforge.net
Wed Sep 28 00:01:23 PDT 2005


Update of /cvsroot/tinyos/tinyos-1.x/tools/python/pytos/util
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15271/pytos/util

Added Files:
	MessageSnooper.py 
Log Message:
Listen.py is a new tool that pretty prints all incoming packets, even in they come in over a routing layer such as Drain.  MessageSnooper is a library that should know about all routing layers.  It uses introspection to see which routing layers are compiled into the application and registers for all packet types for all routing layers

--- NEW FILE: MessageSnooper.py ---
#!/usr/bin/python
#$Id: MessageSnooper.py,v 1.1 2005/09/28 07:01:21 kaminw Exp $

# "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."
#
# @author Kamin Whitehouse 
#

import sys
import pytos.Comm as Comm         
import pytos.tools.Drain as Drain         
import threading

def registerAllMsgs(msgs, msgQueue, connection) :
  for msgName in msgs._msgNames :
    msg = msgs[msgName]
    connection.register( msg , msgQueue )
        
class MessageSnooper( object ) :
  """This module offers \"register\" and \"unregister\" functions that
  take a messageHandler argument but no message type argument.
  Instead, the messageHandler will receive ALL incoming messages.  It
  currently handles local receive, drain messages, rpc messages, and
  ramSymbol messages.  Any new routing protocols should be
  incorporated into this module.

  usage:
  snooper = MessageSnooper(app)
  snooper.start
  snooper.stop
  snooper.register(callbackFcn)
  snooper.unregister(callbackFcn)
  """
   
  def __init__( self , app="" ) :

    self.app = app
    self.listeners = []

    msgQueue = Comm.MessageQueue(10)
        
    #register the msgQueue for all message types with localComm
    for conn in self.app.connections :
      if isinstance(conn, Comm.Comm) :
        registerAllMsgs(self.app.msgs, msgQueue, conn)

    #register the msgQueue for all message types with drain
    if "AM_DRAINMSG" in self.app.enums._enums :
      for conn in self.app.connections :
        if isinstance(conn, Drain.Drain) :
          registerAllMsgs(self.app.msgs, msgQueue, conn)
      #unregister DrainMsg with localComm
      for conn in self.app.connections :
        if isinstance(conn, Comm.Comm) :
          conn.unregister(self.app.msgs.DrainMsg, msgQueue)

    self.running = True
    msgThread = threading.Thread(target=self.processMessages,
                                 args=(msgQueue,))
    msgThread.setDaemon(True)
    msgThread.start()

  def processMessages(self, msgQueue) :
    while True :
      (addr,msg) = msgQueue.get()
      if self.running == True :
        for listener in self.listeners :
          listener.messageReceived(addr, msg)

  def stop(self) :
      self.running = False

  def start(self) :
      self.running = True

  def register(self, msgHandler) :
    self.listeners.append(msgHandler)
    
  def unregister(self, msgHandler) :
    self.listeners.remove(msgHandler)



More information about the Tinyos-commits mailing list