mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-01-30 15:52:12 +01:00
A few more synchronized statements and deep cloning of objects
This commit is contained in:
parent
943dd82a75
commit
63f750c51e
@ -1,6 +1,7 @@
|
|||||||
package org.openpilot.uavtalk;
|
package org.openpilot.uavtalk;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.ListIterator;
|
import java.util.ListIterator;
|
||||||
import java.util.Observer;
|
import java.util.Observer;
|
||||||
@ -128,8 +129,7 @@ public abstract class UAVObject {
|
|||||||
// this.mutex = new QMutex(QMutex::Recursive);
|
// this.mutex = new QMutex(QMutex::Recursive);
|
||||||
};
|
};
|
||||||
|
|
||||||
public void initialize(int instID) {
|
public synchronized void initialize(int instID) {
|
||||||
// QMutexLocker locker(mutex);
|
|
||||||
this.instID = instID;
|
this.instID = instID;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,9 +146,8 @@ public abstract class UAVObject {
|
|||||||
* @throws Exception
|
* @throws Exception
|
||||||
* When unable to unpack a field
|
* When unable to unpack a field
|
||||||
*/
|
*/
|
||||||
public void initializeFields(List<UAVObjectField> fields, ByteBuffer data,
|
public synchronized void initializeFields(List<UAVObjectField> fields, ByteBuffer data,
|
||||||
int numBytes) {
|
int numBytes) {
|
||||||
// TODO: QMutexLocker locker(mutex);
|
|
||||||
this.numBytes = numBytes;
|
this.numBytes = numBytes;
|
||||||
this.fields = fields;
|
this.fields = fields;
|
||||||
// Initialize fields
|
// Initialize fields
|
||||||
@ -267,15 +266,13 @@ public abstract class UAVObject {
|
|||||||
* Get the number of fields held by this object
|
* Get the number of fields held by this object
|
||||||
*/
|
*/
|
||||||
public int getNumFields() {
|
public int getNumFields() {
|
||||||
// QMutexLocker locker(mutex);
|
|
||||||
return fields.size();
|
return fields.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the object's fields
|
* Get the object's fields
|
||||||
*/
|
*/
|
||||||
public List<UAVObjectField> getFields() {
|
public synchronized List<UAVObjectField> getFields() {
|
||||||
// QMutexLocker locker(mutex);
|
|
||||||
return fields;
|
return fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -286,7 +283,6 @@ public abstract class UAVObject {
|
|||||||
* @returns The field or NULL if not found
|
* @returns The field or NULL if not found
|
||||||
*/
|
*/
|
||||||
public UAVObjectField getField(String name) {
|
public UAVObjectField getField(String name) {
|
||||||
// QMutexLocker locker(mutex);
|
|
||||||
// Look for field
|
// Look for field
|
||||||
ListIterator<UAVObjectField> li = fields.listIterator();
|
ListIterator<UAVObjectField> li = fields.listIterator();
|
||||||
while (li.hasNext()) {
|
while (li.hasNext()) {
|
||||||
@ -307,8 +303,7 @@ public abstract class UAVObject {
|
|||||||
* @returns The number of bytes copied
|
* @returns The number of bytes copied
|
||||||
* @note The array must already have enough space allocated for the object
|
* @note The array must already have enough space allocated for the object
|
||||||
*/
|
*/
|
||||||
public int pack(ByteBuffer dataOut) throws Exception {
|
public synchronized int pack(ByteBuffer dataOut) throws Exception {
|
||||||
// QMutexLocker locker(mutex);
|
|
||||||
if (dataOut.remaining() < getNumBytes())
|
if (dataOut.remaining() < getNumBytes())
|
||||||
throw new Exception("Not enough bytes in ByteBuffer to pack object");
|
throw new Exception("Not enough bytes in ByteBuffer to pack object");
|
||||||
int numBytes = 0;
|
int numBytes = 0;
|
||||||
@ -329,7 +324,7 @@ public abstract class UAVObject {
|
|||||||
* @throws Exception
|
* @throws Exception
|
||||||
* @returns The number of bytes copied
|
* @returns The number of bytes copied
|
||||||
*/
|
*/
|
||||||
public int unpack(ByteBuffer dataIn) {
|
public synchronized int unpack(ByteBuffer dataIn) {
|
||||||
if( dataIn == null )
|
if( dataIn == null )
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -532,8 +527,17 @@ public abstract class UAVObject {
|
|||||||
/**
|
/**
|
||||||
* Java specific functions
|
* Java specific functions
|
||||||
*/
|
*/
|
||||||
public UAVObject clone() {
|
public synchronized UAVObject clone() {
|
||||||
return (UAVObject) clone();
|
UAVObject newObj = clone();
|
||||||
|
List<UAVObjectField> newFields = new ArrayList<UAVObjectField>();
|
||||||
|
ListIterator<UAVObjectField> li = fields.listIterator();
|
||||||
|
while(li.hasNext()) {
|
||||||
|
UAVObjectField nf = li.next().clone();
|
||||||
|
nf.initialize(newObj);
|
||||||
|
newFields.add(nf);
|
||||||
|
}
|
||||||
|
newObj.initializeFields(newFields, ByteBuffer.allocate(numBytes), numBytes);
|
||||||
|
return newObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -618,6 +618,17 @@ public class UAVObjectField {
|
|||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UAVObjectField clone()
|
||||||
|
{
|
||||||
|
UAVObjectField newField = new UAVObjectField(new String(name), new String(units), type,
|
||||||
|
new ArrayList<String>(elementNames),
|
||||||
|
new ArrayList<String>(options));
|
||||||
|
newField.initialize(obj);
|
||||||
|
newField.data = data;
|
||||||
|
return newField;
|
||||||
|
}
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
private String units;
|
private String units;
|
||||||
private FieldType type;
|
private FieldType type;
|
||||||
@ -626,7 +637,7 @@ public class UAVObjectField {
|
|||||||
private int numElements;
|
private int numElements;
|
||||||
private int numBytesPerElement;
|
private int numBytesPerElement;
|
||||||
private int offset;
|
private int offset;
|
||||||
private Object data;
|
|
||||||
private UAVObject obj;
|
private UAVObject obj;
|
||||||
|
protected Object data;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,29 @@ package org.openpilot.uavtalk;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.ListIterator;
|
import java.util.ListIterator;
|
||||||
|
import java.util.Observable;
|
||||||
|
import java.util.Observer;
|
||||||
|
|
||||||
public class UAVObjectManager {
|
public class UAVObjectManager {
|
||||||
|
|
||||||
|
public class CallbackListener extends Observable {
|
||||||
|
public void event (UAVObject obj) {
|
||||||
|
setChanged();
|
||||||
|
notifyObservers(obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private CallbackListener newInstance = new CallbackListener();
|
||||||
|
public void addNewInstanceObserver(Observer o) {
|
||||||
|
synchronized(newInstance) {
|
||||||
|
newInstance.addObserver(o);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private CallbackListener newObject = new CallbackListener();
|
||||||
|
public void addNewObjectObserver(Observer o) {
|
||||||
|
synchronized(newObject) {
|
||||||
|
newObject.addObserver(o);
|
||||||
|
}
|
||||||
|
}
|
||||||
private final int MAX_INSTANCES = 10;
|
private final int MAX_INSTANCES = 10;
|
||||||
|
|
||||||
// Use array list to store objects since rarely added or deleted
|
// Use array list to store objects since rarely added or deleted
|
||||||
@ -79,11 +99,12 @@ public class UAVObjectManager {
|
|||||||
UAVDataObject newObj = obj.clone(instID);
|
UAVDataObject newObj = obj.clone(instID);
|
||||||
newObj.initialize(mobj);
|
newObj.initialize(mobj);
|
||||||
instList.add(newObj);
|
instList.add(newObj);
|
||||||
// emit new instance signal
|
newInstance.event(newObj);
|
||||||
}
|
}
|
||||||
obj.initialize(mobj);
|
obj.initialize(mobj);
|
||||||
//emit new instance signal
|
//emit new instance signal
|
||||||
instList.add(obj);
|
instList.add(obj);
|
||||||
|
newInstance.event(obj);
|
||||||
|
|
||||||
instIter = instList.listIterator();
|
instIter = instList.listIterator();
|
||||||
while(instIter.hasNext()) {
|
while(instIter.hasNext()) {
|
||||||
@ -101,13 +122,13 @@ public class UAVObjectManager {
|
|||||||
UAVDataObject cobj = obj.clone(instId);
|
UAVDataObject cobj = obj.clone(instId);
|
||||||
cobj.initialize(mobj);
|
cobj.initialize(mobj);
|
||||||
instList.add(cobj);
|
instList.add(cobj);
|
||||||
// emit newInstance(cobj);
|
newInstance.event(cobj);
|
||||||
}
|
}
|
||||||
// Finally, initialize the actual object instance
|
// Finally, initialize the actual object instance
|
||||||
obj.initialize(mobj);
|
obj.initialize(mobj);
|
||||||
// Add the actual object instance in the list
|
// Add the actual object instance in the list
|
||||||
instList.add(obj);
|
instList.add(obj);
|
||||||
//emit newInstance(obj);
|
newInstance.event(obj);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,81 +170,67 @@ public class UAVObjectManager {
|
|||||||
/**
|
/**
|
||||||
* Same as getObjects() but will only return DataObjects.
|
* Same as getObjects() but will only return DataObjects.
|
||||||
*/
|
*/
|
||||||
public List< List<UAVDataObject> > getDataObjects()
|
public synchronized List< List<UAVDataObject> > getDataObjects()
|
||||||
{
|
{
|
||||||
assert(false); // TOOD This
|
List< List<UAVDataObject> > dObjects = new ArrayList< List<UAVDataObject> > ();
|
||||||
return new ArrayList<List<UAVDataObject>>();
|
|
||||||
|
|
||||||
/* QMutexLocker locker(mutex);
|
|
||||||
QList< QList<UAVDataObject*> > dObjects;
|
|
||||||
|
|
||||||
// Go through objects and copy to new list when types match
|
// Go through objects and copy to new list when types match
|
||||||
for (int objidx = 0; objidx < objects.length(); ++objidx)
|
ListIterator<List<UAVObject>> objIt = objects.listIterator(0);
|
||||||
{
|
|
||||||
if (objects[objidx].length() > 0)
|
// Check if this object type is already in the list
|
||||||
{
|
while(objIt.hasNext()) {
|
||||||
// Check type
|
List<UAVObject> instList = objIt.next();
|
||||||
UAVDataObject* obj = dynamic_cast<UAVDataObject*>(objects[objidx][0]);
|
|
||||||
if (obj != NULL)
|
// If no instances skip
|
||||||
{
|
if(instList.size() == 0)
|
||||||
// Create instance list
|
continue;
|
||||||
QList<UAVDataObject*> list;
|
|
||||||
// Go through instances and cast them to UAVDataObject, then add to list
|
// If meta data skip
|
||||||
for (int instidx = 0; instidx < objects[objidx].length(); ++instidx)
|
if(instList.get(0).isMetadata())
|
||||||
{
|
continue;
|
||||||
obj = dynamic_cast<UAVDataObject*>(objects[objidx][instidx]);
|
|
||||||
if (obj != NULL)
|
List<UAVDataObject> newInstList = new ArrayList<UAVDataObject>();
|
||||||
{
|
ListIterator<UAVObject> instIt = instList.listIterator();
|
||||||
list.append(obj);
|
while(instIt.hasNext()) {
|
||||||
}
|
newInstList.add((UAVDataObject) instIt.next());
|
||||||
}
|
|
||||||
// Append to object list
|
|
||||||
dObjects.append(list);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}*/
|
dObjects.add(newInstList);
|
||||||
|
}
|
||||||
// Done
|
// Done
|
||||||
|
return dObjects;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Same as getObjects() but will only return MetaObjects.
|
* Same as getObjects() but will only return MetaObjects.
|
||||||
*/
|
*/
|
||||||
public List <List<UAVMetaObject> > getMetaObjects()
|
public synchronized List <List<UAVMetaObject> > getMetaObjects()
|
||||||
{
|
{
|
||||||
assert(false); // TODO
|
List< List<UAVMetaObject> > mObjects = new ArrayList< List<UAVMetaObject> > ();
|
||||||
return new ArrayList< List<UAVMetaObject> >();
|
|
||||||
/*
|
|
||||||
QMutexLocker locker(mutex);
|
|
||||||
QList< QList<UAVMetaObject*> > mObjects;
|
|
||||||
|
|
||||||
// Go through objects and copy to new list when types match
|
// Go through objects and copy to new list when types match
|
||||||
for (int objidx = 0; objidx < objects.length(); ++objidx)
|
ListIterator<List<UAVObject>> objIt = objects.listIterator(0);
|
||||||
{
|
|
||||||
if (objects[objidx].length() > 0)
|
// Check if this object type is already in the list
|
||||||
{
|
while(objIt.hasNext()) {
|
||||||
// Check type
|
List<UAVObject> instList = objIt.next();
|
||||||
UAVMetaObject* obj = dynamic_cast<UAVMetaObject*>(objects[objidx][0]);
|
|
||||||
if (obj != NULL)
|
// If no instances skip
|
||||||
{
|
if(instList.size() == 0)
|
||||||
// Create instance list
|
continue;
|
||||||
QList<UAVMetaObject*> list;
|
|
||||||
// Go through instances and cast them to UAVMetaObject, then add to list
|
// If meta data skip
|
||||||
for (int instidx = 0; instidx < objects[objidx].length(); ++instidx)
|
if(!instList.get(0).isMetadata())
|
||||||
{
|
continue;
|
||||||
obj = dynamic_cast<UAVMetaObject*>(objects[objidx][instidx]);
|
|
||||||
if (obj != NULL)
|
List<UAVMetaObject> newInstList = new ArrayList<UAVMetaObject>();
|
||||||
{
|
ListIterator<UAVObject> instIt = instList.listIterator();
|
||||||
list.append(obj);
|
while(instIt.hasNext()) {
|
||||||
}
|
newInstList.add((UAVMetaObject) instIt.next());
|
||||||
}
|
|
||||||
// Append to object list
|
|
||||||
mObjects.append(list);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
mObjects.add(newInstList);
|
||||||
}
|
}
|
||||||
// Done
|
// Done
|
||||||
return mObjects;
|
return mObjects;
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -270,7 +277,6 @@ public class UAVObjectManager {
|
|||||||
*/
|
*/
|
||||||
public synchronized UAVObject getObject(String name, int objId, int instId)
|
public synchronized UAVObject getObject(String name, int objId, int instId)
|
||||||
{
|
{
|
||||||
//QMutexLocker locker(mutex);
|
|
||||||
// Check if this object type is already in the list
|
// Check if this object type is already in the list
|
||||||
ListIterator<List<UAVObject>> objIter = objects.listIterator();
|
ListIterator<List<UAVObject>> objIter = objects.listIterator();
|
||||||
while(objIter.hasNext()) {
|
while(objIter.hasNext()) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user