A peer to peer network framework by TrustNote Foundation.
- Write every events by thread plugin.
- Transmit messages between peers via Protocol Buffer.
- Peers communicate each other with concordant message structure.
Node( client, server )
ThreadBootstrap
threads
threads-available
CThreadHeartbeat
CThreadNode
CThreadCatchup
CThread...
threads-enabled
Driver
Adapters
Web Socket
...
Deliver
Request, Response
CP2pMessage
CP2pPackage
Protocol Buffer
$ npm install trustp2p
trustp2p will try to load all threads in directory threads-enabled. So, the first thing is to create 2 sub-directories in your project directory.
$ mkdir threads-available
$ mkdir threads-enabled
And, then I strongly recommend that you copy thread-available/CThreadHeartbeat.js to ./thread-available/CThreadHeartbeat.js and make it works. Following these steps:
$ cd threads-enabled
$ ln -s ../threads-available/CThreadHeartbeat.js
As you can see in thread-available/CThreadHeartbeat.js, go ahead and try to write your own thread now!
const _trustp2p = require( 'trustp2p' );
/**
* constants
* @type {{}}
*/
const _oOptions = {
cwd : __dirname
};
/**
* create server
*/
_trustp2p.createServer( _oOptions );
const _trustp2p = require( 'trustp2p' );
/**
* constants
* @type {{}}
*/
const _oOptions = {
cwd : __dirname
};
/**
* create server
*/
_trustp2p.createClient( _oOptions );
Register events you like here, trustp2p will transit all these events to you.
return {
[ CP2pPackage.PACKAGE_PING ] :
{
'ping' : this._handleMessagePing, // ping by server
}
}
trustp2p will call this method at the moment while the thread was loaded by thread-bootstrap.
trustp2p will call this method at the moment while the thread was unloaded by thread-bootstrap.
If you implement this method in your thread, trustp2p will thansit CP2pDriver.EVENT_CONNECTION events to you while new clients connected in.
If you implement this method in your thread, trustp2p will thansit CP2pDriver.EVENT_OPEN events to you while your thread connected to servers.
If you implement this method in your thread, trustp2p will thansit CP2pDriver.EVENT_CLOSE events to you while the connection specified by oSocket was closed.
And, if you want to known which side we're on, please check the variable oSocket.bInbound.
if ( oSocket.bInbound )
{
// I am a server, and I accept a client connected in
// Now, the connection for this client was closed
}
If you implement this method in your thread, trustp2p will thansit CP2pDriver.EVENT_ERROR events to you while the connection specified by oSocket occurred an error.
And, if you want to known which side we're on, please check the variable oSocket.bInbound.
if ( oSocket.bInbound )
{
// I am a server, and I accept a client connected in
// Now, the connection for this client occurred an error
}