From 2ee782090d62cf09e1bc00a2ba50571cbad48678 Mon Sep 17 00:00:00 2001 From: Kagami Hiiragi Date: Sun, 1 Feb 2015 23:34:47 +0300 Subject: [PATCH] More net stub --- README.md | 2 +- lib/net/base.js | 78 +++++++++++++++++++++++++++++++++++++++ lib/net/index.js | 0 lib/net/tcp.browser.js | 1 + lib/net/tcp.js | 22 +++++++++++ lib/net/webrtc.browser.js | 1 + lib/net/webrtc.js | 1 + package.json | 4 +- 8 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 lib/net/base.js delete mode 100644 lib/net/index.js diff --git a/README.md b/README.md index cd66e07..1f8cf27 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ API documentation is available [here](https://bitchan.github.io/bitmessage/docs/ - [x] High-level classes - [x] Address - [x] UserAgent -- [ ] Network +- [ ] Network transports - [ ] TCP (Node.js only) - [ ] WebSocket - [ ] WebRTC diff --git a/lib/net/base.js b/lib/net/base.js new file mode 100644 index 0000000..19cb5e1 --- /dev/null +++ b/lib/net/base.js @@ -0,0 +1,78 @@ +/** + * Networking base module. You should import some transport instead in + * order to connect/accept connections to/from other nodes. + * @module bitmessage/net/base + */ +// TODO(Kagami): Write some sort of tutorial. + +"use strict"; + +var inherits = require("inherits"); +var EventEmitter = require("events").EventEmitter; +var PPromise = require("./platform").Promise; + +/** + * Network transport base class. + * @constructor + * @static + */ +function BaseTransport() { + BaseTransport.super_.call(this); +} + +inherits(BaseTransport, EventEmitter); + +/** + * Seed nodes for this transport. Consist of `[host, port]` pairs. + * Note that this nodes shouldn't be advertised via `addr` messages. + * @const {Array.} + */ +BaseTransport.prototype.SEED_NODES = []; + +/** + * Do the transport-specific bootstrap process and return promise that + * contains discovered nodes when fulfilled. + * @return {Promise.} + */ +BaseTransport.prototype.bootstrap = function() { + return PPromise.resolve([].concat(this.SEED_NODES)); +}; + +/** + * Connect to the transport-specific address. + * Should emit `open` event when the connection is established. + * @abstract + */ +BaseTransport.prototype.connect = function() { + throw new Error("Not implemented"); +}; + +/** + * Send [message]{@link module:bitmessage/structs.message} over the + * wire. + * @param {Buffer} msg - Encoded message + * @abstract + */ +BaseTransport.prototype.send = function() { + throw new Error("Not implemented"); +}; + +/** + * Close connection. + * @abstract + */ +BaseTransport.prototype.close = function() { + throw new Error("Not implemented"); +}; + +/** + * Listen for the transport-specific incoming connections. + * Should emit `connection` event with a transport instance for each new + * connection. + * @abstract + */ +BaseTransport.prototype.listen = function() { + throw new Error("Not implemented"); +}; + +exports.BaseTransport = BaseTransport; diff --git a/lib/net/index.js b/lib/net/index.js deleted file mode 100644 index e69de29..0000000 diff --git a/lib/net/tcp.browser.js b/lib/net/tcp.browser.js index e69de29..3e631c2 100644 --- a/lib/net/tcp.browser.js +++ b/lib/net/tcp.browser.js @@ -0,0 +1 @@ +throw new Error("Not implemented"); diff --git a/lib/net/tcp.js b/lib/net/tcp.js index e69de29..c6dd9f7 100644 --- a/lib/net/tcp.js +++ b/lib/net/tcp.js @@ -0,0 +1,22 @@ +/** + * TCP transport for Node. Should be compatible with PyBitmessage. + * @module bitmessage/net/tcp + */ + +"use strict"; + +var inherits = require("inherits"); +var BaseTransport = require("./base").BaseTransport; + +/** + * TCP transport constructor. + * @constructor + * @static + */ +function Transport() { + Transport.super_.call(this); +} + +inherits(Transport, BaseTransport); + +exports.Transport = Transport; diff --git a/lib/net/webrtc.browser.js b/lib/net/webrtc.browser.js index e69de29..3e631c2 100644 --- a/lib/net/webrtc.browser.js +++ b/lib/net/webrtc.browser.js @@ -0,0 +1 @@ +throw new Error("Not implemented"); diff --git a/lib/net/webrtc.js b/lib/net/webrtc.js index e69de29..3e631c2 100644 --- a/lib/net/webrtc.js +++ b/lib/net/webrtc.js @@ -0,0 +1 @@ +throw new Error("Not implemented"); diff --git a/package.json b/package.json index deca5d1..b49604b 100644 --- a/package.json +++ b/package.json @@ -56,10 +56,12 @@ "eccrypto": "^0.9.5", "es6-promise": "^2.0.1", "hash.js": "^1.0.2", + "inherits": "^2.0.1", "nan": "^1.4.1", "object-assign": "^2.0.0", "sha.js": "^2.3.1", - "webworkify": "^1.0.1" + "webworkify": "^1.0.1", + "ws": "^0.7.1" }, "optionalDependencies": { "bignum": "^0.9.0"