bitmessage-js/lib/net/base.js

114 lines
3.0 KiB
JavaScript
Raw Normal View History

2015-02-01 20:34:47 +00:00
/**
2015-02-10 12:57:55 +00:00
* Networking base module. Defines base transport interface, useful for
* implementing new transports. End-users should import some transport
* instead in order to connect/accept connections to/from other nodes.
* **NOTE**: `BaseTransport` is exported as a module.
* @example var BaseTransport = require("bitmessage/net/base");
2015-02-01 20:34:47 +00:00
* @module bitmessage/net/base
*/
// TODO(Kagami): Write some sort of tutorial.
"use strict";
var inherits = require("inherits");
var EventEmitter = require("events").EventEmitter;
2015-02-02 15:30:05 +00:00
var PPromise = require("../platform").Promise;
2015-02-09 21:56:55 +00:00
var structs = require("../structs");
2015-02-01 20:34:47 +00:00
/**
2015-02-10 12:57:55 +00:00
* Base transport class. Allows to use single class for both client and
* server modes (as separate instances).
2015-02-01 20:34:47 +00:00
* @constructor
* @static
*/
function BaseTransport() {
BaseTransport.super_.call(this);
}
inherits(BaseTransport, EventEmitter);
/**
* Do the transport-specific bootstrap process and return promise that
2015-02-10 12:57:55 +00:00
* contains discovered nodes when fulfilled (both modes).
* NOTE: Do not use nodes received by this method in `addr` messages!
* This is meaningless.
* @return {Promise.<Array>}
2015-02-05 18:38:36 +00:00
* @abstract
2015-02-01 20:34:47 +00:00
*/
BaseTransport.prototype.bootstrap = function() {
2015-02-05 18:38:36 +00:00
return PPromise.reject(new Error("Not implemented"));
2015-02-01 20:34:47 +00:00
};
/**
2015-02-10 12:57:55 +00:00
* Connect to the transport-specific address. Enters client mode. Should
* emit `open` event after successful connect and `established` event
* after `verack` messages exchange.
2015-02-01 20:34:47 +00:00
* @abstract
*/
BaseTransport.prototype.connect = function() {
throw new Error("Not implemented");
};
2015-02-05 18:38:36 +00:00
/**
2015-02-10 12:57:55 +00:00
* Listen for the transport-specific incoming connections. Enters server
* mode. Should emit `connection` event with a transport instance for
* each new connection.
2015-02-05 18:38:36 +00:00
* @abstract
*/
BaseTransport.prototype.listen = function() {
throw new Error("Not implemented");
};
2015-02-01 20:34:47 +00:00
/**
* Send [message]{@link module:bitmessage/structs.message} over the
2015-02-10 12:57:55 +00:00
* wire (client mode only).
2015-02-09 14:38:10 +00:00
* @param {(Buffer|string)} msg - Encoded message or command string
2015-02-10 18:23:20 +00:00
* @param {Buffer=} payload - Message payload (used if the first
2015-02-09 14:38:10 +00:00
* argument is a string)
2015-02-01 20:34:47 +00:00
* @abstract
*/
BaseTransport.prototype.send = function() {
throw new Error("Not implemented");
};
/**
2015-02-05 18:38:36 +00:00
* Send [message]{@link module:bitmessage/structs.message} to all
2015-02-10 12:57:55 +00:00
* connected clients (server mode only).
2015-02-09 14:38:10 +00:00
* @param {(Buffer|string)} msg - Encoded message or command string
2015-02-10 18:23:20 +00:00
* @param {Buffer=} payload - Message payload (used if the first
2015-02-09 14:38:10 +00:00
* argument is a string)
2015-02-01 20:34:47 +00:00
* @abstract
*/
2015-02-05 18:38:36 +00:00
BaseTransport.prototype.broadcast = function() {
2015-02-01 20:34:47 +00:00
throw new Error("Not implemented");
};
/**
2015-02-10 12:57:55 +00:00
* Close connection(s) and/or stop listening (both modes).
2015-02-01 20:34:47 +00:00
* @abstract
*/
2015-02-05 18:38:36 +00:00
BaseTransport.prototype.close = function() {
2015-02-01 20:34:47 +00:00
throw new Error("Not implemented");
};
2015-02-10 12:57:55 +00:00
// Static private helpers.
2015-02-09 21:56:55 +00:00
BaseTransport._getmsg = function(args) {
if (typeof args[0] === "string") {
return structs.message.encode(args[0], args[1]);
} else {
return args[0];
}
};
// Unmap IPv4-mapped IPv6 address.
BaseTransport._unmap = function(addr) {
if (addr.slice(0, 7) === "::ffff:") {
return addr.slice(7);
} else {
return addr;
}
};
2015-02-06 09:47:40 +00:00
module.exports = BaseTransport;