From c8f1906479548be3da8737e71d8f37736f6aad59 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Mon, 11 Feb 2008 14:04:16 +0000 Subject: [PATCH] throwing unknown variable exceptions --- .../java/se/sics/cooja/SectionMoteMemory.java | 154 +++++++++++------- 1 file changed, 93 insertions(+), 61 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/SectionMoteMemory.java b/tools/cooja/java/se/sics/cooja/SectionMoteMemory.java index 216c40a10..ba08719f1 100644 --- a/tools/cooja/java/se/sics/cooja/SectionMoteMemory.java +++ b/tools/cooja/java/se/sics/cooja/SectionMoteMemory.java @@ -1,7 +1,7 @@ /* * Copyright (c) 2006, Swedish Institute of Computer Science. All rights * reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, @@ -12,7 +12,7 @@ * 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 @@ -23,8 +23,8 @@ * 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: SectionMoteMemory.java,v 1.5 2007/02/02 11:02:15 fros4943 Exp $ + * + * $Id: SectionMoteMemory.java,v 1.6 2008/02/11 14:04:16 fros4943 Exp $ */ package se.sics.cooja; @@ -43,7 +43,7 @@ import se.sics.cooja.MoteMemory; * When an unhandled memory segment is set a new section is automatically * created for this segment. *

