15 package com.neuronrobotics.sdk.common;
49 for (
int i=0;i<getDefaultPoolSize();i++){
61 public static int getCurrentPoolSize(){
71 public static boolean validateFactory(BowlerDatagramFactory test){
75 throw new RuntimeException(
"Invalid factory generation of packet. Use BowlerDatagramFactory.getNextPacket()");
83 private static BowlerDatagram getNextPacket(){
84 BowlerDatagram ref =
new BowlerDatagram(
instance);
126 public static void freePacket(BowlerDatagram bd){
147 public static BowlerDatagram build(MACAddress addr, BowlerAbstractCommand cmd) {
148 BowlerDatagram bd = getNextPacket();
149 long start = System.currentTimeMillis();
152 addr =
new MACAddress();
155 bd.setMethod(cmd.getMethod());
156 bd.setNamespaceResolutionID((
byte) cmd.getNamespaceIndex());
157 bd.setData(cmd.getBytes());
162 Log.error(
"Timeout detected, duration = "+(System.currentTimeMillis()-start)+
", expected = "+BowlerDatagramFactory.getPacketTimeout());
174 public static BowlerDatagram build(ByteList buffer ){
175 BowlerDatagram buff= getNextPacket();
177 BowlerDatagram ret = build(buffer, buff );
190 private static BowlerDatagram build(ByteList buffer, BowlerDatagram staticMemory){
191 if((buffer.size()==0))
196 }
catch (Exception e){
197 Log.warning(
"Datagram builder first byte warning: "+e.getMessage());
201 while(fb!=BowlerDatagram.REVISION) {
205 if(buffer.size()==0){
212 }
catch (Exception e){
213 Log.warning(
"Datagram builder warning: "+e.getMessage());
222 if(buffer.size()<BowlerDatagram.HEADER_SIZE)
224 boolean check =
false;
225 while(check==
false) {
227 if( (buffer.get(0) != BowlerDatagram.REVISION)
228 || (!BowlerDatagram.CheckCRC(buffer,
false))){
229 if(buffer.get(0) != BowlerDatagram.REVISION)
230 Log.error(
"First Byte Fail (second attempt) Junk byte: "+String.format(
"%02x ", buffer.pop()));
232 Log.error(
"CRC check Fail (second attempt) Junk byte: "+String.format(
"%02x ", buffer.pop()));
237 }
catch (Exception e){
245 if (buffer.size()<BowlerDatagram.HEADER_SIZE) {
252 int len =buffer.getUnsigned(9);
255 Log.error(
"#*#*Warning, packet has no RPC, size: "+len);
257 int totalLen = len+BowlerDatagram.HEADER_SIZE;
259 if(BowlerDatagram.isUseBowlerV4())
261 staticMemory.setFree(
false,
instance);
263 if (buffer.size()>=(totalLen)){
265 ByteList rawContent =
new ByteList(buffer.popList(totalLen));
267 staticMemory.parse(rawContent);
269 }
catch(MalformattedDatagram m){
270 Log.error(
"Data CRC check Fail "+staticMemory);
271 failed = rawContent.size();
285 public static int getDefaultPoolSize() {
294 public static void setPoolSize(
int poolSize) {
295 BowlerDatagramFactory.poolDefaultSize = poolSize;
303 public static long getPacketTimeout() {
static int poolDefaultSize
static BowlerDatagram pool[]
static BowlerDatagramFactory instance
static long packetTimeout