# Path: ebu_tt_live/strings.py
# ERR_WS_INVALID_ACTION = gettext('Invalid action: {action}')
# 
# ERR_WS_NOT_CONSUMER = gettext('This socket does not belong to a consumer')
# 
# ERR_WS_NOT_PRODUCER = gettext('This socket does not belong to a producer.')
# 
# ERR_WS_RECEIVE_VIA_PRODUCER = gettext('Producer socket cannot be used to receive data')
# 
# ERR_WS_SEND_VIA_CONSUMER = gettext('Consumer socket cannot be used to send data')
# 
# Path: ebu_tt_live/errors.py
# class UnexpectedSequenceIdentifierError(Exception):
#     pass


from autobahn.twisted.websocket import WebSocketClientProtocol, WebSocketServerFactory, WebSocketServerProtocol, \
    listenWS, WebSocketClientFactory, connectWS
from twisted.internet import interfaces, reactor
from hyperlink import URL
from zope.interface import implementer
from logging import getLogger
from ebu_tt_live.strings import ERR_WS_INVALID_ACTION, ERR_WS_NOT_CONSUMER, ERR_WS_NOT_PRODUCER, \
    ERR_WS_RECEIVE_VIA_PRODUCER, ERR_WS_SEND_VIA_CONSUMER
from ebu_tt_live.errors import UnexpectedSequenceIdentifierError
from .base import IBroadcaster
import json
import six
    def listen(self):
        listener = listenWS(self)
        self.real_port_number = listener.getHost().port
        if self.producer:
            self.producer.real_port_number = self.real_port_number
        if self.consumer:
            self.consumer.real_port_number = self.real_port_number


class BroadcastClientProtocol(EBUWebsocketProtocolMixin, WebSocketClientProtocol):

    def onOpen(self):
        try:
            self._sequence_identifier, self.action = self._parse_path(self.factory.url)
        except ValueError as err:
            log.error(err)
            self.dropConnection()
        except Exception as err:
            log.exception(err)
            self.dropConnection()

        self.factory.register(self)

    def onMessage(self, payload, isBinary):
        if self.action == 'subscribe':
            try:
                self._write_to_consumer(payload, sequence_identifier=self._sequence_identifier)
            except UnexpectedSequenceIdentifierError:
                self.dropConnection(abort=False)
        else:

# Path: ebu_tt_live/strings.py
# ERR_WS_INVALID_ACTION = gettext('Invalid action: {action}')
# 
# ERR_WS_NOT_CONSUMER = gettext('This socket does not belong to a consumer')
# 
# ERR_WS_NOT_PRODUCER = gettext('This socket does not belong to a producer.')
# 
# ERR_WS_RECEIVE_VIA_PRODUCER = gettext('Producer socket cannot be used to receive data')
# 
# ERR_WS_SEND_VIA_CONSUMER = gettext('Consumer socket cannot be used to send data')
# 
# Path: ebu_tt_live/errors.py
# class UnexpectedSequenceIdentifierError(Exception):
#     pass


# Path: ebu_tt_live/twisted/websocket.py
from autobahn.twisted.websocket import WebSocketClientProtocol, WebSocketServerFactory, WebSocketServerProtocol, \
    listenWS, WebSocketClientFactory, connectWS
from twisted.internet import interfaces, reactor
from hyperlink import URL
from zope.interface import implementer
from logging import getLogger
from ebu_tt_live.strings import ERR_WS_INVALID_ACTION, ERR_WS_NOT_CONSUMER, ERR_WS_NOT_PRODUCER, \
    ERR_WS_RECEIVE_VIA_PRODUCER, ERR_WS_SEND_VIA_CONSUMER
from ebu_tt_live.errors import UnexpectedSequenceIdentifierError
from .base import IBroadcaster
import json
import six

    def listen(self):
        listener = listenWS(self)
        self.real_port_number = listener.getHost().port
        if self.producer:
            self.producer.real_port_number = self.real_port_number
        if self.consumer:
            self.consumer.real_port_number = self.real_port_number


class BroadcastClientProtocol(EBUWebsocketProtocolMixin, WebSocketClientProtocol):

    def onOpen(self):
        try:
            self._sequence_identifier, self.action = self._parse_path(self.factory.url)
        except ValueError as err:
            log.error(err)
            self.dropConnection()
        except Exception as err:
            log.exception(err)
            self.dropConnection()

        self.factory.register(self)

    def onMessage(self, payload, isBinary):
        if self.action == 'subscribe':
            try:
                self._write_to_consumer(payload, sequence_identifier=self._sequence_identifier)
            except UnexpectedSequenceIdentifierError:
                self.dropConnection(abort=False)
        else:

            log.error(ERR_WS_RECEIVE_VIA_PRODUCER)