Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
284 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import os | ||
import hashlib | ||
|
||
from twisted.internet.defer import inlineCallbacks | ||
|
||
from txaio import make_logger | ||
from autobahn import wamp | ||
from autobahn.twisted.wamp import ApplicationSession | ||
|
||
|
||
class Backend(ApplicationSession): | ||
|
||
log = make_logger() | ||
|
||
def __init__(self, config): | ||
self.log.info('Backend.__init__(config={config})', config=config) | ||
ApplicationSession.__init__(self, config) | ||
|
||
@inlineCallbacks | ||
def onJoin(self, details): | ||
self.log.info('Backend.onJoin(details={details})', details=details) | ||
regs = yield self.register(self) | ||
self.log.info('Registered procedures {regs}', regs=[r.procedure for r in regs if r]) | ||
|
||
@wamp.register('any.echo') | ||
def echo(self, whatever): | ||
return whatever | ||
|
||
@wamp.register('binary.random') | ||
def random(self, len=32): | ||
assert type(len) == int | ||
return os.urandom(len) | ||
|
||
@wamp.register('binary.concat') | ||
def concat(self, xbytes, ybytes): | ||
assert type(xbytes) == bytes | ||
assert type(ybytes) == bytes | ||
return xbytes + ybytes | ||
|
||
@wamp.register('binary.hash') | ||
def hash(self, xbytes): | ||
assert type(xbytes) == bytes | ||
return hashlib.sha256(xbytes).digest() | ||
|
||
@wamp.register('binary.reversed') | ||
def reversed(self, xbytes): | ||
assert type(xbytes) == bytes | ||
return bytes(reversed(xbytes)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// AutobahnJS - http://autobahn.ws, http://wamp.ws | ||
// | ||
// A JavaScript library for WAMP ("The Web Application Messaging Protocol"). | ||
// | ||
// Copyright (c) Crossbar.io Technologies GmbH and contributors | ||
// | ||
// Licensed under the MIT License. | ||
// http://www.opensource.org/licenses/mit-license.php | ||
// | ||
/////////////////////////////////////////////////////////////////////////////// | ||
|
||
var autobahn = require('./../index.js'); | ||
var testutil = require('./testutil.js'); | ||
|
||
// var randomBytes = require('randombytes'); | ||
var seed = require('random-bytes-seed') | ||
var randomBytes = seed('a seed') | ||
|
||
|
||
function run_test (test, ser) { | ||
|
||
var done = autobahn.when.defer(); | ||
|
||
var config = { | ||
//url: testutil.config.url, | ||
url: "ws://127.0.0.1:8090", | ||
realm: testutil.config.realm, | ||
serializers: [ser] | ||
}; | ||
var connection = new autobahn.Connection(config); | ||
|
||
connection.onopen = function (session) { | ||
|
||
test.log('Connected'); | ||
|
||
function echo(args) { | ||
return args[0]; | ||
} | ||
|
||
var endpoints = { | ||
'com.myapp.echo': echo | ||
}; | ||
|
||
var pl1 = []; | ||
|
||
for (var uri in endpoints) { | ||
pl1.push(session.register(uri, endpoints[uri])); | ||
} | ||
|
||
autobahn.when.all(pl1).then( | ||
function () { | ||
test.log("All registered."); | ||
test.log("Serializer ID: " + session._socket.serializer.SERIALIZER_ID); | ||
|
||
var pl2 = []; | ||
|
||
var vals1 = [ | ||
1.7, | ||
"hello", | ||
[1, 2, -3], | ||
{a: 5, b: "hello2"}, | ||
[-9007199254740991, 9007199254740991], | ||
null, | ||
Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7]), | ||
randomBytes(32) | ||
]; | ||
|
||
for (var i = 0; i < vals1.length; ++i) { | ||
|
||
pl2.push(session.call('com.myapp.echo', [vals1[i]]).then( | ||
function (res) { | ||
test.log("Result [com.myapp.echo]:", res); | ||
}, | ||
function (err) { | ||
test.log("Error [com.myapp.echo]:", err.error, err.args, err.kwargs); | ||
} | ||
)); | ||
} | ||
|
||
for (var i = 0; i < vals1.length; ++i) { | ||
|
||
pl2.push(session.call('any.echo', [vals1[i]]).then( | ||
function (res) { | ||
test.log("Result [any.echo]:", res); | ||
}, | ||
function (err) { | ||
test.log("Error [any.echo]:", err.error, err.args, err.kwargs); | ||
} | ||
)); | ||
} | ||
|
||
autobahn.when.all(pl2).then(function () { | ||
test.log("All finished."); | ||
connection.close(); | ||
done.resolve(); | ||
}); | ||
}, | ||
function () { | ||
test.log("Registration failed!", arguments); | ||
} | ||
); | ||
}; | ||
|
||
connection.open(); | ||
|
||
return done.promise; | ||
}; | ||
|
||
|
||
exports.testBinaryCBOR = function (testcase) { | ||
|
||
testcase.expect(1); | ||
var test = new testutil.Testlog("test/test_binary_cbor.txt"); | ||
|
||
var dl = []; | ||
|
||
dl.push(run_test(test, new autobahn.serializer.CBORSerializer())); | ||
|
||
autobahn.when.all(dl).then(function () { | ||
var chk = test.check(); | ||
testcase.ok(!chk, chk); | ||
testcase.done(); | ||
}); | ||
}; | ||
|
||
exports.testBinaryMsgPack = function (testcase) { | ||
|
||
testcase.expect(1); | ||
var test = new testutil.Testlog("test/test_binary_msgpack.txt"); | ||
|
||
var dl = []; | ||
|
||
dl.push(run_test(test, new autobahn.serializer.MsgpackSerializer())); | ||
|
||
autobahn.when.all(dl).then(function () { | ||
var chk = test.check(); | ||
testcase.ok(!chk, chk); | ||
testcase.done(); | ||
}); | ||
}; | ||
|
||
exports.testBinaryJSON = function (testcase) { | ||
|
||
testcase.expect(1); | ||
var test = new testutil.Testlog("test/test_binary_json.txt"); | ||
|
||
var dl = []; | ||
|
||
dl.push(run_test(testcase, test, new autobahn.serializer.JSONSerializer())); | ||
|
||
autobahn.when.all(dl).then(function () { | ||
var chk = test.check(); | ||
testcase.ok(!chk, chk); | ||
testcase.done(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
0 "Connected" | ||
1 "All registered." | ||
2 "Serializer ID: cbor" | ||
3 "Result [any.echo]:" 1.7 | ||
4 "Result [any.echo]:" "hello" | ||
5 "Result [any.echo]:" [1,2,-3] | ||
6 "Result [any.echo]:" {"a":5,"b":"hello2"} | ||
7 "Result [any.echo]:" [-9007199254740991,9007199254740991] | ||
8 "Result [any.echo]:" null | ||
9 "Result [any.echo]:" [0,1,2,3,4,5,6,7] | ||
10 "Result [any.echo]:" [143,68,213,9,224,73,42,206,196,252,16,154,3,10,92,117,31,6,63,183,67,204,56,143,14,215,34,186,204,59,198,108] | ||
11 "Result [com.myapp.echo]:" 1.7 | ||
12 "Result [com.myapp.echo]:" "hello" | ||
13 "Result [com.myapp.echo]:" [1,2,-3] | ||
14 "Result [com.myapp.echo]:" {"a":5,"b":"hello2"} | ||
15 "Result [com.myapp.echo]:" [-9007199254740991,9007199254740991] | ||
16 "Result [com.myapp.echo]:" null | ||
17 "Result [com.myapp.echo]:" [0,1,2,3,4,5,6,7] | ||
18 "Result [com.myapp.echo]:" [143,68,213,9,224,73,42,206,196,252,16,154,3,10,92,117,31,6,63,183,67,204,56,143,14,215,34,186,204,59,198,108] | ||
19 "All finished." |