# -*- coding: UTF-8 -*-
from dcs.server import Server
from configparser import ConfigParser
from loguru import logger
from dcs.tools.database import create_user_info
from dcs.requester import Requester
from dcs.spider import Spider
from conf.config import global_var
from dcs.user_process import UP
from dcs.communicate import Communicator

create_user_info()
logger.add('./dcs.log', rotation='10 MB', enqueue=True, backtrace=True, diagnose=True)
logger.debug('reading config args...')
configFile = '../conf/settings.ini'
con = ConfigParser()
con.read(configFile, encoding='utf-8')
items = con.items('server')
items = dict(items)

logger.debug('starting the main server...')
global_var.server = Server(int(items['port']), eval(items['buffer_size']))
global_var.server.daemon = items['daemon']
global_var.server.start()

logger.debug('starting the requester server...')
global_var.requester = Requester()
global_var.requester.daemon = True
global_var.requester.start()

logger.debug('starting the spider server...')
global_var.spider = Spider()
global_var.spider.daemon = True
global_var.spider.start()

logger.debug('starting the user server...')
global_var.up = UP()
global_var.up.daemon = True
global_var.up.start()

logger.debug('starting the communicator server...')
global_var.communicator = Communicator()
global_var.communicator.daemon = True
global_var.communicator.start()

global_var.server.join()
global_var.requester.join()
global_var.spider.join()
global_var.up.join()
global_var.communicator.join()

logger.warning('Overing...')
