# Path: server/websockets/compatibility.py

# 
# Path: server/websockets/exceptions.py
# class ConnectionClosed(InvalidState):
#     """
#     Exception raised when trying to read or write on a closed connection.
# 
#     Provides the connection close code and reason in its ``code`` and
#     ``reason`` attributes respectively.
# 
#     """
#     def __init__(self, code, reason):
#         self.code = code
#         self.reason = reason
#         message = 'WebSocket connection is closed: '
#         message += 'code = {}, '.format(code) if code else 'no code, '
#         message += 'reason = {}.'.format(reason) if reason else 'no reason.'
#         super().__init__(message)
# 
# class InvalidState(Exception):
#     """
#     Exception raised when an operation is forbidden in the current state.
# 
#     """
# 
# class PayloadTooBig(Exception):
#     """
#     Exception raised when a frame's payload exceeds the maximum size.
# 
#     """
# 
# class WebSocketProtocolError(Exception):
#     """
#     Internal exception raised when the remote side breaks the protocol.
# 
#     """


import asyncio
import asyncio.queues
import codecs
import collections
import logging
import random
import struct
from .compatibility import asyncio_ensure_future
from .exceptions import (ConnectionClosed, InvalidState, PayloadTooBig,
                         WebSocketProtocolError)
from .framing import *
from .handshake import *
        yield from self.write_frame(OP_PONG, data)

    # Private methods - no guarantees.

    def encode_data(self, data):
        # Expect str or bytes, return bytes.
        if isinstance(data, str):
            return data.encode('utf-8')
        elif isinstance(data, bytes):
            return data
        else:
            raise TypeError("data must be bytes or str")

    @asyncio.coroutine
    def ensure_open(self):
        # Raise a suitable exception if the connection isn't open.
        # Handle cases from the most common to the least common.

        if self.state == OPEN:
            return

        if self.state == CLOSED:
            raise ConnectionClosed(self.close_code, self.close_reason)

        # If the closing handshake is in progress, let it complete to get the
        # proper close status and code. As an safety measure, the timeout is
        # longer than the worst case (2 * self.timeout) but not unlimited.
        if self.state == CLOSING:
            yield from asyncio.wait_for(
                self.worker_task, 3 * self.timeout, loop=self.loop)

# Path: server/websockets/compatibility.py

# 
# Path: server/websockets/exceptions.py
# class ConnectionClosed(InvalidState):
#     """
#     Exception raised when trying to read or write on a closed connection.
# 
#     Provides the connection close code and reason in its ``code`` and
#     ``reason`` attributes respectively.
# 
#     """
#     def __init__(self, code, reason):
#         self.code = code
#         self.reason = reason
#         message = 'WebSocket connection is closed: '
#         message += 'code = {}, '.format(code) if code else 'no code, '
#         message += 'reason = {}.'.format(reason) if reason else 'no reason.'
#         super().__init__(message)
# 
# class InvalidState(Exception):
#     """
#     Exception raised when an operation is forbidden in the current state.
# 
#     """
# 
# class PayloadTooBig(Exception):
#     """
#     Exception raised when a frame's payload exceeds the maximum size.
# 
#     """
# 
# class WebSocketProtocolError(Exception):
#     """
#     Internal exception raised when the remote side breaks the protocol.
# 
#     """


# Path: server/websockets/protocol.py
import asyncio
import asyncio.queues
import codecs
import collections
import logging
import random
import struct
from .compatibility import asyncio_ensure_future
from .exceptions import (ConnectionClosed, InvalidState, PayloadTooBig,
                         WebSocketProtocolError)
from .framing import *
from .handshake import *

        yield from self.write_frame(OP_PONG, data)

    # Private methods - no guarantees.

    def encode_data(self, data):
        # Expect str or bytes, return bytes.
        if isinstance(data, str):
            return data.encode('utf-8')
        elif isinstance(data, bytes):
            return data
        else:
            raise TypeError("data must be bytes or str")

    @asyncio.coroutine
    def ensure_open(self):
        # Raise a suitable exception if the connection isn't open.
        # Handle cases from the most common to the least common.

        if self.state == OPEN:
            return

        if self.state == CLOSED:
            raise ConnectionClosed(self.close_code, self.close_reason)

        # If the closing handshake is in progress, let it complete to get the
        # proper close status and code. As an safety measure, the timeout is
        # longer than the worst case (2 * self.timeout) but not unlimited.
        if self.state == CLOSING:
            yield from asyncio.wait_for(
                self.worker_task, 3 * self.timeout, loop=self.loop)

            raise ConnectionClosed(self.close_code, self.close_reason)