kopia lustrzana https://github.com/ge0rg/aprsdroid
tcpuploader: add exception catcher
rodzic
db80ae590b
commit
298ccae7f8
|
@ -31,6 +31,7 @@ class TcpUploader(service : AprsService, hostname : String, login : String, filt
|
||||||
|
|
||||||
def stop() {
|
def stop() {
|
||||||
conn.shutdown()
|
conn.shutdown()
|
||||||
|
conn.interrupt()
|
||||||
conn.join()
|
conn.join()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,21 +63,21 @@ class TcpUploader(service : AprsService, hostname : String, login : String, filt
|
||||||
|
|
||||||
override def run() {
|
override def run() {
|
||||||
Log.d(TAG, "TcpSocketThread.run()")
|
Log.d(TAG, "TcpSocketThread.run()")
|
||||||
init_socket()
|
catchLog("init_socket", init_socket)
|
||||||
while (running) {
|
while (running) {
|
||||||
try {
|
try {
|
||||||
if (!socket.isConnected()) {
|
|
||||||
Log.d(TAG, "reconnecting in 30s")
|
|
||||||
Thread.sleep(30*1000)
|
|
||||||
init_socket()
|
|
||||||
}
|
|
||||||
Log.d(TAG, "waiting for data...")
|
Log.d(TAG, "waiting for data...")
|
||||||
var line : String = null
|
var line : String = null
|
||||||
while ({ line = reader.readLine(); line != null }) {
|
while (running && { line = reader.readLine(); line != null }) {
|
||||||
Log.d(TAG, "recv: " + line)
|
Log.d(TAG, "recv: " + line)
|
||||||
if (line(0) != '#')
|
if (line(0) != '#')
|
||||||
service.postSubmit(line)
|
service.postSubmit(line)
|
||||||
}
|
}
|
||||||
|
if (running && (line == null || !socket.isConnected())) {
|
||||||
|
Log.d(TAG, "reconnecting in 30s")
|
||||||
|
Thread.sleep(30*1000)
|
||||||
|
init_socket()
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
case e : Exception => Log.d(TAG, "Exception" + e)
|
case e : Exception => Log.d(TAG, "Exception" + e)
|
||||||
}
|
}
|
||||||
|
@ -91,12 +92,22 @@ class TcpUploader(service : AprsService, hostname : String, login : String, filt
|
||||||
} else "TCP disconnected"
|
} else "TCP disconnected"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def catchLog(tag : String, fun : ()=>Unit) {
|
||||||
|
Log.d(TAG, "catchLog(" + tag + ")")
|
||||||
|
try {
|
||||||
|
fun()
|
||||||
|
} catch {
|
||||||
|
case e : Exception => Log.d(TAG, tag + " execption: " + e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
def shutdown() {
|
def shutdown() {
|
||||||
|
Log.d(TAG, "shutdown()")
|
||||||
this.synchronized {
|
this.synchronized {
|
||||||
running = false
|
running = false
|
||||||
socket.shutdownInput()
|
catchLog("shutdownInput", socket.shutdownInput)
|
||||||
socket.shutdownOutput()
|
catchLog("shutdownOutput", socket.shutdownOutput)
|
||||||
socket.close()
|
catchLog("socket.close", socket.close)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue