2022-03-14 09:53:00 +00:00
|
|
|
|
package com.safemobile.services;
|
|
|
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
|
import android.net.wifi.WifiManager;
|
|
|
|
|
|
|
|
|
|
import com.safemobile.lib.AppParams;
|
|
|
|
|
import com.safemobile.lib.SM;
|
|
|
|
|
import com.safemobile.lib.TCPmsg;
|
|
|
|
|
|
2022-03-25 15:28:53 +00:00
|
|
|
|
import java.io.DataInputStream;
|
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
import java.net.Socket;
|
|
|
|
|
import java.net.UnknownHostException;
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.NoSuchElementException;
|
|
|
|
|
import java.util.Timer;
|
|
|
|
|
import java.util.TimerTask;
|
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
|
2022-03-25 15:28:53 +00:00
|
|
|
|
public class TCPhandler implements Runnable {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
private boolean alive = true;
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
|
|
|
|
public String serverHostname;
|
|
|
|
|
private int port;
|
2022-03-14 09:53:00 +00:00
|
|
|
|
private Thread listenThread;
|
2022-03-25 15:28:53 +00:00
|
|
|
|
private Socket soc = null;
|
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
private DataInputStream input;
|
|
|
|
|
private DataOutputStream output;
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
private String leftOver = "";
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
public static LinkedList<TCPmsg> msgList;
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
public Boolean isConnectionUP = false;
|
|
|
|
|
public Boolean previousConnectionWasUP = false;
|
|
|
|
|
|
2022-03-30 08:50:48 +00:00
|
|
|
|
private final Context context;
|
|
|
|
|
private boolean isWiFiOn;
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
|
|
|
|
public TCPhandler(Context context, String hostName, int p) {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
this.context = context;
|
2022-03-25 15:28:53 +00:00
|
|
|
|
serverHostname = hostName;
|
|
|
|
|
port = p;
|
2022-03-14 09:53:00 +00:00
|
|
|
|
|
2022-03-30 08:50:48 +00:00
|
|
|
|
msgList = new LinkedList<>();
|
2022-03-14 09:53:00 +00:00
|
|
|
|
SM.Debug("---TCPhandler constructor [" + hostName + "," + p + "] ---");
|
2022-03-25 15:28:53 +00:00
|
|
|
|
listenThread = new Thread(this, "TCPlisten");
|
2022-03-14 09:53:00 +00:00
|
|
|
|
listenThread.start(); // (2) Start the thread.
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
// create timer to check socket status
|
2022-03-25 15:28:53 +00:00
|
|
|
|
Timer timer = new Timer();
|
2022-03-14 09:53:00 +00:00
|
|
|
|
timer.scheduleAtFixedRate(new TimerTask() {
|
2022-03-25 15:28:53 +00:00
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
|
|
|
|
previousConnectionWasUP = isConnectionUP;
|
|
|
|
|
// try to send something
|
|
|
|
|
TCPmsgParser._fireonTCPConnectionStatusEvent(isConnectionUP, previousConnectionWasUP);
|
|
|
|
|
} catch (Exception e) {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
SM.Exception("TIMERException", e.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, 0, 3000);
|
|
|
|
|
|
2022-03-30 08:50:48 +00:00
|
|
|
|
// get WiFi state
|
|
|
|
|
isWiFiOn = isNetworkConnected();
|
2022-03-14 09:53:00 +00:00
|
|
|
|
|
|
|
|
|
IntentFilter intentFilter = new IntentFilter();
|
|
|
|
|
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
|
|
|
|
|
context.registerReceiver(mReceived, intentFilter);
|
|
|
|
|
}
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
2022-03-30 08:50:48 +00:00
|
|
|
|
private boolean isNetworkConnected() {
|
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
|
return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
|
|
|
|
|
}
|
2022-03-30 11:22:20 +00:00
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
@Override
|
2022-03-25 15:28:53 +00:00
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
|
|
|
|
if (soc != null)
|
|
|
|
|
soc.close();
|
|
|
|
|
|
|
|
|
|
soc = new Socket();
|
|
|
|
|
soc.connect(new InetSocketAddress(serverHostname, port), 5000);
|
|
|
|
|
|
|
|
|
|
input = new DataInputStream(soc.getInputStream());
|
|
|
|
|
output = new DataOutputStream(soc.getOutputStream());
|
|
|
|
|
|
|
|
|
|
if (soc != null) {
|
|
|
|
|
isConnectionUP = true;
|
|
|
|
|
triggerTCPConnectionStateEvent();
|
2022-03-14 09:53:00 +00:00
|
|
|
|
}
|
2022-03-25 15:28:53 +00:00
|
|
|
|
} catch (UnknownHostException e) {
|
2022-03-30 08:50:48 +00:00
|
|
|
|
SM.Debug("UnknownHostException", "TCPhandler break:"+ e);
|
2022-03-25 15:28:53 +00:00
|
|
|
|
} catch (IllegalArgumentException e) {
|
2022-03-30 08:50:48 +00:00
|
|
|
|
SM.Debug("IllegalArgumentException", "TCPhandler break:"+ e);
|
2022-03-25 15:28:53 +00:00
|
|
|
|
} catch (IOException e) {
|
2022-03-30 08:50:48 +00:00
|
|
|
|
SM.Debug("IOException", "TCPhandler break:"+ e);
|
2022-03-25 15:28:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (alive) {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(3000);
|
|
|
|
|
} catch (InterruptedException e) {
|
2022-03-30 08:50:48 +00:00
|
|
|
|
SM.Debug("TCPhandler Crash1 on sleep:"+ e);
|
2022-03-14 09:53:00 +00:00
|
|
|
|
}
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
|
|
|
|
while (Boolean.TRUE.equals(isConnectionUP)) {
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(100);
|
|
|
|
|
//process leftover
|
|
|
|
|
try {
|
|
|
|
|
boolean FinishLeftOver = true;
|
|
|
|
|
while (FinishLeftOver) {
|
|
|
|
|
String[] tempArr2 = leftOver.split("#");
|
|
|
|
|
if (tempArr2.length > 1) {
|
|
|
|
|
int messLen;
|
|
|
|
|
try {
|
|
|
|
|
messLen = Integer.parseInt(tempArr2[1]);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
SM.Debug("leftovers", "incorect msg len leftOver =" + tempArr2[1]);
|
|
|
|
|
messLen = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (messLen > leftOver.length()) {
|
|
|
|
|
FinishLeftOver = false;
|
|
|
|
|
break;
|
|
|
|
|
} else if (messLen == leftOver.length()) {
|
|
|
|
|
TCPmsg msg = new TCPmsg(leftOver.toCharArray());
|
|
|
|
|
SM.Debug("leftovers", "RX from leftOver:" + msg.allData);
|
|
|
|
|
|
|
|
|
|
if (msg.allData.contains("#92#"))
|
|
|
|
|
prioritizePongReceived();
|
|
|
|
|
|
|
|
|
|
msgList.add(msg);
|
|
|
|
|
leftOver = "";
|
|
|
|
|
FinishLeftOver = false;
|
|
|
|
|
break;
|
|
|
|
|
} else // we have more message in leftover
|
|
|
|
|
{
|
|
|
|
|
TCPmsg msg = new TCPmsg(leftOver.substring(0, messLen).toCharArray());
|
|
|
|
|
SM.Debug("leftovers", "RX from leftOver:" + msg.allData);
|
|
|
|
|
|
|
|
|
|
if (msg.allData.contains("#92#"))
|
|
|
|
|
prioritizePongReceived();
|
|
|
|
|
|
|
|
|
|
msgList.add(msg);
|
|
|
|
|
leftOver = leftOver.substring(messLen, leftOver.length());
|
|
|
|
|
}
|
|
|
|
|
} else FinishLeftOver = false;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
SM.Debug("leftovers", "Error on process leftover" + e.toString());
|
|
|
|
|
}
|
2022-03-14 09:53:00 +00:00
|
|
|
|
//end process leftover
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
|
|
|
|
String data = "";
|
2022-03-30 11:22:20 +00:00
|
|
|
|
int n = 0;
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
byte[] buf = new byte[1024];
|
|
|
|
|
// read data into buffer
|
|
|
|
|
n = input.read(buf);
|
|
|
|
|
//connection closed by server
|
2022-03-25 15:28:53 +00:00
|
|
|
|
if (n == -1) {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
SM.Debug("TCP Client", "Connection closed by server!");
|
|
|
|
|
soc.close();
|
|
|
|
|
isConnectionUP = false;
|
|
|
|
|
triggerTCPConnectionStateEvent();
|
|
|
|
|
soc = null;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
byte[] temp = new byte[n];
|
2022-03-25 15:28:53 +00:00
|
|
|
|
if (n >= 0) System.arraycopy(buf, 0, temp, 0, n);
|
|
|
|
|
|
|
|
|
|
// decryptData
|
|
|
|
|
temp = decryptTEA(temp);
|
|
|
|
|
|
|
|
|
|
data = new String(temp);
|
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
//if we have any leftovers from previous message add them
|
2022-03-30 08:50:48 +00:00
|
|
|
|
if(leftOver.length() > 1) { // avoid case with only one #
|
2022-03-14 09:53:00 +00:00
|
|
|
|
data = leftOver+data;
|
|
|
|
|
leftOver = "";
|
|
|
|
|
}
|
2022-03-30 11:22:20 +00:00
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
//search for overflow message
|
|
|
|
|
String[] tempArr = data.split("#");
|
2022-03-30 08:50:48 +00:00
|
|
|
|
if ((tempArr.length == 0) || (tempArr.length == 1)) {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
SM.Debug("TCP Client", "incorect messagebuss message=" + data);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
//get msg len
|
|
|
|
|
int messLen;
|
|
|
|
|
try {
|
2022-03-25 15:28:53 +00:00
|
|
|
|
messLen = Integer.parseInt(tempArr[1]);
|
2022-03-14 09:53:00 +00:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
SM.Debug("TCP Client", "incorect msg len =" + tempArr[1]);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
//messLen not int
|
2022-03-30 08:50:48 +00:00
|
|
|
|
if (messLen == -1) {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
char[] temMSG = data.toCharArray();
|
2022-03-30 08:50:48 +00:00
|
|
|
|
if (data.length() != messLen) {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
//if expected string message is smaller then actual string then exit processing;
|
2022-03-30 08:50:48 +00:00
|
|
|
|
if (messLen > data.length()) {
|
|
|
|
|
leftOver = data; // Add by bigu
|
2022-03-30 11:22:20 +00:00
|
|
|
|
continue;
|
2022-03-14 09:53:00 +00:00
|
|
|
|
}
|
2022-03-30 11:22:20 +00:00
|
|
|
|
//perform cut
|
2022-03-14 09:53:00 +00:00
|
|
|
|
temMSG = data.substring(0,messLen).toCharArray();
|
2022-03-30 08:50:48 +00:00
|
|
|
|
leftOver = data.substring(messLen);
|
2022-03-14 09:53:00 +00:00
|
|
|
|
}
|
|
|
|
|
//decode TCP msg
|
|
|
|
|
TCPmsg msg = new TCPmsg(temMSG);
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
SM.Debug("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> RX <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", msg.allData);
|
|
|
|
|
|
2022-03-25 15:28:53 +00:00
|
|
|
|
if (msg.allData.contains("#92#"))
|
2022-03-14 09:53:00 +00:00
|
|
|
|
prioritizePongReceived();
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
msgList.add(msg);
|
2022-03-30 08:50:48 +00:00
|
|
|
|
} catch(Exception ex) {
|
|
|
|
|
SM.Debug("TCPHandler", "TCPhandler/run/break:"+ ex);
|
2022-03-14 09:53:00 +00:00
|
|
|
|
isConnectionUP = false;
|
|
|
|
|
triggerTCPConnectionStateEvent();
|
|
|
|
|
}
|
2022-03-25 15:28:53 +00:00
|
|
|
|
}
|
2022-03-14 09:53:00 +00:00
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
} catch (InterruptedException e) {
|
2022-03-30 08:50:48 +00:00
|
|
|
|
SM.Debug("TCPhandler Crash2 on sleep:"+ e);
|
2022-03-14 09:53:00 +00:00
|
|
|
|
}
|
|
|
|
|
//try to restart connection
|
2022-03-25 15:28:53 +00:00
|
|
|
|
if (alive && isWiFiOn)
|
|
|
|
|
restartTCP();
|
|
|
|
|
}
|
2022-03-14 09:53:00 +00:00
|
|
|
|
SM.Debug("==================================");
|
|
|
|
|
SM.Debug("TCP listenThread stoped!! alive = false");
|
|
|
|
|
SM.Debug("==================================");
|
|
|
|
|
}
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
/**
|
2022-03-25 15:28:53 +00:00
|
|
|
|
* Create a bypass in order to trigger the ping received event
|
2022-03-14 09:53:00 +00:00
|
|
|
|
*/
|
|
|
|
|
private void prioritizePongReceived() {
|
|
|
|
|
TCPmsgParser._firePONGReceivedEvent();
|
|
|
|
|
}
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
/* Broadcast Received for WiFi Connect/Disconnect */
|
|
|
|
|
public BroadcastReceiver mReceived = new BroadcastReceiver() {
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
|
final String action = intent.getAction();
|
|
|
|
|
SM.Debug("WIFI STATE", action);
|
|
|
|
|
|
2022-03-30 08:50:48 +00:00
|
|
|
|
if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
|
|
|
|
|
// close socket if the wifi is disconnecting or disconnected
|
|
|
|
|
isWiFiOn = isNetworkConnected();
|
|
|
|
|
if (!isWiFiOn)
|
2022-03-25 15:28:53 +00:00
|
|
|
|
closeSocket();
|
2022-03-14 09:53:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-30 11:22:20 +00:00
|
|
|
|
/** Send a message through the TCP Socket
|
2022-03-14 09:53:00 +00:00
|
|
|
|
* @param seqID The messages's sequence ID (a number of order)
|
|
|
|
|
* @param msg The messages which will be sent
|
|
|
|
|
* @return True if the message was sent
|
|
|
|
|
*/
|
2022-03-25 15:28:53 +00:00
|
|
|
|
public boolean Write(String seqID, String msg) {
|
|
|
|
|
try {
|
|
|
|
|
if (output != null) {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(10);
|
2022-03-25 15:28:53 +00:00
|
|
|
|
String cmdok = "#" + seqID + msg;
|
|
|
|
|
Integer tmp = cmdok.length() + 1;
|
2022-03-14 09:53:00 +00:00
|
|
|
|
|
2022-03-25 15:28:53 +00:00
|
|
|
|
tmp += Integer.toString(tmp).length();
|
|
|
|
|
if ((tmp == 10) || (tmp == 100) || (tmp == 1000)) tmp++;
|
2022-03-14 09:53:00 +00:00
|
|
|
|
cmdok = "#" + Integer.toString(tmp) + cmdok;
|
|
|
|
|
|
|
|
|
|
byte[] mess = encryptTEA(cmdok);
|
2022-03-30 07:34:57 +00:00
|
|
|
|
Thread tcpThread = new Thread(() -> {
|
2022-03-25 15:28:53 +00:00
|
|
|
|
try {
|
|
|
|
|
output.write(mess);
|
|
|
|
|
output.flush();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-03-30 07:34:57 +00:00
|
|
|
|
tcpThread.start();
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
SM.Debug(" ", new String(mess));
|
|
|
|
|
return true;
|
2022-03-25 15:28:53 +00:00
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
SM.Exception("TCPClient[Send]", e.toString());
|
2022-03-30 07:34:57 +00:00
|
|
|
|
Thread.currentThread().interrupt();
|
2022-03-25 15:28:53 +00:00
|
|
|
|
} catch (NoSuchElementException e) {
|
|
|
|
|
SM.Exception("TCPClient[Send]", e.toString());
|
2022-03-14 09:53:00 +00:00
|
|
|
|
}
|
2022-03-25 15:28:53 +00:00
|
|
|
|
} else {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-03-25 15:28:53 +00:00
|
|
|
|
} catch (Exception e) {
|
2022-03-30 07:34:57 +00:00
|
|
|
|
SM.Debug("TCPhandler Write Procedure:" + e);
|
2022-03-14 09:53:00 +00:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 15:28:53 +00:00
|
|
|
|
/* Encrypt a string using an encryption algorithm,
|
2022-03-14 09:53:00 +00:00
|
|
|
|
* in this case TEA */
|
|
|
|
|
public static byte[] encryptTEA(String toEncryptData) {
|
|
|
|
|
// no encryption
|
2022-03-30 08:50:48 +00:00
|
|
|
|
return toEncryptData.getBytes();
|
2022-03-14 09:53:00 +00:00
|
|
|
|
}
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
|
|
|
|
/* Decrypt a string using an encryption algorithm,
|
2022-03-14 09:53:00 +00:00
|
|
|
|
* in this case TEA */
|
|
|
|
|
public static byte[] decryptTEA(byte[] toDecryptData) {
|
2022-03-30 08:50:48 +00:00
|
|
|
|
byte[] decryptedByteArray;
|
2022-03-14 09:53:00 +00:00
|
|
|
|
|
|
|
|
|
// no decryption
|
|
|
|
|
decryptedByteArray = toDecryptData;
|
|
|
|
|
return decryptedByteArray;
|
|
|
|
|
}
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
public int getPort() {
|
|
|
|
|
return port;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 15:28:53 +00:00
|
|
|
|
public void updateTCPparameters(String ip, String _port) {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
// stop socket
|
|
|
|
|
try {
|
2022-03-25 15:28:53 +00:00
|
|
|
|
if (soc != null)
|
2022-03-14 09:53:00 +00:00
|
|
|
|
soc.close();
|
|
|
|
|
} catch (IOException e1) {
|
|
|
|
|
e1.printStackTrace();
|
|
|
|
|
}
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
serverHostname = ip;
|
2022-03-25 15:28:53 +00:00
|
|
|
|
try {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
port = Integer.parseInt(_port);
|
2022-03-30 08:50:48 +00:00
|
|
|
|
} catch (Exception ignored) { }
|
|
|
|
|
finally {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
port = 13589;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
public void triggerTCPConnectionStateEvent() {
|
2022-03-25 15:28:53 +00:00
|
|
|
|
if (!isConnectionUP)
|
2022-03-14 09:53:00 +00:00
|
|
|
|
TCPmsgParser._fireTCPConnectionDownEvent(previousConnectionWasUP);
|
2022-03-25 15:28:53 +00:00
|
|
|
|
else
|
2022-03-14 09:53:00 +00:00
|
|
|
|
TCPmsgParser._fireTCPConnectionUpEvent(previousConnectionWasUP);
|
|
|
|
|
}
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
|
|
|
|
private void restartTCP() {
|
|
|
|
|
try {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
isConnectionUP = false;
|
|
|
|
|
previousConnectionWasUP = false;
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
|
|
|
|
SM.Debug("Restarting TCP...ip:" + serverHostname + ":" + port);
|
2022-03-14 09:53:00 +00:00
|
|
|
|
soc = new Socket();
|
2022-03-25 15:28:53 +00:00
|
|
|
|
soc.connect(new InetSocketAddress(serverHostname, port), 5000);
|
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
input = new DataInputStream(soc.getInputStream());
|
|
|
|
|
//output stream
|
|
|
|
|
output = new DataOutputStream(soc.getOutputStream());
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
|
|
|
|
if (soc != null) {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
isConnectionUP = true;
|
|
|
|
|
}
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
|
SM.Exception("restartTCP break:" + e.toString());
|
2022-03-14 09:53:00 +00:00
|
|
|
|
isConnectionUP = false;
|
2022-03-25 15:28:53 +00:00
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
SM.Debug("IllegalArgumentException", "restartTCP break:" + e.toString());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
SM.Exception("restartTCP break:" + e.toString());
|
2022-03-14 09:53:00 +00:00
|
|
|
|
isConnectionUP = false;
|
2022-03-25 15:28:53 +00:00
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
|
SM.Exception("restartTCP break:" + e.toString());
|
2022-03-14 09:53:00 +00:00
|
|
|
|
isConnectionUP = false;
|
|
|
|
|
}
|
|
|
|
|
triggerTCPConnectionStateEvent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** close Socket when unReachable */
|
|
|
|
|
public void closeSocket() {
|
|
|
|
|
try {
|
|
|
|
|
input = null;
|
|
|
|
|
output = null;
|
2022-03-25 15:28:53 +00:00
|
|
|
|
if (soc != null)
|
2022-03-14 09:53:00 +00:00
|
|
|
|
soc.close();
|
|
|
|
|
soc = null;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 15:28:53 +00:00
|
|
|
|
public void Stop() {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
SM.Debug("Stopping TCP", "TCP Connection is stopping on " + AppParams.IP + ":" + port);
|
|
|
|
|
alive = false;
|
|
|
|
|
|
2022-03-30 08:50:48 +00:00
|
|
|
|
if (mReceived!= null)
|
2022-03-25 15:28:53 +00:00
|
|
|
|
try {
|
|
|
|
|
context.unregisterReceiver(mReceived);
|
2022-03-30 08:50:48 +00:00
|
|
|
|
} catch(Exception ex) {/* receiver not registered //*/}
|
2022-03-25 15:28:53 +00:00
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
|
// stop thread
|
2022-03-25 15:28:53 +00:00
|
|
|
|
if (listenThread != null) {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
Thread moribund = listenThread;
|
|
|
|
|
listenThread = null;
|
|
|
|
|
moribund.interrupt();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 15:28:53 +00:00
|
|
|
|
if (input != null) {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
try {
|
|
|
|
|
input.close();
|
|
|
|
|
input = null;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 15:28:53 +00:00
|
|
|
|
if (output != null) {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
try {
|
|
|
|
|
output.close();
|
|
|
|
|
output = null;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-30 08:50:48 +00:00
|
|
|
|
if (soc !=null) {
|
2022-03-14 09:53:00 +00:00
|
|
|
|
try {
|
|
|
|
|
soc.close();
|
|
|
|
|
soc = null;
|
2022-03-25 15:28:53 +00:00
|
|
|
|
} catch (IOException e) {
|
2022-03-30 08:50:48 +00:00
|
|
|
|
SM.Exception("TCPClient[STOP]", "Stop break:"+ e);
|
2022-03-14 09:53:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-30 08:50:48 +00:00
|
|
|
|
}
|