package nl.ru.ai.projects.parrot.dronecontrol.simulator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.nio.ByteBuffer;
private static Simulator instance = null;
private native static Object[]
runLuaCode0(String code);
private native static ByteBuffer
getImage0(
int droneIndex,
int imageIndex);
private native static SimulatedDrone[]
getDrones0();
static {
System.loadLibrary("ParrotSimulator");
}
synchronized (Simulator.class) {
if (instance == null) {
instance = new Simulator();
} else {
return;
}
}
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
instance.cleanup();
}
});
runSimulator0(instance);
}
public static synchronized boolean waitReady() {
return waitReady0();
}
if (instance == null) {
Simulator tmpInstance = new Simulator();
if (initSimulator(tmpInstance)) {
instance = tmpInstance;
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
instance.cleanup();
}
});
}
} else {
if (!waitReady()) {
return null;
}
}
return instance;
}
private boolean videoActivated = true;
private boolean destroyed = false;
private ExternalTimerSynchronizer externalTimerSynchronizer = new ExternalTimerSynchronizer();
private List<SimulatedDrone> drones = new ArrayList<SimulatedDrone>();
}
if (destroyed) {
return;
}
drones.clear();
drones.addAll(Arrays.asList(getDrones0()));
}
synchronized (this) {
if (destroyed) {
return;
}
destroyed = true;
}
cleanupSimulator();
}
return !destroyed;
}
public synchronized byte[]
getImage(
int droneIndex,
int imageIndex) {
if (destroyed) {
return null;
}
ByteBuffer imageBuffer = getImage0(droneIndex, imageIndex);
if (imageBuffer == null) {
return null;
}
byte[] result = new byte[imageBuffer.capacity()];
imageBuffer.rewind();
imageBuffer.get(result);
return result;
}
public synchronized Object[]
runLuaCode(String code) {
if (destroyed) {
return null;
}
return runLuaCode0(code);
}
if (destroyed) {
return;
}
setCoupleTime0(b);
}
if (destroyed) {
return false;
}
return getCoupleTime0();
}
if (destroyed) {
return -1;
}
return getCurrentTimeStamp0();
}
if (destroyed) {
return -1;
}
return getLastRenderTimeStamp0();
}
if (destroyed) {
return 0;
}
updateDroneArray();
return drones.size();
}
public synchronized SimulatedDrone
getDrone(
int i) {
if (destroyed) {
return null;
}
updateDroneArray();
return drones.get(i);
}
if (destroyed) {
return;
}
videoActivated = b;
activateVideoInterface0(b);
}
if (destroyed) {
return false;
}
return videoActivated && isRenderingActivated();
}
if (destroyed) {
return;
}
activateRendering0(b);
}
if (destroyed) {
return false;
}
return isRenderingActivated0();
}
public void simSleep(
long milliseconds)
throws InterruptedException {
externalTimerSynchronizer.sleep(milliseconds);
}
}