b975b8e63762eec95f2700ab97cac0eb2ce20e92
[contrib/cloudeebus.git] / doc / agent / server.html
1 <!DOCTYPE html>
2 <html>
3     <head>
4         <script src="../../lib/autobahn.min.js"></script>
5         <script src="../../cloudeebus/cloudeebus.js"></script>
6         <script type="text/javascript">
7                 function evalScript(field) {
8                         eval(
9                                 "var lambda = function() {" +
10                                         document.getElementById(field).value +
11                                 "};" + 
12                                 "lambda();"
13                         );
14                 }
15         </script>
16     </head>
17    <body>
18         <center><h1>cloudeebus</h1></center>
19         <br>
20                 <textarea style="width:80%" rows="32" id="script">
21 var manifest = {
22         name: "cloudeebus",
23         key: "secret",
24         services: [
25                 "org.cloudeebus.Sample"
26         ]
27 };
28
29
30 var sampleXml= '<!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><method name="Div"><arg type="d" name="arg1"/><arg type="d" name="arg2"/><arg type="d" name="result" direction="out"/></method><method name="Variant"><arg type="a{sv}" name="arg1"/><arg type="a{sv}" name="result" direction="out"/></method><signal name="ResultChanged"><arg type="v" name="result"/></signal></interface></node>';
31
32 cloudeebus.log = function(msg) {
33   document.getElementById("log").innerHTML += msg + "\n";
34 }
35
36 function logCB(result) {
37   cloudeebus.log(JSON.stringify(result));
38 }
39
40 function errorCB(error) {
41   cloudeebus.log(cloudeebus.getError(error));
42 }
43
44 sampleObjectHandler = {
45   Add: function(a,b) {
46     cloudeebus.log("Add " + a + " + " + b);
47     this.ResultChanged(a+b);
48     return a+b;
49   }, 
50   Variant: function(a) {
51     cloudeebus.log("Get and return :" + JSON.stringify(a));
52     return a;
53   }, 
54   Div: function(a,b) {
55     cloudeebus.log("Div " + a + " / " + b);
56         this.ResultChanged(a/b);
57     return a/b;
58   }
59 };
60
61 function addAgent(service) {
62   window.gService = service;
63   var agent = new cloudeebus.Agent(service.name, "/org/cloudeebus/Sample", sampleObjectHandler, sampleXml);
64   cloudeebus.log("Adding agent " + agent.objectPath + " on " + service.name + ", mapped on Javascript object: 'sampleObjectHandler");
65   service.addAgent(agent).then(logCB, errorCB);
66   window.gAgent = agent;
67 }
68
69 function connectSuccess() {
70   cloudeebus.SessionBus().addService("org.cloudeebus.Sample").then(addAgent, errorCB);
71 }
72
73 if (!window.sampleProxy)
74   cloudeebus.connect("ws://localhost:9003", manifest, connectSuccess, errorCB);
75 </textarea>
76                 <br>
77 -               <input type="button" value="run script" onclick="evalScript('script')"/>
78                 <input type="button" value="clear log" onclick="document.getElementById('log').innerHTML='';"/>
79                 <br>
80 <textarea style="width:80%" rows="7" id="removeCmd">
81 function removeService() {
82 cloudeebus.log("Agent " + JSON.stringify(window.gAgent));
83         cloudeebus.log("Removing service");
84         window.gService.remove()
85 }
86
87 cloudeebus.log("Service = " + JSON.stringify(window.gService));
88 cloudeebus.log("Removing agent " + JSON.stringify(window.gAgent));
89 window.gService.removeAgent(window.gAgent).then(removeService, null);
90 </textarea>
91                 <br>
92                 <input type="button" value="Unregister service" onclick="evalScript('removeCmd')"/>
93                 <br>
94                 <pre id="log" style="height: 20em; overflow-x: auto; overflow-y: auto; background-color: #faa;"></pre>
95    </body>
96 </html>