adc2019system.py 1.34 KB
Newer Older
Kento HASEGAWA's avatar
Kento HASEGAWA committed
1 2 3 4 5
#!python3
# ADC2019 system module

import json

6
from roles import Host, Solver, MergeSolver
Kento HASEGAWA's avatar
Kento HASEGAWA committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

config = None
role = None

def init(args):
    
    if ('config' in args) and (args['config'] is not None):
        load_config(args['config'])

def load_config(path):
    global config
    global role
    
    with open(path, 'r') as fp:
        config = json.load(fp)

    if 'role' in config:
        set_role(config['role'], config)

def set_role(role_name, config_data):
    global role

29 30 31
    if role is not None:
        del role

Kento HASEGAWA's avatar
Kento HASEGAWA committed
32 33 34
    if role_name == 'host':
        role = Host(config_data)
    elif role_name == 'solver':
35
        if not 'partial_mode' in config_data:
36
            config_data['partial_mode'] = False
Kento HASEGAWA's avatar
Kento HASEGAWA committed
37
        role = Solver(config_data)
38 39
    elif role_name == 'merge_solver':
        role = MergeSolver(config_data)
Kento HASEGAWA's avatar
Kento HASEGAWA committed
40 41 42

def call_api(method, cmd, params):

43
    print(f'I: API Received: {cmd}')
44 45 46 47 48 49
    if cmd == 'role':
        if method == 'POST':
            set_role(params['role'], params)
            return {'role': role.type}
        else:
            if role is None:
Kento HASEGAWA's avatar
Kento HASEGAWA committed
50
                return {'role': 'Undefined'}
51 52 53 54 55
            else:
                return {'role': self.type}
    else:
        if role is not None:
            return role.call_api(method, cmd, params)
Kento HASEGAWA's avatar
Kento HASEGAWA committed
56 57
        else:
            return None