optimizando websockets. trabajando en bug batch actions

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