rework backend API for slow-start services

When a backend does not accept packets immediately after start() (i.e.
because it needs to connect to a server first), start() must return
false and the backend needs to call service.postPosterStarted() as soon
as it is ready to accept data.
obj_origin
Georg Lukas 2011-11-11 20:43:29 +01:00
rodzic 538421c7dd
commit 4a81377ce1
8 zmienionych plików z 40 dodań i 25 usunięć

Wyświetl plik

@ -84,6 +84,9 @@ class AprsService extends Service {
} else
showToast(getString(R.string.service_start).format(upd_int, upd_dist))
val callssid = prefs.getCallSsid()
ServiceNotifier.instance.start(this, callssid)
// the poster needs to be running before location updates come in
if (!running) {
running = true
@ -91,21 +94,28 @@ class AprsService extends Service {
// register for outgoing message notifications
registerReceiver(msgNotifier, new IntentFilter(AprsService.MESSAGETX))
}
// continuous GPS tracking for single shot mode
val loc_info = locSource.start(singleShot)
val callssid = prefs.getCallSsid()
val message = "%s: %s".format(callssid, loc_info)
ServiceNotifier.instance.start(this, message)
} else
onPosterStarted()
}
def startPoster() {
if (poster != null)
poster.stop()
poster = AprsIsUploader.instanciateUploader(this, prefs)
poster.start()
if (poster.start())
onPosterStarted()
}
def onPosterStarted() {
Log.d(TAG, "onPosterStarted")
// (re)start location source, get location source name
val loc_info = locSource.start(singleShot)
val callssid = prefs.getCallSsid()
val message = "%s: %s".format(callssid, loc_info)
ServiceNotifier.instance.start(this, message)
msgService.sendPendingMessages()
}
override def onBind(i : Intent) : IBinder = null
@ -226,6 +236,11 @@ class AprsService extends Service {
def postAbort(post : String) {
postAddPost(StorageDatabase.Post.TYPE_ERROR, R.string.post_error, post)
}
def postPosterStarted() {
handler.post {
onPosterStarted()
}
}
}

Wyświetl plik

@ -12,9 +12,6 @@ class MessageService(s : AprsService) {
val NUM_OF_RETRIES = 7
val pendingSender = new Runnable() { override def run() { sendPendingMessages() } }
// lets start transmitting 30s after creation
scheduleNextSend(30*1000)
def createMessageNotifier() = new BroadcastReceiver() {
override def onReceive(ctx : Context, i : Intent) {
sendPendingMessages()

Wyświetl plik

@ -12,8 +12,7 @@ class AfskUploader(prefs : PrefsWrapper) extends AprsIsUploader(prefs) {
var Digis = prefs.getString("digi_path", "WIDE1-1")
val output = new Afsk()
def start() {
}
def start() = true
def update(packet : APRSPacket) : String = {
// Need to "parse" the packet in order to replace the Digipeaters

Wyświetl plik

@ -59,7 +59,9 @@ object AprsIsUploader {
abstract class AprsIsUploader(prefs : PrefsWrapper) {
val login = prefs.getLoginString()
def start()
// returns true if successfully started.
// when returning false, AprsService.postPosterStarted() must be called
def start() : Boolean
def update(packet : APRSPacket) : String

Wyświetl plik

@ -21,9 +21,10 @@ class BluetoothTnc(service : AprsService, prefs : PrefsWrapper) extends AprsIsUp
var digipath = prefs.getString("digi_path", "WIDE1-1")
var conn : BtSocketThread = null
createConnection()
def start() {
def start() = {
if (conn == null)
createConnection()
false
}
def createConnection() {
@ -107,6 +108,7 @@ class BluetoothTnc(service : AprsService, prefs : PrefsWrapper) extends AprsIsUp
} catch {
case e : Exception => e.printStackTrace(); service.postAbort(e.toString())
}
service.postPosterStarted()
while (running) {
try {
Log.d(TAG, "waiting for data...")

Wyświetl plik

@ -12,8 +12,7 @@ class HttpPostUploader(prefs : PrefsWrapper) extends AprsIsUploader(prefs) {
val TAG = "APRSdroid.HttpPost"
val host = prefs.getString("http.server", "srvr.aprs-is.net")
def start() {
}
def start() = true
def doPost(urlString : String, content : String) : String = {
val client = new DefaultHttpClient()

Wyświetl plik

@ -15,9 +15,10 @@ class TcpUploader(service : AprsService, prefs : PrefsWrapper) extends AprsIsUpl
val RECONNECT = 30
var conn : TcpSocketThread = null
createConnection()
def start() {
def start() = {
if (conn == null)
createConnection()
false
}
def setupFilter() : String = {
@ -87,6 +88,7 @@ class TcpUploader(service : AprsService, prefs : PrefsWrapper) extends AprsIsUpl
} catch {
case e : Exception => service.postAbort(e.toString())
}
service.postPosterStarted()
while (running) {
try {
if (need_reconnect) {

Wyświetl plik

@ -10,8 +10,7 @@ class UdpUploader(prefs : PrefsWrapper) extends AprsIsUploader(prefs) {
lazy val socket = new DatagramSocket()
val host = prefs.getString("udp.server", "srvr.aprs-is.net")
def start() {
}
def start() = true
def update(packet : APRSPacket) : String = {
val (h, port) = AprsPacket.parseHostPort(host, 8080)