Pictogram 1.6, Beta 5, issue #400, #435,#431. Optimización de conexiones a…

Pictogram 1.6, Beta 5, issue #400, #435,#431. Optimización de conexiones a internet. Posible solución para caida de websockets
parent c24e7a2f
......@@ -28,7 +28,7 @@ android {
resValue "bool", "ssl_connect", "true"
resValue "bool", "force_img_download", "false"
resValue "integer", "netservice_timing", "5"
resValue "integer", "netservice_force_restfull_synchro", "1800"
resValue "integer", "netservice_force_restfull_synchro", "180"
}
debug {
resValue "string", "server", "https://dev.yottacode.com"
......
......@@ -21,7 +21,7 @@ public class TransactionMonitor {
db.beginTransactionNonExclusive();
Log.i(LOG_TAG,"Transaction begins ");
} else
Log.e(LOG_TAG,"*********************Transaction is previously started and not finished");
Log.e(LOG_TAG,"Transaction is previously started and not finished");
}
static public void endTransaction(boolean succesfull) {
if (db.inTransaction()) {//puede ocurrir que no haya nada
......
......@@ -133,6 +133,7 @@ public class UserLogin {
loginActivity.putExtra("sup_id", localUsers.elementAt(0).get_id_sup());
loginActivity.putExtra("name", localUsers.elementAt(0).get_name_sup());
loginActivity.putExtra("surname", localUsers.elementAt(0).get_surname_sup());
loginActivity.putExtra("email", email);
loginActivity.putExtra("pic", localUsers.elementAt(0).get_url_img_sup());
loginActivity.putExtra("office", localUsers.elementAt(0).get_office());
loginActivity.putExtra("offline", true);
......
......@@ -42,17 +42,19 @@ public class NetService implements Runnable, RestapiWrapper.iSilentLogin {
private final Vector<iNetServiceStatus> listeners;
private static final long restfullSynchroTimming=PCBcontext.getContext().getResources().getInteger(R.integer.netservice_force_restfull_synchro)*1000;
private static final long serverPingTimming=PCBcontext.getContext().getResources().getInteger(R.integer.netservice_timing)*10000;
private long nextRestfullSynchro;
public NetService(int delay, iNetServiceStatus listener) {
private long nextServerPing;
public NetService(iNetServiceStatus listener) {
this.updated=RestapiWrapper.ping(PCBcontext.getContext().getResources().getString(R.string.server), ping_session);
nextServerPing=0;
this.listeners = new Vector<>(2);
this.listeners.add(listener);
if (listener instanceof iNetServiceDevice) ((iNetServiceDevice)listener).build();
Log.i(LOG_TAG, "Checking Pictogram server access...");
Log.i(LOG_TAG, this.updated ? "Pictogram server access ok" : "Pictogram server access failed, Internet connection available?");
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
exec.scheduleWithFixedDelay(this, 0, delay, TimeUnit.SECONDS);
exec.scheduleWithFixedDelay(this, 0, PCBcontext.getContext().getResources().getInteger(R.integer.netservice_timing), TimeUnit.SECONDS);
}
public void addListener(iNetServiceStatus listener) {
if (!listeners.contains(listener))
......@@ -148,6 +150,19 @@ public class NetService implements Runnable, RestapiWrapper.iSilentLogin {
@Override
public void run() {
try {
ConnectivityManager cm =
(ConnectivityManager)PCBcontext.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (!isConnected) {
setOffline(new NetworkErrorException("No Internet access"));
return;
}
long now = new Date().getTime();
if ( (now < nextServerPing)) return;
nextServerPing=now+serverPingTimming;
PCBcontext.getRestapiWrapper().ask(ping_session, new RestapiWrapper.iRestapiListener() {
@Override
public void preExecute() {
......@@ -159,15 +174,6 @@ public class NetService implements Runnable, RestapiWrapper.iSilentLogin {
}
@Override
public void result(final JSONObject result) {
ConnectivityManager cm =
(ConnectivityManager)PCBcontext.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (!isConnected) {
setOffline(new NetworkErrorException("No Internet access"));
return;
}
try {
final float version = ((float)(Float.valueOf(result.getString("version")).intValue()*10))/10;
......@@ -178,31 +184,29 @@ public class NetService implements Runnable, RestapiWrapper.iSilentLogin {
} catch (Exception e) {
Log.e(LOG_TAG, "PING JSON ERROR: " + result + " " + e.getMessage());
}
long now = new Date().getTime();
if (!updated) {
nextRestfullSynchro = new Date().getTime();
nextRestfullSynchro = now;
updated = true;
String TAG_TOKEN="token";
try {
PCBcontext.getRestapiWrapper().setToken(result.getString(TAG_TOKEN));
Log.e(LOG_TAG,"AAAAAAAAAAAAAAAAAAAAAAAAAAAATOken:"+result.getString(TAG_TOKEN));
} catch (JSONException e) {
Log.e(LOG_TAG,"Error when getting token:"+e.getMessage()+" res:"+result.toString());
}
if (PCBcontext.is_user_logged()) //si el usuario aun no hizo login, en realidad no es necesario hacer nada
// Comprobar si hay usuario offline, para hacer login transparente
if (PCBcontext.is_user_offline()) {
Log.e(LOG_TAG, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPCB online login from offline login");
if (PCBcontext.is_user_offline())
login();
} else if (PCBcontext.is_user_online()) {
Log.e(LOG_TAG, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPCB reconnect");
else if (PCBcontext.is_user_online()) {
PCBcontext.getRoom().connect();
synchronizeStuData();
PCBcontext.getActionLog().batch();
} else Log.e(LOG_TAG,"AAAAAAAAAAAAAAAAAAAAAAAAAAAATOkener:"+result.toString());
};
} else {
//cada restfullSynchroTimming aprox. se fuerza sincronización de vocabulario y configuración de usuario
long now = new Date().getTime();
if (PCBcontext.is_user_logged()) {
if (restfullSynchroTimming > 0 && (now >= nextRestfullSynchro)) {
Log.i(LOG_TAG, "Vocabulary request.");
......
......@@ -29,12 +29,20 @@ import java.util.Hashtable;
*/
public class Room {
private static final String LOG_TAG = Room.class.getSimpleName();
private SailsSocketsIO socket=null;
private boolean inRoom=false;
private final Hashtable<String, Emitter.Listener> listeners;
public Room( ) {
Log.i(this.getClass().getName(), "Entering room");
listeners=new Hashtable<>();
listeners.put("disconnect", new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.e(LOG_TAG,"******************Websockect disconnect");
PCBcontext.getNetService().setOffline(new SocketIOException("Websocket connection is lost"));
}
});
connect();
}
......@@ -95,9 +103,11 @@ public class Room {
}
private void listen_again() {
for (String msg: this.listeners.keySet()) {
Log.i(this.getClass().getName(), "Listening to "+msg);
Log.e(this.getClass().getName(), "******************************Listening to "+msg);
this.socket.unregisterMessage(msg);
this.socket.registerMessage(msg, this.listeners.get(msg));
}
}
private void subscribe() {
......@@ -117,7 +127,6 @@ public class Room {
private void unsubscribe() {
try {
UnsubscribeAction action = new UnsubscribeAction();
this.emit(action);
}catch (Exception e) {
......@@ -137,6 +146,8 @@ public class Room {
public void finalize() throws java.lang.Throwable {
super.finalize();
Log.i(this.getClass().getName(), "Discarding room");
Log.e(LOG_TAG,"Websocket connection is lost because ojbect is finalized");
PCBcontext.getNetService().setOffline(new SocketIOException("Websocket connection is lost because ojbect is finalized"));
exit();
}
......
......@@ -49,7 +49,7 @@ public final class PCBcontext {
device = new Device(c, null, DeviceHelper.getDBVersion(context));
activityContext=null;
SSLDummyContext.init(context.getResources().getBoolean(R.bool.ssl_connect));
service = new NetService(context.getResources().getInteger(R.integer.netservice_timing),listener);
service = new NetService(listener);
wrapper = new RestapiWrapper(context.getResources().getString(R.string.server), null, service);
device.deleteDeprecatedImgs();
Log.i(PCBcontext.class.getCanonicalName(), "PCB context started. It's required" +
......
......@@ -137,12 +137,12 @@ public class SerialActivity extends Activity {
editor.putString("username", username);
editor.putString("password", password);
editor.commit();
/*if (!username.equals("") && !password.equals(""))
if (!username.equals("") && !password.equals(""))
try {
new UserLogin().login(username, password, SerialActivity.this, Class.forName(getString(R.string.activityAfterLogin)));
} catch (ClassNotFoundException e) {
Log.e(LOG_TAG,e.toString());
}*/
}
}
});
......
......@@ -18,7 +18,7 @@ public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("MAIN_AC", "Is user log? "+PCBcontext.is_user_logged());
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment