import sys
sys.path.append("wrappers")
from modelverse import *

import random, json, uuid
sys.path.append('services/DEVS_batch/models')

def pypdevs_service(port):
    code = service_get(port)
    experiment = service_get(port)
    model_name = str(uuid.uuid4()).replace("-", "")

    with open("services/DEVS_batch/models/" + model_name + ".py", 'w') as f:
        f.write(code)
        f.write(experiment)

    main = getattr(__import__(model_name), "main")
    service_set(port, main())

try:
    init(sys.argv[1])
    login("pypdevs_batch_service", "my_password")

    service_register("pypdevs_batch_simulator", pypdevs_service)

    try:
        while True:
            # Stay active, as we shouldn't exit while the service is running!
            time.sleep(1)
    finally:
        service_stop()
except KeyboardInterrupt:
    pass
