java - Application IncomingHandler sometimes not receiving Service message -
i'm building android app , i'm struggling handling reconnection.
so flow this:
1. connect token have
2. user connecting server socket.io
2.a) have active token
- positive response server => happy
2.b) have no token or token not in database
- have new token server
- new request made that
- in response user trying connect 1 more time
now code:
application
main function i'm running make sure connected
public static boolean issocketconnected = false; //declared in androidapplication public static boolean isservicebound = false; //declared in androidapplication public volatile boolean allownextsocketconnection = true; //declared in same calss _runmeplease_ private void runmeplease() { //running in new thread if (androidapplication.isservicebound) { if (androidapplication.issocketconnected) { allownextsocketconnection = false; connected = true; logger.log("we connected"); } else { if(isnetworkavailable()){ if(allownextsocketconnection) { allownextsocketconnection = false; androidapplication.senddatatoservice(connect); } else { logger.log("waiting service response"); try { thread.sleep(1000); } catch (interruptedexception e) { e.printstacktrace(); } } runmeplease(); } }
and i'm running after new token coming by:
public void setsocketnewtoken(){ androidapplication.issocketconnected = false; allownextsocketconnection = true; }
and running after successful connection:
public void setsocketconnected(){ logger.log("socket connected"); androidapplication.issocketconnected = true; }
everything runs smoothly on first run , if kill server , reconnect fast. after token expires , reconnection made it's not connecting or it's connecting still looping through "waiting service response" , shouldn't because have issocketconnected on true.
any ideas?
edit
after putting ton of logs can see there point runmeplease() working, i'm receiving update service , it's not running incominghandler.
androidapplication snippet (after implementing atomicboolean)
static class incominghandler extends handler { @override public void handlemessage(message msg) { switch (msg.what) { case connected: logger.log("new connect"); issocketconnected.set(true); connectionmanager.setsocketconnected(); break; case disconnected: logger.log("disconnect"); connectionmanager.setsocketdisconnected(); break; case refresh_token: logger.log("refresh token"); new thread("try2connect") { @override public void run() { getnewtoken(); } }.start(); break; default: super.handlemessage(msg); } }
service snippet
public emitter.listener connected = new emitter.listener() { @override public void call(object... args) { logger.log("connect!!!!!!!!!"); messengersynchronizer.sendtoapplication(connected, ""); // sending data androidapplication } };
so after i'm running runmeplease(), , waiting in logger.log("waiting service response"); response logged in emit.listener connected androidapplication not running connected block code.
it's working first time, after rerunning runmeplease() it's looping without possibility end.
i haven't looked @ detailed flow when access issocketconnected
, isservicebound
different threads have declare them volatile
writes visible when reading them thread. otherwise thread may read cached values of variables though changed in thread.
also when have multple threads can call runmeplease()
make sense use atomicboolean allownextsocketconnection
because otherwise following code
if(allownextsocketconnection) { allownextsocketconnection = false; // ...
two threads enter if
when allownextsocketconnection
true
. prevent can use compareandset(boolean expect, boolean update)
method 1 thread can enter if
.
Comments
Post a Comment