[Tinyos-2-commits] CVS: tinyos-2.x/support/sdk/java/net/tinyos/message Message.java, 1.5, 1.6 Receiver.java, 1.4, 1.5

dmm rincon at users.sourceforge.net
Tue May 29 09:44:52 PDT 2007


Update of /cvsroot/tinyos/tinyos-2.x/support/sdk/java/net/tinyos/message
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv26467

Modified Files:
	Message.java Receiver.java 
Log Message:
Added getSerialPacket() and setSerialPacket() methods to the Message class so we can access packet header information from the Message itself.

Index: Message.java
===================================================================
RCS file: /cvsroot/tinyos/tinyos-2.x/support/sdk/java/net/tinyos/message/Message.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** Message.java	12 Dec 2006 18:22:59 -0000	1.5
--- Message.java	29 May 2007 16:44:50 -0000	1.6
***************
*** 50,588 ****
  public class Message implements Cloneable {
  
!     /**
!      * The maximum number of characters read from an 8-bit array field
!      * being converted into a Java String.
!      */
!     public static final int MAX_CONVERTED_STRING_LENGTH = 512;
  
!     /** 
!      * The underlying byte array storing the data for this message. 
[...1150 lines suppressed...]
! 
!   /**
!    * 
!    * @return the SerialPacket this message originated from, if it was set
!    *     externally
!    */
!   public SerialPacket getSerialPacket() {
!     return serialPacket;
!   }
! 
!   /**
!    * 
!    * @param mySerialPacket the SerialPacket this message originated from
!    */
!   protected void setSerialPacket(SerialPacket mySerialPacket) {
!     serialPacket = mySerialPacket;
!   }
!   
!   
  }

Index: Receiver.java
===================================================================
RCS file: /cvsroot/tinyos/tinyos-2.x/support/sdk/java/net/tinyos/message/Receiver.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** Receiver.java	12 Dec 2006 18:22:59 -0000	1.4
--- Receiver.java	29 May 2007 16:44:50 -0000	1.5
***************
*** 44,189 ****
  import net.tinyos.packet.*;
  import java.util.*;
