add tool methods to generate port and check port availability

This commit is contained in:
Ziad-I
2024-06-11 19:03:12 +03:00
committed by Dion Moult
parent 76e6697bbc
commit 24662d75e4
6 changed files with 73 additions and 5 deletions
@@ -15,16 +15,25 @@
#
# You should have received a copy of the GNU General Public License
# along with BlenderBIM Add-on. If not, see <http://www.gnu.org/licenses/>.
import os
import bpy
import blenderbim.core.web as core
import blenderbim.tool as tool
class ConnectToWebsocketServer(bpy.types.Operator):
bl_idname = "bim.connect_to_websocket_server"
bl_label = "Connect to websocket server"
bl_description = "Connect to a websocket server"
bl_idname = "bim.connect_websocket_server"
bl_label = "Connect/Start websocket server"
bl_description = "Start/Connect to a Websocket server"
def execute(self, context):
port = context.scene.WebProperties.Webserver_port
if port == 0:
context.scene.WebProperties.Webserver_port = core.generate_port_number(tool.Web)
port = context.scene.WebProperties.Webserver_port
core.connect_websocket_server(tool.Web, port)
return {"FINISHED"}
@@ -31,4 +31,4 @@ from bpy.props import (
class WebProperties(PropertyGroup):
Webserver_port: StringProperty(name="Webserver Port")
Webserver_port: IntProperty(name="Webserver Port")
@@ -38,7 +38,7 @@ class BIM_PT_webui(Panel):
row.prop(props, "Webserver_port", text="Webserver Port")
row = layout.row()
row.operator("bim.connect_to_websocket_server")
row.operator("bim.connect_websocket_server")
row = layout.row()
row.operator("bim.kill_websocket_server")
+12
View File
@@ -1,2 +1,14 @@
def open_web_ui(web):
web.open_web_ui()
def generate_port_number(web):
return web.generate_port_number()
def connect_websocket_server(web, port):
# check if port already has a server listening to it
if web.is_port_available(port):
web.start_websocket_server(port)
web.connect_websocket_server(port)
@@ -60,3 +60,4 @@ from blenderbim.tool.type import Type
from blenderbim.tool.unit import Unit
from blenderbim.tool.search import Search
from blenderbim.tool.cost import Cost
from blenderbim.tool.web import Web
+46
View File
@@ -1,7 +1,26 @@
# BlenderBIM Add-on - OpenBIM Blender Add-on
# Copyright (C) 2021 Dion Moult <dion@thinkmoult.com>, 2022 Yassine Oualid <yassine@sigmadimensions.com>
#
# This file is part of BlenderBIM Add-on.
#
# BlenderBIM Add-on is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BlenderBIM Add-on is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BlenderBIM Add-on. If not, see <http://www.gnu.org/licenses/>.
import bpy
import json
import blenderbim.core.tool
from typing import Union, Literal
import socket
import os
import webbrowser
import asyncio
@@ -14,7 +33,34 @@ connected_sockets = set()
websocket_server: websockets.WebSocketServer
ws_thread_loop = asyncio.new_event_loop()
class Web(blenderbim.core.tool.Web):
@classmethod
def generate_port_number(cls):
print("Generating port number")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("localhost", 0)) # Bind to a free port
port = s.getsockname()[1] # get the bound port
print(f"Port number: {port}")
return port
@classmethod
def is_port_available(cls, port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# connect_ex returns 0 if the connection succeeds
# indicating the port is in use
# otherwise returns 10061 if no server is listening
# indicating the port is available for use
return s.connect_ex(("localhost", port)) == 10061
@classmethod
def start_websocket_server(cls, port):
pass
@classmethod
def connect_websocket_server(cls, port):
pass
@classmethod
def get_webui_data(cls):
data = {}