- * + * * @author Fredrik Osterlind */ public class SectionMoteMemory implements MoteMemory, AddressMemory { @@ -56,7 +56,7 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { /** * Create a new mote memory with information about which variables exist and * their relative memory addresses. - * + * * @param variableAddresses * Variable addresses */ @@ -73,12 +73,17 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { return names; } - public int getVariableAddress(String varName) { - if (!variableAddresses.containsKey(varName)) - return -1; + public int getVariableAddress(String varName) throws UnknownVariableException { + if (!variableAddresses.containsKey(varName)) { + throw new UnknownVariableException(varName); + } return ((Integer) variableAddresses.get(varName)).intValue(); } + public int getIntegerLength() { + return 4; + } + public void clearMemory() { sections.clear(); } @@ -107,14 +112,15 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { public int getTotalSize() { int totalSize = 0; - for (MoteMemorySection section : sections) + for (MoteMemorySection section : sections) { totalSize += section.getSize(); + } return totalSize; } /** * Returns the total number of sections in this memory. - * + * * @return Number of sections */ public int getNumberOfSections() { @@ -124,23 +130,23 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { /** * Removes a memory segment from this memory. The section containing the * segment may be split into two sections. - * + * * @param startAddr * Start address * @param size * Length */ public void removeSegmentFromMemory(int startAddr, int size) { - for (MoteMemorySection section : sections) + for (MoteMemorySection section : sections) { // Find section containing segment to remove if (section.includesAddr(startAddr) && section.includesAddr(startAddr + size - 1)) { MoteMemorySection oldSection = section; byte[] dataFirstPart = oldSection.getMemorySegment( - oldSection.startAddr, (int) (startAddr - oldSection.startAddr)); + oldSection.startAddr, (startAddr - oldSection.startAddr)); byte[] dataSecondPart = oldSection - .getMemorySegment(startAddr + size, (int) (oldSection.startAddr + .getMemorySegment(startAddr + size, (oldSection.startAddr + oldSection.getSize() - (startAddr + size))); MoteMemorySection newSectionFirstPart = new MoteMemorySection( @@ -150,51 +156,57 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { // Remove old section, add new sections sections.remove(oldSection); - if (newSectionFirstPart.getSize() > 0) + if (newSectionFirstPart.getSize() > 0) { sections.add(newSectionFirstPart); - if (newSectionSecondPart.getSize() > 0) + } + if (newSectionSecondPart.getSize() > 0) { sections.add(newSectionSecondPart); + } } + } } /** * Get start address of section at given position. - * + * * @param sectionNr * Section position * @return Start address of section */ public int getStartAddrOfSection(int sectionNr) { - if (sectionNr >= sections.size()) + if (sectionNr >= sections.size()) { return 0; + } return sections.elementAt(sectionNr).getStartAddr(); } /** * Get size of section at given position. - * + * * @param sectionNr * Section position * @return Size of section */ public int getSizeOfSection(int sectionNr) { - if (sectionNr >= sections.size()) + if (sectionNr >= sections.size()) { return 0; + } return sections.elementAt(sectionNr).getSize(); } /** * Get data of section at given position. - * + * * @param sectionNr * Section position * @return Data at section */ public byte[] getDataOfSection(int sectionNr) { - if (sectionNr >= sections.size()) + if (sectionNr >= sections.size()) { return null; + } return sections.elementAt(sectionNr).getData(); } @@ -203,20 +215,25 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { return variableAddresses.containsKey(varName); } - public int getIntValueOf(String varName) { + public int getIntValueOf(String varName) throws UnknownVariableException { // Get start address of variable - if (!variableAddresses.containsKey(varName)) - return -1; + if (!variableAddresses.containsKey(varName)) { + throw new UnknownVariableException(varName); + } int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); byte[] varData = getMemorySegment(varAddr, 4); + if (varData == null) { + throw new UnknownVariableException(varName); + } + int retVal = 0; int pos = 0; - retVal += ((int) (varData[pos++] & 0xFF)) << 24; - retVal += ((int) (varData[pos++] & 0xFF)) << 16; - retVal += ((int) (varData[pos++] & 0xFF)) << 8; - retVal += ((int) (varData[pos++] & 0xFF)) << 0; + retVal += ((varData[pos++] & 0xFF)) << 24; + retVal += ((varData[pos++] & 0xFF)) << 16; + retVal += ((varData[pos++] & 0xFF)) << 8; + retVal += ((varData[pos++] & 0xFF)) << 0; // TODO Check if small/big-endian when coming from JNI? retVal = Integer.reverseBytes(retVal); @@ -224,10 +241,11 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { return retVal; } - public void setIntValueOf(String varName, int newVal) { + public void setIntValueOf(String varName, int newVal) throws UnknownVariableException { // Get start address of variable - if (!variableAddresses.containsKey(varName)) - return; + if (!variableAddresses.containsKey(varName)) { + throw new UnknownVariableException(varName); + } int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); // TODO Check if small/big-endian when coming from JNI? @@ -245,21 +263,27 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { setMemorySegment(varAddr, varData); } - public byte getByteValueOf(String varName) { + public byte getByteValueOf(String varName) throws UnknownVariableException { // Get start address of variable - if (!variableAddresses.containsKey(varName)) - return -1; + if (!variableAddresses.containsKey(varName)) { + throw new UnknownVariableException(varName); + } int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); byte[] varData = getMemorySegment(varAddr, 1); + if (varData == null) { + throw new UnknownVariableException(varName); + } + return varData[0]; } - public void setByteValueOf(String varName, byte newVal) { + public void setByteValueOf(String varName, byte newVal) throws UnknownVariableException { // Get start address of variable - if (!variableAddresses.containsKey(varName)) - return; + if (!variableAddresses.containsKey(varName)) { + throw new UnknownVariableException(varName); + } int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); byte[] varData = new byte[1]; @@ -269,20 +293,22 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { setMemorySegment(varAddr, varData); } - public byte[] getByteArray(String varName, int length) { + public byte[] getByteArray(String varName, int length) throws UnknownVariableException { // Get start address of variable - if (!variableAddresses.containsKey(varName)) - return null; + if (!variableAddresses.containsKey(varName)) { + throw new UnknownVariableException(varName); + } int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); // TODO Check if small/big-endian when coming from JNI? return getMemorySegment(varAddr, length); } - public void setByteArray(String varName, byte[] data) { + public void setByteArray(String varName, byte[] data) throws UnknownVariableException { // Get start address of variable - if (!variableAddresses.containsKey(varName)) - return; + if (!variableAddresses.containsKey(varName)) { + throw new UnknownVariableException(varName); + } int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); // TODO Check if small/big-endian when coming from JNI? @@ -291,7 +317,7 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { /** * A memory section contains a byte array and a start address. - * + * * @author Fredrik Osterlind */ private class MoteMemorySection { @@ -301,7 +327,7 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { /** * Create a new memory section. - * + * * @param startAddr * Start address of section * @param data @@ -314,7 +340,7 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { /** * Returns start address of this memory section. - * + * * @return Start address */ public int getStartAddr() { @@ -323,7 +349,7 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { /** * Returns size of this memory section. - * + * * @return Size */ public int getSize() { @@ -332,7 +358,7 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { /** * Returns the entire byte array which defines this section. - * + * * @return Byte array */ public byte[] getData() { @@ -341,22 +367,23 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { /** * True if given address is part of this memory section. - * + * * @param addr * Address * @return True if given address is part of this memory section, false * otherwise */ public boolean includesAddr(int addr) { - if (data == null) + if (data == null) { return false; + } return (addr >= startAddr && addr < (startAddr + data.length)); } /** * Returns memory segment. - * + * * @param addr * Start address of memory segment * @param size @@ -371,7 +398,7 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { /** * Sets a memory segment. - * + * * @param addr * Start of memory segment * @param data @@ -437,9 +464,11 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { return null; } - for (int j = 0; j < sections.get(i).getSize(); j++) - if (this.sections.get(i).data[j] != anotherMem.getDataOfSection(i)[j]) + for (int j = 0; j < sections.get(i).getSize(); j++) { + if (this.sections.get(i).data[j] != anotherMem.getDataOfSection(i)[j]) { differences.add(new Integer(sections.get(i).startAddr + j)); + } + } System.err.println(); } return differences; @@ -448,8 +477,9 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { protected void printMemory() { for (int i = 0; i < sections.size(); i++) { System.err.print("Section[" + i + "]: "); - for (int j = 0; j < sections.get(i).getSize(); j++) + for (int j = 0; j < sections.get(i).getSize(); j++) { System.err.print(sections.get(i).getData()[j] + ","); + } System.err.println(); } } @@ -471,9 +501,11 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { } System.err.print("Section[" + i + "]: "); - for (int j = 0; j < sections.get(i).getSize(); j++) - if (this.sections.get(i).data[j] != anotherMem.getDataOfSection(i)[j]) + for (int j = 0; j < sections.get(i).getSize(); j++) { + if (this.sections.get(i).data[j] != anotherMem.getDataOfSection(i)[j]) { System.err.print(j + ","); + } + } System.err.println(); } } @@ -481,8 +513,8 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory { protected void printChecksum() { byte[] checksum = this.getChecksum(); System.err.print("Checksum: "); - for (int i = 0; i < checksum.length; i++) { - System.err.print(checksum[i] + ","); + for (byte element : checksum) { + System.err.print(element + ","); } System.err.println(); }