You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Comrad/komrade/backend/switchboard.py

64 lines
1.8 KiB
Python

4 years ago
# internal imports
import os,sys; sys.path.append(os.path.abspath(os.path.join(os.path.abspath(os.path.join(os.path.dirname(__file__),'..')),'..')))
from komrade import *
4 years ago
from komrade.backend import *
4 years ago
# external imports
from flask import Flask, request, jsonify
from flask_classful import FlaskView
OPERATOR = None
4 years ago
TELEPHONE = None
4 years ago
from flask_classful import FlaskView, route
4 years ago
class TheSwitchboard(FlaskView, Logger):
4 years ago
default_methods = ['GET']
excluded_methods = ['phone','op','send']
4 years ago
@property
def phone(self):
global TELEPHONE
4 years ago
from komrade.backend.the_telephone import TheTelephone
4 years ago
if not TELEPHONE: TELEPHONE=TheTelephone()
return TELEPHONE
@property
def op(self):
global OPERATOR
4 years ago
from komrade.backend.the_operator import TheOperator
4 years ago
if not OPERATOR: OPERATOR=TheOperator()
return OPERATOR
4 years ago
def send(self,res):
return res
def route(self,msg):
# give to The Operator
try:
self.log('Success! your message was: '+str(msg))
res = self.op.recv(msg)
self.log('Your return result should be:',res)
return self.send(res)
except AssertionError as e:
self.log('got exception!!',e)
return OPERATOR_INTERCEPT_MESSAGE
4 years ago
def get(self,msg):
4 years ago
self.log('Incoming call!:',msg)
4 years ago
if not msg:
self.log('empty request!')
return OPERATOR_INTERCEPT_MESSAGE
4 years ago
# unenescape
msg = msg.replace('_','/')
return self.route(msg)
4 years ago
def run_forever(port='8080'):
4 years ago
global OPERATOR,TELEPHONE
TELEPHONE = TheTelephone(allow_builtin=False)
4 years ago
OPERATOR = TheOperator(allow_builtin=False)
4 years ago
app = Flask(__name__)
4 years ago
TheSwitchboard.register(app, route_base='/op/', route_prefix=None)
4 years ago
app.run(debug=True, port=port, host='0.0.0.0')