Permalink
Browse files

Fix normal exit.

  • Loading branch information...
1 parent b826558 commit 6e1640b70432f8bf5cdff655fb5276acb3c3b887 @arcusfelis committed Mar 18, 2013
@@ -92,10 +92,15 @@ qx.Class.define("cascadae.Socket", {
this.setNamespace(namespace);
}
this.__name = [];
+ this.addListener("disconnect", this.__handleDisconnect, this);
+ this.addListener("reconnect", this.__handleReconnect, this);
},
members:
{
+ __listeners : [],
+ __objectRegister : [],
+
/**
* Trying to using socket.io to connect and plug every event from socket.io to qooxdoo one
*/
@@ -108,8 +113,8 @@ qx.Class.define("cascadae.Socket", {
'reconnect': this.getReconnect(),
'connect timeout' : this.getConnectTimeout(),
'reconnection delay': this.getReconnectionDelay(),
- 'max reconnection attempts': this.getMaxReconnectionAttemps()
-// 'force new connection': true
+ 'max reconnection attempts': this.getMaxReconnectionAttemps(),
+ 'force new connection': true
})
this.setSocket(socket);
var self = this;
@@ -148,7 +153,25 @@ qx.Class.define("cascadae.Socket", {
__addListenerFromErlang: function(eventName, objHash)
{
var item = qx.core.ObjectRegistry.fromHashCode(objHash);
- item.addListener(eventName, this.__listener, this);
+ var lid = item.addListener(eventName, this.__handleQxEvent, this);
+ var hash = item.toHashCode();
+ this.__listeners.push({"listener_id": lid, "hash": hash});
+ },
+
+ /**
+ * Cancel listeners added from Erlang for Qooxdoo objects.
+ */
+ __cancelAllListeners: function()
+ {
+ var a = this.__listeners;
+ for (var i = 0; i < a.length; i++)
+ {
+ var info = a[i];
+ var item = qx.core.ObjectRegistry.fromHashCode(info.hash);
+ if (!item) continue;
+ item.removeListenerById(info.listener_id);
+ }
+ this.__listeners = [];
},
__fireEventFromErlang: function(eventName, objHash)
@@ -167,7 +190,7 @@ qx.Class.define("cascadae.Socket", {
/* Receive events from qooxdoo objects */
- __listener : function(e)
+ __handleQxEvent : function(e)
{
var item = e.getTarget();
var meta = {};
@@ -198,19 +221,22 @@ qx.Class.define("cascadae.Socket", {
},
/* Initially send information about the object */
- registerObject : function(item)
+ registerObject : function(item, emitOnly)
{
+ var hash = item.toHashCode();
var data = {
- "hash" : item.toHashCode(),
+ "hash" : hash,
"path" : item.classname.split(".")
};
this.emit("registerObject", data);
+ if (!emitOnly) this.__objectRegister.push(hash);
},
reconnect : function()
{
this.__destroySocket();
this.connect();
+ this.__handleReconnect();
},
reload : function()
@@ -235,6 +261,25 @@ qx.Class.define("cascadae.Socket", {
});
this.setSocket(null);
}
+ },
+
+ __handleDisconnect: function()
+ {
+ this.__cancelAllListeners();
+ },
+
+ __handleReconnect: function()
+ {
+ this.info("Handle reconnect.");
+ // Submit registered objects again
+ var a = this.__objectRegister;
+ for (var i = 0; i < a.length; i++)
+ {
+ var hash = a[i],
+ item = qx.core.ObjectRegistry.fromHashCode(hash);
+ this.registerObject(item, true);
+ item.fireEvent("reanimate");
+ }
}
},
@@ -117,7 +117,7 @@ qx.Class.define("cascadae.ToolBar",
this.__mainButtons = new qx.ui.toolbar.Part;
this.__ctrlButtons.add(this.__reconnectBtn);
- this.__ctrlButtons.add(this.__reloadBtn);
+// this.__ctrlButtons.add(this.__reloadBtn);
this.__viewButtons.add(this.__peersView);
this.__viewButtons.add(this.__filesView);
@@ -24,7 +24,7 @@ start(_Type, _Args) ->
{etag, {attributes, [filepath, filesize, inode, mtime]}}],
SIOConfig = socketio_session:configure([
{heartbeat, 5000},
- {session_timeout, 30000},
+% {session_timeout, 30000},
{callback, cascadae_session},
{protocol, socketio_data_protocol}]),
@@ -4,6 +4,7 @@
-record(files_state, {
session_pid,
session_tag,
+ session_mref,
torrent_id,
files,
update_tree_tref
@@ -47,9 +48,11 @@ deactivate(Srv) ->
init([Session, Tag]) ->
{ok, TRef} = timer:send_interval(5000, update_tree),
+ SMRef = monitor(process, Session),
State = #files_state{
session_pid=Session,
session_tag=Tag,
+ session_mref=SMRef,
update_tree_tref=TRef
},
{ok, State}.
@@ -110,7 +113,10 @@ handle_info(update_tree, State) ->
end,
Files == Files2
orelse lager:debug("Diff state changed ~p => ~p.", [Files, Files2]),
- {noreply, State#files_state{files=Files2}}.
+ {noreply, State#files_state{files=Files2}};
+handle_info({'DOWN', MRef, process, _, Reason},
+ State=#files_state{session_mref=MRef}) ->
+ {stop, Reason, State}.
terminate(_Reason, _State) ->
ok.
@@ -6,6 +6,7 @@
peers = dict:new(), %% dict(Pid => #peer{})
session_pid,
session_tag,
+ session_mref,
torrent_ids,
update_table_tref,
@@ -67,9 +68,11 @@ deactivate(Srv) ->
init([Session, Tag]) ->
{ok, TRef} = timer:send_interval(5000, update_table),
+ SMRef = monitor(process, Session),
State = #peers_state{
session_pid=Session,
session_tag=Tag,
+ session_mref=SMRef,
update_table_tref=TRef
},
{ok, State}.
@@ -98,7 +101,10 @@ handle_cast(deactivate, State=#peers_state{update_table_tref=TRef}) ->
handle_info(update_table, State) ->
lager:debug("Handle update_table timeout.", []),
- {noreply, cron_find_new(State)}.
+ {noreply, cron_find_new(State)};
+handle_info({'DOWN', MRef, process, _, Reason},
+ State=#peers_state{session_mref=MRef}) ->
+ {stop, Reason, State}.
terminate(_Reason, _State) ->
Oops, something went wrong.

0 comments on commit 6e1640b

Please sign in to comment.