@@ -176,14 +176,11 @@ public synchronized BowlerDatagram sendSynchronusly(BowlerDatagram sendable){
176176 }
177177
178178 if (getLastSyncronousResponse () == null ){
179- try {
180- //new RuntimeException().printStackTrace();
181- Log .error ("No response from device, no response in " +(System .currentTimeMillis ()-startOfReciveTime )+" ms" );
182-
183- } catch (Exception e ) {
184- clearLastSyncronousResponse ();
185- executingLock .unlock ();
186- throw new RuntimeException (e );
179+ Log .error ("No response from device, no response in " +(System .currentTimeMillis ()-startOfReciveTime )+" ms" );
180+ if ( BowlerDatagram .isUseBowlerV4 ()){
181+ //If the ping fails to get a response, try the older bowler format
182+ Log .error ("Switching to legacy parser" );
183+ BowlerDatagram .setUseBowlerV4 (false );
187184 }
188185 }
189186 BowlerDatagram b = getLastSyncronousResponse ();
@@ -939,7 +936,8 @@ public ArrayList<RpcEncapsulation> getRpcList(String namespace,MACAddress addr)
939936 */
940937 public BowlerDatagram send (BowlerAbstractCommand command ,MACAddress addr , int retry ) throws NoConnectionAvailableException , InvalidResponseException {
941938 for (int i =0 ;i <retry ;i ++){
942-
939+ if (i !=0 )
940+ Log .error ("Re-sending" );
943941 BowlerDatagram ret ;
944942 try {
945943 ret = send ( command ,addr );
@@ -949,10 +947,12 @@ public BowlerDatagram send(BowlerAbstractCommand command,MACAddress addr, int re
949947 //if(!ret.getRPC().contains("_err"))
950948
951949 return ret ;
950+ }else {
951+ throw new InvalidResponseException ("No response from device" );
952952 }
953- }catch (MalformattedDatagram | NullPointerException e ){
953+ }catch (MalformattedDatagram | InvalidResponseException | NullPointerException e ){
954954 Log .error ("Sending Synchronus packet and there was a failure, will retry " +(retry -i -1 )+" more times" );
955- ThreadUtil .wait (150 *i );
955+ ThreadUtil .wait (150 *i );
956956 }
957957
958958 }
@@ -980,13 +980,9 @@ public BowlerDatagram send(BowlerAbstractCommand command,MACAddress addr) throws
980980 if (back !=null ){
981981 addr .setValues (back .getAddress ());
982982 }
983- try {
984- return command .validate (back );
985- }catch (InvalidResponseException ex ){
986- ex .printStackTrace ();
987- Log .error ("Failed to send synchronusly: " +cmd +"\r \n Got>> " +back );
988- return null ;
989- }
983+
984+ return command .validate (back );
985+
990986 //BowlerDatagramFactory.freePacket(cmd);
991987
992988 }
@@ -1010,7 +1006,7 @@ public PingCommand() {
10101006 public boolean ping (MACAddress mac ) {
10111007 try {
10121008 //Log.warning("Ping device:");
1013- BowlerDatagram bd = send (new PingCommand (),mac ,2 );
1009+ BowlerDatagram bd = send (new PingCommand (),mac ,5 );
10141010 if (bd !=null ){
10151011 BowlerDatagramFactory .freePacket (bd );
10161012 return true ;
@@ -1022,6 +1018,7 @@ public boolean ping(MACAddress mac) {
10221018 Log .error ("No connection is available." );
10231019 e .printStackTrace ();
10241020 }
1021+
10251022 return false ;
10261023 }
10271024
@@ -1161,7 +1158,7 @@ public void run() {
11611158 private boolean runPacketUpdate () {
11621159 try {
11631160 if (loadPacketFromPhy (bytesToPacketBuffer ))
1164- bytesToPacketBuffer = new ByteList ();
1161+ bytesToPacketBuffer . clear ();
11651162 } catch (Exception e ) {
11661163 if (isConnected ()){
11671164 Log .error ("Data read failed " +e .getMessage ());
@@ -1198,50 +1195,34 @@ public void kill() {
11981195
11991196 public boolean loadPacketFromPhy (ByteList bytesToPacketBuffer ) throws NullPointerException , IOException {
12001197 if (dataIns !=null ){
1201- while (getDataIns ().available ()>0 ){
1202- long dataRead =System .currentTimeMillis ();
1203- //we want to run this until the buffer is clear or a packet is found
1204- int b = getDataIns ().read ();
1205- long dataReadEnd =System .currentTimeMillis ();
1206- if (b <0 ){
1207- Log .error ("Stream is broken - unexpected: claimed to have " +getDataIns ().available ()+" bytes, read in " +b );
1208- //reconnect();
1209- //something went wrong
1210- new RuntimeException ().printStackTrace ();
1211- return false ;
1212- }else {
1213-
1214- bytesToPacketBuffer .add (b );
1215- long dataAdd =System .currentTimeMillis ();
1216- BowlerDatagram bd = BowlerDatagramFactory .build (bytesToPacketBuffer );
1217- long dataBuild =System .currentTimeMillis ();
1218- if (bd !=null ) {
1219- Log .info ("\n R<<" +bd );
1220- onDataReceived (bd );
1221-
1222- long dataSet =System .currentTimeMillis ();
1223- //bytesToPacketBuffer.clear();
1224-
1225- long bufferClear =System .currentTimeMillis ();
1226-
1227- if ((System .currentTimeMillis ()-getLastWrite ())>(getSleepTime ()*(getPercentagePrint () /100.0 ))&& bd .isSyncronous () && syncListen ==null ){
1228- Log .info ("Packet recive took more then " +getPercentagePrint ()+"%. " +
1229- //"\nRaw receive\t"+(start-getLastWrite() )+"" +
1230- //"\nStart Section\t"+(dataRead- start)+"" +
1231- "\n Data Read\t " +(dataReadEnd -dataRead )+"" +
1232- "\n Add data\t " +(dataAdd -dataReadEnd )+
1233- "\n Build Packet\t " +(dataBuild -dataAdd )+
1234- "\n Set Packet\t " +(dataSet -dataBuild )+
1235- "\n Clear Packet buffer\t " +(bufferClear -dataSet )
1236- );
1237- }
1238-
1239- //Packet found, break the loop and deal with it
1240- return true ;
1241- }
1198+ if (getDataIns ().available ()==0 )
1199+ return false ;
1200+ byte [] buff = new byte [getDataIns ().available ()];
1201+ //we want to run this until the buffer is clear or a packet is found
1202+ int b = getDataIns ().read (buff );
1203+
1204+ if (b <0 || buff .length != b ){
1205+ Log .error ("Stream is broken - unexpected: claimed to have " +getDataIns ().available ()+" bytes, read in " +b );
1206+ //reconnect();
1207+ //something went wrong
1208+ new RuntimeException (" Buffer attempted to read " +buff .length +" got " +b ).printStackTrace ();
1209+ return false ;
1210+ }else {
1211+ bytesToPacketBuffer .add (buff );
1212+ BowlerDatagram bd =null ;
1213+ try {
1214+ bd = BowlerDatagramFactory .build (bytesToPacketBuffer );
1215+ }catch (Exception ex ){
1216+ ex .printStackTrace ();
1217+ }
1218+ if (bd !=null ) {
1219+ Log .info ("\n R<<" +bd );
1220+ onDataReceived (bd );
1221+ return true ;
12421222 }
1243- //Log.info("buffer: "+buffer);
12441223 }
1224+ //ThreadUtil.wait(1);
1225+
12451226 }else {
12461227 Log .error ("Data In is null" );
12471228 }
0 commit comments