import Communication from 'foglet-core/src/network/communication/communication.js'
Communication
Communication is a facade to send messages to peers in a network using unicast or broadcast channels.
Constructor Summary
Public Constructor | ||
public |
|
Member Summary
Public Members | ||
public |
broadcast: * |
|
public |
network: * |
|
public |
unicast: * |
Private Members | ||
private |
|
|
private |
|
|
private |
_middlewares: * |
|
private |
|
Method Summary
Public Methods | ||
public |
onBroadcast(callback: MessageCallback): void Listen on broadcasted messages |
|
public |
onOnceBroadcast(callback: MessageCallback): void Listen to a broadcasted message, then remove the listener |
|
public |
onOnceUnicast(callback: MessageCallback): void Listen to an incoming unicasted message, and then remove the listener |
|
public |
onStreamBroadcast(callback: MessageCallback): void Listen on incoming unicasted streams |
|
public |
onStreamUnicast(callback: MessageCallback): void Listen on incoming unicasted streams |
|
public |
onUnicast(callback: MessageCallback): void Listen on incoming unicasted message |
|
public |
removeAllBroacastCallback(): void Remove all 'receive' broadcast callback |
|
public |
removeAllUnicastCallback(): void Remove all 'receive' unicast callback |
|
public |
sendBroadcast(message: Object, id: Object, isReady: Object): Object Send a message to all peers using broadcast, (optionnal: specify uniq message id and the id to wait, see: broadcast.js) |
|
public |
sendMulticast(ids: string[], message: Object): Promise |
|
public |
sendUnicast(id: string, message: Object): Promise Send a message to a specified peer |
|
public |
streamBroadcast(isReady: VersionVector): StreamRequest Begin the streaming of a message to all peers (using broadcast) |
|
public |
streamUnicast(id: string): StreamRequest Begin the streaming of a message to another peer (using unicast) |
|
public |
Register a middleware, with an optional priority |
Private Methods | ||
private |
_closeStream(id: string): void Close an open stream |
|
private |
_handleStreamMessage(id: string, message: Object, callback: function): void Handle an incoming stream message |
Public Constructors
public constructor() source
Public Members
public broadcast: * source
public network: * source
public unicast: * source
Private Members
private _activeStreams: * source
private _broadcastStreams: * source
private _middlewares: * source
private _unicastStreams: * source
Public Methods
public onBroadcast(callback: MessageCallback): void source
Listen on broadcasted messages
Params:
Name | Type | Attribute | Description |
callback | MessageCallback | Callback invoked with the message |
Return:
void |
public onOnceBroadcast(callback: MessageCallback): void source
Listen to a broadcasted message, then remove the listener
Params:
Name | Type | Attribute | Description |
callback | MessageCallback | Callback invoked with the message |
Return:
void |
public onOnceUnicast(callback: MessageCallback): void source
Listen to an incoming unicasted message, and then remove the listener
Params:
Name | Type | Attribute | Description |
callback | MessageCallback | Callback invoked with the message |
Return:
void |
public onStreamBroadcast(callback: MessageCallback): void source
Listen on incoming unicasted streams
Params:
Name | Type | Attribute | Description |
callback | MessageCallback | Callback invoked with a StreamMessage as message |
Return:
void |
Example:
const comm = getSomeCommunication();
comm.onStreamBroadcast((id, stream) => {
console.log('a peer with id = ', id, ' is streaming data to me');
stream.on('data', data => console.log(data));
stream.on('end', () => console.log('no more data available from the stream'));
});
public onStreamUnicast(callback: MessageCallback): void source
Listen on incoming unicasted streams
Params:
Name | Type | Attribute | Description |
callback | MessageCallback | Callback invoked with a StreamMessage as message |
Return:
void |
Example:
const comm = getSomeCommunication();
comm.onStreamUnicast((id, stream) => {
console.log('a peer with id = ', id, ' is streaming data to me');
stream.on('data', data => console.log(data));
stream.on('end', () => console.log('no more data available from the stream'));
});
public onUnicast(callback: MessageCallback): void source
Listen on incoming unicasted message
Params:
Name | Type | Attribute | Description |
callback | MessageCallback | Callback invoked with the message |
Return:
void |
public removeAllBroacastCallback(): void source
Remove all 'receive' broadcast callback
Return:
void |
public sendBroadcast(message: Object, id: Object, isReady: Object): Object source
Send a message to all peers using broadcast, (optionnal: specify uniq message id and the id to wait, see: broadcast.js)
Params:
Name | Type | Attribute | Description |
message | Object | Message to broadcast over the network |
|
id | Object |
|
{_e: <stringId>, _c: <Integer>} this uniquely represents the id of the operation |
isReady | Object |
|
{_e: <stringId>, _c: <Integer>} this uniquely represents the id of the operation that we must wait before delivering the message |
public sendMulticast(ids: string[], message: Object): Promise source
TODO:
- Complete tests of this function Send a message to multiple peers
public streamBroadcast(isReady: VersionVector): StreamRequest source
Begin the streaming of a message to all peers (using broadcast)
Params:
Name | Type | Attribute | Description |
isReady | VersionVector |
|
Id of the message to wait before this message is received |
Example:
const comm = getSomeCommunication();
const stream = comm.sendBroadcast();
stream.write('Hello');
stream.write(' world!');
stream.end();
public streamUnicast(id: string): StreamRequest source
Begin the streaming of a message to another peer (using unicast)
Params:
Name | Type | Attribute | Description |
id | string | Id of the peer |
Example:
const comm = getSomeCommunication();
const peerID = getSomePeerID();
const stream = comm.streamUnicast(peerID);
stream.write('Hello');
stream.write(' world!');
stream.end();
public use(middleware: Object, priority: Number): void source
Register a middleware, with an optional priority
Return:
void |
Private Methods
private _closeStream(id: string): void source
Close an open stream
Params:
Name | Type | Attribute | Description |
id | string | The ID of the stream to close |
Return:
void |