safedispatch-mobile/safeDispatch/src/main/java/com/safemobile/lib/sound/TcpAudioClient.java

173 lines
3.7 KiB
Java

package com.safemobile.lib.sound;
import com.safemobile.lib.SM;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
public class TcpAudioClient implements Runnable {
private boolean alive = true;
private String serverHostname;
private final int port;
private Thread listenThread;
private Socket soc = null;
private InputStream recv;
private OutputStream writer;
private Boolean connOK = false;
byte[] buffer = new byte[16384];
private final List<ITCPaudioLis> listeners = new ArrayList<>();
public TcpAudioClient(String hostName, int port) {
setServerHostname(hostName);
this.port = port;
SM.Debug("---TcpHandler constructor--- port:" + port);
listenThread = new Thread(this, "TcpListen");
listenThread.start(); // (2) Start the thread.
}
@Override
public void run() {
try {
soc = new Socket(getServerHostname(), port);
SM.Debug("Socket timeout:" + soc.getSoTimeout());
recv = soc.getInputStream();
writer = soc.getOutputStream();
if (soc != null)
setConnOK(true);
} catch (UnknownHostException e) {
SM.Debug("UnknownHostException", "break:" + e);
} catch (IOException e) {
SM.Debug("IOException", "break:" + e);
}
while (alive) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
SM.Debug(e.toString());
Thread.currentThread().interrupt();
}
while (Boolean.TRUE.equals(getConnOK())) {
try {
buffer = new byte[16384];
int n = recv.read(buffer);
if (n == -1)
break;
fireDataArrived(buffer, n);
} catch (Exception ex) {
SM.Debug("break:" + ex);
setConnOK(false);
}
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
SM.Debug(e.toString());
Thread.currentThread().interrupt();
}
if (alive) restartTCP();
}
SM.Debug("==================================");
SM.Debug("TCP listenThread stopped!! alive = false");
SM.Debug("==================================");
}
public boolean send(byte[] data, int len) {
try {
if (writer != null) {
writer.write(data, 0, len);
return true;
} else {
return false;
}
} catch (Exception e) {
SM.Debug(e.toString());
}
return false;
}
private void restartTCP() {
try {
SM.Debug("Restarting TCP...ip:" + getServerHostname());
soc = new Socket(getServerHostname(), port);
recv = soc.getInputStream();
writer = soc.getOutputStream();
setConnOK(true);
} catch (IOException e) {
SM.Debug("break:" + e);
}
}
public boolean isAlive() {
return alive;
}
public void setAlive(boolean alive) {
this.alive = alive;
}
public void stop() {
this.alive = false;
if (soc != null) {
try {
soc.close();
soc = null;
} catch (IOException e) {
SM.Debug("break:" + e);
}
setConnOK(false);
}
// stop thread
if (listenThread != null) {
Thread moribund = listenThread;
listenThread = null;
moribund.interrupt();
}
}
public synchronized void addTCPListener(ITCPaudioLis l) {
listeners.add(l);
}
public synchronized void removeTCPListener(ITCPaudioLis l) {
listeners.remove(l);
}
private synchronized void fireDataArrived(byte[] data, int len) {
TCPaudioEvent event = new TCPaudioEvent(this, data, len);
for (ITCPaudioLis listener : listeners) {
listener.dataRecv(event);
}
}
public String getServerHostname() {
return serverHostname;
}
public void setServerHostname(String serverHostname) {
this.serverHostname = serverHostname;
}
public Boolean getConnOK() {
return connOK;
}
public void setConnOK(Boolean connOK) {
this.connOK = connOK;
}
}