optimizando websockets. trabajando en bug batch actions

parent fdf4ee75
...@@ -65,7 +65,7 @@ public class RestapiWrapper { ...@@ -65,7 +65,7 @@ public class RestapiWrapper {
httpAsyncTaskParams.get=postOrGet.equals("get"); httpAsyncTaskParams.get=postOrGet.equals("get");
httpAsyncTaskParams.listener=listener; httpAsyncTaskParams.listener=listener;
httpAsyncTaskParams.url_params=params; httpAsyncTaskParams.url_params=params;
httpAsyncTaskParams.url=this.server + '/' + operation;; httpAsyncTaskParams.url=this.server + '/' + operation;
new HttpAsyncTask().execute(httpAsyncTaskParams); new HttpAsyncTask().execute(httpAsyncTaskParams);
} }
...@@ -132,12 +132,14 @@ public class RestapiWrapper { ...@@ -132,12 +132,14 @@ public class RestapiWrapper {
// convert inputstream to string // convert inputstream to string
if (inputStream!=null) result = convertInputStreamToString(inputStream); if (inputStream!=null) result = convertInputStreamToString(inputStream);
} catch (IOException e) { } catch (IOException e) {
listener.error(e); Log.e(RestapiWrapper.class.getName(), e.getLocalizedMessage());
if (listener!=null) listener.error(e);
result=e.getMessage();
} }
return result; return result;
} }
public String POST(String surl, Hashtable<String, String> params, iRestapiListener listener) { public String POST(String surl, Hashtable<String, String> params) {
URL url = null; URL url = null;
String response = ""; String response = "";
...@@ -181,7 +183,7 @@ public class RestapiWrapper { ...@@ -181,7 +183,7 @@ public class RestapiWrapper {
} }
} catch (IOException e) { } catch (IOException e) {
Log.e(com.yottacode.net.RestapiWrapper.class.getName(), "Error:" + e.getLocalizedMessage() + " when asking for " + surl); Log.e(com.yottacode.net.RestapiWrapper.class.getName(), "Error:" + e.getLocalizedMessage() + " when asking for " + surl);
listener.error(e); response=e.getMessage();
} }
return response; return response;
} }
...@@ -211,9 +213,9 @@ public class RestapiWrapper { ...@@ -211,9 +213,9 @@ public class RestapiWrapper {
@Override @Override
protected HttpAsyncTaskParams doInBackground(HttpAsyncTaskParams... params) { protected HttpAsyncTaskParams doInBackground(HttpAsyncTaskParams... params) {
Log.d(LOG_TAG, "POST params doIn:" + params[0].url_params.toString()); Log.d(LOG_TAG, "POST params doIn:" + params[0].url_params.toString()+" Get?"+params[0].get);
params[0].result = params[0].get ? POST(params[0].url, params[0].url_params,params[0].listener) params[0].result = params[0].get ? GET(params[0].url, params[0].url_params, null)
: GET(params[0].url, params[0].url_params,params[0].listener); : POST(params[0].url, params[0].url_params);
return params[0]; return params[0];
} }
...@@ -231,7 +233,7 @@ public class RestapiWrapper { ...@@ -231,7 +233,7 @@ public class RestapiWrapper {
} else if (jsonResult instanceof JSONArray) { } else if (jsonResult instanceof JSONArray) {
// The result is an array // The result is an array
params.listener.result((JSONArray) jsonResult); params.listener.result((JSONArray) jsonResult);
} } else params.listener.result((JSONObject) null);
}else{ }else{
params.listener.result((JSONObject) null); params.listener.result((JSONObject) null);
} }
......
...@@ -57,12 +57,12 @@ public class SailsSocketsIO { ...@@ -57,12 +57,12 @@ public class SailsSocketsIO {
* @param socket * @param socket
* @param connectListener * @param connectListener
*/ */
private void registerMessages(Socket socket, Emitter.Listener connectListener) { private void registerMessages(Socket socket, Emitter.Listener connectListener, Emitter.Listener errorListener) {
socket.on(Socket.EVENT_CONNECT, connectListener) socket.on(Socket.EVENT_CONNECT, connectListener)
.on(Socket.EVENT_CONNECT_ERROR, new DefaultListener(Socket.EVENT_CONNECT_ERROR)) .on(Socket.EVENT_CONNECT_ERROR, errorListener)
.on(Socket.EVENT_CONNECT_TIMEOUT, new DefaultListener(Socket.EVENT_CONNECT_TIMEOUT)) .on(Socket.EVENT_CONNECT_TIMEOUT, errorListener)
.on(Socket.EVENT_ERROR, new DefaultListener(Socket.EVENT_ERROR)) .on(Socket.EVENT_ERROR, errorListener)
.on(Socket.EVENT_DISCONNECT, new DefaultListener(Socket.EVENT_DISCONNECT)); .on(Socket.EVENT_DISCONNECT, errorListener);
} }
/** /**
...@@ -76,7 +76,7 @@ public class SailsSocketsIO { ...@@ -76,7 +76,7 @@ public class SailsSocketsIO {
} }
public SailsSocketsIO(String sails_socket_io_url, String ptransport, Emitter.Listener connectListener) { public SailsSocketsIO(String sails_socket_io_url, String ptransport, Emitter.Listener connectListener, Emitter.Listener errorListener) {
final String sdk_version= "0.11.0"; final String sdk_version= "0.11.0";
final String sdk_module="node"; final String sdk_module="node";
...@@ -102,7 +102,7 @@ public class SailsSocketsIO { ...@@ -102,7 +102,7 @@ public class SailsSocketsIO {
this.socket = IO.socket(sails_socket_io_url, opts); this.socket = IO.socket(sails_socket_io_url, opts);
this.socket.connect(); this.socket.connect();
this.registerMessages(socket, connectListener); this.registerMessages(socket, connectListener, errorListener);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
Log.e(e.getClass().getName(), e.getClass().getCanonicalName()+": "+e.getMessage() ); Log.e(e.getClass().getName(), e.getClass().getCanonicalName()+": "+e.getMessage() );
...@@ -110,18 +110,12 @@ public class SailsSocketsIO { ...@@ -110,18 +110,12 @@ public class SailsSocketsIO {
} }
public void emit(String msg, Object params) throws JSONException { public void emit(String msg, Object params, Ack ack) throws JSONException {
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.d(this.getClass().getName(), "Emitted messsage:" + obj);
socket.emit(this.EMIT_METHOD, obj, new Ack() { socket.emit(this.EMIT_METHOD, obj, ack);
@Override
public void call(Object... args) {
for (Object arg : args)
Log.d(this.getClass().getName(), "Ack messsage:" + arg);
}
});
} }
public void destroy() { public void destroy() {
......
...@@ -30,16 +30,20 @@ public class ActionLog implements iRestapiListener { ...@@ -30,16 +30,20 @@ public class ActionLog implements iRestapiListener {
// String constant for logs // String constant for logs
private static final String LOG_TAG = ActionLog.class.getCanonicalName(); private static final String LOG_TAG = ActionLog.class.getCanonicalName();
public void log(Action action){ /**
* Online action. It is send byusing web sockets
* @param action
*/
public void log(Action action) {
if (PCBcontext.getRoom().inRoom())
PCBcontext.getRoom().emit("/stu/action", action);
else {
if (PCBcontext.getNetService().online()) { // If there is no room, the action is stored in local DB
PCBcontext.getRoom().emit("/stu/action", action.get_type(), action.get_json());
}else {
// If there is no available connection, the action is stored in local DB
PCBcontext.getPcbdb().insertAction(action); PCBcontext.getPcbdb().insertAction(action);
actions_buffer.add(action.getDescription()); actions_buffer.add(action.getDescription());
Log.i(this.getClass().getCanonicalName(), " Batch action included: " + action.getDescription()); Log.i(this.getClass().getCanonicalName(), " Batch action included: " + action.getDescription());
} }
} }
......
...@@ -9,6 +9,7 @@ import org.json.JSONArray; ...@@ -9,6 +9,7 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import com.github.nkzawa.socketio.client.Ack;
import com.yottacode.pictogram.dao.PCBDBHelper; import com.yottacode.pictogram.dao.PCBDBHelper;
import com.yottacode.pictogram.dao.Picto; import com.yottacode.pictogram.dao.Picto;
...@@ -31,10 +32,10 @@ import java.util.LinkedList; ...@@ -31,10 +32,10 @@ import java.util.LinkedList;
* @author Fernando Martinez Santiago * @author Fernando Martinez Santiago
* @version 1.0 * @version 1.0
*/ */
public class Room implements Emitter.Listener { public class Room {
protected SailsSocketsIO socket=null; protected SailsSocketsIO socket=null;
protected boolean inRoom=false;
public Room( ) { public Room( ) {
reconnect(); reconnect();
} }
...@@ -44,19 +45,30 @@ public class Room implements Emitter.Listener { ...@@ -44,19 +45,30 @@ public class Room implements Emitter.Listener {
final String attributes_param="attributes"; final String attributes_param="attributes";
return new JSONObject().put(action_param, action).put(attributes_param, attributes); return new JSONObject().put(action_param, action).put(attributes_param, attributes);
} }
public void emit(String url, String action, JSONObject attributes) { public void emit(String url, final Action action) {
Log.d("Room.java", "Action: "+ action +" / Attributes emitted: " + attributes.toString()); Log.d(this.getClass().getName(), "Action: " + action.get_type() + " / Attributes emitted: " + action.get_json().toString());
try{ try{
this.socket.emit(url, this.common_data(action, attributes));
this.socket.emit(url, this.common_data(action.get_type(), action.get_json()), new Ack() {
@Override
public void call(Object... args) {
Log.d(this.getClass().getCanonicalName(),"Action ack received");
}
});
} catch (JSONException e) { } catch (JSONException e) {
Log.e(this.getClass().getCanonicalName(), e.getClass().getCanonicalName() + e.getMessage() + "--" + e.getLocalizedMessage()); Log.e(this.getClass().getCanonicalName(), e.getClass().getCanonicalName() + e.getMessage() + "--" + e.getLocalizedMessage());
} }
} }
public void emit(String url, String action, JSONObject attributes, String token) { public void emit(String url, final Action action, String token) {
final String token_param="token"; final String token_param="token";
Log.d("Room.java", "Action: " + action + " / Attributes emitted: " + attributes.toString()); Log.d(this.getClass().getName(), "Action: " + action.get_type() + " / Attributes emitted: " + action.get_json().toString());
try{ try{
this.socket.emit(url, this.common_data(action, attributes).put(token_param, token)); this.socket.emit(url, this.common_data(action.get_type(), action.get_json()).put(token_param, token), new Ack() {
@Override
public void call(Object... args) {
Log.d(this.getClass().getCanonicalName(), "Action ack received");
}
});
} catch (JSONException e) { } catch (JSONException e) {
Log.e(this.getClass().getCanonicalName(), e.getClass().getCanonicalName() + e.getMessage() + "--" + e.getLocalizedMessage()); Log.e(this.getClass().getCanonicalName(), e.getClass().getCanonicalName() + e.getMessage() + "--" + e.getLocalizedMessage());
} }
...@@ -67,30 +79,34 @@ public class Room implements Emitter.Listener { ...@@ -67,30 +79,34 @@ public class Room implements Emitter.Listener {
*/ */
public void reconnect() { public void reconnect() {
final String transport="polling"; final String transport="polling";
this.socket=new SailsSocketsIO(PCBcontext.getRestapiWrapper().getServer(),transport, this); this.socket=new SailsSocketsIO(PCBcontext.getRestapiWrapper().getServer(), transport, new Emitter.Listener() {
@Override
public void call(Object... args) {
subscribe();
inRoom=true;
}
}, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.i(this.getClass().getName(), "Error sending Action: "+args[0]);
if (args.length>0 && args[0].toString().equals("transport error") ) {
inRoom=false;
}
}
});
} }
public boolean inRoom() {return this.inRoom;}
public void listen(String msg, Emitter.Listener listener) { public void listen(String msg, Emitter.Listener listener) {
this.socket.registerMessage(msg,listener); this.socket.registerMessage(msg, listener);
} }
@Override
public void call(Object... args) {
subscribe();
Log.d(this.getClass().getName(), "Connected");
for (Object arg : args)
Log.d(this.getClass().getName(), "Argument:" + arg);
}
void subscribe() { void subscribe() {
final String url="/stu/subscribe"; final String url="/stu/subscribe";
final String action="subscribe"; final String action="subscribe";
final String param_id_stu="id_stu";
int id_stu = PCBcontext.getPcbdb().getCurrentUser().get_id_stu();
try { try {
JSONObject msg = new JSONObject().put(param_id_stu, id_stu); this.emit(url, new Action(action), PCBcontext.getRestapiWrapper().getToken());
this.emit(url, action, msg, PCBcontext.getRestapiWrapper().getToken());
}catch (Exception e) { }catch (Exception e) {
Log.e(this.getClass().getCanonicalName(), e.getMessage()); Log.e(this.getClass().getCanonicalName(), e.getMessage());
} }
......
...@@ -33,14 +33,14 @@ public class NetService implements Runnable, iRestapiListener { ...@@ -33,14 +33,14 @@ public class NetService implements Runnable, iRestapiListener {
static final String ping_session="server/ping"; static final String ping_session="server/ping";
private boolean updated; private boolean updated;
public NetService(int delay) { public NetService(int delay) {
this.updated=this.online(); this.updated=RestapiWrapper.ping(PCBcontext.getContext().getResources().getString(R.string.server), ping_session, this);
Log.i(this.getClass().getName(), "Checking Pictogram server access..."); Log.i(this.getClass().getName(), "Checking Pictogram server access...");
Log.d(this.getClass().getName(), this.updated ? "Pictogram server access ok" : "Pictogram server access failed, Internet connection available?"); Log.d(this.getClass().getName(), 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, delay, TimeUnit.SECONDS);
} }
public boolean online() {return RestapiWrapper.ping(PCBcontext.getContext().getResources().getString(R.string.server), ping_session, this);} public boolean online() {return updated;}
/** /**
* ping to the server by using a restapi call. If ok, the call will return the empty set * ping to the server by using a restapi call. If ok, the call will return the empty set
......
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