Api Agent - fpaut/cloudeebus GitHub Wiki


Javascript objects and interfaces

Following is a brief description of the cloudeebus Javascript objects and their methods. This API is designed to be somewhat similar to the higher level dbus-python APIs. All asynchronous methods take optional success and error callbacks.


cloudeebus Object

There is one global instance of the cloudeebus object created when cloudeebus.js is loaded. It manages the initial connection to the server, and holds the Session and System bus connection.

Attributes

version : current version of the Javascript library
minVersion : minimum supported version of the Python server

Methods

cloudeebus.connect(uri, manifest, successCB, errorCB)

Connection to the cloudeebus.py server. Returns nothing.

uri : websocket uri, for instance ws://localhost:9000
manifest : A json manifest. See the "managing security" section for an example. When server runs in --opendoor mode, pass null.
successCB : callback fired when connection succeeds, takes no argument.
errorCB : callback fired when connection fails, takes error message string for argument.

cloudeebus.SessionBus()

Returns a cloudeebus.BusConnection object holding a connection to the Session bus.

cloudeebus.SystemBus()

Returns a cloudeebus.BusConnection object holding a connection to the System bus.

cloudeebus.log(message)

Does nothing. Replace by your custom method to display internal error messages, for instance:

cloudeebus.log = function(msg) {
	alert(msg);
}

cloudeebus.BusConnection Interface

A BusConnection object holds a connection to the Session bus or the System bus, for instance:

var bus = cloudeebus.SessionBus();

Methods

getObject(busName, objectPath, introspectCB, errorCB)

Returns a cloudeebus.ProxyObject object that can call DBus methods and be registered for DBus signals.

busName : DBus service, for instance org.gnome.ScreenSaver
objectPath : Object for which to get the proxy, for instance /
introspectCB : Optional callback. If passed, the object is introspected, i.e. populated with its methods and attributes and passed to the callback as argument.
errorCB : Optional callback fired on introspection error, takes error message string for argument.

cloudeebus.BusConnection.addService(serviceName)

Attempt to create your own Named Services across DBus. Return a DOMPromise and could be used to set optional success and error callbacks using the method then method.
serviceName : your own Named DBus Service requested.
successCB : callback fired when service creation succeeds, takes the cloudeebus.service object created for argument on which agents could be added. At this step, the whole necessary methods to manage the service (remove service/add remove agents...) are provided by cloudeebus.service.
errorCB : callback fired when service creation fails, takes error message string for argument.


cloudeebus.ProxyObject Interface

A ProxyObject is a proxy to a remote DBus objects, with its methods and attributes, for instance:

var obj = bus.getObject("org.gnome.ScreenSaver", "/");

Attributes

busName : DBus service for this object.
objectPath : DBus path for this object.
interfaceProxies : Dictionary of cloudeebus.ProxyObject objects indexed by interfaces.

If the object is introspected, i.e. if a introspectCB callback was passed to bus.getObject, then it also has the DBus Properties of the remote object as attributes.

Methods

callMethod(ifName, method, args, successCB, errorCB)

Calls a DBus method on the proxy object. Returns nothing.

ifName : Interface name, for instance org.gnome.ScreenSaver
method : Name of the method to call, for instance SetActive
args : Argument list for the method, for instance [true]
successCB : Optional callback fired when method is completed successfully, takes the dbus method results for arguments.
errorCB : Optional callback fired on error, takes error message string for argument.

If the object is introspected then the method can be called directly by its name, for instance in the example above, callbacks omitted:
obj.SetActive(true)
instead of:
obj.callMethod("org.gnome.ScreenSaver", "SetActive", [true])

getInterface(ifName)

Use on introspected objects. Returns a version of this cloudeebus.ProxyObject with only attributes and methods of interface which name is passed as parameter. This is useful if the object implements interfaces with conflicting method or attribute names.

ifName : Interface name, for instance org.gnome.ScreenSaver
Interface objects are stored in the interfaceProxies dictionary. Iterate on the dictionary to get interfaces name.

connectToSignal(ifName, signal, handlerCB, errorCB)

Connects the proxy object to a DBus signal. Returns nothing.

ifName : Interface name, for instance org.gnome.ScreenSaver
signal : Name of the signal to register for, for instance ActiveChanged
handlerCB : Signal handler callback, fired when the signal is emitted. Takes the dbus signal data for arguments.
errorCB : Optional callback fired on error, takes error message string for argument.

