# Very basic MEC PLAT simulator

import http.server
import socketserver
import mec_http_server
import threading
import sys, os, time
import _Getch as getc

# MEC simulator IPv4 address
ADDRESS_BINDING = '127.0.0.1'
# MEC simulator IPv4 listening port
PORT_NUMBER = 8081

if __name__ == '__main__':
    getch = getc._Getch()
    exit = False
    try:
        # Create a web server and define the handler to manage the incoming request
        server = http.server.HTTPServer((ADDRESS_BINDING, PORT_NUMBER), mec_http_server.myHandler)
        server.wbufsize = -1
        print ('Started httpserver on port ', PORT_NUMBER)
        # Wait forever for incoming http requests in background
        #server.serve_forever(poll_interval=2)
        threading.Thread(target=server.serve_forever).start()
        # Use keybord entries to trigBger notifications
        while exit == False:
            ch = getch()
            if ch == 'q':
                exit = True
            else:
                print(">>> ch= ", ch)
        server.shutdown()
        server.socket.close()
    except KeyboardInterrupt:
        print ('^C received, shutting down the web server')
        server.shutdown()
        server.socket.close()
# End of __main__
