Skip to content

Commit

Permalink
chore: extenTion to extenSion
Browse files Browse the repository at this point in the history
  • Loading branch information
soumt-r committed Apr 3, 2023
1 parent c30f45b commit bad0b77
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 182 deletions.
22 changes: 11 additions & 11 deletions OSC-SRTC.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from modules.SRTC_Utils import *
from modules.SRTC_Recognizer import SRecognizer
from modules.SRTC_Translator import STranslator
from modules.SRTC_Extention import Extention_MainServer
from modules.SRTC_Extension import Extension_Main_Server
from PIL import Image

sys.stdout = sys.stderr = open(os.devnull, 'w') # noconsole fix
Expand All @@ -20,7 +20,7 @@
OSC_Send_Port = 9000
OSC_Recv_IP = "127.0.0.1"
OSC_Recv_Port = 9001
Extention_Port = 9002
Extension_Port = 9002

log_temp = ""
log_temp_printed = False
Expand All @@ -45,7 +45,7 @@

OSC_Client: udp_client.SimpleUDPClient = None
OSC_Server: osc_server.ThreadingOSCUDPServer = None
Extention_System: Extention_MainServer = None
Extension_System: Extension_Main_Server = None

is_running: bool = False
is_ptt: bool = False
Expand Down Expand Up @@ -112,13 +112,13 @@ def initialize():

global OSC_Client
global OSC_Server
global Extention_System
global Extension_System

global OSC_Send_IP
global OSC_Send_Port
global OSC_Recv_IP
global OSC_Recv_Port
global Extention_Port
global Extension_Port

print_log("Initializing...")

Expand All @@ -133,8 +133,8 @@ def initialize():
if settings.get("osc_serv_port"):
OSC_Recv_Port = settings.get("osc_serv_port")

if settings.get("extention_port"):
Extention_Port = settings.get("extention_port")
if settings.get("extension_port"):
Extension_Port = settings.get("extension_port")

Recognizer = SRecognizer(settings, print_log)
Translator = STranslator(settings, print_log)
Expand All @@ -152,8 +152,8 @@ def initialize():

OSC_Client = udp_client.SimpleUDPClient(OSC_Send_IP, OSC_Send_Port)

Extention_System = Extention_MainServer(OSC_Recv_IP, Extention_Port, print_log)
Extention_System.start_server()
Extension_System = Extension_Main_Server(OSC_Recv_IP, Extension_Port, print_log)
Extension_System.start_server()

def option_changed(*args):
OSC_Client.send_message("/avatar/parameters/SRTC/SLang", Supported_Languages.index(Source_Selection.get()))
Expand Down Expand Up @@ -227,8 +227,8 @@ def main_thread():


if to_send_message != "":
print_log("[Info] Executing extention...")
to_send_message = Extention_System.execute_extention(to_send_message)
print_log("[Info] Executing extension...")
to_send_message = Extension_System.execute_extension(to_send_message)
print_log("[Info] Sending message: " + to_send_message)
print_log(" ")
if to_send_message != "{Sended-Already}":
Expand Down
171 changes: 171 additions & 0 deletions modules/SRTC_Extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# 양방향 http 기반 익스텐션 메인 서버
# http://main-server-ip:port/extension/register?name=(name)&ip=(extension-server-ip)&port=(port) (등록)
# 결과: 200 only
# 200: 성공

# http://main-server-ip:port/extension/forward?name=(name) (익스텐션 순서 변경 -> 앞으로)
# http://main-server-ip:port/extension/backward?name=(name) (익스텐션 순서 변경 -> 뒤로)
# 결과: 200, 400, 404
# 200: 성공
# 400: 이미 가장 앞에 있음
# 404: 해당 이름의 익스텐션이 없음

# http://extension-server-ip:port/extension/execute?message=(message) (익스텐션 실행)
# 결과: 200, 400
# 200: 성공
# 400: 실패

# http://extension-server-ip:port/extension/heartbeat?num=(num) (하트비트)
# 200 신호가 2번 이상 돌아오지 않으면 메인 측 서버에서 제거
# 서버는 5초마다 하트비트를 보낸다.
# num은 해당 익스텐션의 순서를 나타낸다.
# 결과: 200, 400
# 200: 성공
# 400: num이 없거나 숫자가 아님

# 음성 인식시 익스텐션 사이드 서버로 결과 전달
# 익스텐션 사이드 서버는 메인 서버로 결과 전달 (처리된 메시지 혹은 "{Sended-Already}")
# {Sended-Already}는 이미 VRChat으로 전송되었음을 의미한다. -> 더 이상 메인 서버는 해당 메시지를 처리하지 않는다.

