-
Notifications
You must be signed in to change notification settings - Fork 0
added nodejs tfwconnector which implements the basic connection to th… #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
06b2f2a
16caeca
fc110d3
fdc77e1
f60c1be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| 'use strict'; | ||
| let TFWConnector = require('./TWFConnector'); | ||
|
|
||
|
|
||
| class MessageSender{ | ||
|
|
||
| constructor(connector){ | ||
| if(connector instanceof TFWConnector){ | ||
| this.connector = connector; | ||
| }else{ | ||
| console.error("MessageSender require an instance of TFWConnector!"); | ||
| } | ||
| } | ||
|
|
||
| send(key, data){ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| let messageToSend = createTFWMessage(data); | ||
| this.connector.pushSocket.send(messageToSend); | ||
| } | ||
|
|
||
| sendMessage(message, name = "avataobot") { | ||
| let key = "message"; | ||
| let data = { | ||
| key: key, | ||
| data: { | ||
| originator: name, | ||
| timestamp: Date.now(), | ||
| message: message | ||
| } | ||
| }; | ||
| let messageToSend = createTFWMessage(data); | ||
| this.connector.pushSocket.send(messageToSend); | ||
| } | ||
| //TODO waiting for it to be implemented by core team | ||
| sendMessages(messages){ | ||
| if(Array.isArray(messages) && messages.length > 0){ | ||
| messages.forEach(message=>{ | ||
| console.log("Sending message: " + message); | ||
| setTimeout(this.sendMessage(message), 1000); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| sendToEventHandlers(message){ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| let key = "mirror"; | ||
| let data = { | ||
| key: key, | ||
| data: message | ||
| }; | ||
| let messageToSend = createTFWMessage(data); | ||
| this.connector.pushSocket.send(messageToSend); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| let createTFWMessage = (message) => { | ||
| return [Buffer.from(message.key), Buffer.from(JSON.stringify(message))] | ||
| }; | ||
|
|
||
| module.exports = MessageSender; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| 'use strict'; | ||
| const zmq = require('zeromq'), | ||
| subSock = zmq.socket('sub'), | ||
| pushSock = zmq.socket('push'); | ||
|
|
||
| const SUB_PORT = process.env.TFW_PUBLISHER_PORT || 7654, | ||
| PUSH_PORT = process.env.TFW_RECEIVER_PORT || 8765; | ||
|
|
||
| const SUB_ADDRESS = "tcp://localhost:" + SUB_PORT, | ||
| PUSH_ADDRESS = "tcp://localhost:" + PUSH_PORT; | ||
|
|
||
|
|
||
| class TFWConnector { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be named
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
|
||
| constructor() { | ||
| this.pushSocket = pushSock.connect(PUSH_ADDRESS); | ||
| this.subSocket = subSock.connect(SUB_ADDRESS); | ||
| console.log("Push socket connected to: " + PUSH_ADDRESS + "\n" + "Subscribe socket connected to: " + SUB_ADDRESS); | ||
| } | ||
|
|
||
| subscribe(key) { | ||
| this.subSocket.subscribe(key); | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| module.exports = TFWConnector; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| 'use strict'; | ||
| let MessageSender = require('./MessageSender'); | ||
|
|
||
|
|
||
| class Utilities{ | ||
|
|
||
| constructor(messageSender){ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use a |
||
| if(messageSender instanceof MessageSender){ | ||
| this.messageSender = messageSender; | ||
| }else{ | ||
| console.error("Utilities class needs an instance of MessageSender!"); | ||
| } | ||
| } | ||
|
|
||
| setView(viewType) { | ||
| let key = "dashboard"; | ||
| let data = { | ||
| key: key, | ||
| data: { | ||
| command: "layout", | ||
| value: viewType | ||
| } | ||
| }; | ||
| this.messageSender.send(key, data); | ||
| } | ||
|
|
||
| writeShell(command) { | ||
| let data = { | ||
| key: "shell", | ||
| data:{ | ||
| command: "write", | ||
| value: command | ||
| } | ||
| }; | ||
| this.messageSender.sendToEventHandlers(data); | ||
| } | ||
|
|
||
| stepNextState(){ | ||
| let key = ""; | ||
| let data = { | ||
| key: key, | ||
| data: { | ||
| event: "Stepping FSM to next state, from node event handler" | ||
| }, | ||
| trigger: "step_next" | ||
| }; | ||
| this.messageSender.send(key,data); | ||
| } | ||
|
|
||
| stepToState(state){ | ||
| let key = ""; | ||
| let data = { | ||
| key: key, | ||
| data: { | ||
| event: "Stepping FSM to next state, from node event handler" | ||
| }, | ||
| trigger: "to_"+state | ||
| }; | ||
| this.messageSender.send(key,data); | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| module.exports = Utilities; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
connectorshould be optional, and if the user does not provide one it should be initialised.