- import java.io.*;
  
  /**
   * Receiver class (receive tinyos messages).
!  *
!  * A receiver class provides a simple interface built on Message for
!  * receiving tinyos messages from a SerialForwarder
!  *
!  * @version	1, 15 Jul 2002
!  * @author	David Gay
   */
  public class Receiver implements PacketListenerIF {
!     public static final boolean DEBUG = false;
!     public static final boolean DISPLAY_ERROR_MSGS = true;
  
!     Hashtable templateTbl; // Mapping from AM type to msgTemplate
!     PhoenixSource source;
!     /**
!      * Inner class representing a single MessageListener and its
!      * associated Message template.
!      */
!     class msgTemplate {
! 	Message template;
! 	MessageListener listener;
! 	msgTemplate(Message template, MessageListener listener) {
! 	    this.template = template;
! 	    this.listener = listener;
! 	}
  
! 	public boolean equals(Object o) {
! 	    try {
! 		msgTemplate mt = (msgTemplate)o;
! 		if (mt.template.getClass().equals(this.template.getClass()) &&
! 		    mt.listener.equals(this.listener)) {
! 		    return true;
! 		}
! 	    } catch (Exception e) {
! 		return false;
! 	    }
! 	    return false;
! 	}
  
! 	public int hashCode() {
! 	    return listener.hashCode();
! 	}
      }
  
!     /**
!      * Create a receiver messages from forwarder of any group id and
!      * of active message type m.getType()
!      * When such a message is received, a new instance of m's class is
!      * created with the received data and send to listener.messageReceived
!      * @param forwarder packet source to listen to
!      */
!     public Receiver(PhoenixSource forwarder) {
! 	this.templateTbl = new Hashtable();
! 	this.source = forwarder;
! 	forwarder.registerPacketListener(this);
      }
  
!     /**
!      * Register a particular listener for a particular message type.
!      * More than one listener can be registered for each message type.
!      * @param template specify message type and template we're listening for
!      * @param listener destination for received messages
!      */
!     public void registerListener(Message template, MessageListener listener) {
! 	Integer amType = new Integer(template.amType());
! 	Vector vec = (Vector)templateTbl.get(amType);
! 	if (vec == null) {
! 	    vec = new Vector();
! 	}
! 	vec.addElement(new msgTemplate(template, listener));
! 	templateTbl.put(amType, vec);
      }
  
!     /**
!      * Stop listening for messages of the given type with the given listener.
!      * @param template specify message type and template we're listening for
!      * @param listener destination for received messages
!      */
!     public void deregisterListener(Message template, MessageListener listener) {
! 	Integer amType = new Integer(template.amType());
! 	Vector vec = (Vector)templateTbl.get(amType);
! 	if (vec == null) {
! 	    throw new IllegalArgumentException("No listeners registered for message type "+template.getClass().getName()+" (AM type "+template.amType()+")");
! 	}
! 	msgTemplate mt = new msgTemplate(template, listener);
! 	// Remove all occurrences
! 	while (vec.removeElement(mt)) ;
! 	if (vec.size() == 0) templateTbl.remove(amType);
      }
  
!     private void error(msgTemplate temp, String msg) {
! 	System.err.println("receive error for " + temp.template.getClass().getName() +
! 			   " (AM type " + temp.template.amType() +
! 			   "): " + msg);
      }
  
!     public void packetReceived(byte[] packet) {
! 	if (DEBUG) Dump.dump("Received message", packet);
  
! 	if (packet[0] != Serial.TOS_SERIAL_ACTIVE_MESSAGE_ID)
! 	    return; // not for us.
  
! 	SerialPacket msg = new SerialPacket(packet, 1);
! 	Integer type = new Integer(msg.get_header_type());
! 	Vector vec = (Vector)templateTbl.get(type);
! 	if (vec == null) {
! 	    if (DEBUG) Dump.dump("Received packet with type " + type +
! 				 ", but no listeners registered", packet);
! 	    return;
! 	}
! 	int length = msg.get_header_length();
  
! 	Enumeration en = vec.elements();
! 	while (en.hasMoreElements()) {
! 	    msgTemplate temp = (msgTemplate)en.nextElement();
  
! 	    Message received;
  
! 	    // Erk - end up cloning the message multiple times in case
! 	    // different templates used for different listeners
! 	    try {
! 		received = temp.template.clone(length);
! 		received.dataSet(msg.dataGet(), msg.offset_data(0) + msg.baseOffset(), 0, length);
! 	    } catch (ArrayIndexOutOfBoundsException e) {
! 		error(temp, "invalid length message received (too long)");
! 		continue;
! 	    } catch (Exception e) {
! 		error(temp, "couldn't clone message!");
! 		continue;
! 	    }
  
! 	    /* Messages that are longer than the template might have
! 	       a variable-sized array at their end */
! 	    if (temp.template.dataGet().length > length) {
! 		error(temp, "invalid length message received (too short)");
! 		continue;
! 	    }
! 	    temp.listener.messageReceived(msg.get_header_dest(), received);
! 	}
      }
  }
