use of com.neuronrobotics.sdk.common.BowlerDatagram in project java-bowler by NeuronRobotics.
the class BowlerDatagramFactoryTests method test.
@Test
public void test() {
// Must be a factor of default pool size
int testBifferSize = 10 * BowlerDatagramFactory.getDefaultPoolSize();
ArrayList<BowlerDatagram> myList = new ArrayList<BowlerDatagram>();
// verify initial state
if (BowlerDatagramFactory.getCurrentPoolSize() != BowlerDatagramFactory.getDefaultPoolSize()) {
fail();
}
for (int i = 0; i < testBifferSize; i++) {
myList.add(BowlerDatagramFactory.build(new MACAddress(), new PingCommand()));
}
for (BowlerDatagram b : myList) {
if (b.isFree()) {
// if any packets not marked as allocated
fail();
}
// System.out.println(b);
}
// wait for packets to timeout
ThreadUtil.wait((int) ((double) BowlerDatagramFactory.getPacketTimeout()) / 2);
for (BowlerDatagram b : myList) {
if (b.isFree())
// if any packets not marked as free too soon
fail();
// System.out.println(b);
}
// wait for packets to timeout
ThreadUtil.wait((int) ((double) BowlerDatagramFactory.getPacketTimeout()));
for (BowlerDatagram b : myList) {
if (!b.isFree())
// any packets that failed to timeout
fail();
}
// refill the array
myList.clear();
for (int i = 0; i < testBifferSize; i++) {
myList.add(BowlerDatagramFactory.build(new MACAddress(), new PingCommand()));
}
for (BowlerDatagram b : myList) {
if (b.isFree())
// if any packets not marked as before freeing
fail();
BowlerDatagramFactory.freePacket(b);
if (!b.isFree())
// if any packets not marked as free after freeing it
fail();
}
}
use of com.neuronrobotics.sdk.common.BowlerDatagram in project java-bowler by NeuronRobotics.
the class PacketValidationTest method packetTest.
@Test
public void packetTest() {
Log.enableInfoPrint();
BowlerDatagram bd = BowlerDatagramFactory.build(new MACAddress(), new PingCommand());
System.out.println(bd);
ByteList data = new ByteList(bd.getBytes());
System.out.println(data);
BowlerDatagram back = BowlerDatagramFactory.build(data);
if (back == null)
fail();
System.out.println(back);
}
use of com.neuronrobotics.sdk.common.BowlerDatagram in project java-bowler by NeuronRobotics.
the class PPMReaderChannel method updateValues.
private void updateValues() {
if (getChannel().getDevice().isLegacyParser()) {
BowlerDatagram b = null;
// System.out.println("Updating value map");
try {
b = getChannel().getDevice().send(new GetValueCommand(23));
} catch (Exception e) {
e.printStackTrace();
}
if (b != null) {
crossLinks = new int[6];
values = new int[6];
for (int i = 0; i < values.length; i++) {
values[i] = b.getData().getUnsigned(1 + i);
}
for (int i = 0; i < crossLinks.length; i++) {
crossLinks[i] = b.getData().getUnsigned(1 + 6 + i);
}
}
} else {
Object[] args = getChannel().getDevice().send("bcs.io.*;0.3;;", BowlerMethod.GET, "strm", new Object[] { 23 });
crossLinks = new int[6];
values = new int[6];
ByteList data = (ByteList) args[1];
Log.debug("PPM link data: " + data.size());
for (int i = 0; i < values.length; i++) {
values[i] = data.getUnsigned(i);
}
for (int i = 0; i < crossLinks.length; i++) {
crossLinks[i] = data.getUnsigned(6 + i);
}
}
}
use of com.neuronrobotics.sdk.common.BowlerDatagram in project java-bowler by NeuronRobotics.
the class SPIChannel method write.
/**
* This performs a full read/write transaction. The data is sent down, and the corosponding data is read back in.
* @param ss the index of the DyIO channel to use as a slave select pin for the SPI
* @param stream the Bytes to be sent out
* @return the data received
*/
public byte[] write(int ss, byte[] stream) {
if (dyio.isLegacyParser()) {
BowlerDatagram b = sendSPIStream(ss, stream);
if (b == null)
return new byte[0];
return b.getData().getBytes(2);
} else {
ByteList data = new ByteList(stream);
data.insert(0, (byte) ss);
dyio.send("bcs.io.*;0.3;;", BowlerMethod.POST, "strm", new Object[] { 0, data });
int timeout = 1000;
rx = null;
while (timeout-- > 0) {
ThreadUtil.wait(1);
if (rx != null) {
return rx;
}
}
return new byte[0];
}
}
use of com.neuronrobotics.sdk.common.BowlerDatagram in project java-bowler by NeuronRobotics.
the class UDPBowlerConnection method loadPacketFromPhy.
@Override
public BowlerDatagram loadPacketFromPhy(ByteList bytesToPacketBuffer) throws NullPointerException, IOException {
long start = System.currentTimeMillis();
Log.info("Waiting for UDP packet");
// Timeout the socket after 1 ms
udpSock.setSoTimeout(1);
// System.err.println("Timeout set "+(System.currentTimeMillis()-start));
start = System.currentTimeMillis();
try {
udpSock.receive(receivePacket);
} catch (SocketTimeoutException ste) {
return null;
} catch (Exception ex) {
// disconnect called
// Log. warning("Receive bailed out because of close");
ex.printStackTrace();
return null;
}
// System.err.println("Recv "+(System.currentTimeMillis()-start));
start = System.currentTimeMillis();
Log.info("Got UDP packet");
if (addrs == null)
addrs = new ArrayList<InetAddress>();
getAllAddresses().add(receivePacket.getAddress());
byte[] data = receivePacket.getData();
for (int i = 0; i < receivePacket.getLength(); i++) {
bytesToPacketBuffer.add(data[i]);
}
// System.err.println("copy "+(System.currentTimeMillis()-start));
start = System.currentTimeMillis();
BowlerDatagram bd = BowlerDatagramFactory.build(bytesToPacketBuffer);
// System.err.println("build "+(System.currentTimeMillis()-start));
return bd;
}
Aggregations