def __init__(self, templates_dir="templates", static_dir="static"): self.routes = {} self.templates_env = Environment( loader=FileSystemLoader(os.path.abspath(templates_dir))) self.exception_handler = None self.whitenoise = WhiteNoise(self._wsgi_app, root=static_dir) self.middleware = Middleware(self)
class middlewareTest(unittest.TestCase): _middleware = Middleware() def __validateRegister(self): self.assertIsNotNone(self. _middleware.__validateRegister('Ridoy','5622#','Mehei','hasan','*****@*****.**'))
def test_9(self): m = Middleware() m.remove_user('test user', '123456') m.remove_user('test user1', '123456dafgda') m.remove_game_record('test user') m.remove_game_record('test user1') m.remove_game_record('test user2') m.remove_game_record('test user null')
def __init__(self, templates_directory="templates", static_dir="static"): self.routes = {} #paths are the keys and handlers (functions or classes) are the values self.templates_environment = Environment( loader=FileSystemLoader(os.path.abspath(templates_directory)) ) self.exception_handler = None self.whitenoise = WhiteNoise(self.wsgi_application, root=static_dir) #wrap wsgi application to serve static files self.middleware = Middleware(self)
def __init__(self, templates_dir="templates", static_dir="static"): self.templates_dir = templates_dir self.static_dir = static_dir # url路由 self.routes = {} # html文件夹 self.templates_env = Environment( loader=FileSystemLoader(os.path.abspath(self.templates_dir))) # css、JavaScript文件夹 self.whitenoise = WhiteNoise(self.wsgi_app, root=static_dir) # 自定义错误 self.exception_handler = None # 请求中间件,将api对象传入 self.middleware = Middleware(self)
def manage_events(self, params): sid = request.sid try: if params["streaming"]: with self._thread_lock: if not sid in self._threads: mw = Middleware(self._socket, sid, params['topics'], params['reservoirSize']) self._middlewares[sid] = mw self._threads[ sid] = self._socket.start_background_task( target=mw.start_background_job) elif not self._threads[sid].is_alive(): mw = Middleware(self._socket, sid, params['topics'], params['reservoirSize']) self._middlewares[sid] = mw self._threads[ sid] = self._socket.start_background_task( target=mw.start_background_job) self.send_status(streaming=True) else: mw = self._middlewares.get(request.sid, None) if mw: mw.stop_backgroung_job() self.send_status(streaming=False) except: print('Request Invalid') self.send_status(streaming=False, error=[{ 'type': 'Request Invalid' }])
def start_experiment(self, results_dir, update_and_install=False, experiment_runtime = 5, # minutes runtime_buffer = 1, # minutes stats_frequency ="30s", num_threads_in_pool = 5, replication_factor = 1, memaslap_workload = "smallvalue.cfg", memaslap_window_size = "10k", hibernate_at_end = True, ssh_username = "******", num_memaslaps = 1, num_memcacheds = 1, concurrency = 64, is_first_run=True ): experiment_runtime_string = "{}m".format(experiment_runtime) with fabric.api.settings(warn_only=True): fabric.api.local("rm -r {}/*".format(results_dir)) fabric.api.local("mkdir -p {}".format(results_dir)) fabric.api.local("mkdir {}/graphs".format(results_dir)) # region ---- Parameters ---- TOTAL_MACHINE_COUNT = 11 # this is fixed by the template resource_group_name = 'template11vms' my_pub_ssh_key_path = '~/.ssh/id_rsa_asl.pub' template_path = "azure-templates/template11vms.json" pub_ssh_key_path = os.path.expanduser(my_pub_ssh_key_path) with open(pub_ssh_key_path, 'r') as pub_ssh_file_fd: pub_ssh_key = pub_ssh_file_fd.read().strip() parameters = { "virtualMachines_name": "foraslvms", "virtualMachines_adminPassword": "******", "networkInterfaces_name": "MyNetworkInterface", "virtualNetworks_testeth_vnet_name": "MyVNet", "key": pub_ssh_key, "uniquedns": "pungast" } # endregion # Initialize the deployer class if is_first_run: self.deployer = Deployer(resource_group_name, template_path, parameters) self.deployer.deploy_wait() # region ---- Extract VMs' IPs and other information ---- vms = self.deployer.compute_client.virtual_machines.list(resource_group_name) vm_names = [] vm_types = [] public_hostnames = [] private_hostnames = [] for vm in vms: vm_type = vm.hardware_profile.vm_size vm_types.append(vm_type) vm_names.append(vm.name) self.log.info("VM {} [{}]".format(Colors.ok_blue(vm.name), vm_type)) # Get machine's public address that we can use for SSH-ing public_ip = self.deployer.network_client.public_ip_addresses.get(resource_group_name, vm.name) public_host_address = public_ip.dns_settings.fqdn public_hostnames.append(public_host_address) #self.log.info("Public host name: {}".format(Colors.ok_green(public_host_address))) # Get machine's private IP address network_interface_id = vm.network_profile.network_interfaces[0].id network_interface_name = network_interface_id.split("/")[-1] network_interface = self.deployer.network_client.network_interfaces.get(resource_group_name, network_interface_name) private_host_address = network_interface.ip_configurations[0].private_ip_address private_hostnames.append(private_host_address) #self.log.info("Private host name: {}".format(Colors.ok_green(private_host_address))) # endregion # region ---- Set up all machines ---- index_a4 = vm_types.index("Basic_A4") indices_smallmachines = list(range(TOTAL_MACHINE_COUNT)) indices_smallmachines.remove(index_a4) memcached_machines = [vm_names.index("foraslvms" + str(x)) for x in Experiment.default_memcached_machines()] memcached_machines = memcached_machines[0:num_memcacheds] memaslap_machines = [vm_names.index("foraslvms" + str(x)) for x in Experiment.default_memaslap_machines()] memaslap_machines = memaslap_machines[0:num_memaslaps] self.log.info("A4 machine: " + str(index_a4)) self.log.info("A2 machines: " + str(indices_smallmachines)) self.log.info("Memcached machines: " + str(memcached_machines)) self.log.info("Memaslap machines: " + str(memaslap_machines)) # Wait for all servers to be responsive if is_first_run: aslutil.wait_for_servers(ssh_username, public_hostnames, "~/.ssh/id_rsa_asl", self.log, check_every_n_sec=10) # Set up memcached servers memcached_port = 11211 mc_servers = [] mc_server_string_list = [] for i in memcached_machines: self.log.info("Setting up memcached on machine {} ({}).".format(i, vm_names[i])) mc_server = Memcached(memcached_port, public_hostnames[i], ssh_username=ssh_username, id_number=int(aslutil.server_name_to_number(vm_names[i]))) mc_servers.append(mc_server) mc_server_string_list.append("{}:{}".format(private_hostnames[i], memcached_port)) if update_and_install: mc_server.update_and_install() for s in mc_servers: s.start() sleep_for = 15 self.log.info("Sleeping for {} seconds so memcached servers can start...".format(sleep_for)) time.sleep(sleep_for) # Set up middleware server middleware_port = 11212 self.log.info("Setting up middleware on machine {} ({}).".format(index_a4, vm_names[index_a4])) mw_server = Middleware(public_hostnames[index_a4], private_hostnames[index_a4], middleware_port, num_threads_in_pool, replication_factor, mc_server_string_list, ssh_username=ssh_username) if update_and_install: mw_server.update_and_install() if is_first_run: mw_server.upload_jar() mw_server.clear_logs() mw_server.start() # Sleep a bit so middleware has time to start while not mw_server.is_running(): sleep_for = 5 self.log.info("Sleeping for {} seconds so middleware can start...".format(sleep_for)) time.sleep(sleep_for) time.sleep(10) # Set up memaslap servers ms_servers = [] first_memaslap = True for i in memaslap_machines: self.log.info("Setting up memaslap on machine {} ({}).".format(i, vm_names[i])) ms_server = Memaslap(public_hostnames[i], private_hostnames[index_a4], middleware_port, ssh_username=ssh_username, id_number=int(aslutil.server_name_to_number(vm_names[i]))) # i is zero-indexed ms_servers.append(ms_server) if is_first_run: ms_server.upload_resources() if update_and_install: if not first_memaslap: ms_server.upload_built_files() ms_server.update_and_install() if first_memaslap: ms_server.download_built_files() first_memaslap = False for s in ms_servers: s.clear_logs() s.start(runtime=experiment_runtime_string, log_filename="memaslap{}.out".format(s.id_number), stats_freq=stats_frequency, workload_filename=memaslap_workload, concurrency=concurrency, window_size=memaslap_window_size) # endregion sleep_time = experiment_runtime + runtime_buffer self.log.info("Waiting for the experiment to finish, sleeping for up to {} minutes.".format(sleep_time)) already_slept = 0 while True: sleep_interval = 30 time.sleep(sleep_interval) already_slept += sleep_interval num_running_memaslaps = sum([s.is_running() for s in ms_servers]) self.log.info("Waiting for the experiment to finish (total {} minutes), {:.0f}/{} minutes elapsed ({:.0f}%), {} memaslaps running." .format(sleep_time, already_slept / 60, experiment_runtime, 100 * already_slept / 60.0 / experiment_runtime, num_running_memaslaps)) if already_slept >= sleep_time * 60: self.log.info("Stopping because of time limit.") break if num_running_memaslaps == 0: self.log.info("Stopping because no memaslaps are left.") break # region ---- Kill everyone ---- # Memaslap for ms_server in ms_servers: ms_server.stop() # Middleware mw_server.stop() # Memcached for mc_server in mc_servers: mc_server.stop() # endregion # region ---- Download logs, extract data, plot ---- mw_server.download_logs(local_path=results_dir) for ms_server in ms_servers: ms_server.download_logs(local_path=results_dir) # endregion if hibernate_at_end: self.deployer.hibernate_wait() self.log.info("Done.")
import casbin from middleware import Middleware from flask import Flask app = Flask(__name__) enforcer = casbin.Enforcer("authz_model.conf", "policy.csv") app.wsgi_app = Middleware(app.wsgi_app, enforcer) @app.route("/") def hello_world(): return "Hello World!" if __name__ == '__main__': app.run()
def process_start_request(): # print("正在访问: "+request.path) app.wsgi_app = Middleware(app.wsgi_app, request.path)
from flask import Flask from flask import request from flask import jsonify from middleware import Middleware app = Flask(__name__) m = Middleware() @app.route("/") def hello(): return "Hello World!" @app.route("/register", methods=['POST']) def register(): username = request.json.get('username', None) password = request.json.get('password', None) return jsonify(m.register_user(username=username, password=password)) @app.route("/login", methods=['POST']) def login(): username = request.json.get('username', None) password = request.json.get('password', None) return jsonify(m.login_user(username=username, password=password)) @app.route('/send_game_data', methods=['POST']) def send_game_data(): username = request.json.get('username', None)
from flask import Flask, request from middleware import Middleware app = Flask('app') app.wsgi_app = Middleware(app.wsgi_app) @app.route('/', methods=['GET']) def hello_world(): user = request.environ['user']['name'] return f'Hi {user}' if __name__ == '__main__': app.run('127.0.0.1', '5000', debug=True)
def initialSetup(): global quadcopter, middleware, message_sender, pi_logger from middleware import Middleware from CustomLogger import pi_logger pi_logger = pi_logger() quadcopter = Quadcopter(pi_logger) from message_sender import Message_sender message_sender = Message_sender(socketio) middleware = Middleware(quadcopter) # TODO Implement Logging if Constants.ENABLE_FLASK_LOGGING: formatter = logging.Formatter( Constants.LOG_FORMAT_FLASK) handler = RotatingFileHandler(os.path.join(Constants.LOG_LOCATION_FLASK, Constants.LOG_FILENAME_FLASK), maxBytes=10000000, backupCount=5) handler.setLevel(logging.DEBUG) handler.setFormatter(formatter) app.logger.addHandler(handler) import bonjour service = bonjour.Bonjour(pi_logger) service.publish() global queue from gevent import queue, Greenlet queue = queue.Queue() # http://www.gevent.org/gevent.queue.html global sender, reader, senderexternal, readerexternal reader, sender = gipc.pipe() readerexternal, senderexternal = gipc.pipe() global thread, thread3 if Constants.ENABLE_SERIAL: # thread = threading.Thread(name="Serial Thread", thread = gipc.start_process(name="Serial Thread", daemon=True, target=read_from_port, kwargs={'port': Constants.ARDUINO_PORT, 'baud_rate': Constants.ARDUINO_BAUDRATE, 'sender': sender, 'logger': pi_logger, } ) # Greenlet.spawn(passmessagetoproc,senderexternal) thread3 = gipc.start_process(name="PID Thread", daemon=True, target=speed_control, kwargs={'reader': reader, 'quadcopter': quadcopter, 'message_sender': message_sender, 'middleware': middleware, 'logger': pi_logger, 'reader2': readerexternal} ) # speed_control(reader=reader,quadcopter=quadcopter,message_sender=message_sender,middleware=middleware,logger=pi_logger) # message_sender.__send_msg_to_arduino__("HELLOn\n") @socketio.on('connect', namespace=Constants.SOCKETIO_NAMESPACE) def test_connect(): print 'Client connected : %s' % request return True @socketio.on('disconnect', namespace=Constants.SOCKETIO_NAMESPACE) def test_disconnect(): print 'Client disconnected : %s' % request return True @socketio.on('message', namespace=Constants.SOCKETIO_NAMESPACE) def handle_message(data): print data socketio.emit('message', 'HELLOAGAIN') global middleware middleware.parseMessage(data)
mc_servers.append(mc_server) mc_server_string_list.append("{}:{}".format(private_hostnames[i], memcached_port)) if UPDATE_AND_INSTALL: mc_server.update_and_install() for s in mc_servers: s.start() # Set up middleware server middleware_port = 11212 log.info("Setting up middleware on machine {} ({}).".format( index_a4, vm_names[index_a4])) mw_server = Middleware(public_hostnames[index_a4], private_hostnames[index_a4], middleware_port, NUM_THREADS_IN_POOL, REPLICATION_FACTOR, mc_server_string_list, ssh_username=ssh_username) if UPDATE_AND_INSTALL or UPDATE_AND_INSTALL_ONLY_MIDDLEWARE: mw_server.update_and_install() mw_server.clear_logs() mw_server.start() # Sleep a bit so middleware has time to start if not mw_server.is_running(): sleep_for = 5 log.info( "Sleeping for {} seconds so middleware can start...".format(sleep_for)) time.sleep(sleep_for)
def setUp(self): self.m = Middleware()