Merge pull request 'allow server to reconnect on mobile data' (#15) from feature/allow_server_reconnection_on_mobile_data into develop
Reviewed-on: #15
This commit is contained in:
commit
cbeb912820
@ -4,6 +4,7 @@
|
|||||||
android:versionCode="1"
|
android:versionCode="1"
|
||||||
android:versionName="1.0" >
|
android:versionName="1.0" >
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:icon="@mipmap/ic_launcher"
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package com.safemobile.services;
|
package com.safemobile.services;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
@ -12,7 +11,6 @@ import android.content.Context;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.net.NetworkInfo;
|
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
|
|
||||||
import com.safemobile.lib.AppParams;
|
import com.safemobile.lib.AppParams;
|
||||||
@ -20,247 +18,178 @@ import com.safemobile.lib.SM;
|
|||||||
import com.safemobile.lib.TCPmsg;
|
import com.safemobile.lib.TCPmsg;
|
||||||
|
|
||||||
|
|
||||||
public class TCPhandler implements Runnable
|
public class TCPhandler implements Runnable {
|
||||||
{
|
|
||||||
private boolean alive = true;
|
private boolean alive = true;
|
||||||
|
|
||||||
public String serverHostname = new String ("10.120.1.114");//
|
public String serverHostname;
|
||||||
private int port;
|
private int port;
|
||||||
private Thread listenThread;
|
private Thread listenThread;
|
||||||
private Socket soc =null;
|
private Socket soc =null;
|
||||||
|
|
||||||
//private BufferedReader recv;
|
|
||||||
//private PrintWriter writer;
|
|
||||||
|
|
||||||
private DataInputStream input;
|
private DataInputStream input;
|
||||||
private DataOutputStream output;
|
private DataOutputStream output;
|
||||||
private Timer timer;
|
|
||||||
|
|
||||||
private String leftOver = "";
|
private String leftOver = "";
|
||||||
|
|
||||||
public static LinkedList<TCPmsg> msgList;
|
public static LinkedList<TCPmsg> msgList;
|
||||||
private volatile int n=0;
|
|
||||||
|
|
||||||
public Boolean isConnectionUP = false;
|
public Boolean isConnectionUP = false;
|
||||||
public Boolean previousConnectionWasUP = false;
|
public Boolean previousConnectionWasUP = false;
|
||||||
|
|
||||||
private Context context;
|
private final Context context;
|
||||||
private boolean isWiFiOn = true;
|
private boolean isWiFiOn;
|
||||||
|
|
||||||
public TCPhandler(Context context, String hostName, int p)
|
public TCPhandler(Context context, String hostName, int p) {
|
||||||
{
|
|
||||||
|
|
||||||
this.context = context;
|
this.context = context;
|
||||||
serverHostname=hostName;
|
serverHostname=hostName;
|
||||||
port=p;
|
port = p;
|
||||||
|
|
||||||
msgList = new LinkedList<TCPmsg>();
|
msgList = new LinkedList<>();
|
||||||
SM.Debug("---TCPhandler constructor [" + hostName + "," + p + "] ---");
|
SM.Debug("---TCPhandler constructor [" + hostName + "," + p + "] ---");
|
||||||
listenThread = new Thread(this, "TCPlisten");
|
listenThread = new Thread(this, "TCPlisten");
|
||||||
listenThread.start(); // (2) Start the thread.
|
listenThread.start(); // (2) Start the thread.
|
||||||
|
|
||||||
|
|
||||||
// create timer to check socket status
|
// create timer to check socket status
|
||||||
timer = new Timer();
|
Timer timer = new Timer();
|
||||||
timer.scheduleAtFixedRate(new TimerTask() {
|
timer.scheduleAtFixedRate(new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
//Looper.prepare();
|
previousConnectionWasUP = isConnectionUP;
|
||||||
//mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
// try to send something
|
||||||
// get if is authenticated
|
TCPmsgParser._fireonTCPConnectionStatusEvent(isConnectionUP, previousConnectionWasUP);
|
||||||
//Write("0.0", "d");
|
} catch (Exception e) {
|
||||||
previousConnectionWasUP = isConnectionUP;
|
|
||||||
|
|
||||||
// try to send something
|
|
||||||
TCPmsgParser._fireonTCPConnectionStatusEvent(isConnectionUP, previousConnectionWasUP);
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
//e.printStackTrace();
|
|
||||||
SM.Exception("TIMERException", e.toString());
|
SM.Exception("TIMERException", e.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 0, 3000);
|
}, 0, 3000);
|
||||||
|
|
||||||
// get WiFi state
|
// get WiFi state
|
||||||
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
isWiFiOn = isNetworkConnected();
|
||||||
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
|
||||||
if(mWifi.isConnectedOrConnecting())
|
|
||||||
isWiFiOn = true;
|
|
||||||
|
|
||||||
IntentFilter intentFilter = new IntentFilter();
|
IntentFilter intentFilter = new IntentFilter();
|
||||||
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
|
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
|
||||||
context.registerReceiver(mReceived, intentFilter);
|
context.registerReceiver(mReceived, intentFilter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isNetworkConnected() {
|
||||||
|
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
|
return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void run() {
|
||||||
{
|
try {
|
||||||
try
|
if (soc != null)
|
||||||
{
|
soc.close();
|
||||||
if(soc!=null)
|
|
||||||
soc.close();
|
|
||||||
|
|
||||||
soc = new Socket();
|
soc = new Socket();
|
||||||
soc.connect(new InetSocketAddress(serverHostname, port), 5000);
|
soc.connect(new InetSocketAddress(serverHostname, port), 5000);
|
||||||
|
|
||||||
//soc.setSoTimeout(3000);
|
input = new DataInputStream(soc.getInputStream());
|
||||||
//SM.Debug("Socket timeout:" + soc.getSoTimeout() );
|
output = new DataOutputStream(soc.getOutputStream());
|
||||||
//soc.setSoTimeout(5000);
|
|
||||||
|
if (soc != null) {
|
||||||
input = new DataInputStream(soc.getInputStream());
|
isConnectionUP = true;
|
||||||
output = new DataOutputStream(soc.getOutputStream());
|
triggerTCPConnectionStateEvent();
|
||||||
|
|
||||||
//recv= new BufferedReader(new InputStreamReader(soc.getInputStream()));
|
|
||||||
//writer =new PrintWriter(soc.getOutputStream()) ;
|
|
||||||
|
|
||||||
|
|
||||||
if(soc !=null) {
|
|
||||||
//previousConnectionWasUP = isConnectionUP;
|
|
||||||
isConnectionUP = true;
|
|
||||||
triggerTCPConnectionStateEvent();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (UnknownHostException e)
|
|
||||||
{
|
|
||||||
SM.Debug("UnknownHostException", "TCPhandler break:"+e.toString());
|
|
||||||
}
|
|
||||||
catch (IllegalArgumentException e)
|
|
||||||
{
|
|
||||||
SM.Debug("IllegalArgumentException", "TCPhandler break:"+e.toString());
|
|
||||||
}
|
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
SM.Debug("IOException", "TCPhandler break:"+e.toString());
|
|
||||||
}
|
}
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
SM.Debug("UnknownHostException", "TCPhandler break:"+ e);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
SM.Debug("IllegalArgumentException", "TCPhandler break:"+ e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
SM.Debug("IOException", "TCPhandler break:"+ e);
|
||||||
|
}
|
||||||
|
|
||||||
while(alive)
|
while (alive) {
|
||||||
{
|
|
||||||
//SM.Debug("Waiting for data...");
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(3000);
|
Thread.sleep(3000);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
SM.Debug("TCPhandler Crash1 on sleep:"+e.toString());
|
SM.Debug("TCPhandler Crash1 on sleep:"+ e);
|
||||||
}
|
}
|
||||||
|
|
||||||
while(isConnectionUP)
|
while(isConnectionUP) {
|
||||||
{
|
try {
|
||||||
try
|
|
||||||
{
|
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
//SM.Debug("Waiting for data...");
|
try {
|
||||||
//process leftover
|
boolean FinishLeftOver = true;
|
||||||
try
|
while (FinishLeftOver) {
|
||||||
{
|
// add this replacement if message length doesn't contain the last #
|
||||||
boolean FinishLeftOver =true;
|
String[] tempArr2 = leftOver.split("#");
|
||||||
while (FinishLeftOver)
|
if (tempArr2.length > 1) {
|
||||||
{
|
int messLen;
|
||||||
// add this replacement if message length doesn't contain the last #
|
try {
|
||||||
// leftOver = leftOver.replace("##", "#");
|
messLen = Integer.parseInt(tempArr2[1]);
|
||||||
String[] tempArr2 = leftOver.split("#");
|
|
||||||
if (tempArr2.length > 1)
|
|
||||||
{
|
|
||||||
int messLen;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
messLen = Integer.parseInt(tempArr2[1]);
|
|
||||||
|
|
||||||
//TODO talk to Gaby to fix this on Bridge
|
//TODO talk to Gaby to fix this on Bridge
|
||||||
|
} catch (Exception e) {
|
||||||
}
|
SM.Debug("leftovers", "incorect msg len leftOver =" + tempArr2[1]);
|
||||||
catch (Exception e)
|
messLen =-1;
|
||||||
{
|
}
|
||||||
SM.Debug("leftovers", "incorect msg len leftOver =" + tempArr2[1]);
|
|
||||||
messLen =-1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(messLen>leftOver.length())
|
if (messLen > leftOver.length()) {
|
||||||
{
|
break;
|
||||||
FinishLeftOver =false;
|
} else if (messLen == leftOver.length()) {
|
||||||
break;
|
TCPmsg msg = new TCPmsg(leftOver.toCharArray());
|
||||||
}
|
SM.Debug("leftovers", "RX from leftOver:"+msg.allData);
|
||||||
else if(messLen==leftOver.length())
|
|
||||||
{
|
|
||||||
TCPmsg msg = new TCPmsg(leftOver.toCharArray());
|
|
||||||
SM.Debug("leftovers", "RX from leftOver:"+msg.allData);
|
|
||||||
|
|
||||||
if(msg.allData.contains("#92#"))
|
if(msg.allData.contains("#92#"))
|
||||||
prioritizePongReceived();
|
prioritizePongReceived();
|
||||||
|
|
||||||
msgList.add(msg);
|
msgList.add(msg);
|
||||||
leftOver ="";
|
leftOver ="";
|
||||||
FinishLeftOver =false;
|
break;
|
||||||
break;
|
} else { // we have more message in leftover
|
||||||
}
|
TCPmsg msg = new TCPmsg(leftOver.substring(0,messLen).toCharArray());
|
||||||
else // we have more message in leftover
|
SM.Debug("leftovers", "RX from leftOver:"+msg.allData);
|
||||||
{
|
|
||||||
TCPmsg msg = new TCPmsg(leftOver.substring(0,messLen).toCharArray());
|
|
||||||
SM.Debug("leftovers", "RX from leftOver:"+msg.allData);
|
|
||||||
|
|
||||||
if(msg.allData.contains("#92#"))
|
if(msg.allData.contains("#92#"))
|
||||||
prioritizePongReceived();
|
prioritizePongReceived();
|
||||||
|
|
||||||
msgList.add(msg);
|
msgList.add(msg);
|
||||||
leftOver = leftOver.substring(messLen,leftOver.length());
|
leftOver = leftOver.substring(messLen);
|
||||||
}
|
}
|
||||||
}
|
} else FinishLeftOver = false;
|
||||||
else FinishLeftOver = false;
|
}
|
||||||
}
|
} catch (Exception e) {
|
||||||
}
|
SM.Debug("leftovers", "Error on process leftover"+ e);
|
||||||
catch (Exception e)
|
}
|
||||||
{
|
String data;
|
||||||
SM.Debug("leftovers", "Error on process leftover"+e.toString());
|
int n;
|
||||||
}
|
|
||||||
//end process leftover
|
|
||||||
|
|
||||||
String data ="";
|
|
||||||
n=0;
|
|
||||||
//char[] buf = new char[1024];
|
|
||||||
//n = recv.read(buf);
|
|
||||||
|
|
||||||
byte[] buf = new byte[1024];
|
byte[] buf = new byte[1024];
|
||||||
// read data into buffer
|
// read data into buffer
|
||||||
n = input.read(buf);
|
n = input.read(buf);
|
||||||
//connection closed by server
|
//connection closed by server
|
||||||
if(n==-1)
|
if (n == -1) {
|
||||||
{
|
|
||||||
SM.Debug("TCP Client", "Connection closed by server!");
|
SM.Debug("TCP Client", "Connection closed by server!");
|
||||||
soc.close();
|
soc.close();
|
||||||
//previousConnectionWasUP = isConnectionUP;
|
|
||||||
isConnectionUP = false;
|
isConnectionUP = false;
|
||||||
triggerTCPConnectionStateEvent();
|
triggerTCPConnectionStateEvent();
|
||||||
soc = null;
|
soc = null;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
byte[] temp = new byte[n];
|
byte[] temp = new byte[n];
|
||||||
for(int i=0;i<n;i++) temp[i] = buf[i];
|
System.arraycopy(buf, 0, temp, 0, n);
|
||||||
//String data =new String(temp);
|
|
||||||
|
|
||||||
// decryptData
|
// decryptData
|
||||||
temp = decryptTEA(temp);
|
temp = decryptTEA(temp);
|
||||||
|
|
||||||
data = new String(temp);
|
data = new String(temp);
|
||||||
|
|
||||||
//if we have any leftovers from previous message add them
|
//if we have any leftovers from previous message add them
|
||||||
if(leftOver.length()>1) // avoid case with only one #
|
if(leftOver.length() > 1) { // avoid case with only one #
|
||||||
{
|
|
||||||
data = leftOver+data;
|
data = leftOver+data;
|
||||||
leftOver = "";
|
leftOver = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
//search for overflow message
|
//search for overflow message
|
||||||
String[] tempArr = data.split("#");
|
String[] tempArr = data.split("#");
|
||||||
if ((tempArr.length == 0) || (tempArr.length == 1))
|
if ((tempArr.length == 0) || (tempArr.length == 1)) {
|
||||||
{
|
|
||||||
SM.Debug("TCP Client", "incorect messagebuss message=" + data);
|
SM.Debug("TCP Client", "incorect messagebuss message=" + data);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
//get msg len
|
//get msg len
|
||||||
// for(int i=0;i<tempArr.length;i++)
|
|
||||||
// SM.Debug("I:"+i+" tempArr[i]:"+tempArr[i]);
|
|
||||||
int messLen;
|
int messLen;
|
||||||
try {
|
try {
|
||||||
messLen = Integer.parseInt(tempArr[1]);
|
messLen = Integer.parseInt(tempArr[1]);
|
||||||
@ -269,80 +198,58 @@ public class TCPhandler implements Runnable
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
//messLen not int
|
//messLen not int
|
||||||
if(messLen == -1)
|
if (messLen == -1) {
|
||||||
{
|
|
||||||
//SM.Debug("messLen not int=" + messLen +" tempArr[1]:" +tempArr[1]);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
char[] temMSG = data.toCharArray();
|
char[] temMSG = data.toCharArray();
|
||||||
//SM.Debug("MessLen:"+messLen+" Data len:"+data.length()+"IF:"+(data.length() != messLen));
|
if (data.length() != messLen) {
|
||||||
if (data.length() != messLen)
|
|
||||||
{
|
|
||||||
//if expected string message is smaller then actual string then exit processing;
|
//if expected string message is smaller then actual string then exit processing;
|
||||||
if(messLen>data.length())
|
if (messLen > data.length()) {
|
||||||
//if(messLen>n)
|
leftOver = data; // Add by bigu
|
||||||
{
|
|
||||||
//SM.Debug("duda","messLen=" + messLen +" data.length():" +data.length()+"n:"+n);
|
|
||||||
leftOver = data; // Add by bigu
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
//perform cut
|
//perform cut
|
||||||
//SM.Debug("We got leftover ....message length("+messLen+") != actual length("+data.length()+")");
|
|
||||||
temMSG = data.substring(0,messLen).toCharArray();
|
temMSG = data.substring(0,messLen).toCharArray();
|
||||||
//SM.Debug("temMSG:"+temMSG.toString());
|
leftOver = data.substring(messLen);
|
||||||
leftOver = data.substring(messLen,data.length());
|
|
||||||
//SM.Debug("leftOver:"+leftOver);
|
|
||||||
//leftOver = data.substring(messLen,n);
|
|
||||||
//SM.Debug("left over string:"+leftOver);
|
|
||||||
}
|
}
|
||||||
//decode TCP msg
|
//decode TCP msg
|
||||||
TCPmsg msg = new TCPmsg(temMSG);
|
TCPmsg msg = new TCPmsg(temMSG);
|
||||||
|
|
||||||
SM.Debug("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> RX <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", msg.allData);
|
SM.Debug("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> RX <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", msg.allData);
|
||||||
|
|
||||||
if(msg.allData.contains("#92#"))
|
if (msg.allData.contains("#92#"))
|
||||||
prioritizePongReceived();
|
prioritizePongReceived();
|
||||||
|
|
||||||
msgList.add(msg);
|
msgList.add(msg);
|
||||||
|
} catch(Exception ex) {
|
||||||
}
|
SM.Debug("TCPHandler", "TCPhandler/run/break:"+ ex);
|
||||||
catch(Exception ex)
|
|
||||||
{
|
|
||||||
SM.Debug("TCPHandler", "TCPhandler/run/break:"+ex.toString());
|
|
||||||
//previousConnectionWasUP = isConnectionUP;
|
|
||||||
isConnectionUP = false;
|
isConnectionUP = false;
|
||||||
triggerTCPConnectionStateEvent();
|
triggerTCPConnectionStateEvent();
|
||||||
}
|
}
|
||||||
}//while(connOK)
|
}
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
SM.Debug("TCPhandler Crash2 on sleep:"+e.toString());
|
SM.Debug("TCPhandler Crash2 on sleep:"+ e);
|
||||||
//previousConnectionWasUP = isConnectionUP;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//try to restart connection
|
//try to restart connection
|
||||||
if(alive && isWiFiOn)
|
if (alive && isWiFiOn)
|
||||||
RestartTCP();
|
RestartTCP();
|
||||||
|
}
|
||||||
}//while(alive)
|
|
||||||
SM.Debug("==================================");
|
SM.Debug("==================================");
|
||||||
SM.Debug("TCP listenThread stoped!! alive = false");
|
SM.Debug("TCP listenThread stoped!! alive = false");
|
||||||
SM.Debug("==================================");
|
SM.Debug("==================================");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a bypass in order to trigger the ping received event
|
* Create a bypass in order to trigger the ping received event
|
||||||
*/
|
*/
|
||||||
private void prioritizePongReceived() {
|
private void prioritizePongReceived() {
|
||||||
TCPmsgParser._firePONGReceivedEvent();
|
TCPmsgParser._firePONGReceivedEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Broadcast Received for WiFi Connect/Disconnect */
|
/* Broadcast Received for WiFi Connect/Disconnect */
|
||||||
public BroadcastReceiver mReceived = new BroadcastReceiver() {
|
public BroadcastReceiver mReceived = new BroadcastReceiver() {
|
||||||
|
|
||||||
@ -351,183 +258,74 @@ public class TCPhandler implements Runnable
|
|||||||
final String action = intent.getAction();
|
final String action = intent.getAction();
|
||||||
SM.Debug("WIFI STATE", action);
|
SM.Debug("WIFI STATE", action);
|
||||||
|
|
||||||
if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
|
if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
|
||||||
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
|
// close socket if the wifi is disconnecting or disconnected
|
||||||
// close socket if the wifi is disconnecting or disconnected
|
isWiFiOn = isNetworkConnected();
|
||||||
if(!info.isConnectedOrConnecting()) {
|
if (!isWiFiOn)
|
||||||
closeSocket();
|
closeSocket();
|
||||||
isWiFiOn = false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
isWiFiOn = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Send a message through the TCP Socket
|
/** Send a message through the TCP Socket
|
||||||
* @param seqID The messages's sequence ID (a number of order)
|
* @param seqID The messages's sequence ID (a number of order)
|
||||||
* @param msg The messages which will be sent
|
* @param msg The messages which will be sent
|
||||||
* @return True if the message was sent
|
* @return True if the message was sent
|
||||||
*/
|
*/
|
||||||
public boolean Write(String seqID, String msg)
|
public boolean Write(String seqID, String msg) {
|
||||||
{
|
try {
|
||||||
|
if (output != null) {
|
||||||
try
|
|
||||||
{
|
|
||||||
|
|
||||||
/*
|
|
||||||
if(writer != null)
|
|
||||||
{
|
|
||||||
String cmdok = "#" + seqID + msg;
|
|
||||||
Integer tmp = cmdok.length() + 1;
|
|
||||||
//SM.Debug("tmp:"+tmp);
|
|
||||||
|
|
||||||
tmp += Integer.toString(tmp).length();// tmp.ToString().Length;
|
|
||||||
if((tmp==10)||(tmp==100)||(tmp==1000)) tmp++;
|
|
||||||
cmdok = "#" + Integer.toString(tmp) + cmdok;
|
|
||||||
writer.write(encryptTEA(cmdok));
|
|
||||||
writer.flush();
|
|
||||||
SM.Debug("TX:"+encryptTEA(cmdok));
|
|
||||||
return true;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
if(output != null)
|
|
||||||
{
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(10);
|
Thread.sleep(10);
|
||||||
String cmdok = "#" + seqID + msg;
|
String cmdok = "#" + seqID + msg;
|
||||||
Integer tmp = cmdok.length() + 1;
|
int tmp = cmdok.length() + 1;
|
||||||
//SM.Debug("tmp:"+tmp);
|
//SM.Debug("tmp:"+tmp);
|
||||||
|
|
||||||
tmp += Integer.toString(tmp).length();// tmp.ToString().Length;
|
tmp += Integer.toString(tmp).length();
|
||||||
if((tmp==10)||(tmp==100)||(tmp==1000)) tmp++;
|
if ((tmp == 10)||(tmp == 100)||(tmp == 1000)) tmp++;
|
||||||
cmdok = "#" + Integer.toString(tmp) + cmdok;
|
cmdok = "#" + tmp + cmdok;
|
||||||
|
|
||||||
byte[] mess = encryptTEA(cmdok);
|
byte[] mess = encryptTEA(cmdok);
|
||||||
|
|
||||||
|
|
||||||
output.write(mess);
|
output.write(mess);
|
||||||
output.flush();
|
output.flush();
|
||||||
|
|
||||||
// show only notACK messages
|
// show only notACK messages
|
||||||
//if(mess[3] != 0x0C)
|
|
||||||
SM.Debug(" ", new String(mess));
|
SM.Debug(" ", new String(mess));
|
||||||
return true;
|
return true;
|
||||||
|
} catch (IOException | NoSuchElementException | InterruptedException e) {
|
||||||
|
SM.Exception("TCPClient[Send]", e.toString());
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
} else
|
||||||
//e.printStackTrace();
|
|
||||||
SM.Exception("TCPClient[Send]", e.toString());
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
SM.Exception("TCPClient[Send]", e.toString());
|
|
||||||
} catch (NoSuchElementException e) {
|
|
||||||
SM.Exception("TCPClient[Send]", e.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
} catch (Exception e) {
|
||||||
}
|
SM.Debug("TCPhandler Write Procedure:"+ e);
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
SM.Debug("TCPhandler Write Procedure:"+e.toString());
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void setConnectionIsDown() {
|
|
||||||
if(input != null) {
|
|
||||||
try {
|
|
||||||
input.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
input = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
isConnectionUP = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Encrypt a string using an encryption algorithm,
|
/* Encrypt a string using an encryption algorithm,
|
||||||
* in this case TEA */
|
* in this case TEA */
|
||||||
public static byte[] encryptTEA(String toEncryptData) {
|
public static byte[] encryptTEA(String toEncryptData) {
|
||||||
byte[] encryptedByteArray = new byte[]{};
|
|
||||||
|
|
||||||
/*
|
|
||||||
//encrypt message using TEA
|
|
||||||
try {
|
|
||||||
encryptedByteArray = TEA.encrypt(toEncryptData);
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
SM.Exception("encryptTEA Exception(UEE): " + e.toString());
|
|
||||||
} catch (IndexOutOfBoundsException e) {
|
|
||||||
SM.Exception("encryptTEA Exception(IOoBE): " + e.toString());
|
|
||||||
} catch (NullPointerException e) {
|
|
||||||
SM.Exception("encryptTEA Exception(NPE): " + e.toString());
|
|
||||||
}
|
|
||||||
//*/
|
|
||||||
|
|
||||||
// no encryption
|
// no encryption
|
||||||
encryptedByteArray = toEncryptData.getBytes();
|
return toEncryptData.getBytes();
|
||||||
|
|
||||||
return encryptedByteArray;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Decrypt a string using an encryption algorithm,
|
/* Decrypt a string using an encryption algorithm,
|
||||||
* in this case TEA */
|
* in this case TEA */
|
||||||
public static byte[] decryptTEA(byte[] toDecryptData) {
|
public static byte[] decryptTEA(byte[] toDecryptData) {
|
||||||
byte[] decryptedByteArray = new byte[]{};
|
byte[] decryptedByteArray;
|
||||||
|
|
||||||
/*
|
|
||||||
String sm = "";
|
|
||||||
for(int i=0; i<toDecryptData.length; i++)
|
|
||||||
sm+=toDecryptData[i] + " ";
|
|
||||||
|
|
||||||
SM.Debug("DDDDDD: " + sm);
|
|
||||||
//*/
|
|
||||||
/*
|
|
||||||
//decrypt message using TEA
|
|
||||||
try {
|
|
||||||
decryptedByteArray = TEA.decrypt(toDecryptData);
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
SM.Exception("encryptTEA Exception(UEE): " + e.toString());
|
|
||||||
} catch (IndexOutOfBoundsException e) {
|
|
||||||
SM.Exception("encryptTEA Exception(IOoBE): " + e.toString());
|
|
||||||
} catch (NullPointerException e) {
|
|
||||||
SM.Exception("encryptTEA Exception(NPE): " + e.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
String sm = "";
|
|
||||||
for(int i=0; i<decryptedByteArray.length; i++)
|
|
||||||
sm+= (int)decryptedByteArray[i] + " ";
|
|
||||||
//sm = new String(decryptedByteArray.toString());
|
|
||||||
try {
|
|
||||||
SM.Debug("##### " + new String(decryptedByteArray, "UTF-8"));
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
SM.Debug("DEcrypted: " + decryptedByteArray.toString());
|
|
||||||
|
|
||||||
//*/
|
|
||||||
|
|
||||||
// no decryption
|
// no decryption
|
||||||
decryptedByteArray = toDecryptData;
|
decryptedByteArray = toDecryptData;
|
||||||
|
|
||||||
return decryptedByteArray;
|
return decryptedByteArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public int getPort() {
|
public int getPort() {
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateTCPparameters(String ip, String _port)
|
public void updateTCPparameters(String ip, String _port) {
|
||||||
{
|
|
||||||
|
|
||||||
// stop socket
|
// stop socket
|
||||||
try {
|
try {
|
||||||
if(soc != null)
|
if(soc != null)
|
||||||
@ -537,30 +335,23 @@ public class TCPhandler implements Runnable
|
|||||||
}
|
}
|
||||||
|
|
||||||
serverHostname = ip;
|
serverHostname = ip;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
port = Integer.parseInt(_port);
|
port = Integer.parseInt(_port);
|
||||||
}
|
} catch (Exception ignored) { }
|
||||||
catch (Exception e) {
|
finally {
|
||||||
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
port = 13589;
|
port = 13589;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void triggerTCPConnectionStateEvent() {
|
public void triggerTCPConnectionStateEvent() {
|
||||||
if(!isConnectionUP)
|
if (!isConnectionUP)
|
||||||
TCPmsgParser._fireTCPConnectionDownEvent(previousConnectionWasUP);
|
TCPmsgParser._fireTCPConnectionDownEvent(previousConnectionWasUP);
|
||||||
else
|
else
|
||||||
TCPmsgParser._fireTCPConnectionUpEvent(previousConnectionWasUP);
|
TCPmsgParser._fireTCPConnectionUpEvent(previousConnectionWasUP);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RestartTCP()
|
private void RestartTCP() {
|
||||||
{
|
try {
|
||||||
try
|
|
||||||
{
|
|
||||||
isConnectionUP = false;
|
isConnectionUP = false;
|
||||||
previousConnectionWasUP = false;
|
previousConnectionWasUP = false;
|
||||||
|
|
||||||
@ -569,76 +360,22 @@ public class TCPhandler implements Runnable
|
|||||||
soc.connect(new InetSocketAddress(serverHostname, port), 5000);
|
soc.connect(new InetSocketAddress(serverHostname, port), 5000);
|
||||||
|
|
||||||
input = new DataInputStream(soc.getInputStream());
|
input = new DataInputStream(soc.getInputStream());
|
||||||
//input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
|
||||||
//output stream
|
//output stream
|
||||||
output = new DataOutputStream(soc.getOutputStream());
|
output = new DataOutputStream(soc.getOutputStream());
|
||||||
|
|
||||||
//recv= new BufferedReader(new InputStreamReader(soc.getInputStream()));
|
if (soc != null)
|
||||||
//writer =new PrintWriter(soc.getOutputStream()) ;
|
|
||||||
if(soc !=null)
|
|
||||||
{
|
|
||||||
//previousConnectionWasUP = isConnectionUP;
|
|
||||||
isConnectionUP = true;
|
isConnectionUP = true;
|
||||||
//triggerTCPConnectionStateEvent();
|
} catch (NullPointerException | IOException e) {
|
||||||
|
SM.Exception("RestartTCP break:"+ e);
|
||||||
/*
|
|
||||||
//init connection
|
|
||||||
boolean res = Write("0.0", "#19#" + AppParams.crtRadio.ip + "&"
|
|
||||||
+ AppParams.crtRadio.id + "#");
|
|
||||||
if(res){
|
|
||||||
SM.Debug("Message (AirPadInit) sent to controller");
|
|
||||||
}else{
|
|
||||||
SM.Debug("Could not send message(AirPadInit)!!");
|
|
||||||
}
|
|
||||||
|
|
||||||
//get
|
|
||||||
res = Write("0.0", "#30#104#0#0#0#0#");
|
|
||||||
if(res){
|
|
||||||
SM.Debug("Message (GetSetZoneAndChannel) sent to app server zoneNR:"+0+ " channelNR:"+0);
|
|
||||||
}else{
|
|
||||||
SM.Debug("Could not send message(GetSetZoneAndChannel)!!!");
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
//init audio
|
|
||||||
//AudioHandle.b_SendinitUDP = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (UnknownHostException e)
|
|
||||||
{
|
|
||||||
SM.Exception("RestartTCP break:"+e.toString());
|
|
||||||
isConnectionUP = false;
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (IllegalArgumentException e)
|
|
||||||
{
|
|
||||||
SM.Debug("IllegalArgumentException", "RestartTCP break:"+e.toString());
|
|
||||||
}
|
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
SM.Exception("RestartTCP break:"+e.toString());
|
|
||||||
isConnectionUP = false;
|
isConnectionUP = false;
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
SM.Debug("IllegalArgumentException", "RestartTCP break:"+ e);
|
||||||
}
|
}
|
||||||
catch (NullPointerException e)
|
|
||||||
{
|
|
||||||
SM.Exception("RestartTCP break:"+e.toString());
|
|
||||||
isConnectionUP = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
triggerTCPConnectionStateEvent();
|
triggerTCPConnectionStateEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isAlive() {
|
|
||||||
return alive;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAlive(boolean alive) {
|
|
||||||
this.alive = alive;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** close Socket when unReachable */
|
/** close Socket when unReachable */
|
||||||
public void closeSocket() {
|
public void closeSocket() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
input = null;
|
input = null;
|
||||||
output = null;
|
output = null;
|
||||||
@ -650,27 +387,23 @@ public class TCPhandler implements Runnable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Stop() {
|
||||||
public void Stop()
|
|
||||||
{
|
|
||||||
SM.Debug("Stopping TCP", "TCP Connection is stopping on " + AppParams.IP + ":" + port);
|
SM.Debug("Stopping TCP", "TCP Connection is stopping on " + AppParams.IP + ":" + port);
|
||||||
alive = false;
|
alive = false;
|
||||||
|
|
||||||
|
if (mReceived!= null)
|
||||||
if(mReceived!= null)
|
try {
|
||||||
try{ context.unregisterReceiver(mReceived); } catch(Exception ex) {/* receiver not registered //*/};
|
context.unregisterReceiver(mReceived);
|
||||||
|
} catch(Exception ex) {/* receiver not registered //*/}
|
||||||
|
|
||||||
// stop thread
|
// stop thread
|
||||||
if(listenThread != null)
|
if (listenThread != null) {
|
||||||
{
|
|
||||||
Thread moribund = listenThread;
|
Thread moribund = listenThread;
|
||||||
listenThread = null;
|
listenThread = null;
|
||||||
moribund.interrupt();
|
moribund.interrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (input != null) {
|
||||||
if(input!=null)
|
|
||||||
{
|
|
||||||
try {
|
try {
|
||||||
input.close();
|
input.close();
|
||||||
input = null;
|
input = null;
|
||||||
@ -679,8 +412,7 @@ public class TCPhandler implements Runnable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(output!=null)
|
if (output != null) {
|
||||||
{
|
|
||||||
try {
|
try {
|
||||||
output.close();
|
output.close();
|
||||||
output = null;
|
output = null;
|
||||||
@ -689,16 +421,13 @@ public class TCPhandler implements Runnable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(soc !=null)
|
if (soc !=null) {
|
||||||
{
|
|
||||||
try {
|
try {
|
||||||
soc.close();
|
soc.close();
|
||||||
soc = null;
|
soc = null;
|
||||||
}
|
} catch (IOException e) {
|
||||||
catch (IOException e) {
|
SM.Exception("TCPClient[STOP]", "Stop break:"+ e);
|
||||||
SM.Exception("TCPClient[STOP]", "Stop break:"+e.toString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
BIN
safeDispatch/release/SafeDispatchMobile_v.8.0.49-release.aab
Normal file
BIN
safeDispatch/release/SafeDispatchMobile_v.8.0.49-release.aab
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user