import threading
import time
import requests
from flask import Flask, request

class Extension_Main_Server:

def __send_heartbeat(self):
i=0
while i < len(self.__extension_list):
self.__extension_list_lock.acquire()
try:
heartbeat_res = requests.get(f"http://{self.__extension_list[i]['ip']}:{self.__extension_list[i]['port']}/extension/heartbeat?num={i}")

if heartbeat_res.status_code != 200:
self.__extension_list[i]["heartbeat-fail"] += 1
else:
self.__extension_list[i]["heartbeat-fail"] = 0
except:
self.__extension_list[i]["heartbeat-fail"] += 1
finally:
if self.__extension_list[i]["heartbeat-fail"] >= 2:
self.__log(f"[Extension] {self.__extension_list[i]['name']} heartbeat failed. Removing extension.")
del self.__extension_list[i]
else:
i += 1
self.__extension_list_lock.release()

def __heartbeat_check(self):
while True:
self.__send_heartbeat()
time.sleep(2)

def __init__(self, server_ip: str, port: int, log: callable):
self.__server_ip = server_ip
self.__port = port
self.__extension_list = []
self.__extension_list_lock = threading.Lock()

self.__server = Flask(__name__)

self.__server.add_url_rule("/extension/register", view_func=self.__register_extension, methods=["GET"])
self.__server.add_url_rule("/extension/forward", view_func=self.__forward_extension, methods=["GET"])
self.__server.add_url_rule("/extension/backward", view_func=self.__backward_extension, methods=["GET"])
self.__server.add_url_rule("/extension/test", view_func=self.__extension_test, methods=["GET"])
self.__log = log

def __extension_test(self):
msg = request.args.get("message")
self.__log(f"[Extension] testing [{msg}]")
msg = self.execute_extension(msg)
return msg


def __register_extension(self):
name = request.args.get("name")
ip = request.args.get("ip")
port = request.args.get("port")
self.__log(f"[Extension] {name} registering. [{ip}:{port}]")

self.__extension_list_lock.acquire()

for i in range(len(self.__extension_list)):
if self.__extension_list[i]["name"] == name:
self.__extension_list[i]["ip"] = ip
self.__extension_list[i]["port"] = port
self.__extension_list_lock.release()
return str(i)

self.__extension_list.append({"name": name, "ip": ip, "port": port, "heartbeat-fail": 0})
self.__extension_list_lock.release()
return str(len(self.__extension_list) - 1)

def __forward_extension(self):
name = request.args.get("name")
self.__log(f"[Extension] {name} moving forward.")

self.__extension_list_lock.acquire()
for i in range(len(self.__extension_list)):
if self.__extension_list[i]["name"] == name:
if i == 0:
self.__extension_list_lock.release()
return "Already first extension", 400
self.__extension_list[i], self.__extension_list[i - 1] = self.__extension_list[i - 1], self.__extension_list[i]
self.__extension_list_lock.release()
return str(i - 1)
self.__extension_list_lock.release()
return "Not found extension", 404

def __backward_extension(self):
name = request.args.get("name")
self.__log(f"[Extension] {name} moving backward.")

self.__extension_list_lock.acquire()
for i in range(len(self.__extension_list)):
if self.__extension_list[i]["name"] == name:
if i == len(self.__extension_list) - 1:
self.__extension_list_lock.release()
return "Already last extension", 400
self.__extension_list[i], self.__extension_list[i + 1] = self.__extension_list[i + 1], self.__extension_list[i]
self.__extension_list_lock.release()
return str(i + 1)
self.__extension_list_lock.release()
return "Not found extension", 404

def execute_extension(self, message: str):
self.__extension_list_lock.acquire()
extension_len = len(self.__extension_list)

execute_result = message
i = 0
while i < extension_len:
try:
req_data = requests.get(f"http://{self.__extension_list[i]['ip']}:{self.__extension_list[i]['port']}/extension/execute?message={execute_result}")
if req_data.status_code == 200:
execute_result = req_data.text
if execute_result == "{Sended-Already}":
break
i += 1

else:
# 익스텐션 서버가 응답하지 않는다면 제거
del self.__extension_list[i]
extension_len -= 1
except:
pass

self.__extension_list_lock.release()
return execute_result

def start_server(self):
threading.Thread(target=self.__heartbeat_check).start()
#threaded
t1 = threading.Thread(target=self.__server.run, kwargs={"host": self.__server_ip, "port": self.__port})
t1.daemon= True
t1.start()






Loading

0 comments on commit bad0b77

Please sign in to comment.