Skip to content

Commit

Permalink
[refactor] runtime, ping: Just a quick refactor of the pull request #108
Browse files Browse the repository at this point in the history
, no more set_connection_lost_handler but add_connection_lost_handler and move to PingClient
  • Loading branch information
BourgerieQuentin committed Mar 5, 2013
1 parent 51e00c0 commit 4e48661
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 57 deletions.
67 changes: 30 additions & 37 deletions lib/plugins/server/session.js
@@ -1,5 +1,5 @@
/* /*
Copyright © 2011, 2012 MLstate Copyright © 2011-2013 MLstate
This file is part of Opa. This file is part of Opa.
Expand Down Expand Up @@ -116,15 +116,21 @@ var LowLevelPingLoop = {};
// for setting a custom handler when the connection // for setting a custom handler when the connection
// between client and server is lost. // between client and server is lost.
var connection_lost_handler = null; var connection_lost_handlers = null;
function default_connection_lost_handler(){
jlog("Error: the connection with the server seems to be lost. Please reload");
}
function break_ping_loop() { function break_ping_loop() {
linked_with_server = false; linked_with_server = false;
if (connection_lost_handler == null) { if (connection_lost_handlers == null) {
jlog("Error: the connection with the server seems to be lost. Please reload"); default_connection_lost_handler();
throw("Error: the connection with the server seems to be lost. Please reload");
} else { } else {
connection_lost_handler(); var i;
for(i in connection_lost_handlers){
connection_lost_handlers[i]();
}
} }
} }
Expand Down Expand Up @@ -477,13 +483,7 @@ var LowLevelPingLoop = {};
/** Make somethings when an error occurs with the ping loop */ /** Make somethings when an error occurs with the ping loop */
function error_ping_response(xhr, str, exn){ function error_ping_response(xhr, str, exn){
if (failure_delay == max_failure_delay){ if (failure_delay == max_failure_delay){
if (connection_lost_handler == null) { break_ping_loop();
jlog("Error: the connection with the server seems to be lost.");
return;
} else {
connection_lost_handler();
return;
}
} else { } else {
//jlog("Warning: could not reach the server. Retrying in " + failure_delay); //jlog("Warning: could not reach the server. Retrying in " + failure_delay);
} }
Expand Down Expand Up @@ -580,14 +580,6 @@ var LowLevelPingLoop = {};
} }
} }


LowLevelSession.set_domain_url = function(d) {
domain_url = d;
}

LowLevelSession.set_connection_lost_handler = function(h) {
connection_lost_handler = h;
}

LowLevelSession.llmake = function(st, unserialize, fun_session, LowLevelSession.llmake = function(st, unserialize, fun_session,
ctx, dfun, more, concurrent) { ctx, dfun, more, concurrent) {
the_ping_loop(); the_ping_loop();
Expand Down Expand Up @@ -722,6 +714,15 @@ var LowLevelPingLoop = {};
max_pang_attempt = i; max_pang_attempt = i;
} }


LowLevelPingLoop.set_domain_url = function(d) {
domain_url = d;
}

LowLevelPingLoop.add_connection_lost_handler = function(h) {
if(connection_lost_handlers == null) connection_lost_handlers = new Array();
connection_lost_handlers.push(h);
}

/** /**
* Make an internal synchronous request using the pang system. Can * Make an internal synchronous request using the pang system. Can
* froze the client GUI. But the client can always interact with * froze the client GUI. But the client can always interact with
Expand Down Expand Up @@ -771,22 +772,6 @@ function set_uu(x0, x1) {
return js_void; return js_void;
} }
/**
* @register {(-> void) -> void}
*/
function set_connection_lost_handler(f) {
LowLevelSession.set_connection_lost_handler(f);
return js_void;
}
/**
* @register {string -> void}
*/
function set_domain_url(d) {
LowLevelSession.set_domain_url(d);
return js_void;
}
/** /**
* @register {'st, \ * @register {'st, \
(opa[option('ctx)], RPC.Json.private.native, \ (opa[option('ctx)], RPC.Json.private.native, \
Expand Down Expand Up @@ -1004,6 +989,14 @@ function get_server_id(chan, _) {
/** @register {string} internal_prefix LowLevelPingLoop.internal_prefix */ /** @register {string} internal_prefix LowLevelPingLoop.internal_prefix */
/**
* @register {(-> void) -> void} add_connection_lost_handler LowLevelPingLoop.add_connection_lost_handler
*/
/**
* @register {string -> void} set_domain_url LowLevelPingLoop.set_domain_url
*/

/** @endModule */ /** @endModule */


/* Hack for non cps client ****************************** */ /* Hack for non cps client ****************************** */
Expand Down
23 changes: 22 additions & 1 deletion lib/stdlib/core/rpc/core/ping_client.opa
@@ -1,5 +1,5 @@
/* /*
Copyright © 2011, 2012 MLstate Copyright © 2011-2013 MLstate
This file is part of Opa. This file is part of Opa.
Expand All @@ -19,6 +19,7 @@ PingClient = {{


@private registers = Reference.create([]): reference(list(RPC.Json.json)) @private registers = Reference.create([]): reference(list(RPC.Json.json))


@package
register_actor(id) = register_actor(id) =
@atomic( @atomic(
x = Reference.get(registers) x = Reference.get(registers)
Expand Down Expand Up @@ -77,6 +78,26 @@ PingClient = {{
async_request(url, body) = async_request(url, body) =
wrap_request(url, body, %%Session.PingRegister.ping_async_call%%) wrap_request(url, body, %%Session.PingRegister.ping_async_call%%)


/**
* Explicitely set the domain's URL used by AJAX request, no domain by default
*/
set_domain_url =
%%Session.PingRegister.set_domain_url%%

/**
* Add a custom handler function for the case when the
* connection between client and server appears to be
* lost. If they are no custom handler, the default is to print
* "the connection with the server seems to be lost" to
* the client screen.
*
* Be sure to pass a `client` function here (e.g.
* `client function my_handler() { ... }`), as it will
* probably be called when the server is not available!
*/
@client add_connection_lost_handler =
%%Session.PingRegister.add_connection_lost_handler%%

}} }}


@private @private
Expand Down
19 changes: 0 additions & 19 deletions lib/stdlib/core/rpc/core/session.opa
Expand Up @@ -184,25 +184,6 @@ type make_at_response = option(RPC.Json.json)


Session = {{ Session = {{


/**
* Explicitely set the domain's URL used by AJAX request, no domain by default
*/
@client set_domain_url = %%Session.set_domain_url%%

/**
* Set a custom handler function for the case when the
* connection between client and server appears to be
* lost. If this in not set, the default is to print
* "the connection with the server seems to be lost" to
* the client screen.
*
* Be sure to pass a `client` function here (e.g.
* `client function my_handler() { ... }`), as it will
* probably be called when the server is not available!
*/
@client set_connection_lost_handler =
%%Session.set_connection_lost_handler%%

/** /**
* {2 Creating sessions} * {2 Creating sessions}
*/ */
Expand Down

0 comments on commit 4e48661

Please sign in to comment.