--- 44,213 ----
  import net.tinyos.packet.*;
  import java.util.*;
  
  /**
   * Receiver class (receive tinyos messages).
!  * 
!  * A receiver class provides a simple interface built on Message for receiving
!  * tinyos messages from a SerialForwarder
!  * 
!  * @version 1, 15 Jul 2002
!  * @author David Gay
   */
  public class Receiver implements PacketListenerIF {
!   public static final boolean DEBUG = false;
  
!   public static final boolean DISPLAY_ERROR_MSGS = true;
  
!   Hashtable templateTbl; // Mapping from AM type to msgTemplate
  
!   PhoenixSource source;
! 
!   /**
!    * Inner class representing a single MessageListener and its associated
!    * Message template.
!    */
!   class msgTemplate {
!     Message template;
! 
!     MessageListener listener;
! 
!     msgTemplate(Message template, MessageListener listener) {
!       this.template = template;
!       this.listener = listener;
      }
  
!     public boolean equals(Object o) {
!       try {
!         msgTemplate mt = (msgTemplate) o;
!         if (mt.template.getClass().equals(this.template.getClass())
!             && mt.listener.equals(this.listener)) {
!           return true;
!         }
!       } catch (Exception e) {
!         return false;
!       }
!       return false;
      }
  
!     public int hashCode() {
!       return listener.hashCode();
      }
+   }
  
!   /**
!    * Create a receiver messages from forwarder of any group id and of active
!    * message type m.getType() When such a message is received, a new instance of
!    * m's class is created with the received data and send to
!    * listener.messageReceived
!    * 
!    * @param forwarder
!    *          packet source to listen to
!    */
!   public Receiver(PhoenixSource forwarder) {
!     this.templateTbl = new Hashtable();
!     this.source = forwarder;
!     forwarder.registerPacketListener(this);
!   }
! 
!   /**
!    * Register a particular listener for a particular message type. More than one
!    * listener can be registered for each message type.
!    * 
!    * @param template
!    *          specify message type and template we're listening for
!    * @param listener
!    *          destination for received messages
!    */
!   public void registerListener(Message template, MessageListener listener) {
!     Integer amType = new Integer(template.amType());
!     Vector vec = (Vector) templateTbl.get(amType);
!     if (vec == null) {
!       vec = new Vector();
      }
+     vec.addElement(new msgTemplate(template, listener));
+     templateTbl.put(amType, vec);
+   }
  
!   /**
!    * Stop listening for messages of the given type with the given listener.
!    * 
!    * @param template
!    *          specify message type and template we're listening for
!    * @param listener
!    *          destination for received messages
!    */
!   public void deregisterListener(Message template, MessageListener listener) {
!     Integer amType = new Integer(template.amType());
!     Vector vec = (Vector) templateTbl.get(amType);
!     if (vec == null) {
!       throw new IllegalArgumentException(
!           "No listeners registered for message type "
!               + template.getClass().getName() + " (AM type "
!               + template.amType() + ")");
      }
+     msgTemplate mt = new msgTemplate(template, listener);
+     // Remove all occurrences
+     while (vec.removeElement(mt))
+       ;
+     if (vec.size() == 0)
+       templateTbl.remove(amType);
+   }
  
!   private void error(msgTemplate temp, String msg) {
!     System.err.println("receive error for "
!         + temp.template.getClass().getName() + " (AM type "
!         + temp.template.amType() + "): " + msg);
!   }
  
!   public void packetReceived(byte[] packet) {
!     if (DEBUG)
!       Dump.dump("Received message", packet);
  
!     if (packet[0] != Serial.TOS_SERIAL_ACTIVE_MESSAGE_ID)
!       return; // not for us.
  
!     SerialPacket msg = new SerialPacket(packet, 1);
!     Integer type = new Integer(msg.get_header_type());
!     Vector vec = (Vector) templateTbl.get(type);
!     if (vec == null) {
!       if (DEBUG)
!         Dump.dump("Received packet with type " + type
!             + ", but no listeners registered", packet);
!       return;
!     }
!     int length = msg.get_header_length();
  
!     Enumeration en = vec.elements();
!     while (en.hasMoreElements()) {
!       msgTemplate temp = (msgTemplate) en.nextElement();
  
!       Message received;
  
!       // Erk - end up cloning the message multiple times in case
!       // different templates used for different listeners
!       try {
!         received = temp.template.clone(length);
!         received.dataSet(msg.dataGet(), SerialPacket.offset_data(0) + msg.baseOffset(),
!             0, length);
!         received.setSerialPacket(msg); 
!         
!       } catch (ArrayIndexOutOfBoundsException e) {
!         error(temp, "invalid length message received (too long)");
!         continue;
!       } catch (Exception e) {
!         error(temp, "couldn't clone message!");
!         continue;
!       }
! 
!       /*
!        * Messages that are longer than the template might have a variable-sized
!        * array at their end
!        */
!       if (temp.template.dataGet().length > length) {
!         error(temp, "invalid length message received (too short)");
!         continue;
!       }
!       temp.listener.messageReceived(msg.get_header_dest(), received);
      }
+   }
  }



More information about the Tinyos-2-commits mailing list