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 { ...@@ -28,7 +28,7 @@ android {
resValue "bool", "ssl_connect", "true" resValue "bool", "ssl_connect", "true"
resValue "bool", "force_img_download", "false" resValue "bool", "force_img_download", "false"
resValue "integer", "netservice_timing", "5" resValue "integer", "netservice_timing", "5"
resValue "integer", "netservice_force_restfull_synchro", "1800" resValue "integer", "netservice_force_restfull_synchro", "180"
} }
debug { debug {
resValue "string", "server", "https://dev.yottacode.com" resValue "string", "server", "https://dev.yottacode.com"
......
...@@ -21,7 +21,7 @@ public class TransactionMonitor { ...@@ -21,7 +21,7 @@ public class TransactionMonitor {
db.beginTransactionNonExclusive(); db.beginTransactionNonExclusive();
Log.i(LOG_TAG,"Transaction begins "); Log.i(LOG_TAG,"Transaction begins ");
} else } 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) { static public void endTransaction(boolean succesfull) {
if (db.inTransaction()) {//puede ocurrir que no haya nada if (db.inTransaction()) {//puede ocurrir que no haya nada
......
...@@ -133,6 +133,7 @@ public class UserLogin { ...@@ -133,6 +133,7 @@ public class UserLogin {
loginActivity.putExtra("sup_id", localUsers.elementAt(0).get_id_sup()); loginActivity.putExtra("sup_id", localUsers.elementAt(0).get_id_sup());
loginActivity.putExtra("name", localUsers.elementAt(0).get_name_sup()); loginActivity.putExtra("name", localUsers.elementAt(0).get_name_sup());
loginActivity.putExtra("surname", localUsers.elementAt(0).get_surname_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("pic", localUsers.elementAt(0).get_url_img_sup());
loginActivity.putExtra("office", localUsers.elementAt(0).get_office()); loginActivity.putExtra("office", localUsers.elementAt(0).get_office());
loginActivity.putExtra("offline", true); loginActivity.putExtra("offline", true);
......
...@@ -42,17 +42,19 @@ public class NetService implements Runnable, RestapiWrapper.iSilentLogin { ...@@ -42,17 +42,19 @@ public class NetService implements Runnable, RestapiWrapper.iSilentLogin {
private final Vector<iNetServiceStatus> listeners; 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 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; 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); this.updated=RestapiWrapper.ping(PCBcontext.getContext().getResources().getString(R.string.server), ping_session);
nextServerPing=0;
this.listeners = new Vector<>(2); this.listeners = new Vector<>(2);
this.listeners.add(listener); this.listeners.add(listener);
if (listener instanceof iNetServiceDevice) ((iNetServiceDevice)listener).build(); if (listener instanceof iNetServiceDevice) ((iNetServiceDevice)listener).build();
Log.i(LOG_TAG, "Checking Pictogram server access..."); 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?"); Log.i(LOG_TAG, this.updated ? "Pictogram server access ok" : "Pictogram server access failed, Internet connection available?");
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1); 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) { public void addListener(iNetServiceStatus listener) {
if (!listeners.contains(listener)) if (!listeners.contains(listener))
...@@ -148,6 +150,19 @@ public class NetService implements Runnable, RestapiWrapper.iSilentLogin { ...@@ -148,6 +150,19 @@ public class NetService implements Runnable, RestapiWrapper.iSilentLogin {
@Override @Override
public void run() { public void run() {
try { 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() { PCBcontext.getRestapiWrapper().ask(ping_session, new RestapiWrapper.iRestapiListener() {
@Override @Override
public void preExecute() { public void preExecute() {
...@@ -159,15 +174,6 @@ public class NetService implements Runnable, RestapiWrapper.iSilentLogin { ...@@ -159,15 +174,6 @@ public class NetService implements Runnable, RestapiWrapper.iSilentLogin {
} }
@Override @Override
public void result(final JSONObject result) { 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 { try {
final float version = ((float)(Float.valueOf(result.getString("version")).intValue()*10))/10; final float version = ((float)(Float.valueOf(result.getString("version")).intValue()*10))/10;
...@@ -178,31 +184,29 @@ public class NetService implements Runnable, RestapiWrapper.iSilentLogin { ...@@ -178,31 +184,29 @@ public class NetService implements Runnable, RestapiWrapper.iSilentLogin {
} catch (Exception e) { } catch (Exception e) {
Log.e(LOG_TAG, "PING JSON ERROR: " + result + " " + e.getMessage()); Log.e(LOG_TAG, "PING JSON ERROR: " + result + " " + e.getMessage());
} }
long now = new Date().getTime();
if (!updated) { if (!updated) {
nextRestfullSynchro = new Date().getTime(); nextRestfullSynchro = now;
updated = true; updated = true;
String TAG_TOKEN="token"; String TAG_TOKEN="token";
try { try {
PCBcontext.getRestapiWrapper().setToken(result.getString(TAG_TOKEN)); PCBcontext.getRestapiWrapper().setToken(result.getString(TAG_TOKEN));
Log.e(LOG_TAG,"AAAAAAAAAAAAAAAAAAAAAAAAAAAATOken:"+result.getString(TAG_TOKEN));
} catch (JSONException e) { } catch (JSONException e) {
Log.e(LOG_TAG,"Error when getting token:"+e.getMessage()+" res:"+result.toString()); 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 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 // Comprobar si hay usuario offline, para hacer login transparente
if (PCBcontext.is_user_offline()) { if (PCBcontext.is_user_offline())
Log.e(LOG_TAG, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPCB online login from offline login");
login(); login();
} else if (PCBcontext.is_user_online()) { else if (PCBcontext.is_user_online()) {
Log.e(LOG_TAG, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPCB reconnect");
PCBcontext.getRoom().connect(); PCBcontext.getRoom().connect();
synchronizeStuData(); synchronizeStuData();
PCBcontext.getActionLog().batch(); PCBcontext.getActionLog().batch();
} else Log.e(LOG_TAG,"AAAAAAAAAAAAAAAAAAAAAAAAAAAATOkener:"+result.toString()); };
} else { } else {
//cada restfullSynchroTimming aprox. se fuerza sincronización de vocabulario y configuración de usuario //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 (PCBcontext.is_user_logged()) {
if (restfullSynchroTimming > 0 && (now >= nextRestfullSynchro)) { if (restfullSynchroTimming > 0 && (now >= nextRestfullSynchro)) {
Log.i(LOG_TAG, "Vocabulary request."); Log.i(LOG_TAG, "Vocabulary request.");
......
...@@ -29,12 +29,20 @@ import java.util.Hashtable; ...@@ -29,12 +29,20 @@ import java.util.Hashtable;
*/ */
public class Room { public class Room {
private SailsSocketsIO socket=null; private static final String LOG_TAG = Room.class.getSimpleName();
private SailsSocketsIO socket=null;
private boolean inRoom=false; private boolean inRoom=false;
private final Hashtable<String, Emitter.Listener> listeners; private final Hashtable<String, Emitter.Listener> listeners;
public Room( ) { public Room( ) {
Log.i(this.getClass().getName(), "Entering room"); Log.i(this.getClass().getName(), "Entering room");
listeners=new Hashtable<>(); 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(); connect();
} }
...@@ -95,9 +103,11 @@ public class Room { ...@@ -95,9 +103,11 @@ public class Room {
} }
private void listen_again() { private void listen_again() {
for (String msg: this.listeners.keySet()) { 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)); this.socket.registerMessage(msg, this.listeners.get(msg));
} }
} }
private void subscribe() { private void subscribe() {
...@@ -117,7 +127,6 @@ public class Room { ...@@ -117,7 +127,6 @@ public class Room {
private void unsubscribe() { private void unsubscribe() {
try { try {
UnsubscribeAction action = new UnsubscribeAction(); UnsubscribeAction action = new UnsubscribeAction();
this.emit(action); this.emit(action);
}catch (Exception e) { }catch (Exception e) {
...@@ -135,9 +144,11 @@ public class Room { ...@@ -135,9 +144,11 @@ public class Room {
} }
public void finalize() throws java.lang.Throwable { public void finalize() throws java.lang.Throwable {
super.finalize(); super.finalize();
Log.i(this.getClass().getName(), "Discarding room"); Log.i(this.getClass().getName(), "Discarding room");
exit(); 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 { ...@@ -49,7 +49,7 @@ public final class PCBcontext {
device = new Device(c, null, DeviceHelper.getDBVersion(context)); device = new Device(c, null, DeviceHelper.getDBVersion(context));
activityContext=null; activityContext=null;
SSLDummyContext.init(context.getResources().getBoolean(R.bool.ssl_connect)); 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); wrapper = new RestapiWrapper(context.getResources().getString(R.string.server), null, service);
device.deleteDeprecatedImgs(); device.deleteDeprecatedImgs();
Log.i(PCBcontext.class.getCanonicalName(), "PCB context started. It's required" + Log.i(PCBcontext.class.getCanonicalName(), "PCB context started. It's required" +
......
...@@ -137,12 +137,12 @@ public class SerialActivity extends Activity { ...@@ -137,12 +137,12 @@ public class SerialActivity extends Activity {
editor.putString("username", username); editor.putString("username", username);
editor.putString("password", password); editor.putString("password", password);
editor.commit(); editor.commit();
/*if (!username.equals("") && !password.equals("")) if (!username.equals("") && !password.equals(""))
try { try {
new UserLogin().login(username, password, SerialActivity.this, Class.forName(getString(R.string.activityAfterLogin))); new UserLogin().login(username, password, SerialActivity.this, Class.forName(getString(R.string.activityAfterLogin)));
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
Log.e(LOG_TAG,e.toString()); Log.e(LOG_TAG,e.toString());
}*/ }
} }
}); });
......
...@@ -18,7 +18,7 @@ public class MainActivity extends Activity { ...@@ -18,7 +18,7 @@ public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); 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); final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override @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