1 /******************************************************************************
2 * Copyright 2012 Intel Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *****************************************************************************/
19 /*****************************************************************************/
21 var cloudeebus = window.cloudeebus = {};
23 cloudeebus.reset = function() {
24 cloudeebus.sessionBus = null;
25 cloudeebus.systemBus = null;
26 cloudeebus.wampSession = null;
27 cloudeebus.uri = null;
31 cloudeebus.log = function(msg) {
35 cloudeebus.connect = function(uri, successCB, errorCB) {
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);
48 function onWAMPSessionErrorCB(code, reason) {
49 if (code == ab.CONNECTION_UNSUPPORTED) {
50 cloudeebus.log("Browser is not supported");
53 cloudeebus.log("Failed to open session, code = " + code + ", reason = " + reason);
59 return ab.connect(cloudeebus.uri, onWAMPSessionConnectedCB, onWAMPSessionErrorCB);
63 cloudeebus.SessionBus = function() {
64 return cloudeebus.sessionBus;
68 cloudeebus.SystemBus = function() {
69 return cloudeebus.systemBus;
74 /*****************************************************************************/
76 cloudeebus.BusConnection = function(name, session) {
78 this.wampSession = session;
83 cloudeebus.BusConnection.prototype.listNames = function(successCB, errorCB) {
87 function listNamesSuccessCB(str) {
89 successCB(JSON.parse(str));
92 function listNamesErrorCB(error) {
93 cloudeebus.log("Failed to list names for bus: " + self.name);
94 cloudeebus.log(error.desc);
99 // call listNames with bus name
100 self.wampSession.call("listNames", [self.name]).then(listNamesSuccessCB, listNamesErrorCB);