# Path: config.py
# class API:
# 
#     LISTEN_HOST = chain_load_setting("API_HOST", "YAMS_API_HOST", "127.0.0.1")
#     LISTEN_PORT = chain_load_setting("API_PORT", "YAMS_API_PORT", default=1111)
#     LISTEN_URL = "http://%s:%s" % (LISTEN_HOST, LISTEN_PORT)
#     # ----------------------------------------- #
#     # ---- Socket API ---- #
#     LISTEN_PORT_SOCKET = chain_load_setting("API_SOCKET_PORT", "YAMS_API_SOCKET_PORT", default=1112)
# 
#     # Yield epsilon is to avoid busy-waiting.  0 denotes yielding to any ready thread.  See scheduler docs.
#     # I've found 0 to fully utilize a core, so defaults to a longer interval to save electricity.
#     YIELD_TO_READY_THREADS=0
#     SOCKET_THREAD_YIELD_EPSILON = 0.25
#     SOCKET_TIMEOUT_SECONDS = 30
#     SOCKET_RX_BUFFER_BYTES = 4096
# 
#     # ----------------------------------------- #
# 
#     DEBUG = chain_load_setting("DEBUG", "YAMS_API_DEBUG", True)
# 
#     API_VERSION_CORE = chain_load_setting("API_VERSION_CORE", "YAMS_API_VERSION_CORE", default="dev")
#     API_VERSION_PLUGINS = chain_load_setting("API_VERSION_PLUGINS", "YAMS_API_VERSION_PLUGINS", default="dev")
# 
#     default_datadir = os.path.join(basedir, "yams_api", "data")
#     DATADIR = chain_load_setting("API_DATADIR", "YAMS_API_DATADIR", default=default_datadir)
# 
#     default_database = "sqlite:///" + os.path.join(basedir, "yams_api.sqlite")
#     SQLALCHEMY_DATABASE_URI = chain_load_setting("API_DATABASE_URI", "YAMS_API_DATABASE_URI", default=default_database)
# 
# 
#     # If we have redis, we have rate limiting available
#     try:
#         r = redis.Redis()
#         r.ping()
#         USE_RATE_LIMITS = True
#     except redis.ConnectionError:
#         USE_RATE_LIMITS = False
# 
# Path: yams_api/utils/logger.py
# class ContextFilter(logging.Filter):
# class Logger:
#     def filter(self, record):
#     def __init__(self, formatter=None, log_level=config.DEFAULT_LOG_LEVEL, syslog_address="/dev/log"):
#     def syslog(self):


import select
import socket
import socketserver
import sys
import _thread
import time
from config import API
from yams_api.utils.logger import log
from hashlib import md5
from ctypes import Structure
            log.info("Received poison pill for shutdown.")
            # if we didn't have any clients between startup and shutdown, rx_sockets will be empty.
            if rx_sockets:
                for sock in rx_sockets:
                    try:
                        sock.shutdown(1)
                        # give it a little time for the socket to process the shutdown
                        time.sleep(.2)
                        sock.close()
                    except OSError:
                        # socket probably wasn't connected at .shutdown()
                        pass

                    SOCKET_CONNECTIONS.remove(sock)

            raise SystemExit('YAMS Socket Server exiting due to a poison pill.')



        # to respond to conversation initiated by a client (recv())
        for sock in rx_sockets:

            if sock == sock_tcp:
                # The server socket is responsible for accepting incoming connections and responding with its own client
                # connection socket.  We know we have a new client conn when data comes into our server socket buffer.

                # There shouldn't be an issue with this strategy for select/async with non-blocking sockets, but if so, this
                # would be where to dispatch a new thread/native thread
                socket_fd, address = sock_tcp.accept()
                SOCKET_CONNECTIONS.append(socket_fd)

# Path: config.py
# class API:
# 
#     LISTEN_HOST = chain_load_setting("API_HOST", "YAMS_API_HOST", "127.0.0.1")
#     LISTEN_PORT = chain_load_setting("API_PORT", "YAMS_API_PORT", default=1111)
#     LISTEN_URL = "http://%s:%s" % (LISTEN_HOST, LISTEN_PORT)
#     # ----------------------------------------- #
#     # ---- Socket API ---- #
#     LISTEN_PORT_SOCKET = chain_load_setting("API_SOCKET_PORT", "YAMS_API_SOCKET_PORT", default=1112)
# 
#     # Yield epsilon is to avoid busy-waiting.  0 denotes yielding to any ready thread.  See scheduler docs.
#     # I've found 0 to fully utilize a core, so defaults to a longer interval to save electricity.
#     YIELD_TO_READY_THREADS=0
#     SOCKET_THREAD_YIELD_EPSILON = 0.25
#     SOCKET_TIMEOUT_SECONDS = 30
#     SOCKET_RX_BUFFER_BYTES = 4096
# 
#     # ----------------------------------------- #
# 
#     DEBUG = chain_load_setting("DEBUG", "YAMS_API_DEBUG", True)
# 
#     API_VERSION_CORE = chain_load_setting("API_VERSION_CORE", "YAMS_API_VERSION_CORE", default="dev")
#     API_VERSION_PLUGINS = chain_load_setting("API_VERSION_PLUGINS", "YAMS_API_VERSION_PLUGINS", default="dev")
# 
#     default_datadir = os.path.join(basedir, "yams_api", "data")
#     DATADIR = chain_load_setting("API_DATADIR", "YAMS_API_DATADIR", default=default_datadir)
# 
#     default_database = "sqlite:///" + os.path.join(basedir, "yams_api.sqlite")
#     SQLALCHEMY_DATABASE_URI = chain_load_setting("API_DATABASE_URI", "YAMS_API_DATABASE_URI", default=default_database)
# 
# 
#     # If we have redis, we have rate limiting available
#     try:
#         r = redis.Redis()
#         r.ping()
#         USE_RATE_LIMITS = True
#     except redis.ConnectionError:
#         USE_RATE_LIMITS = False
# 
# Path: yams_api/utils/logger.py
# class ContextFilter(logging.Filter):
# class Logger:
#     def filter(self, record):
#     def __init__(self, formatter=None, log_level=config.DEFAULT_LOG_LEVEL, syslog_address="/dev/log"):
#     def syslog(self):


# Path: yams_api/core/dev/transport/tcpsocket.py
import select
import socket
import socketserver
import sys
import _thread
import time
from config import API
from yams_api.utils.logger import log
from hashlib import md5
from ctypes import Structure

            log.info("Received poison pill for shutdown.")
            # if we didn't have any clients between startup and shutdown, rx_sockets will be empty.
            if rx_sockets:
                for sock in rx_sockets:
                    try:
                        sock.shutdown(1)
                        # give it a little time for the socket to process the shutdown
                        time.sleep(.2)
                        sock.close()
                    except OSError:
                        # socket probably wasn't connected at .shutdown()
                        pass

                    SOCKET_CONNECTIONS.remove(sock)

            raise SystemExit('YAMS Socket Server exiting due to a poison pill.')



        # to respond to conversation initiated by a client (recv())
        for sock in rx_sockets:

            if sock == sock_tcp:
                # The server socket is responsible for accepting incoming connections and responding with its own client
                # connection socket.  We know we have a new client conn when data comes into our server socket buffer.

                # There shouldn't be an issue with this strategy for select/async with non-blocking sockets, but if so, this
                # would be where to dispatch a new thread/native thread
                socket_fd, address = sock_tcp.accept()
                SOCKET_CONNECTIONS.append(socket_fd)

                log.info("Client (%s, %s) connected" % address)