#!/usr/bin/env python


import sys
import os
import eventlet
import socket
from liasis.core.cmd import args
from liasis.config import config
from liasis.log import log
from liasis.core.server import Server
from liasis.core.listener import listener, pool
from liasis.core.defaults import *
eventlet.monkey_patch()


def main():
    while True:
        try:
            server = Server()
            sock, addr = listener.sock.accept()
            pool.spawn_n(server.handle, sock, addr)
        except socket.error as e:
            log.warn(e)
            eventlet.StopServer()
            sys.exit(0)
        except (SystemExit, KeyboardInterrupt):
            log.info("Stopping Liasis ...")
            listener.close_socket()
            sys.exit(os.EX_OK)


if __name__ == "__main__":
    log.info("Starting Liasis ... ")
    main()
