/*****************************************************************************/
var cloudeebus = window.cloudeebus = {
- version: "0.5.1",
+ version: "0.5.99",
minVersion: "0.3.2"
};
cloudeebus.getError = function(error) {
if (error.desc && error.uri)
- return error.desc + " : " + error.uri;
+ return error.desc + " : " + error.uri; // error cloudeebus (from python)
if (error.desc)
return error.desc;
if (error.uri)
return error.name + " : " + error.message;
if (error.message)
return error.message;
- return error;
+ if (error.name)
+ return error.name;
+ return error; // error from Autobahn
};
cloudeebus.versionCheck = function(version) {
function onWAMPSessionConnectedCB(session) {
cloudeebus.wampSession = session;
- if (manifest)
+ if (manifest) {
cloudeebus.wampSession.authreq(
manifest.name,
- {permissions: manifest.permissions}
+ {permissions: manifest.permissions,
+ services: manifest.services}
).then(onWAMPSessionChallengedCB, onWAMPSessionAuthErrorCB);
+ }
else
cloudeebus.wampSession.authreq().then(function() {
cloudeebus.wampSession.auth().then(onWAMPSessionAuthenticatedCB, onWAMPSessionAuthErrorCB);
cloudeebus.BusConnection.prototype.addService = function(serviceName) {
var self = this;
+
+ if (!serviceName)
+ serviceName = "";
var promise = new cloudeebus.Promise(function (resolver) {
- cloudeebusService = new cloudeebus.Service(self.wampSession, self, serviceName);
-
- function busServiceAddedSuccessCB(service) {
- try {
- service.isCreated = true;
- var result = [cloudeebusService];
- resolver.fulfill(result[0], true);
- }
- catch (e) {
- cloudeebus.log("Method callback exception: " + e);
- resolver.reject(e, true);
- }
- }
-
- function busServiceErrorSuccessCB(error) {
- resolver.reject(cloudeebus.getError(error), true);
- }
-
- cloudeebusService.add(this).then(busServiceAddedSuccessCB, busServiceErrorSuccessCB);
+ var cloudeebusService = new cloudeebus.Service(self.wampSession, self, serviceName);
+
+ function ServiceAddedSuccessCB(serviceName) {
+ try {
+ cloudeebusService.isCreated = true;
+ resolver.fulfill(cloudeebusService, true);
+ }
+ catch (e) {
+ var errorStr = cloudeebus.getError(e);
+ cloudeebus.log("Method callback exception: " + errorStr);
+ resolver.reject(errorStr, true);
+ }
+ }
+
+ function ServiceAddedErrorCB(error) {
+ var errorStr = cloudeebus.getError(error);
+ cloudeebus.log("Error adding service method: " + self.name + ", error: " + errorStr);
+ resolver.reject(errorStr, true);
+ }
+
+ var arglist = [
+ self.name,
+ serviceName
+ ];
+
+ // call dbusSend with bus type, destination, object, message and arguments
+ self.wampSession.call("serviceAdd", arglist).then(ServiceAddedSuccessCB, ServiceAddedErrorCB);
});
return promise;
};
-cloudeebus.BusConnection.prototype.removeService = function(serviceName, successCB, errorCB) {
- var self = this;
-
- function busServiceRemovedSuccessCB(serviceName) {
- // Be sure we are removing the service requested...
- if (serviceName == self.service.name) {
- self.service = null;
- if (successCB)
- successCB(serviceName);
- }
- }
-
- cloudeebusService.remove(busServiceRemovedSuccessCB, errorCB);
-};
/*****************************************************************************/
-//Generic definition for an agent. An agent need :
+//Generic definition for an agent. An agent needs :
//srvDbusName : the DBus parent service
//objPath : a DBus path to access it
//jsHdl : a Javascript handler to process methods,
//xml : the xml which describe interface/methods/signals...
-Agent = function(srvDbusName, objPath, jsHdl, xml) {
+cloudeebus.Agent = function(srvDbusName, objPath, jsHdl, xml) {
this.srvName = srvDbusName;
this.registered = false;
this.xml = xml;
this.wampSession = session;
this.busConnection = busConnection;
this.name = name;
+ this.agents = [];
this.isCreated = false;
return this;
};
-cloudeebus.Service.prototype.add = function(promise) {
+
+cloudeebus.Service.prototype.remove = function() {
var self = this;
- self.promise = promise;
- var resolver = self.promise.resolver;
-
- function ServiceAddedSuccessCB(serviceName) {
- try { // calling dbus hook object function for un-translated types
- var result = [self];
- resolver.fulfill(result[0], true);
- }
- catch (e) {
- cloudeebus.log("Method callback exception: " + e);
- resolver.reject(e, true);
- }
- }
- function ServiceAddedErrorCB(error) {
- var errorStr = cloudeebus.getError(error);
- cloudeebus.log("Error adding service method: " + self.name + ", error: " + errorStr);
- self.promise.resolver.reject(errorStr, true);
- }
-
- var arglist = [
- this.busConnection,
- this.name
- ];
-
- // call dbusSend with bus type, destination, object, message and arguments
- this.wampSession.call("serviceAdd", arglist).then(ServiceAddedSuccessCB, ServiceAddedErrorCB);
- return promise;
-};
-
-cloudeebus.Service.prototype.remove = function(successCB, errorCB) {
- function ServiceRemovedSuccessCB(serviceName) {
- if (successCB) {
+ var promise = new cloudeebus.Promise(function (resolver) {
+ function ServiceRemovedSuccessCB(serviceName) {
try {
- successCB(serviceName);
+ resolver.fulfill(serviceName, true);
}
catch (e) {
- alert("Exception removing service " + serviceName + " : " + cloudeebus.getError(e));
+ var errorStr = cloudeebus.getError(e);
+ cloudeebus.log("Method callback exception: " + errorStr);
+ resolver.reject(errorStr, true);
+ }
+ }
+
+ function ServiceRemovedErrorCB(error) {
+ var errorStr = cloudeebus.getError(error);
+ cloudeebus.log("Error removing service : " + self.name + ", error: " + errorStr);
+ self.promise.resolver.reject(errorStr, true);
+ }
+
+ for (var idx in self.agents) {
+ if (self.agents[idx]) {
+ self.removeAgent(self.agents[idx]);
}
}
- }
+
+ var arglist = [
+ self.name
+ ];
- var arglist = [
- this.name
- ];
-
- // call dbusSend with bus type, destination, object, message and arguments
- this.wampSession.call("serviceRelease", arglist).then(ServiceRemovedSuccessCB, errorCB);
+ // call dbusSend with bus type, destination, object, message and arguments
+ self.wampSession.call("serviceRelease", arglist).then(ServiceRemovedSuccessCB, ServiceRemovedErrorCB);
+ });
+
+ return promise;
};
+
cloudeebus.Service.prototype._searchMethod = function(ifName, method, objectJS) {
var funcToCall = null;
return funcToCall;
};
+
cloudeebus.Service.prototype._addMethod = function(ifName, method, agent) {
var service = this;
service._returnMethod(methodId, callDict.callIndex, true, result);
}
catch (e) {
- cloudeebus.log("Method " + ifName + "." + method + " call on " + agent.objectPath + " exception: " + e);
- service._returnMethod(methodId, callDict.callIndex, false, e.message);
+ var errorStr = cloudeebus.getError(e);
+ cloudeebus.log("Method " + ifName + "." + method + " call on " + agent.objectPath + " exception: " + errorStr);
+ service._returnMethod(methodId, callDict.callIndex, false, errorStr);
}
};
agent.jsHdl.methodId[agent.objectPath].push(methodId);
- cloudeebus.log("subscribe " + methodId);
this.wampSession.subscribe(methodId, agent.jsHdl.wrapperFunc[method]);
}
};
+
cloudeebus.Service.prototype._addSignal = function(ifName, signal, agent) {
var service = this;
var methodExist = false;
if ((agent.jsHdl[signal] == undefined || agent.jsHdl[signal] == null) && !methodExist)
agent.jsHdl[signal] = function() {
- service.emitSignal(agent.objectPath, signal, arguments[0]);
+ service._emitSignal(agent.objectPath, signal, arguments[0]);
};
else
cloudeebus.log("Can not create new method to emit signal '" + signal + "' in object JS this method already exist!");
};
+
cloudeebus.Service.prototype._createWrapper = function(agent) {
var self = this;
var parser = new DOMParser();
}
if (ifChild.nodeName == "signal") {
var metName = ifChild.attributes.getNamedItem("name").value;
- self._addSignal(objectPath, ifName, metName, objectJS);
+ self._addSignal(ifName, metName, agent);
}
ifChild = ifChild.nextSibling;
}
}
};
-cloudeebus.Service.prototype.addAgent = function(agent, successCB, errorCB) {
- function ServiceAddAgentSuccessCB(objPath) {
- if (successCB) {
- try {
- successCB(objPath);
+
+cloudeebus.Service.prototype.addAgent = function(agent) {
+ var self = this;
+
+ var promise = new cloudeebus.Promise(function (resolver) {
+ function ServiceAddAgentSuccessCB(objPath) {
+ try { // calling dbus hook object function for un-translated types
+ self.agents.push(agent);
+ agent.registered = true;
+ resolver.fulfill(objPath, true);
}
catch (e) {
- alert("Exception adding agent " + agent.objectPath + " : " + cloudeebus.getError(e));
- }
+ var errorStr = cloudeebus.getError(e);
+ cloudeebus.log("Method callback exception: " + errorStr);
+ resolver.reject(errorStr, true);
+ }
}
- }
+
+ function ServiceAddAgenterrorCB(error) {
+ var errorStr = cloudeebus.getError(error);
+ cloudeebus.log("Error adding agent : " + agent.objectPath + ", error: " + errorStr);
+ self.promise.resolver.reject(errorStr, true);
+ }
+
+ try {
+ self._createWrapper(agent);
+ }
+ catch (e) {
+ var errorStr = cloudeebus.getError(e);
+ cloudeebus.log("Exception creating agent wrapper " + agent.objectPath + " : " + errorStr);
+ resolver.reject(errorStr, true);
+ return;
+ }
+
+ var arglist = [
+ agent.objectPath,
+ agent.xml
+ ];
- try {
- this._createWrapper(agent);
- }
- catch (e) {
- var errorStr = cloudeebus.getError(e);
- alert("Exception creating agent wrapper " + agent.objectPath + " : " + errorStr);
- errorCB(errorStr);
- return;
- }
+ // call dbusSend with bus type, destination, object, message and arguments
+ self.wampSession.call("serviceAddAgent", arglist).then(ServiceAddAgentSuccessCB, ServiceAddAgenterrorCB);
+ });
- var arglist = [
- agent.objectPath,
- agent.xml
- ];
-
- // call dbusSend with bus type, destination, object, message and arguments
- this.wampSession.call("serviceAddAgent", arglist).then(ServiceAddAgentSuccessCB, errorCB);
+ return promise;
};
+
cloudeebus.Service.prototype._deleteWrapper = function(agent) {
- var self = this;
var objJs = agent.jsHdl;
if (objJs.methodId[agent.objectPath]) {
for (var idx in objJs.methodId[agent.objectPath]) {
}
};
-cloudeebus.Service.prototype.delAgent = function(rmAgent, successCB, errorCB) {
- function ServiceDelAgentSuccessCB(agent) {
- if (successCB) {
- try {
- successCB(agent);
+
+cloudeebus.Service.prototype.removeAgent = function(rmAgent) {
+ var self = this;
+
+ var promise = new cloudeebus.Promise(function (resolver) {
+ function ServiceRemoveAgentSuccessCB(agent) {
+ try { // calling dbus hook object function for un-translated types
+ self.agents.pop(agent);
+ agent.registered = false;
+ resolver.fulfill(agent, true);
}
catch (e) {
var errorStr = cloudeebus.getError(e);
- alert("Exception deleting agent " + rmAgent.objectPath + " : " + errorStr);
- errorCB(errorStr);
- }
+ cloudeebus.log("Method callback exception: " + errorStr);
+ resolver.reject(errorStr, true);
+ }
}
- }
- try {
- this._deleteWrapper(rmAgent);
- }
- catch (e) {
- var errorStr = cloudeebus.getError(e);
- alert("Exception deleting agent wrapper " + rmAgent.objectPath + " : " + errorStr);
- errorCB(errorStr);
- }
-
- var arglist = [
- rmAgent.objectPath
- ];
+ function ServiceRemoveAgentErrorCB(error) {
+ var errorStr = cloudeebus.getError(error);
+ cloudeebus.log("Error removing agent : " + rmAgent.objectPath + ", error: " + errorStr);
+ self.promise.resolver.reject(errorStr, true);
+ }
- // call dbusSend with bus type, destination, object, message and arguments
- this.wampSession.call("serviceDelAgent", arglist).then(ServiceDelAgentSuccessCB, errorCB);
+ try {
+ self._deleteWrapper(rmAgent);
+ }
+ catch (e) {
+ var errorStr = cloudeebus.getError(e);
+ cloudeebus.log("Exception removing wrapper of agent " + rmAgent.objectPath + " : " + errorStr);
+ errorCB(errorStr);
+ }
+
+ var arglist = [
+ rmAgent.objectPath
+ ];
+
+ // call dbusSend with bus type, destination, object, message and arguments
+ self.wampSession.call("serviceDelAgent", arglist).then(ServiceRemoveAgentSuccessCB, ServiceRemoveAgentErrorCB);
+ });
+
+ return promise;
};
+
cloudeebus.Service.prototype._returnMethod = function(methodId, callIndex, success, result, successCB, errorCB) {
var arglist = [
methodId,
this.wampSession.call("returnMethod", arglist).then(successCB, errorCB);
};
+
cloudeebus.Service.prototype._emitSignal = function(objectPath, signalName, result, successCB, errorCB) {
var arglist = [
objectPath,
then.apply(value, [fulfillCallback, rejectCallback]);
}
catch (e) {
- this.reject(e, true);
+ this.reject(cloudeebus.getError(e), true);
}
}
init.apply(this, [this.resolver]);
}
catch (e) {
- this.resolver.reject(e, true);
+ this.resolver.reject(cloudeebus.getError(e), true);
}
}
return this;
resolver.resolve(value, true);
}
catch (e) {
- resolver.reject(e, true);
+ resolver.reject(cloudeebus.getError(e), true);
}
};
else
resolver.resolve(value, true);
}
catch (e) {
- resolver.reject(e, true);
+ resolver.reject(cloudeebus.getError(e), true);
}
};
else
resolver.fulfill(result[0], true);
}
catch (e) {
- cloudeebus.log("Method callback exception: " + e);
- resolver.reject(e, true);
+ var errorStr = cloudeebus.getError(e);
+ cloudeebus.log("Method callback exception: " + errorStr);
+ resolver.reject(errorStr, true);
}
}
var errorStr = cloudeebus.getError(error);
cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + errorStr);
if (errorCB)
- errorCB(cloudeebus.getError(errorStr));
+ errorCB(errorStr);
}
var arglist = [