Spaces:
Running
Running
Upload peer_discovery.py
Browse files- peer_discovery.py +23 -14
peer_discovery.py
CHANGED
|
@@ -6,19 +6,27 @@ import time
|
|
| 6 |
import logging
|
| 7 |
import requests
|
| 8 |
from zeroconf import Zeroconf, ServiceInfo, ServiceBrowser
|
| 9 |
-
import random
|
| 10 |
|
| 11 |
# إعداد السجلات
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
-
# منفذ الخدمة
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
SERVICE = "_tasknode._tcp.local."
|
| 18 |
-
PEERS = set()
|
| 19 |
-
PEERS_INFO = {}
|
| 20 |
|
| 21 |
-
# قائمة السيرفرات المركزية
|
| 22 |
CENTRAL_REGISTRY_SERVERS = [
|
| 23 |
"https://cv4790811.regru.cloud",
|
| 24 |
"https://amaloffload.onrender.com",
|
|
@@ -58,21 +66,22 @@ def discover_lan_peers():
|
|
| 58 |
|
| 59 |
def main():
|
| 60 |
logger.info("🚀 بدء نظام اكتشاف الأقران...")
|
| 61 |
-
|
| 62 |
# تسجيل الخدمة المحلية
|
| 63 |
zeroconf = Zeroconf()
|
| 64 |
info = ServiceInfo(
|
| 65 |
-
SERVICE,
|
| 66 |
-
f"{socket.gethostname()}.{SERVICE}",
|
| 67 |
-
socket.inet_aton(get_local_ip()),
|
| 68 |
-
PORT,
|
| 69 |
-
properties={b'version': b'1.0'}
|
|
|
|
| 70 |
)
|
| 71 |
zeroconf.register_service(info)
|
| 72 |
-
|
| 73 |
# بدء اكتشاف الأقران
|
| 74 |
discover_lan_peers()
|
| 75 |
-
|
| 76 |
try:
|
| 77 |
while True:
|
| 78 |
logger.info(f"عدد الأقران المكتشفين: {len(PEERS)}")
|
|
|
|
| 6 |
import logging
|
| 7 |
import requests
|
| 8 |
from zeroconf import Zeroconf, ServiceInfo, ServiceBrowser
|
|
|
|
| 9 |
|
| 10 |
# إعداد السجلات
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
+
# منفذ الخدمة (بدءاً من 1000 مع زيادة متسلسلة)
|
| 15 |
+
current_port = 1000
|
| 16 |
+
|
| 17 |
+
def get_sequential_port():
|
| 18 |
+
global current_port
|
| 19 |
+
port = current_port
|
| 20 |
+
current_port += 1
|
| 21 |
+
if current_port > 9999:
|
| 22 |
+
current_port = 1000
|
| 23 |
+
return port
|
| 24 |
+
|
| 25 |
+
PORT = "7520" and int(os.getenv("CPU_PORT", get_sequential_port()))
|
| 26 |
SERVICE = "_tasknode._tcp.local."
|
| 27 |
+
PEERS = set()
|
| 28 |
+
PEERS_INFO = {}
|
| 29 |
|
|
|
|
| 30 |
CENTRAL_REGISTRY_SERVERS = [
|
| 31 |
"https://cv4790811.regru.cloud",
|
| 32 |
"https://amaloffload.onrender.com",
|
|
|
|
| 66 |
|
| 67 |
def main():
|
| 68 |
logger.info("🚀 بدء نظام اكتشاف الأقران...")
|
| 69 |
+
|
| 70 |
# تسجيل الخدمة المحلية
|
| 71 |
zeroconf = Zeroconf()
|
| 72 |
info = ServiceInfo(
|
| 73 |
+
type_=SERVICE,
|
| 74 |
+
name=f"{socket.gethostname()}.{SERVICE}",
|
| 75 |
+
addresses=[socket.inet_aton(get_local_ip())],
|
| 76 |
+
port=int(PORT),
|
| 77 |
+
properties={b'version': b'1.0'},
|
| 78 |
+
server=f"{socket.gethostname()}.local."
|
| 79 |
)
|
| 80 |
zeroconf.register_service(info)
|
| 81 |
+
|
| 82 |
# بدء اكتشاف الأقران
|
| 83 |
discover_lan_peers()
|
| 84 |
+
|
| 85 |
try:
|
| 86 |
while True:
|
| 87 |
logger.info(f"عدد الأقران المكتشفين: {len(PEERS)}")
|