cloudeebus server: fix message handling in error callbacks
[contrib/cloudeebus.git] / cloudeebus / cloudeebus.js
1 /******************************************************************************
2  * Copyright 2012 Intel Corporation.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16
17
18
19 /*****************************************************************************/
20
21 var cloudeebus = window.cloudeebus = {};
22
23 cloudeebus.reset = function() {
24         cloudeebus.sessionBus = null;
25         cloudeebus.systemBus = null;
26         cloudeebus.wampSession = null;
27         cloudeebus.uri = null;
28 };
29
30
31 cloudeebus.log = function(msg) {
32 };
33
34
35 cloudeebus.connect = function(uri, successCB, errorCB) {
36         cloudeebus.reset();
37         cloudeebus.uri = uri;
38         
39         function onWAMPSessionConnectedCB(session) {
40                 cloudeebus.wampSession = session;
41                 cloudeebus.sessionBus = new cloudeebus.BusConnection("session", cloudeebus.wampSession);
42                 cloudeebus.systemBus = new cloudeebus.BusConnection("system", cloudeebus.wampSession);
43                 cloudeebus.log("Connected to " + cloudeebus.uri);
44                 if (successCB)
45                         successCB();
46         };
47
48         function onWAMPSessionErrorCB(code, reason) {
49                 if (code == ab.CONNECTION_UNSUPPORTED) {
50                         cloudeebus.log("Browser is not supported");
51                 }
52                 else {
53                         cloudeebus.log("Failed to open session, code = " + code + ", reason = " + reason);
54                 }
55                 if (errorCB)
56                         errorCB(reason);
57         };
58
59         return ab.connect(cloudeebus.uri, onWAMPSessionConnectedCB, onWAMPSessionErrorCB);
60 };
61
62
63 cloudeebus.SessionBus = function() {
64         return cloudeebus.sessionBus;
65 };
66
67
68 cloudeebus.SystemBus = function() {
69         return cloudeebus.systemBus;
70 };
71
72
73
74 /*****************************************************************************/
75
76 cloudeebus.BusConnection = function(name, session) {
77         this.name = name;
78         this.wampSession = session;
79         return this;
80 }
81
82
83 cloudeebus.BusConnection.prototype.listNames = function(successCB, errorCB) {
84         
85         var self = this; 
86
87         function listNamesSuccessCB(str) {
88                 if (successCB)
89                         successCB(JSON.parse(str));
90         };
91
92         function listNamesErrorCB(error) {
93                 cloudeebus.log("Failed to list names for bus: " + self.name);
94                 cloudeebus.log(error.desc);
95                 if (errorCB)
96                         errorCB(error.desc);
97         };
98
99     // call listNames with bus name
100         self.wampSession.call("listNames", [self.name]).then(listNamesSuccessCB, listNamesErrorCB);
101 }
102
103