clean code

This commit is contained in:
CiufudeanDani 2022-03-30 12:52:38 +03:00
parent cbeb912820
commit ca4255943a

View File

@ -1,7 +1,6 @@
package com.safemobile.services; package com.safemobile.services;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
@ -12,16 +11,14 @@ import com.safemobile.lib.SM;
import com.safemobile.lib.TCPmsg; import com.safemobile.lib.TCPmsg;
public class TCPmsgParser implements Runnable{ public class TCPmsgParser implements Runnable {
public boolean run = true; public boolean run = true;
private TCPmsg _msg; private TCPmsg _msg;
private static List<ITCPListener> _listeners = new ArrayList<ITCPListener>(); private static final List<ITCPListener> _listeners = new ArrayList<>();
private Thread TCPmsgParserThread; private Thread TCPmsgParserThread;
public TCPmsgParser() public TCPmsgParser() {
{
TCPmsgParserThread = new Thread(this, "TCPmsgParserThread"); TCPmsgParserThread = new Thread(this, "TCPmsgParserThread");
TCPmsgParserThread.start(); // (2) Start the thread. TCPmsgParserThread.start(); // (2) Start the thread.
} }
@ -38,177 +35,151 @@ public class TCPmsgParser implements Runnable{
_listeners.clear(); _listeners.clear();
} }
public int getListenersSize()
{
return _listeners.size();
}
private synchronized void _fireLoginEvent() { private synchronized void _fireLoginEvent() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onLoginReceived(event);
( (ITCPListener) listeners.next() ).onLoginReceived( event );
} }
} }
private synchronized void _fireGPSEvent() { private synchronized void _fireGPSEvent() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onGPSReceived(event);
( (ITCPListener) listeners.next() ).onGPSReceived(event);
} }
} }
private synchronized void _fireSMSEvent() { private synchronized void _fireSMSEvent() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onSMSReceived(event);
( (ITCPListener) listeners.next() ).onSMSReceived( event );
} }
} }
private synchronized void _fireLastSMSEvent() { private synchronized void _fireLastSMSEvent() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onLastSMSsReceived(event);
( (ITCPListener) listeners.next() ).onLastSMSsReceived(event);
} }
} }
private synchronized void _fireVehEvent() { private synchronized void _fireVehEvent() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onVehiclesReceived(event);
( (ITCPListener) listeners.next() ).onVehiclesReceived( event );
} }
} }
private synchronized void _fireNewSMS() { private synchronized void _fireNewSMS() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onNewSMSReceived(event);
( (ITCPListener) listeners.next() ).onNewSMSReceived(event);
} }
} }
private synchronized void _fireSMSconfirm() { private synchronized void _fireSMSconfirm() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onSMSAckReceived(event);
( (ITCPListener) listeners.next() ).onSMSAckReceived(event);
} }
} }
private synchronized void _fireLastPos() { private synchronized void _fireLastPos() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onLastPositionsReceived(event);
( (ITCPListener) listeners.next() ).onLastPositionsReceived(event);
} }
} }
private synchronized void _fireRadioEvent() { private synchronized void _fireRadioEvent() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onRadioMsgReceived(event);
( (ITCPListener) listeners.next() ).onRadioMsgReceived(event);
} }
} }
private synchronized void _fireHistPos() { private synchronized void _fireHistPos() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onHistoryPositionsReceived(event);
( (ITCPListener) listeners.next() ).onHistoryPositionsReceived(event);
} }
} }
private synchronized void _fireHistCount() { private synchronized void _fireHistCount() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onHistoryPositionsCountReceived(event);
( (ITCPListener) listeners.next() ).onHistoryPositionsCountReceived(event);
} }
} }
private synchronized void _fireAlarmList() { private synchronized void _fireAlarmList() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onAlarmsReceived(event);
( (ITCPListener) listeners.next() ).onAlarmsReceived(event);
} }
} }
private synchronized void _fireAlarmACK() { private synchronized void _fireAlarmACK() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onAlarmAckReceived(event);
( (ITCPListener) listeners.next() ).onAlarmAckReceived(event);
} }
} }
private synchronized void _fireAlarmLive() { private synchronized void _fireAlarmLive() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.alarmLiveReceived(event);
( (ITCPListener) listeners.next() ).alarmLiveReceived(event);
} }
} }
private synchronized void _fireRecordList() { private synchronized void _fireRecordList() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onRecordingsListReceived(event);
( (ITCPListener) listeners.next()).onRecordingsListReceived(event);
} }
} }
private synchronized void _fireRecordPlay() { private synchronized void _fireRecordPlay() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onRecordingPlayReceived(event);
( (ITCPListener) listeners.next()).onRecordingPlayReceived(event);
} }
} }
private synchronized void _firePOLLEvent() { private synchronized void _firePOLLEvent() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onPollReceived(event);
( (ITCPListener) listeners.next()).onPollReceived(event);
} }
} }
private synchronized void _fireConnectionReplyEvent() { private synchronized void _fireConnectionReplyEvent() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onConnectionReplyReceived(event);
( (ITCPListener) listeners.next()).onConnectionReplyReceived(event);
} }
} }
private synchronized void _fireContactsReceivedEvent() { private synchronized void _fireContactsReceivedEvent() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onContactsListReceived(event);
( (ITCPListener) listeners.next()).onContactsListReceived(event);
} }
} }
private synchronized void _fireRecordingsReceivedEvent() { private synchronized void _fireRecordingsReceivedEvent() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onRecordingsListReceived(event);
( (ITCPListener) listeners.next()).onRecordingsListReceived(event);
} }
} }
private synchronized void _fireTextMessagesReceivedEvent() { private synchronized void _fireTextMessagesReceivedEvent() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onTextMessagesListReceived(event);
( (ITCPListener) listeners.next()).onTextMessagesListReceived(event);
} }
} }
@ -216,66 +187,55 @@ public class TCPmsgParser implements Runnable{
private synchronized void _fireRecordingPlayReceivedEvent() { private synchronized void _fireRecordingPlayReceivedEvent() {
TCPEvent event = new TCPEvent( this, _msg ); TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onRecordingPlayReceived(event);
( (ITCPListener) listeners.next()).onRecordingPlayReceived(event);
} }
} }
public static synchronized void _fireTCPConnectionDownEvent(boolean previuosWasConnectionUp) { public static synchronized void _fireTCPConnectionDownEvent(boolean previuosWasConnectionUp) {
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onTCPConnectionDown(previuosWasConnectionUp);
( (ITCPListener) listeners.next()).onTCPConnectionDown(previuosWasConnectionUp);
} }
} }
public static synchronized void _fireTCPConnectionUpEvent(boolean previuosWasConnectionUp) { public static synchronized void _fireTCPConnectionUpEvent(boolean previuosWasConnectionUp) {
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onTCPConnectionUp(previuosWasConnectionUp);
( (ITCPListener) listeners.next()).onTCPConnectionUp(previuosWasConnectionUp);
} }
} }
public static synchronized void _fireonTCPConnectionStatusEvent(boolean isConnectionUp, boolean previuosWasConnectionUp) { public static synchronized void _fireonTCPConnectionStatusEvent(boolean isConnectionUp, boolean previuosWasConnectionUp) {
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onTCPConnectionStatusReceived(isConnectionUp, previuosWasConnectionUp);
( (ITCPListener) listeners.next()).onTCPConnectionStatusReceived(isConnectionUp, previuosWasConnectionUp);
} }
} }
public static synchronized void _firePONGReceivedEvent() { public static synchronized void _firePONGReceivedEvent() {
Iterator<ITCPListener> listeners = _listeners.iterator(); for (ITCPListener listener : _listeners) {
while( listeners.hasNext() ) { listener.onPONGReceived();
( (ITCPListener) listeners.next()).onPONGReceived();
} }
} }
@Override @Override
public void run() public void run() {
{ while(run) {
while(run)
{
//SM.Debug("TCPmsgParser waiting for data...");
sleep(1); sleep(1);
if(TCPhandler.msgList==null) if (TCPhandler.msgList == null)
continue; continue;
if(!TCPhandler.msgList.isEmpty()) if(!TCPhandler.msgList.isEmpty()) {
{ try {
try
{
_msg = TCPhandler.msgList.poll(); _msg = TCPhandler.msgList.poll();
if(_msg == null) if(_msg == null)
continue; continue;
if(_msg.OK == false) if(!_msg.OK)
continue; continue;
//parse the rest of the message; //parse the rest of the message;
switch(_msg.opCode) switch(_msg.opCode) {
{
case 40:{ case 40:{
_fireLoginEvent(); _fireLoginEvent();
break; break;
@ -296,11 +256,8 @@ public class TCPmsgParser implements Runnable{
break; break;
} }
case OperationCodes.TM_ACK:{ case OperationCodes.TM_ACK:
_fireSMSconfirm(); case OperationCodes.TM_ACK_SD: {
break;
}
case OperationCodes.TM_ACK_SD:{
_fireSMSconfirm(); _fireSMSconfirm();
break; break;
} }
@ -403,38 +360,23 @@ public class TCPmsgParser implements Runnable{
default: default:
break; break;
} }
} } catch(Exception ex) {
catch(Exception ex)
{
try { try {
if(TCPhandler.msgList.size() > 0) if(TCPhandler.msgList.size() > 0)
_msg = TCPhandler.msgList.remove(0); _msg = TCPhandler.msgList.remove(0);
} } catch(NoSuchElementException exe) {
catch(NoSuchElementException exe)
{
SM.Exception("TCP msg Parser", "NoSuchElementException"); SM.Exception("TCP msg Parser", "NoSuchElementException");
} }
} }
} }
} }
/*
catch (Exception e)
{
SM.Debug("Error on fire Event:"+e.toString());
break;
}
}*/
SM.Debug("TCPmsgParser listen thread stoped."); SM.Debug("TCPmsgParser listen thread stoped.");
} }
private void sleep(int miliseconds) { private void sleep(int miliseconds) {
try { try {
Thread.sleep(miliseconds); Thread.sleep(miliseconds);
} catch (InterruptedException e) { } catch (InterruptedException ignored) { }
}
} }
public void clearMsgList() public void clearMsgList()
@ -442,17 +384,14 @@ public class TCPmsgParser implements Runnable{
TCPhandler.msgList.clear(); TCPhandler.msgList.clear();
} }
public void Stop() public void Stop() {
{
run = false; run = false;
// stop thread // stop thread
if(TCPmsgParserThread != null) if(TCPmsgParserThread != null) {
{
Thread moribund = TCPmsgParserThread; Thread moribund = TCPmsgParserThread;
TCPmsgParserThread = null; TCPmsgParserThread = null;
moribund.interrupt(); moribund.interrupt();
} }
SM.Debug("Stoping TCPmsgParser"); SM.Debug("Stoping TCPmsgParser");
} }
} }