disconnectSignal(ifName, signal)

Disconnects the proxy object from a DBus signal. Returns nothing.

ifName : Interface name, for instance org.gnome.ScreenSaver
signal : Name of the signal to register for, for instance ActiveChanged


cloudeebus.Service Interface

The 'class' object holding the DBus service.

Attributes

Methods

cloudeebus.Service.remove().then(successCB, errorCB).

Attempt to remove the service across DBus. Return a DOMPromise and could be used to set optional success and error callbacks using the then method.
successCB : callback fired when service removing succeeds, takes the service name of the removed service.
errorCB : callback fired when service creation fails, takes error message string for argument.

cloudeebus.Service.addAgent(objectPath, xmlTemplate, objectJS).then(successCB, errorCB)

Attempt to create an agent attach it to the cloudeebus.Service object. Return a DOMPromise and could be used to set optional success and error callbacks using the then method.
objectPath : the path on service where this agent could be accessible.
xmlTemplate : XML format of the interfaces/methods and signals describing this agent.
objectJS : Javascript format (object) of the interfaces/methods describing this agent. Methods to emit signals are automatically generated by this function and named as describe in xmlTemplate.
successCB : callback fired when agent creation succeeds, takes the objectPath of the agent created for argument.
errorCB : callback fired when agent creation fails, takes error message string for argument.

cloudeebus.Service.delAgent(objectPath, successCB, errorCB)

Attempt to remove an agent attached to the cloudeebus.Service object.
objectPath : the path of the agent to remove.
successCB : callback fired when removing agent succeeds, takes the objectPath of the agent removed for argument.
errorCB : callback fired when agent creation fails, takes error message string for argument.


Example

The following example create a sample DBus service and the Javascript object linked with.

No manifest is passed, so the server must be executed in --opendoor mode, for instance:

cloudeebus.py --debug --opendoor --port=9001

The Javascript code connects to cloudeebus without a manifest, create a DBus service and add two same agent (one interface/one method/one signal) with two Javascript objects linked (and show two kind of implementation of Javascript objects).

var xmlTemplate = "<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"\n"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">\n
<node>
  <interface name="org.cloudeebus.Sample">
    <method name="Add">
      <arg type="i" name="arg1"/>
      <arg type="i" name="arg2"/>
      <arg type="i" name="result" direction="out"/>
    </method>
    <signal name="ResultChanged">
      <arg type="v" name="result"/>
    </signal>
  </interface>
</node>"

    // In this first Javascript sample object, no interface are named, also the methods are searched
    // in the 'root' path of the object. Two methods Add on two interfaces can not be distinguished
    // and can not be created

JsObjectHandler_sample1 = {
  Add: function(a,b) {
    var result = a + b;

    // Emit a signal with method auto-created and 'result' as argument
    this.ResultChanged(result);
    // Return the 'result' to the dbus method
    return result;
  }, 
};

    // In this second Javascript sample object, interface are named, also the methods are searched
    // in the correct interface under 'interfaceProxies' member. Also, two methods Add on two
    // interfaces can be distinguished and can be created

JsObjectHandler_sample2 = {
  interfaceProxies : {
	    "org.cloudeebus.Sample" : {
  	      Add: function(a,b) {
    	var result = a + b;

        // Emit a signal with method auto-created and 'result' as argument
        this.ResultChanged(result);
        // Return the 'result' to the dbus method
        return result;
      } 
    }
  }
};

function errorCB(error_string) {
        // Log error
        cloudeebus.log(error_string);
};

function addAgents(service) {
	var agentName = "/org/cloudeebus/Sample"; // = DBUS object name
	cloudeebus.SessionBus().service.addAgent(agentName, xmlTemplate, JsObjectHandler_sample1, logCB, errorCB);

	// adding another agent using parameter 'service'
	agentName = agentName +"2";
	service.addAgent(agentName, xmlTemplate, JsObjectHandler_sample2, logCB, errorCB);

	// Now, all refer to Dbus methods will be processed in Javascript object handler
};

function connectSuccess() {
	var bus = cloudeebus.SessionBus();
	bus.addService("org.cloudeebus.Sample").then(addAgents, errorCB);
}

cloudeebus.connect("ws://localhost:9001", null, connectSuccess);
⚠️ **GitHub.com Fallback** ⚠️