2006-08-21 14:11:16 +02:00
|
|
|
|
/*
|
2008-10-28 13:50:38 +01:00
|
|
|
|
* Copyright (c) 2008, Swedish Institute of Computer Science.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2008-10-28 13:50:38 +01:00
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
|
* without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*
|
|
|
|
|
* $Id: MoteInterface.java,v 1.4 2008/10/28 12:50:38 fros4943 Exp $
|
2006-08-21 14:11:16 +02:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package se.sics.cooja;
|
|
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.Observable;
|
|
|
|
|
import javax.swing.JPanel;
|
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
|
import org.jdom.Element;
|
|
|
|
|
|
2008-10-28 13:50:38 +01:00
|
|
|
|
import se.sics.cooja.interfaces.PolledAfterActiveTicks;
|
|
|
|
|
import se.sics.cooja.interfaces.PolledAfterAllTicks;
|
|
|
|
|
import se.sics.cooja.interfaces.PolledBeforeActiveTicks;
|
|
|
|
|
import se.sics.cooja.interfaces.PolledBeforeAllTicks;
|
|
|
|
|
|
2006-08-21 14:11:16 +02:00
|
|
|
|
/**
|
2008-10-28 13:50:38 +01:00
|
|
|
|
* A mote interface represents a mote property. Typically, this is a simulated
|
|
|
|
|
* hardware peripheral such as a button or LEDs. This may also be a property
|
|
|
|
|
* that the mote itself is unaware of, for example the current position of
|
2007-01-10 15:57:42 +01:00
|
|
|
|
* the mote.
|
2008-10-28 13:50:38 +01:00
|
|
|
|
*
|
2007-01-10 15:57:42 +01:00
|
|
|
|
* Interfaces are the main way for the simulator to interact with a simulated
|
|
|
|
|
* mote.
|
2008-10-28 13:50:38 +01:00
|
|
|
|
*
|
|
|
|
|
* Each interface can be polled before and after mote ticks.
|
|
|
|
|
* This is controlled by implementing the correct Java interfaces,
|
|
|
|
|
* such as PolledBeforeActiveTicks.
|
|
|
|
|
*
|
|
|
|
|
* @see PolledBeforeActiveTicks
|
|
|
|
|
* @see PolledAfterActiveTicks
|
|
|
|
|
* @see PolledBeforeAllTicks
|
|
|
|
|
* @see PolledAfterAllTicks
|
|
|
|
|
*
|
|
|
|
|
* @author Fredrik <EFBFBD>sterlind
|
2006-08-21 14:11:16 +02:00
|
|
|
|
*/
|
|
|
|
|
public abstract class MoteInterface extends Observable {
|
|
|
|
|
private static Logger logger = Logger.getLogger(MoteInterface.class);
|
|
|
|
|
|
|
|
|
|
/**
|
2007-01-10 15:57:42 +01:00
|
|
|
|
* This method creates an instance of the given class with the given mote as
|
|
|
|
|
* constructor argument. Instead of calling the interface constructors
|
|
|
|
|
* directly this method may be used.
|
2008-10-28 13:50:38 +01:00
|
|
|
|
*
|
2007-01-10 15:57:42 +01:00
|
|
|
|
* @param interfaceClass
|
|
|
|
|
* Mote interface class
|
|
|
|
|
* @param mote
|
|
|
|
|
* Mote that will hold the interface
|
2006-08-21 14:11:16 +02:00
|
|
|
|
* @return Mote interface instance
|
|
|
|
|
*/
|
2007-01-10 15:57:42 +01:00
|
|
|
|
public static final MoteInterface generateInterface(
|
|
|
|
|
Class<? extends MoteInterface> interfaceClass, Mote mote) {
|
2006-08-21 14:11:16 +02:00
|
|
|
|
try {
|
2008-10-28 13:50:38 +01:00
|
|
|
|
MoteInterface instance = interfaceClass.getConstructor(
|
2006-08-21 14:11:16 +02:00
|
|
|
|
new Class[] { Mote.class }).newInstance(new Object[] { mote });
|
2007-01-10 15:57:42 +01:00
|
|
|
|
|
2006-08-21 14:11:16 +02:00
|
|
|
|
return instance;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.fatal("Exception when creating " + interfaceClass + ": " + e);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-10-28 13:50:38 +01:00
|
|
|
|
* Returns a panel visualizing this interface. This could for
|
2007-01-10 15:57:42 +01:00
|
|
|
|
* example show last messages sent/received for a radio interface, or logged
|
|
|
|
|
* message for a log interface.
|
2008-10-28 13:50:38 +01:00
|
|
|
|
*
|
2007-01-10 15:57:42 +01:00
|
|
|
|
* All panels returned from this method must later be released for memory
|
|
|
|
|
* reasons.
|
2008-10-28 13:50:38 +01:00
|
|
|
|
*
|
|
|
|
|
* This method may return null.
|
|
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
|
* @see #releaseInterfaceVisualizer(JPanel)
|
|
|
|
|
* @return Interface visualizer or null
|
|
|
|
|
*/
|
|
|
|
|
public abstract JPanel getInterfaceVisualizer();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method should be called when a visualizer panel is no longer in use.
|
2007-01-10 15:57:42 +01:00
|
|
|
|
* Any resources of that panel, for example registered observers, will be
|
|
|
|
|
* released.
|
2008-10-28 13:50:38 +01:00
|
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
|
* @see #getInterfaceVisualizer()
|
2007-01-10 15:57:42 +01:00
|
|
|
|
* @param panel
|
|
|
|
|
* A interface visualizer panel fetched earlier for this mote
|
|
|
|
|
* interface.
|
2006-08-21 14:11:16 +02:00
|
|
|
|
*/
|
|
|
|
|
public abstract void releaseInterfaceVisualizer(JPanel panel);
|
2007-01-10 15:57:42 +01:00
|
|
|
|
|
2006-08-21 14:11:16 +02:00
|
|
|
|
/**
|
2008-10-28 13:50:38 +01:00
|
|
|
|
* Returns total energy (mQ) consumed by this interface.
|
|
|
|
|
*
|
2007-01-10 15:57:42 +01:00
|
|
|
|
* The interface is responsible to gather information about the current
|
|
|
|
|
* internal state, and calculate whatever energy it needs in that state and
|
|
|
|
|
* during one tick.
|
2008-10-28 13:50:38 +01:00
|
|
|
|
*
|
|
|
|
|
* This method is typically used battery interface, to keep track of the
|
|
|
|
|
* mote energy consumption.
|
|
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
|
* @see se.sics.cooja.interfaces.Battery
|
2008-10-28 13:50:38 +01:00
|
|
|
|
* @return Total energy consumption of mote interface
|
2006-08-21 14:11:16 +02:00
|
|
|
|
*/
|
2008-10-28 13:50:38 +01:00
|
|
|
|
public abstract double energyConsumption();
|
2006-08-21 14:11:16 +02:00
|
|
|
|
|
|
|
|
|
/**
|
2007-01-10 15:57:42 +01:00
|
|
|
|
* Returns XML elements representing the current config of this mote
|
|
|
|
|
* interface. This is fetched by the simulator for example when saving a
|
|
|
|
|
* simulation configuration file. For example an IP interface may return one
|
|
|
|
|
* element with the mote IP address. This method should however not return
|
|
|
|
|
* state specific information such as a log history. (All nodes are restarted
|
|
|
|
|
* when loading a simulation.)
|
2008-10-28 13:50:38 +01:00
|
|
|
|
*
|
2007-01-10 15:57:42 +01:00
|
|
|
|
* @see #setConfigXML(Collection, boolean)
|
2006-08-21 14:11:16 +02:00
|
|
|
|
* @return XML elements representing the current interface config
|
|
|
|
|
*/
|
|
|
|
|
public abstract Collection<Element> getConfigXML();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the current mote interface config depending on the given XML elements.
|
2008-10-28 13:50:38 +01:00
|
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
|
* @see #getConfigXML()
|
2007-01-10 15:57:42 +01:00
|
|
|
|
* @param configXML
|
|
|
|
|
* Config XML elements
|
|
|
|
|
* @param visAvailable
|
|
|
|
|
* Is this object allowed to show a visualizer?
|
2006-08-21 14:11:16 +02:00
|
|
|
|
*/
|
2007-01-10 15:57:42 +01:00
|
|
|
|
public abstract void setConfigXML(Collection<Element> configXML,
|
|
|
|
|
boolean visAvailable);
|
|
|
|
|
|
2006-08-21 14:11:16 +02:00
|
|
|
|
}
|