# 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
        
        remaining_tasks = 0
        def terminate_task():
            remaining_tasks -= 1
            if remaining_tasks <= 0:
                self.handle_pause()
        
        if self.reading:
            remaining_tasks += 1
            self.once('read-done', terminate_task)

        if self.writing:
            remaining_tasks += 1
            self.once('write-done', terminate_task)

    def read(self):
        self.reading = True
        logger.debug("Polling")
        r = self.session.get(self.get_uri(), stream=True)

        self.reading = False
        self.emit('read-done')

        r.raise_for_status()
        payload = r.raw.read()
        logger.debug(format_long("Received payload: %s", repr(payload)))
        return payload

    def write(self, payload):
        self.writing = True

# 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

        
        remaining_tasks = 0
        def terminate_task():
            remaining_tasks -= 1
            if remaining_tasks <= 0:
                self.handle_pause()
        
        if self.reading:
            remaining_tasks += 1
            self.once('read-done', terminate_task)

        if self.writing:
            remaining_tasks += 1
            self.once('write-done', terminate_task)

    def read(self):
        self.reading = True
        logger.debug("Polling")
        r = self.session.get(self.get_uri(), stream=True)

        self.reading = False
        self.emit('read-done')

        r.raise_for_status()
        payload = r.raw.read()
        logger.debug(format_long("Received payload: %s", repr(payload)))
        return payload

    def write(self, payload):
        self.writing = True

        logger.debug(format_long("Sending payload: %s", repr(payload)))