-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Python TCP server and Node.js TCP client for IPC (wip)
- Loading branch information
Showing
12 changed files
with
575 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import socket | ||
import os | ||
import json | ||
from os.path import join, dirname | ||
from dotenv import load_dotenv | ||
import spacy | ||
|
||
dotenv_path = join(dirname(__file__), '../../.env') | ||
load_dotenv(dotenv_path) | ||
|
||
nlp = spacy.load('en_core_web_trf', disable=['tagger', 'parser', 'attribute_ruler', 'lemmatizer']) | ||
|
||
ws_server_host = os.environ.get('LEON_PY_WS_SERVER_HOST', '0.0.0.0') | ||
ws_server_port = os.environ.get('LEON_PY_WS_SERVER_PORT', 1342) | ||
|
||
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
tcp_socket.bind((ws_server_host, int(ws_server_port))) | ||
tcp_socket.listen() | ||
|
||
def extract_spacy_entities(utterance): | ||
doc = nlp(utterance) | ||
|
||
for ent in doc.ents: | ||
print(ent.text, ent.label_) | ||
|
||
while True: | ||
print('Waiting for connection...') | ||
connection, addr = tcp_socket.accept() | ||
|
||
try: | ||
print(f'Client connected: {addr}') | ||
|
||
while True: | ||
data = connection.recv(1024) | ||
data_dict = json.loads(data) | ||
|
||
print('data', data) | ||
|
||
if data_dict['topic'] == 'get-spacy-entities': | ||
extract_spacy_entities(data_dict['data']) | ||
|
||
print(f'Received data: {data_dict}') | ||
|
||
if not data: | ||
break | ||
|
||
connection.sendall(data) | ||
|
||
finally: | ||
connection.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import socketio | ||
import eventlet | ||
import os | ||
from os.path import join, dirname | ||
from dotenv import load_dotenv | ||
import spacy | ||
|
||
dotenv_path = join(dirname(__file__), '../../.env') | ||
load_dotenv(dotenv_path) | ||
|
||
nlp = spacy.load('en_core_web_trf', disable=['tagger', 'parser', 'attribute_ruler', 'lemmatizer']) | ||
|
||
sio = socketio.Server(async_mode='eventlet', cors_allowed_origins="*", logger=False, engineio_logger=False) | ||
# sio = socketio.Server(async_mode='eventlet', cors_allowed_origins="*") | ||
app = socketio.WSGIApp(sio) | ||
|
||
ws_server_host = os.environ.get('LEON_PY_WS_SERVER_HOST', '0.0.0.0') | ||
ws_server_port = os.environ.get('LEON_PY_WS_SERVER_PORT', 1342) | ||
|
||
@sio.event | ||
def connect(sid, env, auth): | ||
print('Client connected ', sid) | ||
|
||
@sio.event | ||
def disconnect(sid): | ||
print('Client disconnected', sid) | ||
|
||
@sio.event | ||
def extract_entities(sid, utterance): | ||
print('DO YOUR JOOOB') | ||
doc = nlp(utterance) | ||
|
||
for ent in doc.ents: | ||
print(ent.text, ent.label_) | ||
|
||
sio.emit('entities_extracted', {'data': 'foobar'}, room=sid) | ||
|
||
try: | ||
print('Python WebSocket server is running on ' + ws_server_host + ':' + ws_server_port) | ||
eventlet.wsgi.server(eventlet.listen((ws_server_host, int(ws_server_port))), app) | ||
except: | ||
print('Python WebSocket server failed to run. Please check that the ' + ws_server_port + ' port is free on ' + ws_server_host) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { io } from 'socket.io-client' | ||
|
||
export default class PyWsClient { | ||
constructor (serverUrl) { | ||
this.serverUrl = serverUrl | ||
this.pySocket = io(this.serverUrl) | ||
|
||
this.pySocket.on('connect', () => { | ||
console.log('CONNECTED') | ||
}) | ||
|
||
this.pySocket.on('entities_extracted', (data) => { | ||
console.log('entities_extracted data', data) | ||
}) | ||
} | ||
|
||
extractEntities (utterance) { | ||
return new Promise((resolve) => { | ||
console.log('EMMIIIT') | ||
this.pySocket.emit( | ||
'extract_entities', | ||
utterance, | ||
(conf) => { | ||
resolve(conf) | ||
} | ||
) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import Net from 'net' | ||
import { EventEmitter } from 'events' | ||
|
||
import log from '@/helpers/log' | ||
|
||
export default class TcpClient { | ||
constructor (host, port) { | ||
this.tcpSocket = new Net.Socket() | ||
this._ee = new EventEmitter() | ||
this._status = this.tcpSocket.readyState | ||
|
||
log.title('TCP Client') | ||
log.success('New instance') | ||
|
||
this.tcpSocket.connect({ host, port }, () => { | ||
log.title('TCP Client') | ||
log.success(`Connected to TCP server tcp://${host}:${port}`) | ||
}) | ||
|
||
this.tcpSocket.on('data', (chunk) => { | ||
log.title('TCP Client') | ||
|
||
const data = JSON.parse(chunk) | ||
|
||
this._ee.emit(data.topic, data.data) | ||
|
||
console.log('RECEIIIIVED', chunk.toString()) | ||
}) | ||
|
||
this.tcpSocket.on('error', (err) => { | ||
log.title('TCP Client') | ||
log.error(err) | ||
}) | ||
|
||
this.tcpSocket.on('end', () => { | ||
log.title('TCP Client') | ||
log.success('Disconnected from TCP server') | ||
}) | ||
} | ||
|
||
get status () { | ||
return this._status | ||
} | ||
|
||
get ee () { | ||
return this._ee | ||
} | ||
|
||
emit (topic, data) { | ||
const obj = { | ||
topic, | ||
data | ||
} | ||
|
||
this.tcpSocket.write(JSON.stringify(obj)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import dotenv from 'dotenv' | ||
// import { command } from 'execa' | ||
|
||
import server from '@/core/http-server/server' | ||
|
||
(async () => { | ||
dotenv.config() | ||
|
||
/* command('pipenv run python bridges/python/ws-server.py', { | ||
shell: true | ||
}) */ | ||
await server.init() | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Include Nodejs' net module. | ||
const Net = require('net'); | ||
// The port number and hostname of the server. | ||
const port = 1342; | ||
const host = '0.0.0.0'; | ||
|
||
// Create a new TCP client. | ||
const client = new Net.Socket(); | ||
// Send a connection request to the server. | ||
client.connect({ port: port, host: host }, function() { | ||
// If there is no error, the server has accepted the request and created a new | ||
// socket dedicated to us. | ||
console.log('TCP connection established with the server.'); | ||
|
||
// The client can now send data to the server by writing to its socket. | ||
client.write('Hello, server.'); | ||
}); | ||
|
||
// The client can also receive data from the server by reading from its socket. | ||
client.on('data', function(chunk) { | ||
console.log(`Data received from the server: ${chunk.toString()}.`); | ||
|
||
// Request an end to the connection after the data has been received. | ||
client.end(); | ||
}); | ||
|
||
client.on('end', function() { | ||
console.log('Requested an end to the TCP connection'); | ||
}); |