rewriting mote interfaces for faster simulation execution.

ipv4 interface
This commit is contained in:
fros4943 2008-10-28 10:12:43 +00:00
parent ae80b1a585
commit 892ed6f316

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2006, Swedish Institute of Computer Science. * Copyright (c) 2008, Swedish Institute of Computer Science.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: ContikiIPAddress.java,v 1.2 2007/01/09 10:05:19 fros4943 Exp $ * $Id: ContikiIPAddress.java,v 1.3 2008/10/28 10:12:43 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -41,9 +41,9 @@ import se.sics.cooja.contikimote.ContikiMoteInterface;
import se.sics.cooja.interfaces.IPAddress; import se.sics.cooja.interfaces.IPAddress;
/** /**
* This class represents an uIP IP address. * uIP IP address.
* *
* It needs write access to the following core variables: * Contiki variables:
* <ul> * <ul>
* <li>char simIPa * <li>char simIPa
* <li>char simIPb * <li>char simIPb
@ -52,22 +52,22 @@ import se.sics.cooja.interfaces.IPAddress;
* <li>char simIPChanged (1 if new IP should be set) * <li>char simIPChanged (1 if new IP should be set)
* </ul> * </ul>
* <p> * <p>
* The new IP will be "simIPa.simIPb.simIPc.simIPd". Dependency core interfaces *
* are: * The new IP will be "simIPa.simIPb.simIPc.simIPd".
* Note that this mote interface does not detect if Contiki changes IP address.
*
* Core interface:
* <ul> * <ul>
* <li>ip_interface * <li>ip_interface
* </ul> * </ul>
* *
* This observable notifies observers if the IP address is set or changed. * This observable notifies when the IP address is set or altered.
* *
* @author Fredrik Osterlind * @author Fredrik Österlind
*/ */
public class ContikiIPAddress extends IPAddress implements ContikiMoteInterface { public class ContikiIPAddress extends IPAddress implements ContikiMoteInterface {
private SectionMoteMemory moteMem = null; private SectionMoteMemory moteMem = null;
private static Logger logger = Logger.getLogger(ContikiIPAddress.class); private static Logger logger = Logger.getLogger(ContikiIPAddress.class);
private boolean setIP;
char a = 0, b = 0, c = 0, d = 0;
/** /**
* Creates an interface to the IP address at mote. * Creates an interface to the IP address at mote.
@ -79,7 +79,6 @@ public class ContikiIPAddress extends IPAddress implements ContikiMoteInterface
*/ */
public ContikiIPAddress(Mote mote) { public ContikiIPAddress(Mote mote) {
this.moteMem = (SectionMoteMemory) mote.getMemory(); this.moteMem = (SectionMoteMemory) mote.getMemory();
setIP = false;
} }
public static String[] getCoreInterfaceDependencies() { public static String[] getCoreInterfaceDependencies() {
@ -87,30 +86,28 @@ public class ContikiIPAddress extends IPAddress implements ContikiMoteInterface
} }
public String getIPString() { public String getIPString() {
return "" + (int) a + "." + (int) b + "." + (int) c + "." + (int) d; return
(int) moteMem.getByteValueOf("simIPa")
+ "." +
(int) moteMem.getByteValueOf("simIPb")
+ "." +
(int) moteMem.getByteValueOf("simIPc")
+ "." +
(int) moteMem.getByteValueOf("simIPd");
} }
public void setIPString(String ipAddress) { public void setIPString(String ipAddress) {
String[] ipArray = ipAddress.split("\\."); String[] ipArray = ipAddress.split("\\.");
if (ipArray.length < 4) { if (ipArray.length < 4) {
logger.warn("Could not set ip address (" + ipAddress + ")"); logger.warn("Could not set ip address (" + ipAddress + ")");
} else } else {
setIPNumber((char) Integer.parseInt(ipArray[0]), (char) Integer setIPNumber((char) Integer.parseInt(ipArray[0]), (char) Integer
.parseInt(ipArray[1]), (char) Integer.parseInt(ipArray[2]), .parseInt(ipArray[1]), (char) Integer.parseInt(ipArray[2]),
(char) Integer.parseInt(ipArray[3])); (char) Integer.parseInt(ipArray[3]));
} }
public void setIPNumber(char a, char b, char c, char d) {
setIP = true;
this.a = a;
this.b = b;
this.c = c;
this.d = d;
} }
public void doActionsBeforeTick() { public void setIPNumber(char a, char b, char c, char d) {
if (setIP) {
setIP = false;
moteMem.setByteValueOf("simIPa", (byte) a); moteMem.setByteValueOf("simIPa", (byte) a);
moteMem.setByteValueOf("simIPb", (byte) b); moteMem.setByteValueOf("simIPb", (byte) b);
moteMem.setByteValueOf("simIPc", (byte) c); moteMem.setByteValueOf("simIPc", (byte) c);
@ -120,26 +117,19 @@ public class ContikiIPAddress extends IPAddress implements ContikiMoteInterface
setChanged(); setChanged();
notifyObservers(); notifyObservers();
} }
}
public void doActionsAfterTick() {
// Nothing to do
}
public JPanel getInterfaceVisualizer() { public JPanel getInterfaceVisualizer() {
JPanel panel = new JPanel(); JPanel panel = new JPanel();
final JLabel ipLabel = new JLabel(); final JLabel ipLabel = new JLabel();
ipLabel.setText("Current address: " + (int) a + "." + (int) b + "." ipLabel.setText("IPv4 address: " + getIPString());
+ (int) c + "." + (int) d);
panel.add(ipLabel); panel.add(ipLabel);
Observer observer; Observer observer;
this.addObserver(observer = new Observer() { this.addObserver(observer = new Observer() {
public void update(Observable obs, Object obj) { public void update(Observable obs, Object obj) {
ipLabel.setText("Current address: " + (int) a + "." + (int) b + "." ipLabel.setText("IPv4 address: " + getIPString());
+ (int) c + "." + (int) d);
} }
}); });