Skip to content

Commit

Permalink
add feedback for msg
Browse files Browse the repository at this point in the history
  • Loading branch information
tubleronchik committed Jul 25, 2024
1 parent 0742844 commit 2bd32f0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
25 changes: 25 additions & 0 deletions src/feedbackManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const protocol = "/feedback"

export class FeedbackManager {
constructor(logger, wsManager, messageHandler) {
this.logger = logger;
this.wsManager = wsManager;
this.messageHandler = messageHandler;
}

sendFeedbackMessage(response, ws) {
this.logger.INFO(response, "feedback handler")
const msg = this.#formatMessage(response, protocol)
for (const client of this.wsManager.wsServer.clients) {

Check failure on line 13 in src/feedbackManager.js

View workflow job for this annotation

GitHub Actions / Linting

iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations
if (ws == client) {

Check failure on line 14 in src/feedbackManager.js

View workflow job for this annotation

GitHub Actions / Linting

Expected '===' and instead saw '=='
client.send(JSON.stringify(msg))
}
}
return
}

Check failure on line 20 in src/feedbackManager.js

View workflow job for this annotation

GitHub Actions / Linting

Expected 'this' to be used by class private method #formatMessage

Check failure on line 20 in src/feedbackManager.js

View workflow job for this annotation

GitHub Actions / Linting

'protocol' is already declared in the upper scope on line 1 column 7
#formatMessage(response, protocol) {
return { "data": { "feedback": response } , "protocol": protocol}
}

}
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { WebSocketManager } from './wsManager.js';
import { MessageHandler } from './messageHandler.js';
import { createDir4SavedData } from '../utils/saveData.js';
import { Logger } from '../utils/logger.js';
import { FeedbackManager } from './feedbackManager.js';

async function run() {
const logger = new Logger();
Expand All @@ -13,9 +14,11 @@ async function run() {

const node = await libp2pManager.createNode();
const messageHandler = new MessageHandler(libp2pManager, logger);
const wsManager = new WebSocketManager(messageHandler, logger);
const wsManager = new WebSocketManager(messageHandler, logger, node);
const relayAddr = multiaddr(libp2pManager.realayAddress)
wsManager.onConnectionManager(node);
const feedbackManager = new FeedbackManager(logger, wsManager, messageHandler)
libp2pManager.setFeedbackManager(feedbackManager)
wsManager.onConnectionManager();

logger.INFO(`Node started with id ${node.peerId.toString()}`);
const conn = await node.dial(relayAddr);
Expand Down
13 changes: 12 additions & 1 deletion src/libp2pManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export class Libp2pManager {
this.configuration = new ConfigurationManager(logger);
this.realayAddress = '/dns4/libp2p-relay-1.robonomics.network/tcp/443/wss/p2p/12D3KooWEMFXXvpZUjAuj1eKR11HuzZTCQ5HmYG9MNPtsnqPSERD';
this.logger = logger;
this.feedbackManager = undefined
}

setFeedbackManager(feedbackManager) {
this.feedbackManager = feedbackManager
}

/**
Expand Down Expand Up @@ -174,16 +179,22 @@ export class Libp2pManager {
* @param data Message to send.
* @param protocol Name of the Libp2p protocol.
*/
async sendMsg(connection, data, protocol) {
async sendMsg(connection, data, protocol, ws) {
try {
const isRelay = this.#isConnectionNotRelay(connection.remoteAddr.toString());
if (!isRelay) {
const response = await this.#request(connection, protocol, data);
this.logger.INFO(`Sending message to ${connection.remoteAddr.toString()}`);
this.logger.INFO(response, ' got response from sendMsg');
if (!(typeof ws === "undefined")) {
this.feedbackManager.sendFeedbackMessage(response, ws)
}
}
} catch (error) {
this.logger.ERROR(error.message, 'sendMsg');
if (ws) {
this.feedbackManager.sendFeedbackMessage(error.message, ws)
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/messageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class MessageHandler {
* @param msg The message from the ws client.
* @param node Instance of libp2p node
*/
onWSMessage(msg, node) {
onWSMessage(msg, node, ws) {
this.logger.INFO('Sending msg from ws to libp2p...');
const { protocol } = msg;
const { serverPeerId } = msg;
Expand All @@ -127,13 +127,13 @@ export class MessageHandler {
this.libp2pManager.sendMsg(connection, msg.data, protocol);
} else {
this.libp2pManager.connect2NodeViaRelay(node, serverPeerId).then((connection) => {
this.libp2pManager.sendMsg(connection, msg.data, protocol);
this.libp2pManager.sendMsg(connection, msg.data, protocol, ws)
}).catch((error) => { this.logger.ERROR(error, "onWSMessage couldn't resolve promise"); });
}
} else {
node.getConnections().forEach((connection) => {
this.libp2pManager.sendMsg(connection, msg.data, protocol);
this.libp2pManager.sendMsg(connection, msg.data, protocol, ws);
});
}
}
}
}
15 changes: 8 additions & 7 deletions src/wsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import { WebSocketServer } from 'ws';
* @param logger Instance of the Logger class.
*/
export class WebSocketManager {
constructor(messageHandler, logger) {
constructor(messageHandler, logger, node) {
this.wsServer = this.#createWebsocketServer();
this.messageHandler = messageHandler;
this.clientIdCounter = 1;
this.clients = new Map();
this.logger = logger;
this.node = node;
}

/**
Expand Down Expand Up @@ -59,26 +60,26 @@ export class WebSocketManager {
* @param ws Instance of ws connection.
* @param newInfo Info to add.
*/
onConnectionManager(node) {
onConnectionManager() {
this.wsServer.on('connection', (ws, req) => {
this.#setClient(ws, req);
const multiAddresses = node.getMultiaddrs().map((addr) => addr.toString());

const multiAddresses = this.node.getMultiaddrs().map((addr) => addr.toString());
this.messageHandler.sendMsg2WSClients(
this.wsServer,
this.clients,
{ peerId: node.peerId.toString(), multiAddresses },
{ peerId: this.node.peerId.toString(), multiAddresses },
);
ws.on('error', console.error);
ws.on('message', (data) => {
try {
const msg = JSON.parse(data);
this.logger.INFO('Received ws message');

if ('protocols_to_listen' in msg) {
this.updateClientInfo(ws, { protocolsToListen: msg.protocols_to_listen });
this.messageHandler.onWSInitialMessage(msg, this.wsServer, this.clients, node);
this.messageHandler.onWSInitialMessage(msg, this.wsServer, this.clients, this.node);
} else {
this.messageHandler.onWSMessage(msg, node);
this.messageHandler.onWSMessage(msg, this.node, ws);
}
} catch (error) {
this.logger.ERROR(error, 'onConnectionManager');
Expand Down

0 comments on commit 2bd32f0

Please sign in to comment.