93 lines
3.6 KiB
Markdown
Raw Normal View History

2015-09-15 16:50:58 +01:00
abstract-connection
===================
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
> A test suite and interface you can use to implement a connection. A connection is understood as something that offers a dial+listen interface
The primary goal of this module is to enable developers to pick and swap their Record Store module as they see fit for their libp2p installation, without having to go through shims or compatibility issues. This module and test suite were heavily inspired by abstract-blob-store and abstract-stream-muxer.
Publishing a test suite as a module lets multiple modules all ensure compatibility since they use the same test suite.
2015-09-15 19:36:44 +01:00
The purpose of this abstraction is not to reinvent any wheels when it comes to dialing and listening to connections, instead, it tries to uniform several transports through a shimmed interface.
2015-09-15 16:50:58 +01:00
The API is presented with both Node.js and Go primitives, however, there is not actual limitations for it to be extended for any other language, pushing forward the cross compatibility and interop through diferent stacks.
# Modules that implement the interface
- [node-libp2p-tcp](https://github.com/diasdavid/node-libp2p-tcp)
2015-09-15 18:23:09 +01:00
note: for any new given implementation that adds one more option to the multiaddr space that was not expected yet, the respective multiaddr should be added to the PeerInfo objects available on the tests, so that implementation can be properly tested.
2015-09-15 16:50:58 +01:00
# Badge
Include this badge in your readme if you make a module that is compatible with the abstract-connection API. You can validate this by running the tests.
2015-09-15 16:51:41 +01:00
![](https://raw.githubusercontent.com/diasdavid/abstract-connection/master/img/badge.png)
2015-09-15 16:50:58 +01:00
# How to use the battery of tests
## Node.js
```
var tape = require('tape')
var tests = require('abstract-connection/tests')
var YourConnectionHandler = require('../src')
var common = {
setup: function (t, cb) {
cb(null, YourConnectionHandler)
},
teardown: function (t, cb) {
cb()
}
}
tests(tape, common)
```
## Go
> WIP
# API
A valid (read: that follows this abstraction) connection, must implement the following API.
2015-09-15 18:23:09 +01:00
### Dialing to another Peer
2015-09-15 19:36:44 +01:00
- `Node.js` var stream = conn.dial(multiaddr, [options])
2015-09-15 18:23:09 +01:00
This method dials a connection to the Peer referenced by the peerInfo object.
2015-09-15 19:36:44 +01:00
multiaddr must be of the type [`multiaddr`](http://npmjs.org/multiaddr).
2015-09-15 18:23:09 +01:00
2015-09-16 11:52:42 +01:00
`stream` must implements the [abstract-transport](https://github.com/diasdavid/abstract-transport) interface.
2015-09-15 18:23:09 +01:00
2015-09-15 19:36:44 +01:00
`[options]` are not mandatory fields for all the implementations that might be passed for certain implementations for them to work (e.g. a Signalling Server for a WebRTC transport/connection implementation)
2015-09-15 18:23:09 +01:00
### Listening for incoming connections from other Peers
2015-09-15 19:36:44 +01:00
- `Node.js` var listener = conn.createListener(options, function (stream) {})
2015-09-15 18:23:09 +01:00
This method waits and listens for incoming connections by other peers.
2015-09-15 16:50:58 +01:00
2015-09-16 11:52:42 +01:00
`stream` must be a stream that implements the [abstract-transport](https://github.com/diasdavid/abstract-transport) interface.
2015-09-15 16:50:58 +01:00
2015-09-15 18:23:09 +01:00
Options are the properties this listener must have access in order to properly listen on a given transport/socket
2015-09-15 19:36:44 +01:00
### Start listening
- `Node.js` listener.listen(options, [callback])
This method opens the listener to start listening for incoming connections
### Close an active listener
- `Node.js` listener.close([callback])
This method closes the listener so that no more connections can be open
`callback` is function that gets called when the listener is closed. It is optional