Commit f463b4fc authored by Marcel Rieger's avatar Marcel Rieger
Browse files

Open socket functionality for extensions, draft.

parent bad4a075
......@@ -89,28 +89,32 @@ class AbstractController(object):
def get(self, key, *args):
key = key.lower()
try:
session = cherrypy.session
request = cherrypy.request
if key == 'session_id':
return cherrypy.session.id # @UndefinedVariable
return session.id # @UndefinedVariable
elif key == 'user_id':
return cherrypy.request.user.id
return request.user.id
elif key == 'user_name':
return cherrypy.request.user.name
return request.user.name
elif key == 'db':
return cherrypy.request.db
return request.db
elif key == 'workspace_ids':
return cherrypy.session.get('workspace_ids', None) # @UndefinedVariable
return session.get('workspace_ids', None) # @UndefinedVariable
elif key == 'workspace':
if len(args) == 0:
return cherrypy.request.workspace
return request.workspace
else:
return Workspace.get_by_id(cherrypy.request.db, int(args[0]))
return Workspace.get_by_id(request.db, int(args[0]))
elif key == 'profile_id':
return cherrypy.session.get('profile_id', None) # @UndefinedVariable
return session.get('profile_id', None) # @UndefinedVariable
elif key == 'profile':
return cherrypy.request.profile
return request.profile
elif key == 'fs':
workspace = cherrypy.request.workspace if len(args) == 0 else args[0]
workspace = request.workspace if len(args) == 0 else args[0]
return vispa.workspace.get_instance('vispa.remote.filesystem.FileSystem', workspace=workspace)
elif key == "view_id":
return request.private_params.get("_viewId", None)
except:
vispa.log_exception()
return None
......
......@@ -13,6 +13,6 @@ class DummyExtension(AbstractExtension):
return []
def setup(self):
self.add_controller(DummyController())
self.add_controller(DummyController(self))
self.add_js("js/extension.js")
self.add_workspace_directoy()
......@@ -9,9 +9,9 @@ from vispa import MessageException
class DummyController(AbstractController):
def __init__(self):
def __init__(self, extension):
AbstractController.__init__(self)
self.extension = None
self.extension = extension
@cherrypy.expose
@cherrypy.tools.ajax()
......@@ -24,6 +24,10 @@ class DummyController(AbstractController):
dummyrpc = self.extension.get_workspace_instance("DummyRpc")
content += "<p>dummy: %s</p>" % dummyrpc.dummy()
session_id = self.get("session_id")
view_id = self.get("view_id")
self.extension.send_socket(view_id, "test-topic", session_id, data="foobar")
return content
@cherrypy.expose
......
......@@ -81,6 +81,10 @@ var DummyView = vispa.ExtensionView.Center.extend({
setup: function() {
this._super();
this.setLabel("id: " + this.getId());
this.onSocket("test-topic", function(data) {
console.log("received data on topic 'test-topic':", data.data);
});
},
render: function(node) {
......
......@@ -97,6 +97,10 @@ class AbstractExtension(object):
return vispa.workspace.clear_instance(classname, key, user,
workspace, db)
def send_socket(self, view_id, topic, *args, **kwargs):
topic = "extension.%s.socket.%s" % (view_id, topic)
vispa.bus.send_topic(topic, *args, **kwargs)
class Server(object):
......@@ -240,8 +244,8 @@ class Server(object):
if vispa.config('websockets', 'enabled', False):
from ws4py.server.cherrypyserver import WebSocketPlugin
WebSocketPlugin(cherrypy.engine).subscribe()
from vispa.socketbus import Bus
vispa.bus = Bus()
from vispa.socketbus import Bus
vispa.bus = Bus()
mako_lookup_dir = os.path.join(os.path.dirname(__file__), "templates")
vispa.plugins.template.MakoPlugin(cherrypy.engine,
......
......@@ -116,16 +116,17 @@ class Bus:
# session_id has to be set for safety reasons
if not session_id:
return
self.publish(data["topic"], session_id, data=data)
self.__publish(data["topic"], session_id, data=data)
def publish(self, topic, session_id, data=None):
def __publish(self, topic, session_id, data=None):
if topic in self._handlers.keys():
# user_id?
user_id = None
if session_id in SESSION_OWNERS.keys():
user_id = SESSION_OWNERS[session_id]
for handler in self._handlers[topic]:
handler(session_id=session_id, user_id=user_id, data=data)
handler(session_id=session_id, user_id=user_id, data=data,
topic=topic)
def subscribe(self, topic, handler):
if topic not in self._handlers.keys():
......
......@@ -79,6 +79,15 @@ var Socket = Emitter.extend({
return status;
},
type: function() {
if (this._socket instanceof window.WebSocket)
return "WebSocket";
else if (this._socket instanceof PollSocket)
return "PollSocket";
else
return null;
},
checkConnection: function() {
if (this.status() == socketStates["CLOSED"])
this.setup();
......
......@@ -1100,6 +1100,31 @@ var ExtensionView = Emitter.extend({
return this;
},
onSocket: function(event) {
var args = Array.prototype.slice.call(arguments, 1);
args.unshift(this._msgContext + ".socket." + event);
vispa.socket.on.apply(vispa.socket, args);
return this;
},
onceSocket: function(event) {
var args = Array.prototype.slice.call(arguments, 1);
args.unshift(this._msgContext + ".socket." + event);
vispa.socket.once.apply(vispa.socket, args);
return this;
},
removeSocketListener: function(event) {
var args = Array.prototype.slice.call(arguments, 1);
if (event)
args.unshift(this._msgContext + ".socket." + event);
if (args.length >= 2)
vispa.socket.removeListener.apply(vispa.socket, args);
else
vispa.socket.removeAllListeners.apply(vispa.socket, args);
return this;
},
onResize: function() {
return this;
},
......
......@@ -32,6 +32,12 @@ var Vispa = Emitter.extend({
// start the socket
this.socket = new Socket(preferences.socket.url, preferences.socket);
this.socket.on("open", function() {
self.logger.info("socket connected (" + self.socket.type() + ")");
});
this.socket.on("close", function() {
self.logger.info("socket closed");
});
this.settings = {};
this.varData = {};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment