issue #750 y #751 solved

parent 5d973e7e
...@@ -2,25 +2,17 @@ package com.yottacode.net; ...@@ -2,25 +2,17 @@ package com.yottacode.net;
import android.util.Log; import android.util.Log;
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.Ack; import com.github.nkzawa.socketio.client.Ack;
import com.github.nkzawa.socketio.client.IO; import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket; import com.github.nkzawa.socketio.client.Socket;
import com.github.nkzawa.emitter.Emitter;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import java.security.SecureRandom;
/** /**
...@@ -74,7 +66,14 @@ public class SailsSocketsIO { ...@@ -74,7 +66,14 @@ public class SailsSocketsIO {
Log.d(this.getClass().getName(), " Listening:"+message); Log.d(this.getClass().getName(), " Listening:"+message);
socket.on(message, listener); socket.on(message, listener);
} }
/**
* Unregisters a connection listener to a given message
* @param message
*/
public void unregisterMessage(String message) {
Log.d(this.getClass().getName(), " Unlistening:"+message);
socket.off(message);
}
public SailsSocketsIO(String sails_socket_io_url, String ptransport, Emitter.Listener connectListener, Emitter.Listener errorListener) { public SailsSocketsIO(String sails_socket_io_url, String ptransport, Emitter.Listener connectListener, Emitter.Listener errorListener) {
...@@ -114,11 +113,13 @@ public class SailsSocketsIO { ...@@ -114,11 +113,13 @@ public class SailsSocketsIO {
JSONObject obj=new JSONObject().put("url", msg).put("data", params); JSONObject obj=new JSONObject().put("url", msg).put("data", params);
Log.d(this.getClass().getName(), "Emitted messsage:" + obj); Log.i(this.getClass().getName(), "Emitted messsage:" + obj);
socket.emit(this.EMIT_METHOD, obj, ack); socket.emit(this.EMIT_METHOD, obj, ack);
} }
public void destroy() { public void destroy() {
this.socket.disconnect(); if (this.socket.connected()) {
this.socket.off(); this.socket.off();
this.socket.disconnect();
}
} }
} }
...@@ -113,14 +113,14 @@ public class NetService implements Runnable { ...@@ -113,14 +113,14 @@ public class NetService implements Runnable {
public boolean online() {return updated;} public boolean online() {return updated;}
public void restart_app(boolean direct_login) { public void restart_app(boolean direct_login) {
PCBcontext.unset_user();
for (iNetServiceStatus listener: listeners) for (iNetServiceStatus listener: listeners)
if (listener instanceof iNetServiceDevice) ((iNetServiceDevice)listener).restart_app(direct_login); if (listener instanceof iNetServiceDevice) ((iNetServiceDevice)listener).restart_app(direct_login);
PCBcontext.unset_user();
} }
public void restart_app(Intent intent, boolean direct_login) { public void restart_app(Intent intent, boolean direct_login) {
PCBcontext.unset_user();
for (iNetServiceStatus listener: listeners) for (iNetServiceStatus listener: listeners)
if (listener instanceof iNetServiceDevice) ((iNetServiceDevice)listener).restart_app(intent, direct_login); if (listener instanceof iNetServiceDevice) ((iNetServiceDevice)listener).restart_app(intent, direct_login);
PCBcontext.unset_user();
} }
/** /**
......
...@@ -70,7 +70,6 @@ public class Room { ...@@ -70,7 +70,6 @@ public class Room {
public void connect() { public void connect() {
final String transport="polling"; final String transport="polling";
exit(); exit();
this.socket=new SailsSocketsIO(PCBcontext.getRestapiWrapper().getServer(), transport, new Emitter.Listener() { this.socket=new SailsSocketsIO(PCBcontext.getRestapiWrapper().getServer(), transport, new Emitter.Listener() {
@Override @Override
public void call(Object... args) { public void call(Object... args) {
...@@ -83,6 +82,7 @@ public class Room { ...@@ -83,6 +82,7 @@ public class Room {
@Override @Override
public void call(Object... args) { public void call(Object... args) {
Log.e(this.getClass().getName(), "IO sockects error: "+args[0]); Log.e(this.getClass().getName(), "IO sockects error: "+args[0]);
for (StackTraceElement element: Thread.currentThread().getStackTrace() ) Log.e(this.getClass().getName(), "IO sockect error detail: "+element.getClassName()+"."+element.getMethodName());
inRoom=false; inRoom=false;
PCBcontext.getNetService().setOffline(new SocketIOException(args[0].toString())); PCBcontext.getNetService().setOffline(new SocketIOException(args[0].toString()));
} }
...@@ -108,9 +108,16 @@ public class Room { ...@@ -108,9 +108,16 @@ public class Room {
Log.e(this.getClass().getCanonicalName(), "subscribe room failed:"+e.getMessage()); Log.e(this.getClass().getCanonicalName(), "subscribe room failed:"+e.getMessage());
} }
} }
void unlisten() {
for (String msg: this.listeners.keySet()) {
Log.i(this.getClass().getName(), "Unlistening to "+msg);
this.socket.unregisterMessage(msg);
}
}
void unsubscribe() { void unsubscribe() {
try { try {
UnsubscribeAction action = new UnsubscribeAction(); UnsubscribeAction action = new UnsubscribeAction();
this.emit(action); this.emit(action);
}catch (Exception e) { }catch (Exception e) {
...@@ -120,13 +127,16 @@ public class Room { ...@@ -120,13 +127,16 @@ public class Room {
public void exit() { public void exit() {
if (this.socket!=null) { if (this.socket!=null) {
Log.i(this.getClass().getName(), "Leaving room"); Log.i(this.getClass().getName(), "Leaving room");
unlisten();
unsubscribe(); unsubscribe();
this.socket.destroy(); this.socket.destroy();
this.socket=null; this.socket=null;
} }
} }
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");
exit(); exit();
} }
......
...@@ -33,7 +33,7 @@ public class StudentTalk implements Emitter.Listener { ...@@ -33,7 +33,7 @@ public class StudentTalk implements Emitter.Listener {
@Override @Override
public void call(Object... args) { public void call(Object... args) {
if (PCBcontext.getPcbdb()!=null)
try { try {
JSONObject msg = ((JSONObject) args[0]).getJSONObject("student"); JSONObject msg = ((JSONObject) args[0]).getJSONObject("student");
Log.i(this.getClass().getName(), "raw Received message " +msg.toString()); Log.i(this.getClass().getName(), "raw Received message " +msg.toString());
......
...@@ -45,8 +45,8 @@ public final class PCBcontext { ...@@ -45,8 +45,8 @@ public final class PCBcontext {
Log.i(PCBcontext.class.getCanonicalName(), "PCB context started. It's required" + Log.i(PCBcontext.class.getCanonicalName(), "PCB context started. It's required" +
"set_user method call"); "set_user method call");
} else { } else {
Log.e(PCBcontext.class.getClass().getCanonicalName(), "Init method was previously" + Log.e(PCBcontext.class.getCanonicalName(), "Init method was previously " +
"invoked! Please, check your code"); " invoked! Please, check your code");
} }
} }
...@@ -75,7 +75,7 @@ public final class PCBcontext { ...@@ -75,7 +75,7 @@ public final class PCBcontext {
public void change(User updatedStudent) { public void change(User updatedStudent) {
PCBcontext.getDevice().insertUser(updatedStudent); PCBcontext.getDevice().insertUser(updatedStudent);
if (updatedStudent.is_picto_size_big()!=getPcbdb().getCurrentUser().is_picto_size_big() || updatedStudent.has_categories()!=getPcbdb().getCurrentUser().has_categories()) if (updatedStudent.is_picto_size_big()!=getPcbdb().getCurrentUser().is_picto_size_big() || updatedStudent.has_categories()!=getPcbdb().getCurrentUser().has_categories())
PCBcontext.getNetService().getNetServiceDevice().restart_app(true); PCBcontext.getNetService().restart_app(true);
else { else {
PCBcontext.getPcbdb().setCurrentUser(updatedStudent); PCBcontext.getPcbdb().setCurrentUser(updatedStudent);
PCBcontext.getNetService().getNetServiceDevice().updateUserConfig(updatedStudent); PCBcontext.getNetService().getNetServiceDevice().updateUserConfig(updatedStudent);
...@@ -88,8 +88,8 @@ public final class PCBcontext { ...@@ -88,8 +88,8 @@ public final class PCBcontext {
public static void unset_user() { public static void unset_user() {
Log.i(PCBcontext.class.getCanonicalName(), "User unset. Student " + getPcbdb().getCurrentUser().get_name_stu()); Log.i(PCBcontext.class.getCanonicalName(), "User unset. Student " + getPcbdb().getCurrentUser().get_name_stu());
pcbdb = null;
if (room!=null) room.exit(); if (room!=null) room.exit();
pcbdb = null;
room = null; room = null;
vocabulary = null; vocabulary = null;
getNetService().notifyStatus(); getNetService().notifyStatus();
......
...@@ -112,7 +112,7 @@ public class LoginActivity extends FragmentActivity { ...@@ -112,7 +112,7 @@ public class LoginActivity extends FragmentActivity {
@Override @Override
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
Log.i(LOG_TAG,"Closing app"); Log.i(LOG_TAG,"Closing Login window");
PCBcontext.getNetService().closeNotifyStatus(); PCBcontext.getNetService().closeNotifyStatus();
} }
} }
...@@ -131,8 +131,9 @@ public class SerialActivity extends Activity { ...@@ -131,8 +131,9 @@ public class SerialActivity extends Activity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
Log.e(this.getClass().getCanonicalName(),"Creating serial activity"); Log.i(this.getClass().getCanonicalName(),"Creating serial activity");
PCBcontext.init(this, new NetServiceTablet()); if (!PCBcontext.init())
PCBcontext.init(this, new NetServiceTablet());
requestWindowFeature(Window.FEATURE_NO_TITLE); requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_serial); setContentView(R.layout.activity_serial);
...@@ -170,7 +171,7 @@ public class SerialActivity extends Activity { ...@@ -170,7 +171,7 @@ public class SerialActivity extends Activity {
@Override @Override
public void onStart() { public void onStart() {
Vector<User> users; Vector<User> users;
Log.e(this.getClass().getCanonicalName(),"Starting serial activity (direct login:"+getIntent().getBooleanExtra("resetPrevUser", true)+")"); Log.i(this.getClass().getCanonicalName(),"Starting serial activity (direct login:"+!getIntent().getBooleanExtra("resetPrevUser", true)+")");
if (!PCBcontext.init()) PCBcontext.init(this, new NetServiceTablet()); if (!PCBcontext.init()) PCBcontext.init(this, new NetServiceTablet());
String default_user[] = loginUserPolicy(); String default_user[] = loginUserPolicy();
String username = default_user[0]; String username = default_user[0];
......
...@@ -83,6 +83,10 @@ public class NetServiceTablet implements NetService.iNetServiceDevice { ...@@ -83,6 +83,10 @@ public class NetServiceTablet implements NetService.iNetServiceDevice {
serialActivity = new Intent(PCBcontext.getContext(), serialClass); serialActivity = new Intent(PCBcontext.getContext(), serialClass);
} }
serialActivity.putExtra("resetPrevUser", !direct_login); serialActivity.putExtra("resetPrevUser", !direct_login);
if (direct_login) {
serialActivity.putExtra("switch_usr",PCBcontext.getPcbdb().getCurrentUser().is_supervisor() ? PCBcontext.getPcbdb().getCurrentUser().get_email_sup() : PCBcontext.getPcbdb().getCurrentUser().get_nickname_stu());
serialActivity.putExtra("switch_pwd",PCBcontext.getPcbdb().getCurrentUser().is_supervisor() ? PCBcontext.getPcbdb().getCurrentUser().get_pwd_sup() : PCBcontext.getPcbdb().getCurrentUser().get_pwd_stu());
}
PCBcontext.getContext().startActivity(serialActivity); PCBcontext.getContext().startActivity(serialActivity);
} }
......
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