added better comments to api.js and moved server test code into subdir
authorTodd Brandt <tebrandt@frontier.com>
Thu, 23 Aug 2012 08:58:46 +0000 (01:58 -0700)
committerMichael Carpenter <malcom2073@gmail.com>
Thu, 23 Aug 2012 09:51:23 +0000 (05:51 -0400)
plugins/websocketsink/test/api.js
plugins/websocketsink/test/servertest/client.html [new file with mode: 0644]
plugins/websocketsink/test/servertest/server.html [moved from plugins/websocketsink/test/server.html with 89% similarity]
plugins/websocketsink/test/servertest/server.js [moved from plugins/websocketsink/test/server.js with 97% similarity]
plugins/websocketsink/test/test.js

index 30bff95..b1f0d5b 100644 (file)
@@ -7,25 +7,86 @@
  *
  */
 
-/* ------------------------ vehicle interface ----------------------------- */
+/*****************************************************************************
+* Class name: Vehicle
+* Description:
+*    A javascript implementation of the IVI vehicle API that communicates
+*    to the automotive message broker through a websocket
+* Optional constructor arguments:
+*    sCB: success callback, called when socket is connected, argument is 
+*         success message string
+*    eCB: error callback, called on socket close or error, argument is error
+*         message string
+*    url: the URL to use for the websocket, in the form "ws://host:port/script"
+*    protocol: the protocol to use for the websocket, default is "http-only"
+*
+* [Public Member functions]
+*  Function name: getSupportedEventTypes(type, writeable, successCB, errorCB)
+*    Description:
+*        Retrieves a list of vehicle events for the requested type
+*    Required arguments:
+*        type: target event or group to query (use empty string for all events)
+*        writeable: if true, return only writeable events, otherwise get all
+*        successCB: success callback, gets called with a string list of names
+*              for all the events and event groups that are children of the 
+*              target. e.g. "vehicle_info" returns all events/groups with the 
+*              vehicle_info prefix. If the target is an event group, it's
+*              omitted from the returned list
+*        errorCB: error callback, called with error message string
+*
+*  Function name: get(type, successCB, errorCB)
+*    Description:
+*        Retrieves a list of event/value pairs for a target event or event group
+*    Required arguments:
+*        type: target event group to query (use empty string for all events)
+*        successCB: success callback, gets called with the event/value pair list
+*                   for all event children of the target. The list is the in the 
+*                   form of data[n].name/data[n].value
+*        errorCB: error callback, called with error message string
+*
+*  Function name: set(type, value, successCB, errorCB)
+*    Description:
+*        Sets a single event's value (triggers error if it's read-only)
+*    Required arguments:
+*        type: target event to set (an event group will trigger an error)
+*        successCB: success callback, gets called with the event/value pair
+*                   that was successfully set in the form data.name/data.value
+*        errorCB: error callback, called with error message string
+*
+******************************************************************************/
 
-function Vehicle(socketUrl, sCB, eCB, calltimeout)
+function Vehicle(sCB, eCB, url, protocol)
 {
+    /* store a copy of Vehicle this for reference in callbacks */
     var self = this;
+
     this.iSuccessCB = sCB;
     this.iErrorCB = eCB;
-    this.retries = 5;
-    this.connected = false;
     this.transactionid = 0;
-    this.methodCalls = [];
-    this.methodIdx = 0;
-    this.timeouttime = (calltimeout == undefined)?5000:calltimeout;
 
+    /* variables for call management, supports up to 100 simultaneously */
+    this.methodIdx = 0;
+    this.methodCalls = [];
     for(var i = 0; i < 100; i++)
     {
         this.methodCalls[i] = null;
     }
 
+    /* number of connection retries to attempt if the socket closes */
+    this.retries = 5;
+    this.connected = false;
+
+    /* timeout for method calls in milliseconds */
+    this.timeouttime = 5000;
+
+    /* default values for WebSocket */
+    this.socketUrl = "ws://localhost:23000/vehicle";
+    this.socketProtocol = "http-only";
+
+    /* override the websocket address if parameters are given */
+    if(url != undefined) this.socketUrl = url;
+    if(protocol != undefined) this.socketProtocol = protocol;
+
     this.VehicleMethodCall = function(id, name, successCB, errorCB)
     {
         var me = this;
@@ -57,11 +118,17 @@ function Vehicle(socketUrl, sCB, eCB, calltimeout)
     function init() {
         if ("WebSocket" in window)
         {
-            self.socket = new WebSocket(socketUrl,"http-only");
+            if(self.socketProtocol.length > 0)
+            {
+                self.socket = new WebSocket(self.socketUrl, self.socketProtocol);
+            }
+            else
+            {
+                self.socket = new WebSocket(self.socketUrl);
+            }
             self.socket.onopen = function()
             {
                 self.connected = true;
-                this.send("client");
                 self.iSuccessCB((self.retries < 5)?"(RECONNECTED)":"");
                 self.retries = 5;
             };
diff --git a/plugins/websocketsink/test/servertest/client.html b/plugins/websocketsink/test/servertest/client.html
new file mode 100644 (file)
index 0000000..9ef2ee3
--- /dev/null
@@ -0,0 +1,17 @@
+<!doctype html>
+<html lang="en">
+<head>
+    <title>IVI API Tester</title>
+    <meta charset="utf-8">
+    <link rel="stylesheet" href="../style.css"/>
+    <script src="../api.js"></script>
+</head>
+<body onload='init("ws://localhost:23023/vehicle?client", "")'>
+    <div id="result">
+    </div>
+    <div id="tester">
+    </div>
+    <script src="../events.js"></script>
+    <script src="../test.js"></script>
+</body>
+</html>
similarity index 89%
rename from plugins/websocketsink/test/server.html
rename to plugins/websocketsink/test/servertest/server.html
index 0e5d50c..43dc72a 100644 (file)
@@ -12,7 +12,7 @@
     word-wrap: break-word;
 }
     </style>
-    <script src="events.js"></script>
+    <script src="../events.js"></script>
     <script src="server.js"></script>
 </head>
 <body>
similarity index 97%
rename from plugins/websocketsink/test/server.js
rename to plugins/websocketsink/test/servertest/server.js
index 54b5ee4..180c23f 100644 (file)
@@ -59,7 +59,6 @@ function VehicleServer(socketUrl)
             self.socket.onopen = function()
             {
                 PRINT.pass("Server READY");
-                this.send("server");
             };
             self.socket.onclose = function()
             {
@@ -173,5 +172,5 @@ VehicleServer.prototype.receive = function(msg)
 window.addEventListener('load', function () {
     "use strict";
     PRINT.init("result");
-    var server = new VehicleServer("ws://localhost:23000/vehicle");
+    var server = new VehicleServer("ws://localhost:23023/vehicle?server");
 });
index db4f8e2..ebeb4be 100644 (file)
@@ -150,8 +150,7 @@ function error(msg)
     PRINT.fail(msg);
 }
 
-function init() {
+function init(url, protocol) {
     PRINT.init("result");
-    window.vehicle = new Vehicle("ws://localhost:23000/vehicle",
-        start, error);
+    window.vehicle = new Vehicle(start, error, url, protocol);
 }