Skip to content

Commit

Permalink
Merge 7cb775e into 5993c39
Browse files Browse the repository at this point in the history
  • Loading branch information
peuter committed Sep 17, 2017
2 parents 5993c39 + 7cb775e commit b560b51
Show file tree
Hide file tree
Showing 53 changed files with 2,866 additions and 431 deletions.
62 changes: 53 additions & 9 deletions client/source/class/cv/io/Client.js
Expand Up @@ -196,6 +196,14 @@ qx.Class.define('cv.io.Client', {
*/
currentTransport: {
init: null
},

/**
* The server we are currently speaking to (read from the login response)
*/
server: {
check: "String",
nullable: true
}
},

Expand Down Expand Up @@ -247,12 +255,25 @@ qx.Class.define('cv.io.Client', {
if (backend.baseURL && backend.baseURL.substr(-1) !== "/") {
backend.baseURL += "/";
}
var currentTransport = this.getCurrentTransport();
switch(backend.transport) {
case "long-polling":
this.setCurrentTransport(new cv.io.transport.LongPolling(this));
if (!(currentTransport instanceof cv.io.transport.LongPolling)) {
// replace old transport
if (currentTransport) {
currentTransport.dispose();
}
this.setCurrentTransport(new cv.io.transport.LongPolling(this));
}
break;
case "sse":
this.setCurrentTransport(new cv.io.transport.Sse(this));
if (!(currentTransport instanceof cv.io.transport.Sse)) {
// replace old transport
if (currentTransport) {
currentTransport.dispose();
}
this.setCurrentTransport(new cv.io.transport.Sse(this));
}
break;
}
if (this.backend.name === "openHAB") {
Expand Down Expand Up @@ -364,14 +385,16 @@ qx.Class.define('cv.io.Client', {
* Get the json response from the parameter received from the used XHR transport
*/
getResponse: qx.core.Environment.select("cv.xhr", {
"jquery": function(data) {
"jquery": function(args) {
var data = args[0];
if (data && $.type(data) === "string") {
data = cv.io.parser.Json.parse(data);
}
return data;
},

"qx": function(ev) {
"qx": function(args) {
var ev = args[0];
if (!ev) { return null; }
var json = ev.getTarget().getResponse();
if (!json) { return null; }
Expand All @@ -382,6 +405,18 @@ qx.Class.define('cv.io.Client', {
}
}),

getResponseHeader: qx.core.Environment.select("cv.xhr", {
"jquery": function (args, name) {
return args[2].getResponseHeader(name);
},
"qx": function (args, name) {
if (!args[0]) {
return null;
}
return args[0].getTarget().getResponseHeader(name);
}
}),

getQueryString: function(data) {
var prefix = "";
var suffix = "";
Expand Down Expand Up @@ -500,21 +535,30 @@ qx.Class.define('cv.io.Client', {
* backend and forwards to the configurated transport handleSession
* function
*
* @param ev {Event} the 'success' event from the XHR request
* Parameter vary dependent from the XHR type used
* qx (Qooxdoo):
* ev {Event} the 'success' event from the XHR request
*
* jQuery:
* data {Object} The JSON data returned from the server
* textStatus {String} a string describing the status
* request {Object} the jqXHR object
*/
handleLogin : function (ev) {
var json = this.getResponse(ev);
handleLogin : function () {
var args = Array.prototype.slice.call(arguments, 0);
var json = this.getResponse(args);
// read backend configuration if send by backend
if (json.c) {
this.setBackend(qx.lang.Object.mergeWith(this.getBackend(), json.c));
}
this.session = json.s || "SESSION";
this.setServer(this.getResponseHeader(args, "Server"));

this.setDataReceived(false);
if (this.loginSettings.loginOnly) {
this.getCurrentTransport().handleSession(ev, false);
this.getCurrentTransport().handleSession(args, false);
} else {
this.getCurrentTransport().handleSession(ev, true);
this.getCurrentTransport().handleSession(args, true);
// once the connection is set up, start the watchdog
this.watchdog.start(5);
}
Expand Down
16 changes: 7 additions & 9 deletions client/source/class/cv/io/transport/LongPolling.js
Expand Up @@ -53,11 +53,11 @@ qx.Class.define('cv.io.transport.LongPolling', {
* This function gets called once the communication is established
* and this.client information is available.
*
* @param ev {Event|Object} qx event or json response
* @param args {Array} arguments from the XHR response callback
* @param connect {Boolean} whether to start the connection or not
*/
handleSession: function (ev, connect) {
var json = this.client.getResponse(ev);
handleSession: function (args, connect) {
var json = this.client.getResponse(args);
this.sessionId = json.s;
this.version = json.v.split('.', 3);

Expand Down Expand Up @@ -103,11 +103,9 @@ qx.Class.define('cv.io.transport.LongPolling', {
/**
* This function gets called once the communication is established
* and this.client information is available
*
* @param ev {Event}
*/
handleRead: function (ev) {
var json = this.client.getResponse(ev);
handleRead: function () {
var json = this.client.getResponse(Array.prototype.slice.call(arguments, 0));
if (this.doRestart || (!json && (-1 === this.lastIndex))) {
this.client.setDataReceived(false);
if (this.running) { // retry initial request
Expand Down Expand Up @@ -149,8 +147,8 @@ qx.Class.define('cv.io.transport.LongPolling', {
}
},

handleReadStart: function (ev) {
var json = this.client.getResponse(ev);
handleReadStart: function () {
var json = this.client.getResponse(Array.prototype.slice.call(arguments, 0));
if (!json && (-1 === this.lastIndex)) {
this.client.setDataReceived(false);
if (this.running) { // retry initial request
Expand Down
35 changes: 26 additions & 9 deletions client/source/class/cv/io/transport/Sse.js
Expand Up @@ -32,7 +32,7 @@ qx.Class.define('cv.io.transport.Sse', {
*/
construct: function(client) {
this.client = client;
this.__additionalTopics = [];
this.__additionalTopics = {};
},

/*
Expand All @@ -51,11 +51,11 @@ qx.Class.define('cv.io.transport.Sse', {
* This function gets called once the communication is established
* and session information is available
*
* @param ev {Event}
* @param args {Array} arguments from the XHR response callback
* @param connect {Boolean} whether to start the connection or not
*/
handleSession: function (ev, connect) {
var json = this.client.getResponse(ev);
handleSession: function (args, connect) {
var json = this.client.getResponse(args);
this.sessionId = json.s;
this.version = json.v.split('.', 3);

Expand All @@ -82,9 +82,7 @@ qx.Class.define('cv.io.transport.Sse', {
this.eventSource.addEventListener('message', this.handleMessage.bind(this), false);
this.eventSource.addEventListener('error', this.handleError.bind(this), false);
// add additional listeners
this.__additionalTopics.forEach(function(entry) {
this.eventSource.addEventListener(entry[0], entry[1].bind(entry[2]), false);
}, this);
Object.getOwnPropertyNames(this.__additionalTopics).forEach(this.__addRecordedEventListener, this);
this.eventSource.onerror = function () {
this.error("connection lost");
this.client.setConnected(false);
Expand All @@ -108,19 +106,38 @@ qx.Class.define('cv.io.transport.Sse', {
this.client.setDataReceived(true);
},

dispatchTopicMessage: function(topic, message) {
this.client.record(topic, message);
if (this.__additionalTopics[topic]) {
this.__additionalTopics[topic].forEach(function(entry) {
entry[0].call(entry[1], message);
});
}
},

/**
* Subscribe to SSE events of a certain topic
* @param topic {String}
* @param callback {Function}
* @param context {Object}
*/
subscribe: function(topic, callback, context) {
this.__additionalTopics.push([topic, callback, context]);
if (!this.__additionalTopics[topic]) {
this.__additionalTopics[topic] = [];
}
this.__additionalTopics[topic].push([callback, context]);
if (this.isConnectionRunning()) {
this.eventSource.addEventListener(topic, callback.bind(context, false));
this.__addRecordedEventListener(topic);
}
},

__addRecordedEventListener: function(topic) {
this.debug("subscribing to topic "+topic);
this.eventSource.addEventListener(topic, function(e) {
this.dispatchTopicMessage(topic, e);
}.bind(this), false);
},

/**
* Handle errors
*/
Expand Down
19 changes: 16 additions & 3 deletions config.json
Expand Up @@ -26,6 +26,7 @@
"source-all",
"source-hybrid",
"source-server",
"source-error",
"source-server-reload",
"source-httpd-config",
"test",
Expand All @@ -43,7 +44,8 @@
{
"APPLICATION" : "cv",
"QOOXDOO_PATH" : "./external/qooxdoo",
"API_EXCLUDE" : ["qx.test.*", "${APPLICATION}.test.*"],
"QXTHEME" : "cv.theme.Dark",
"API_EXCLUDE" : ["qx.test.*", "${APPLICATION}.test.*", "${QXTHEME}"],
"LOCALES" : [ "en", "de" ],
"CACHE" : "${TMPDIR}/qx${QOOXDOO_VERSION}/cache",
"ROOT" : ".",
Expand Down Expand Up @@ -82,7 +84,8 @@
"${APPLICATION}.transforms.*",
"${APPLICATION}.plugins.*",
"${APPLICATION}.parser.*",
"${APPLICATION}.core.*"
"${APPLICATION}.core.*",
"${QXTHEME}"
],

"lint-check" : {
Expand Down Expand Up @@ -262,7 +265,7 @@
},
"plugin-openhab" :
{
"include" : [ "${APPLICATION}.plugins.openhab.*" ]
"include" : [ "${APPLICATION}.plugins.openhab.*", "${QXTHEME}" ]
}
}
}
Expand All @@ -283,6 +286,15 @@
]
},

"source-error": {
"extend" : [ "source" ],
"environment" :
{
"cv.build": "source",
"qx.globalErrorHandling": true
},
},

"source-hybrid" :
{
"extend" : [ "parts-config" ]
Expand Down Expand Up @@ -393,6 +405,7 @@
"Sunlight",
"replayLog",
"svg4everybody",
"EVENT_RECORDER",
"Favico"
],
"generate-widget-examples": true
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b560b51

Please sign in to comment.