# Path: smarkets/signal.py
# class Signal(object):
# 
#     """
#     All instance methods of this class are thread safe.
#     """
# 
#     def __init__(self):
#         self._handlers = set()
# 
#     def add(self, handler):
#         """
#         Add signal handler. You can also do::
# 
#             signal = Signal()
#             signal += handler
#         """
#         self._handlers.add(handler)
# 
#         return self
# 
#     __iadd__ = add
#     handle = add
# 
#     def remove(self, handler):
#         """
#         Remove signal handler. You can also do::
# 
#             signal = Signal()
#             # add a handler "handler"
#             signal -= handler
#         """
#         self._handlers.remove(handler)
# 
#         return self
# 
#     __isub__ = remove
#     __unhandle__ = remove
# 
#     def fire(self, **kwargs):
#         """Execute all handlers associated with this Signal.
# 
#         You can also call signal object to get the same result::
# 
#             signal = Signal()
#             signal()  # calls the signal handler
#         """
#         for handler in copy(self._handlers):
#             handler(**kwargs)
# 
#     __call__ = fire
# 
#     def __len__(self):
#         return len(self._handlers)
# 
#     def __iter__(self):
#         return iter(self._handlers)
# 
# Path: smarkets/streaming_api/eto.py

# 
# Path: smarkets/streaming_api/seto.py

# 
# Path: smarkets/streaming_api/exceptions.py
# class InvalidCallbackError(_Error):
# 
#     "Invalid callback was specified"
#     pass
# 
# class LoginError(_Error):
# 
#     "Raised when a login is not successful"
#     def __init__(self, reason):
#         self.reason_msg = 'Unknown'
#         self.reason = reason
#         if reason in LogoutReason.values():
#             self.reason_msg = LogoutReason.Name(reason)
# 
# class LoginTimeout(_Error):
# 
#     "Raised when no message is received after sending login request"
#     pass
# 
# Path: smarkets/streaming_api/utils.py
# def set_payload_message(payload, message):
#     underscore_form = camel_case_to_underscores(type(message).__name__)
#     payload_type = getattr(seto, 'PAYLOAD_' + underscore_form.upper())
#     payload.type = payload_type
#     getattr(payload, underscore_form).CopyFrom(message)


import logging
import sys
from smarkets.signal import Signal
from smarkets.streaming_api import eto
from smarkets.streaming_api import seto
from smarkets.streaming_api.exceptions import InvalidCallbackError, LoginError, LoginTimeout
from smarkets.streaming_api.utils import set_payload_message
                    self._dispatch(frame)
                    processed += 1
                else:
                    break

        return processed

    def flush(self):
        "Flush the send buffer"
        self.session.flush()

    def send(self, message):
        payload = self.session.out_payload
        payload.Clear()
        set_payload_message(payload, message)
        self._send()

    def ping(self):
        "Ping the service"
        msg = self.session.out_payload
        msg.Clear()
        msg.type = seto.PAYLOAD_ETO
        msg.eto_payload.type = eto.PAYLOAD_PING
        self._send()

    def add_handler(self, name, callback):
        "Add a callback handler"
        if not hasattr(callback, '__call__'):
            raise ValueError('callback must be a callable')
        if name not in self.callbacks:

# Path: smarkets/signal.py
# class Signal(object):
# 
#     """
#     All instance methods of this class are thread safe.
#     """
# 
#     def __init__(self):
#         self._handlers = set()
# 
#     def add(self, handler):
#         """
#         Add signal handler. You can also do::
# 
#             signal = Signal()
#             signal += handler
#         """
#         self._handlers.add(handler)
# 
#         return self
# 
#     __iadd__ = add
#     handle = add
# 
#     def remove(self, handler):
#         """
#         Remove signal handler. You can also do::
# 
#             signal = Signal()
#             # add a handler "handler"
#             signal -= handler
#         """
#         self._handlers.remove(handler)
# 
#         return self
# 
#     __isub__ = remove
#     __unhandle__ = remove
# 
#     def fire(self, **kwargs):
#         """Execute all handlers associated with this Signal.
# 
#         You can also call signal object to get the same result::
# 
#             signal = Signal()
#             signal()  # calls the signal handler
#         """
#         for handler in copy(self._handlers):
#             handler(**kwargs)
# 
#     __call__ = fire
# 
#     def __len__(self):
#         return len(self._handlers)
# 
#     def __iter__(self):
#         return iter(self._handlers)
# 
# Path: smarkets/streaming_api/eto.py

# 
# Path: smarkets/streaming_api/seto.py

# 
# Path: smarkets/streaming_api/exceptions.py
# class InvalidCallbackError(_Error):
# 
#     "Invalid callback was specified"
#     pass
# 
# class LoginError(_Error):
# 
#     "Raised when a login is not successful"
#     def __init__(self, reason):
#         self.reason_msg = 'Unknown'
#         self.reason = reason
#         if reason in LogoutReason.values():
#             self.reason_msg = LogoutReason.Name(reason)
# 
# class LoginTimeout(_Error):
# 
#     "Raised when no message is received after sending login request"
#     pass
# 
# Path: smarkets/streaming_api/utils.py
# def set_payload_message(payload, message):
#     underscore_form = camel_case_to_underscores(type(message).__name__)
#     payload_type = getattr(seto, 'PAYLOAD_' + underscore_form.upper())
#     payload.type = payload_type
#     getattr(payload, underscore_form).CopyFrom(message)


# Path: smarkets/streaming_api/client.py
import logging
import sys
from smarkets.signal import Signal
from smarkets.streaming_api import eto
from smarkets.streaming_api import seto
from smarkets.streaming_api.exceptions import InvalidCallbackError, LoginError, LoginTimeout
from smarkets.streaming_api.utils import set_payload_message

                    self._dispatch(frame)
                    processed += 1
                else:
                    break

        return processed

    def flush(self):
        "Flush the send buffer"
        self.session.flush()

    def send(self, message):
        payload = self.session.out_payload
        payload.Clear()
        set_payload_message(payload, message)
        self._send()

    def ping(self):
        "Ping the service"
        msg = self.session.out_payload
        msg.Clear()
        msg.type = seto.PAYLOAD_ETO
        msg.eto_payload.type = eto.PAYLOAD_PING
        self._send()

    def add_handler(self, name, callback):
        "Add a callback handler"
        if not hasattr(callback, '__call__'):
            raise ValueError('callback must be a callable')
        if name not in self.callbacks:

            raise InvalidCallbackError(name)