# Path: wdom/util.py
# def install_asyncio() -> None:
#     """Ensure that asyncio's io-loop is installed to tornado."""
#     pass
#     # from tornado.ioloop import IOLoop
#     # from tornado.platform.asyncio import AsyncIOMainLoop
#     # if not IOLoop.initialized():
#     #     AsyncIOMainLoop().install()
# 
# Path: wdom/options.py
# def level_to_int(level: Union[str, int]) -> int:
# def set_loglevel(level: Union[int, str, None] = None) -> None:
# def parse_command_line() -> Namespace:
# 
# Path: wdom/server/handler.py
# def on_websocket_message(message: str) -> None:
#     """Handle messages from browser."""
#     msgs = json.loads(message)
#     for msg in msgs:
#         if not isinstance(msg, dict):
#             logger.error('Invalid WS message format: {}'.format(message))
#             continue
#         _type = msg.get('type')
#         if _type == 'log':
#             log_handler(msg['level'], msg['message'])
#         elif _type == 'event':
#             event_handler(msg['event'])
#         elif _type == 'response':
#             response_handler(msg)
#         else:
#             raise ValueError('Unkown message type: {}'.format(message))


import asyncio
import logging
import socket
from typing import Any, TYPE_CHECKING
from tornado import web, websocket
from tornado.httpserver import HTTPServer
from wdom.util import install_asyncio
from wdom.options import config
from wdom.server.handler import on_websocket_message
    from typing import List  # noqa
        from wdom.document import get_document


def is_connected() -> bool:
    """Check if tornado web server has a client connection."""
    return any(connections)


class MainHandler(web.RequestHandler):
    """Main handler to serve document of the application."""

    def get(self) -> None:
        """Return whole html representation of the root document."""
        logger.info('connected')
        self.write(get_document().build())


class WSHandler(websocket.WebSocketHandler):
    """Handler class of web socket connection."""

    def open(self) -> None:
        """Execute when connection open."""
        logger.info('WebSocket OPEN')
        connections.append(self)

    def on_message(self, message: str) -> None:
        """Execute when get message from client."""
        on_websocket_message(message)

    async def terminate(self) -> None:
        """Terminate server if no more connection exists."""

# Path: wdom/util.py
# def install_asyncio() -> None:
#     """Ensure that asyncio's io-loop is installed to tornado."""
#     pass
#     # from tornado.ioloop import IOLoop
#     # from tornado.platform.asyncio import AsyncIOMainLoop
#     # if not IOLoop.initialized():
#     #     AsyncIOMainLoop().install()
# 
# Path: wdom/options.py
# def level_to_int(level: Union[str, int]) -> int:
# def set_loglevel(level: Union[int, str, None] = None) -> None:
# def parse_command_line() -> Namespace:
# 
# Path: wdom/server/handler.py
# def on_websocket_message(message: str) -> None:
#     """Handle messages from browser."""
#     msgs = json.loads(message)
#     for msg in msgs:
#         if not isinstance(msg, dict):
#             logger.error('Invalid WS message format: {}'.format(message))
#             continue
#         _type = msg.get('type')
#         if _type == 'log':
#             log_handler(msg['level'], msg['message'])
#         elif _type == 'event':
#             event_handler(msg['event'])
#         elif _type == 'response':
#             response_handler(msg)
#         else:
#             raise ValueError('Unkown message type: {}'.format(message))


# Path: wdom/server/_tornado.py
import asyncio
import logging
import socket
from typing import Any, TYPE_CHECKING
from tornado import web, websocket
from tornado.httpserver import HTTPServer
from wdom.util import install_asyncio
from wdom.options import config
from wdom.server.handler import on_websocket_message
    from typing import List  # noqa
        from wdom.document import get_document



def is_connected() -> bool:
    """Check if tornado web server has a client connection."""
    return any(connections)


class MainHandler(web.RequestHandler):
    """Main handler to serve document of the application."""

    def get(self) -> None:
        """Return whole html representation of the root document."""
        logger.info('connected')
        self.write(get_document().build())


class WSHandler(websocket.WebSocketHandler):
    """Handler class of web socket connection."""

    def open(self) -> None:
        """Execute when connection open."""
        logger.info('WebSocket OPEN')
        connections.append(self)

    def on_message(self, message: str) -> None:
        """Execute when get message from client."""
        on_websocket_message(message)

    async def terminate(self) -> None:
        """Terminate server if no more connection exists."""

        await asyncio.sleep(config.shutdown_wait)