Skip to content

Commit

Permalink
sync (#473)
Browse files Browse the repository at this point in the history
* add http_get_json helper; bump version
* add xbr buyer/seller examples
  • Loading branch information
oberstet committed Sep 23, 2019
1 parent cc3950c commit 0fba010
Show file tree
Hide file tree
Showing 22 changed files with 634 additions and 20 deletions.
8 changes: 5 additions & 3 deletions Makefile
Expand Up @@ -20,9 +20,10 @@ distclean: clean
-sudo rm -f ./packages/autobahn/package-lock.json

clean:
-sudo rm -f .sconsign.dblite
-sudo rm -rf ./build
-sudo rm -rf ./packages/autobahn/build
-rm -f .sconsign.dblite
-rm -rf ./build
-rm -rf ./packages/autobahn/build
-rm -rf ./packages/autobahn-xbr/build


#
Expand Down Expand Up @@ -79,6 +80,7 @@ build_browser_ab_host:
JAVA_HOME=/usr/lib/jvm/default-java JS_COMPILER=${PWD}/packages/autobahn/node_modules/google-closure-compiler-java/compiler.jar scons -C ./packages/autobahn
ls -la packages/autobahn/build/
cp ./packages/autobahn/build/autobahn.js ~/scm/crossbario/dsq-examples/examples/js/browser/
cp ./packages/autobahn/build/autobahn.js ~/scm/crossbario/crossbar-examples/rlinks/router_cluster/ha_setup/web/

# FIXME: fails at minimization
#
Expand Down
1 change: 1 addition & 0 deletions doc/README.md
Expand Up @@ -16,3 +16,4 @@ And finally, we suggest visiting the [Crossbar.io site](http://crossbar.io). Cro
* [Examples Overview](examples.md) - An overview of the available code examples
* [Building](building.md) - Instructions for how to build Autobahn|JS from source
* [Release Process](release-process.md) - Instructions on how to do a new release (only relevant for project members)
* [Utilities](utils.md) - Autobahn utilities.
64 changes: 64 additions & 0 deletions doc/utils.md
@@ -0,0 +1,64 @@
# Utilities

## HTTP requests

Autobahn includes helpers to do HTTP requests returning JS promises, allowing
a modern style of asynchronous programming without callback hell.

To do a HTTP/GET requesting JSON data:

```javascript
autobahn.util.http_get_json("/config").then(
function (config) {
console.log(config);
},
function (err) {
console.log(err);
}
);
```

JSON data can be served directly from Crossbar.io using a webservice of type `json`.

Here is a node configuration that will serve the `value` configured on the HTTP
URL `/config`:


```json
{
"workers": [
{
"type": "router",
"transports": [
{
"type": "web",
"endpoint": {
"type": "tcp",
"port": 8080
},
"paths": {
"/": {
"type": "static",
"directory": "../web",
"options": {
"enable_directory_listing": true
}
},
"config": {
"type": "json",
"value": {
"nodes": [
"ws://localhost:8081/ws",
"ws://localhost:8082/ws",
"ws://localhost:8083/ws",
"ws://localhost:8084/ws"
]
}
},
}
}
]
}
]
}
```
2 changes: 1 addition & 1 deletion packages/autobahn-xbr/package.json
@@ -1,6 +1,6 @@
{
"name": "autobahn-xbr",
"version": "19.9.3",
"version": "19.10.1",
"description": "The XBR Protocol (smart contracts package)",
"main": "index.js",
"files": [
Expand Down
35 changes: 20 additions & 15 deletions packages/autobahn/lib/connection.js
Expand Up @@ -67,7 +67,7 @@ var Connection = function (options) {
self._max_retries = typeof self._options.max_retries !== 'undefined' ? self._options.max_retries : 15;

// initial retry delay in seconds
self._initial_retry_delay = self._options.initial_retry_delay || 1.5;
self._initial_retry_delay = typeof self._options.initial_retry_delay !== 'undefined' ? self._options.initial_retry_delay : 1.5;

// maximum seconds between reconnection attempts
self._max_retry_delay = self._options.max_retry_delay || 300;
Expand Down Expand Up @@ -104,9 +104,9 @@ var Connection = function (options) {


Connection.prototype._create_transport = function () {

var self = this;

for (var i = 0; i < this._transport_factories.length; ++i) {
var transport_factory = this._transport_factories[i];
log.debug("trying to create WAMP transport of type: " + transport_factory.type);
Expand All @@ -130,9 +130,9 @@ Connection.prototype._create_transport = function () {
Connection.prototype._init_transport_factories = function () {
// WAMP transport
//

var self = this;

var transports, transport_options, transport_factory, transport_factory_klass;

util.assert(this._options.transports, "No transport.factory specified");
Expand Down Expand Up @@ -332,21 +332,24 @@ Connection.prototype.open = function () {

var next_retry = self._autoreconnect_advance();

var details = {
reason: self._session_close_reason,
message: self._session_close_message,
retry_delay: next_retry.delay,
retry_count: next_retry.count,
will_retry: next_retry.will_retry
};

log.warn("connection closed", reason, details);

// fire app code handler
//
if (self.onclose) {
var details = {
reason: self._session_close_reason,
message: self._session_close_message,
retry_delay: next_retry.delay,
retry_count: next_retry.count,
will_retry: next_retry.will_retry
};
try {
// Connection.onclose() allows to cancel any subsequent retry attempt
var stop_retrying = self.onclose(reason, details);
} catch (e) {
util.handle_error(self._options.on_user_error, e, "Exception raised from app code while firing Connection.onclose()");
util.handle_error(self._options.on_user_error, e, "Exception raised from app code while firing Connection.onclose()");
}
}

Expand All @@ -367,12 +370,14 @@ Connection.prototype.open = function () {

self._is_retrying = true;

log.debug("retrying in " + next_retry.delay + " s");
log.warn("auto-reconnecting in " + next_retry.delay + "s ..");
self._retry_timer = setTimeout(retry, next_retry.delay * 1000);

} else {
log.debug("giving up trying to reconnect");
log.warn("giving up trying to auto-reconnect!");
}
} else {
log.warn("auto-reconnect disabled!", self._retry, stop_retrying);
}
}
}
Expand Down
67 changes: 67 additions & 0 deletions packages/autobahn/lib/util.js
Expand Up @@ -185,6 +185,72 @@ var http_post = function (url, data, timeout) {
}
};

// Helper to do HTTP/GET requests returning JSON parsed result as a promise.
var http_get_json = function (url, timeout) {

var d = when.defer();
var req = new XMLHttpRequest();
req.withCredentials = true; // pass along cookies
req.onreadystatechange = function () {

if (req.readyState === 4) {

// Normalize IE's response to HTTP 204 when Win error 1223.
// http://stackoverflow.com/a/10047236/884770
//
var status = (req.status === 1223) ? 204 : req.status;

if (status === 200) {

// parse response
var data = JSON.parse(req.responseText);

// response with content
//
d.resolve(data);

} if (status === 204) {

// empty response
//
d.resolve();

} else {

// anything else is a fail
//
var statusText = null;
try {
statusText = req.statusText;
} catch (e) {
// IE8 fucks up on this
}
d.reject({code: status, text: statusText});
}
}
}

req.open("GET", url, true);
req.setRequestHeader("Content-type", "application/json; charset=utf-8");

if (timeout > 0) {
req.timeout = timeout; // request timeout in ms

req.ontimeout = function () {
d.reject({code: 501, text: "request timeout"});
}
}

req.send();

if (d.promise.then) {
// whenjs has the actual user promise in an attribute
return d.promise;
} else {
return d;
}
};

/**
* Merge a list of objects from left to right
*
Expand Down Expand Up @@ -345,6 +411,7 @@ exports.handle_error = handle_error;
exports.rand_normal = rand_normal;
exports.assert = assert;
exports.http_post = http_post;
exports.http_get_json = http_get_json;
exports.defaults = defaults;
exports.new_global_id = new_global_id;
exports.deferred_factory = deferred_factory;
Expand Down
2 changes: 1 addition & 1 deletion packages/autobahn/package.json
@@ -1,6 +1,6 @@
{
"name": "autobahn",
"version": "19.9.1",
"version": "19.10.1",
"description": "An implementation of The Web Application Messaging Protocol (WAMP).",
"main": "index.js",
"files": [
Expand Down
8 changes: 8 additions & 0 deletions test/xbr/client/Makefile
@@ -0,0 +1,8 @@
diff:
clear
@echo ""
@echo "\n******* SELLER DIFF between browser/node:\n"
-diff browser/seller.js nodejs/seller.js
@echo "\n******* BUYER DIFF between browser/node:\n"
-diff browser/buyer.js nodejs/buyer.js
@echo ""
33 changes: 33 additions & 0 deletions test/xbr/client/README.md
@@ -0,0 +1,33 @@
# XBR Seller/Buyer with Autobahn-JS

Install NodeJS:

```bash
cd $HOME
wget https://nodejs.org/dist/v10.16.0/node-v10.16.0-linux-x64.tar.xz
tar xvf node-v10.16.0-linux-x64.tar.xz
```

and add the following to `$HOME/.profile`:

```bash
export PATH=${HOME}/node-v10.16.0-linux-x64/bin:${PATH}
```

Install dependencies

```bash
make deps
```

To start the seller run

```bash
make seller
```

For the buyer

```bash
make buyer
```
2 changes: 2 additions & 0 deletions test/xbr/client/browser/Makefile
@@ -0,0 +1,2 @@
run:
twistd -n web --listen tcp:8050 --path .
12 changes: 12 additions & 0 deletions test/xbr/client/browser/README.md
@@ -0,0 +1,12 @@
# XBR Buyer/Seller for Browser

This folder contains a simple XBR buyer and XBR seller that runs in a HTML5 browser (Firefox/Chrome tested).

Open

* [XBR Buyer](buyer.html)
* [XBR Seller](seller.html)

and hit F12 to watch console output.

> The respective JS code is identical to the NodeJS examples - the only difference are the very top couple of lines that import the Autobahn libraries.
11 changes: 11 additions & 0 deletions test/xbr/client/browser/buyer.html
@@ -0,0 +1,11 @@
<html>
<meta charset="UTF-8">
<body>
<script>AUTOBAHN_DEBUG = false;</script>
<script src="autobahn.js"></script>
<script src="autobahn-xbr.js"></script>
<script src="buyer.js"></script>
<h1>XBR Buyer Example</h1>
<p>Open JavaScript console to watch output.</p>
</body>
</html>

0 comments on commit 0fba010

Please sign in to comment.