# Path: lib/engineio_client/transport.py
# class Transport(Emitter):
#     """Abstract base class for transports implementations
#     """
#     def __init__(self, client, scheme, hostname, port, path, parser=None):
#         super(Transport, self).__init__()
#         self.client = client
#         self.scheme = scheme
#         self.hostname = hostname
#         self.port = port
#         self.path = path
#         self.parser = parser
# 
#         self.state = 'closed'
# 
#     def open(self):
#         if self.state == 'closed':
#             self.state = 'opening'
#             self.do_open()
# 
#     def close(self, send=True):
#         if self.state in ['open', 'opening']:
#             if send:
#                 self.do_close()
#             self.handle_close()
# 
#     def send(self, packets):
#         if self.state != 'open':
#             raise TransportNotOpenException()
# 
#         self.do_send(packets)
# 
#     def handle_error(self, error):
#         logger.warning("Transport error: %s", error)
#         self.emit('error', error)
# 
#     def handle_open(self):
#         logger.debug("Transport open")
#         self.state = 'open'
#         self.emit('open')
# 
#     def handle_close(self):
#         logger.debug("Transport closed")
#         self.state = 'closed'
#         self.emit('close')
# 
#     def handle_packet(self, packet):
#         logger.debug(format_long("Received packet: %s - %s", packet.type_string, repr(packet.data)))
#         self.emit('packet', packet)
# 
#     def do_open(self):
#         raise NotImplementedError()
# 
#     def do_close(self):
#         raise NotImplementedError()
# 
#     def do_send(self, packets):
#         raise NotImplementedError()
# 
# Path: lib/engineio_client/parser.py
# class Packet(object):
#     OPEN    = 0
#     CLOSE   = 1
#     PING    = 2
#     PONG    = 3
#     MESSAGE = 4
#     UPGRADE = 5
#     NOOP    = 6
# 
#     def __init__(self, _type, data='', binary=False):
#         self.type = _type
#         self.data = data
#         self.binary = binary
# 
#     @property
#     def type_string(self):
#         return {
#             self.OPEN:     'open',
#             self.CLOSE:    'close',
#             self.PING:     'ping',
#             self.PONG:     'pong',
#             self.MESSAGE:  'message',
#             self.UPGRADE:  'upgrade',
#             self.NOOP:     'noop'
#         }[self.type]
# 
# Path: lib/engineio_client/utils.py
# def format_long(message, *args):
#     size = 700
#     ellipsis = '[...]'
#     formatted = message % args
#     return formatted[:size-len(ellipsis)]+ellipsis if len(formatted) > size else formatted


from ..transport import Transport
from ..parser import Packet
from ..utils import format_long
import requests
import gevent
import json
import logging
            'EIO': '3',
            'transport': self.name
        }
        if self.client and self.client.sid:
            query['sid'] = self.client.sid
        querystring = '?' + '&'.join(['='.join(item) for item in query.iteritems()]) if query else ''

        return '%s://%s:%d%s/%s' % (self.scheme, self.hostname, self.port, self.path, querystring)

    def handle_payload(self, payload):
        packets = self.parser.decode_payload(payload)
        for packet in packets:
            if self.state == 'opening':
                self.handle_open()
            self.handle_packet(packet)

    def loop_read(self):
        while self.state in ['opening', 'open']:
            try:
                payload = self.read()
                self.handle_payload(payload)
            except requests.RequestException as e:
                self.handle_error(e)

    def do_open(self):
        self.read_loop = self.client.start_loop(self.loop_read)

    def do_close(self):
        def close():
            logger.debug('Writing close packet')

# Path: lib/engineio_client/transport.py
# class Transport(Emitter):
#     """Abstract base class for transports implementations
#     """
#     def __init__(self, client, scheme, hostname, port, path, parser=None):
#         super(Transport, self).__init__()
#         self.client = client
#         self.scheme = scheme
#         self.hostname = hostname
#         self.port = port
#         self.path = path
#         self.parser = parser
# 
#         self.state = 'closed'
# 
#     def open(self):
#         if self.state == 'closed':
#             self.state = 'opening'
#             self.do_open()
# 
#     def close(self, send=True):
#         if self.state in ['open', 'opening']:
#             if send:
#                 self.do_close()
#             self.handle_close()
# 
#     def send(self, packets):
#         if self.state != 'open':
#             raise TransportNotOpenException()
# 
#         self.do_send(packets)
# 
#     def handle_error(self, error):
#         logger.warning("Transport error: %s", error)
#         self.emit('error', error)
# 
#     def handle_open(self):
#         logger.debug("Transport open")
#         self.state = 'open'
#         self.emit('open')
# 
#     def handle_close(self):
#         logger.debug("Transport closed")
#         self.state = 'closed'
#         self.emit('close')
# 
#     def handle_packet(self, packet):
#         logger.debug(format_long("Received packet: %s - %s", packet.type_string, repr(packet.data)))
#         self.emit('packet', packet)
# 
#     def do_open(self):
#         raise NotImplementedError()
# 
#     def do_close(self):
#         raise NotImplementedError()
# 
#     def do_send(self, packets):
#         raise NotImplementedError()
# 
# Path: lib/engineio_client/parser.py
# class Packet(object):
#     OPEN    = 0
#     CLOSE   = 1
#     PING    = 2
#     PONG    = 3
#     MESSAGE = 4
#     UPGRADE = 5
#     NOOP    = 6
# 
#     def __init__(self, _type, data='', binary=False):
#         self.type = _type
#         self.data = data
#         self.binary = binary
# 
#     @property
#     def type_string(self):
#         return {
#             self.OPEN:     'open',
#             self.CLOSE:    'close',
#             self.PING:     'ping',
#             self.PONG:     'pong',
#             self.MESSAGE:  'message',
#             self.UPGRADE:  'upgrade',
#             self.NOOP:     'noop'
#         }[self.type]
# 
# Path: lib/engineio_client/utils.py
# def format_long(message, *args):
#     size = 700
#     ellipsis = '[...]'
#     formatted = message % args
#     return formatted[:size-len(ellipsis)]+ellipsis if len(formatted) > size else formatted


# Path: lib/engineio_client/transports/polling.py
from ..transport import Transport
from ..parser import Packet
from ..utils import format_long
import requests
import gevent
import json
import logging

            'EIO': '3',
            'transport': self.name
        }
        if self.client and self.client.sid:
            query['sid'] = self.client.sid
        querystring = '?' + '&'.join(['='.join(item) for item in query.iteritems()]) if query else ''

        return '%s://%s:%d%s/%s' % (self.scheme, self.hostname, self.port, self.path, querystring)

    def handle_payload(self, payload):
        packets = self.parser.decode_payload(payload)
        for packet in packets:
            if self.state == 'opening':
                self.handle_open()
            self.handle_packet(packet)

    def loop_read(self):
        while self.state in ['opening', 'open']:
            try:
                payload = self.read()
                self.handle_payload(payload)
            except requests.RequestException as e:
                self.handle_error(e)

    def do_open(self):
        self.read_loop = self.client.start_loop(self.loop_read)

    def do_close(self):
        def close():
            logger.debug('Writing close packet')

            self.send([Packet(Packet.CLOSE)])