throwing unknown variable exceptions

This commit is contained in:
fros4943 2008-02-11 14:04:16 +00:00
parent afebaa9e97
commit c8f1906479
1 changed files with 93 additions and 61 deletions

View File

@ -24,7 +24,7 @@
* (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;
@ -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,8 +112,9 @@ 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;
}
@ -131,16 +137,16 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
* 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,12 +156,15 @@ 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.
@ -165,8 +174,9 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
* @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();
}
@ -179,8 +189,9 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
* @return Size of section
*/
public int getSizeOfSection(int sectionNr) {
if (sectionNr >= sections.size())
if (sectionNr >= sections.size()) {
return 0;
}
return sections.elementAt(sectionNr).getSize();
}
@ -193,8 +204,9 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
* @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?
@ -348,8 +374,9 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
* otherwise
*/
public boolean includesAddr(int addr) {
if (data == null)
if (data == null) {
return false;
}
return (addr >= startAddr && addr < (startAddr + data.length));
}
@ -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();
}