SDSM-for-SDI / experiment-scripts / serverCoAP.py
serverCoAP.py
Raw
from coapthon.server.coap import CoAP
from coapthon.resources.resource import Resource
import os
import time


def run_command(mtype:int, dest_addr:str) -> None:
#ssh://server_one/home/osboxes/aioquic-explicit_UniPisa/experiment-scripts/migrate-server-console.py
    command = "python /home/osboxes/aioquic-explicit_UniPisa/experiment-scripts/migrate_from_shell.py python_bundle " + dest_addr
    if(mtype == 0):
        command = command + " false false"  #cold migration
    elif(mtype == 1):
        command = command + " true false"   #pre-copy migration
    elif(mtype == 2):
        command = command + " false true"   #post-copy migration
    elif(mtype == 3):
        command = command + " true true"    #hybrid migration

    #time.sleep(1)
    os.system(command)


class BasicResource(Resource):
    def __init__(self, name="BasicResource", coap_server=None):
        super(BasicResource, self).__init__(name, coap_server, visible=True,observable=True, allow_children=True)
        self.payload = "Basic Resource"

    def render_GET(self, request):
        return self

    def render_PUT(self, request):
        print("ServerCoAP: starting to call the migration command")
        info = request.payload
        info = info.split(",")
        run_command(int(info[0]), info[1])
        self.payload = "OK"
        #kill instance of migrate_from_shell.py to let migration be successful next time
        os.system("pkill -f migrate_from_shell.py")
        return self

    def render_POST(self, request):
        res = BasicResource()
        res.location_query = request.uri_query
        res.payload = request.payload
        return res

    def render_DELETE(self, request):
        return True


class CoAPServer(CoAP):
    def __init__(self, host, port):
        CoAP.__init__(self, (host, port))
        self.add_resource('basic/', BasicResource())

def main():
    print("Before server starts listening")
    try:
        server = CoAPServer("0.0.0.0", 5683)
        print("After CoAP server declaration")
    except Exception as e:
        print(e)
    try:
        print("Before server listens")
        server.listen(10)
        print("After server listens")
    except Exception as e:
        print(e)
    except KeyboardInterrupt:
        print("Server Shutdown")
        server.close()
        print("Exiting...")

if __name__ == '__main__':
    main()