# Path: amfast/remoting/wsgi_channel.py
# class WsgiChannelSet(ChannelSet):
#     def __call__(self, environ, start_response):
#         channel_name = environ['PATH_INFO'][1:]
#         channel = self.getChannel(channel_name)
#         return channel(environ, start_response)
# 
# class WsgiChannel(HttpChannel):
#     """WSGI app channel."""
# 
#     def __init__(self, *args, **kwargs):
# 
#         if len(args) > 3:
#             wait_interval = args[3]
#         elif 'wait_interval' in kwargs:
#             wait_interval = kwargs['wait_interval']
#         else:
#             wait_interval = 0
# 
#         if wait_interval < 0:
#             # The only reliable way to detect
#             # when a client has disconnected with
#             # WSGI is that the 'write' function will fail.
#             #
#             # With long-polling, nothing is written
#             # until a message is published to a client,
#             # so we are unable to detect if we are
#             # waiting for a message for a disconnected client.
#             #
#             # wait_interval must be non-negative to avoid
#             # zombie threads.
#             raise ChannelError('wait_interval < 0 is not supported by WsgiChannel')
#             
#         HttpChannel.__init__(self, *args, **kwargs)
# 
#     def __call__(self, environ, start_response):
#         if environ['REQUEST_METHOD'] != 'POST':
#             return self.badMethod(start_response)
# 
#         len_str = 'CONTENT_LENGTH'
#         raw_request = environ['wsgi.input'].read(int(environ[len_str]))
# 
#         try:
#             request_packet = self.decode(raw_request)
#         except AmFastError, exc:
#             return self.badRequest(start_response, self.getBadEncodingMsg())
#         except (KeyboardInterrupt, SystemExit):
#             raise
#         except Exception, exc:
#             amfast.log_exc(exc)
#             return self.badServer(start_response, self.getBadServerMsg())
# 
#         try:
#             content = self.invoke(request_packet)
#             response = self.encode(content)
#         except (KeyboardInterrupt, SystemExit):
#             raise
#         except Exception, exc:
#             amfast.log_exc(exc)
#             return self.badServer(start_response, self.getBadServerMsg())
# 
#         return self.getResponse(start_response, response)
# 
#     def getResponse(self, start_response, response):
#         start_response('200 OK', [
#             ('Content-Type', self.CONTENT_TYPE),
#             ('Content-Length', str(len(response)))
#         ])
# 
#         return [response]
# 
#     def badMethod(self, start_response):
#         response = self.getBadMethodMsg()
# 
#         start_response('405 Method Not Allowed', [
#             ('Content-Type', 'text/plain'),
#             ('Content-Length', str(len(response)))
#         ])
# 
#         return [response]
# 
#     def badRequest(self, start_response, response):
#         start_response('400 Bad Request', [
#             ('Content-Type', 'text/plain'),
#             ('Content-Length', str(len(response)))
#         ])
# 
#         return [response]
# 
#     def badPage(self, start_response, response):
#         start_response('404 Not Found', [
#             ('Content-Type', 'text/plain'),
#             ('Content-Length', str(len(response)))
#         ])
# 
#         return [response]
# 
#     def badServer(self, start_response, response):
#         start_response('500 Internal Server Error', [
#             ('Content-Type', 'text/plain'),
#             ('Content-Length', str(len(response)))
#         ])
# 
#         return [response]


import os
import optparse
import logging
import sys
import cherrypy
import amfast
from amfast.remoting.wsgi_channel import WsgiChannelSet, WsgiChannel
    handler = logging.StreamHandler(sys.stdout)
    handler.setLevel(logging.DEBUG)
    amfast.logger.addHandler(handler)

    cp_options = {
        'global':
        {
            'server.socket_port': int(options.port),
            'server.socket_host': str(options.domain),
        },
        '/':
        {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': os.path.join(os.getcwd(), '../flex/deploy')
        }
    }

    channel_set = WsgiChannelSet(notify_connections=False)

    # Clients connect every x seconds
    # to polling channels to check for messages.
    # If messages are available, they are
    # returned to the client.
    polling_channel = WsgiChannel('amf')
    channel_set.mapChannel(polling_channel)

    # Long-poll channels do not return
    # a response to the client until
    # a message is available, or channel.max_interval
    # is reached.

