-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
664 additions
and
508 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* @author Deepkolos / https://github.com/deepkolos | ||
*/ | ||
|
||
export class WorkerPool { | ||
|
||
constructor( pool = 4 ) { | ||
|
||
this.pool = pool; | ||
this.queue = []; | ||
this.workers = []; | ||
this.workersResolve = []; | ||
this.workerStatus = 0; | ||
|
||
} | ||
|
||
_initWorker( workerId ) { | ||
|
||
if ( ! this.workers[ workerId ] ) { | ||
|
||
const worker = this.workerCreator(); | ||
worker.addEventListener( 'message', this._onMessage.bind( this, workerId ) ); | ||
this.workers[ workerId ] = worker; | ||
|
||
} | ||
|
||
} | ||
|
||
_getIdleWorker() { | ||
|
||
for ( let i = 0; i < this.pool; i ++ ) | ||
if ( ! ( this.workerStatus & ( 1 << i ) ) ) return i; | ||
|
||
return - 1; | ||
|
||
} | ||
|
||
_onMessage( workerId, msg ) { | ||
|
||
const resolve = this.workersResolve[ workerId ]; | ||
resolve && resolve( msg ); | ||
|
||
if ( this.queue.length ) { | ||
|
||
const { resolve, msg, transfer } = this.queue.shift(); | ||
this.workersResolve[ workerId ] = resolve; | ||
this.workers[ workerId ].postMessage( msg, transfer ); | ||
|
||
} else { | ||
|
||
this.workerStatus ^= 1 << workerId; | ||
|
||
} | ||
|
||
} | ||
|
||
setWorkerCreator( workerCreator ) { | ||
|
||
this.workerCreator = workerCreator; | ||
|
||
} | ||
|
||
setWorkerLimit( pool ) { | ||
|
||
this.pool = pool; | ||
|
||
} | ||
|
||
postMessage( msg, transfer ) { | ||
|
||
return new Promise( ( resolve ) => { | ||
|
||
const workerId = this._getIdleWorker(); | ||
|
||
if ( workerId !== - 1 ) { | ||
|
||
this._initWorker( workerId ); | ||
this.workerStatus |= 1 << workerId; | ||
this.workersResolve[ workerId ] = resolve; | ||
this.workers[ workerId ].postMessage( msg, transfer ); | ||
|
||
} else { | ||
|
||
this.queue.push( { resolve, msg, transfer } ); | ||
|
||
} | ||
|
||
} ); | ||
|
||
} | ||
|
||
dispose() { | ||
|
||
this.workers.forEach( ( worker ) => worker.terminate() ); | ||
this.workersResolve.length = 0; | ||
this.workers.length = 0; | ||
this.queue.length = 0; | ||
this.workerStatus = 0; | ||
|
||
} | ||
|
||
} |
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
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,48 +1,80 @@ | ||
#!/usr/bin/env python3 | ||
# Aug 2024 https://nimadez.github.io/ | ||
# | ||
# Websockets Server | ||
# This script can be used to connect with machine learning models | ||
# $ python3 ws-connect.py | ||
|
||
|
||
import os, sys, json, random, time | ||
import asyncio | ||
import os, sys, json, random, time, asyncio | ||
|
||
sys.path.insert(1, os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + '/libs') | ||
from websockets.asyncio.server import serve | ||
from websockets.server import serve | ||
|
||
if __import__('platform').system == 'Windows': # fix [WinError 10054] | ||
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) | ||
|
||
|
||
PORT = 8014 | ||
TOTAL = 100 | ||
CLEAR = False # clear scene or add voxel | ||
CLEAR = True # clear scene or add a voxel | ||
POST = True # test in send mode? | ||
|
||
|
||
def generator(): | ||
data = [] | ||
for x in range(20): | ||
for y in range(20): | ||
for z in range(20): | ||
if random.randint(0,9) < 5: | ||
hex = "#%06X" % random.randint(0, 0xFFFFFF) | ||
data.append({ | ||
"position": { "x": x, "y": y, "z": z }, | ||
"color": hex, #.upper() | ||
"visible": True | ||
}) | ||
return data | ||
|
||
|
||
class Server(): | ||
def __init__(self): | ||
pass | ||
|
||
|
||
async def handler(self, ws): | ||
async for msg in ws: | ||
msg = json.loads(msg) | ||
if not POST: | ||
match msg['key']: | ||
case 'init': | ||
print("Connection established.") | ||
case 'get': | ||
print(f"Received: {str(msg['voxels'])[:40]}...") | ||
else: | ||
voxel_data = generator() | ||
payload = { "voxels": voxel_data, "is_clear": CLEAR } | ||
await ws.send(json.dumps(payload)) | ||
print("Sent") | ||
time.sleep(0.2) | ||
|
||
time.sleep(0.1) | ||
|
||
async def handler(ws): | ||
while True: | ||
data = [] | ||
for i in range(TOTAL): | ||
hex = "#%06x" % random.randint(0, 0xFFFFFF) | ||
data.append({ | ||
"position": { "x": random.randint(-20,20), "y": random.randint(0,20), "z": random.randint(-20,20) }, | ||
"color": hex.upper(), | ||
"visible": True | ||
}) | ||
|
||
payload = { "voxels": data, "is_clear": CLEAR } | ||
async def main(self): | ||
try: | ||
async with serve(self.handler, "localhost", PORT, max_queue=1, max_size=2 ** 25): | ||
print(f"Running websockets server at ws://localhost:{PORT}") | ||
await asyncio.get_running_loop().create_future() | ||
except Exception as err: | ||
print(err) | ||
|
||
await ws.send(json.dumps(payload)) | ||
print('sent') | ||
message = await ws.recv() | ||
print('receive', message[:40], '...') | ||
time.sleep(0.1) | ||
|
||
def start(self): | ||
try: | ||
asyncio.run(self.main()) | ||
except KeyboardInterrupt: | ||
print('Shutdown.') | ||
finally: | ||
pass | ||
|
||
async def main(): | ||
async with serve(handler, "", PORT): | ||
print(f"Websockets server is ready on port {PORT}") | ||
await asyncio.get_running_loop().create_future() | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) | ||
Server().start() |
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,48 @@ | ||
#!/usr/bin/env node | ||
// Aug 2024 https://nimadez.github.io/ | ||
// Voxel Builder Server | ||
|
||
const http = require('http'); | ||
const url = require('url'); | ||
const fs = require('fs'); | ||
|
||
const PORT = 8011; | ||
|
||
http.createServer((req, res) => { | ||
const parsedUrl = url.parse(req.url, true); | ||
let filePath = `.${parsedUrl.pathname}`; | ||
|
||
if (parsedUrl.pathname == '/') | ||
filePath = './index.html'; | ||
|
||
fs.readFile(filePath, (err, data) => { | ||
if (err) { | ||
console.log(`GET ${filePath} 404 NOT FOUND`) | ||
res.writeHead(404, { 'Content-Type': 'text/html' }); | ||
return res.end("404 Not Found"); | ||
} | ||
|
||
console.log(`GET ${filePath} 200 OK`) | ||
res.writeHead(200, { 'Content-Type': getContentType(filePath) }); | ||
res.write(data); | ||
return res.end(); | ||
}); | ||
}).listen(PORT, () => { | ||
console.log('Voxel Builder') | ||
console.log(`Server running at http://localhost:${PORT}`); | ||
}); | ||
|
||
function getContentType(filePath) { | ||
const extname = String(filePath).split('.').pop().toLowerCase(); | ||
const mimeTypes = { | ||
'html': 'text/html', | ||
'js': 'text/javascript', | ||
'css': 'text/css', | ||
'json': 'application/json', | ||
'png': 'image/png', | ||
'jpg': 'image/jpg', | ||
'jpeg': 'image/jpeg', | ||
'gif': 'image/gif' | ||
}; | ||
return mimeTypes[extname] || 'application/octet-stream'; | ||
} |
Oops, something went wrong.