# Path: amfast/remoting/wsgi_channel.py
# class WsgiChannelSet(ChannelSet):
#     def __call__(self, environ, start_response):
#         channel_name = environ['PATH_INFO'][1:]
#         channel = self.getChannel(channel_name)
#         return channel(environ, start_response)
# 
# class WsgiChannel(HttpChannel):
#     """WSGI app channel."""
# 
#     def __init__(self, *args, **kwargs):
# 
#         if len(args) > 3:
#             wait_interval = args[3]
#         elif 'wait_interval' in kwargs:
#             wait_interval = kwargs['wait_interval']
#         else:
#             wait_interval = 0
# 
#         if wait_interval < 0:
#             # The only reliable way to detect
#             # when a client has disconnected with
#             # WSGI is that the 'write' function will fail.
#             #
#             # With long-polling, nothing is written
#             # until a message is published to a client,
#             # so we are unable to detect if we are
#             # waiting for a message for a disconnected client.
#             #
#             # wait_interval must be non-negative to avoid
#             # zombie threads.
#             raise ChannelError('wait_interval < 0 is not supported by WsgiChannel')
#             
#         HttpChannel.__init__(self, *args, **kwargs)
# 
#     def __call__(self, environ, start_response):
#         if environ['REQUEST_METHOD'] != 'POST':
#             return self.badMethod(start_response)
# 
#         len_str = 'CONTENT_LENGTH'
#         raw_request = environ['wsgi.input'].read(int(environ[len_str]))
# 
#         try:
#             request_packet = self.decode(raw_request)
#         except AmFastError, exc:
#             return self.badRequest(start_response, self.getBadEncodingMsg())
#         except (KeyboardInterrupt, SystemExit):
#             raise
#         except Exception, exc:
#             amfast.log_exc(exc)
#             return self.badServer(start_response, self.getBadServerMsg())
# 
#         try:
#             content = self.invoke(request_packet)
#             response = self.encode(content)
#         except (KeyboardInterrupt, SystemExit):
#             raise
#         except Exception, exc:
#             amfast.log_exc(exc)
#             return self.badServer(start_response, self.getBadServerMsg())
# 
#         return self.getResponse(start_response, response)
# 
#     def getResponse(self, start_response, response):
#         start_response('200 OK', [
#             ('Content-Type', self.CONTENT_TYPE),
#             ('Content-Length', str(len(response)))
#         ])
# 
#         return [response]
# 
#     def badMethod(self, start_response):
#         response = self.getBadMethodMsg()
# 
#         start_response('405 Method Not Allowed', [
#             ('Content-Type', 'text/plain'),
#             ('Content-Length', str(len(response)))
#         ])
# 
#         return [response]
# 
#     def badRequest(self, start_response, response):
#         start_response('400 Bad Request', [
#             ('Content-Type', 'text/plain'),
#             ('Content-Length', str(len(response)))
#         ])
# 
#         return [response]
# 
#     def badPage(self, start_response, response):
#         start_response('404 Not Found', [
#             ('Content-Type', 'text/plain'),
#             ('Content-Length', str(len(response)))
#         ])
# 
#         return [response]
# 
#     def badServer(self, start_response, response):
#         start_response('500 Internal Server Error', [
#             ('Content-Type', 'text/plain'),
#             ('Content-Length', str(len(response)))
#         ])
# 
#         return [response]


# Path: examples/messaging/python/cp_wsgi_server.py
import os
import optparse
import logging
import sys
import cherrypy
import amfast
from amfast.remoting.wsgi_channel import WsgiChannelSet, WsgiChannel

    handler = logging.StreamHandler(sys.stdout)
    handler.setLevel(logging.DEBUG)
    amfast.logger.addHandler(handler)

    cp_options = {
        'global':
        {
            'server.socket_port': int(options.port),
            'server.socket_host': str(options.domain),
        },
        '/':
        {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': os.path.join(os.getcwd(), '../flex/deploy')
        }
    }

    channel_set = WsgiChannelSet(notify_connections=False)

    # Clients connect every x seconds
    # to polling channels to check for messages.
    # If messages are available, they are
    # returned to the client.
    polling_channel = WsgiChannel('amf')
    channel_set.mapChannel(polling_channel)

    # Long-poll channels do not return
    # a response to the client until
    # a message is available, or channel.max_interval
    # is reached.

    long_poll_channel = WsgiChannel('longPoll', wait_interval=90)