# Path: chirp/common/http_console_server.py
# DEFAULT_HOST = "localhost"
# DEFAULT_PORT = 9000
#     _TIMEOUT_S = 2.0
# class _RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
# class _HttpConsoleServer(object):
#     def _dispatch(self):
#     def do_GET(self):
#     def do_POST(self):
#     def _send_full_response(self, content_type, body):
#     def send_text(self, body):
#     def send_html(self, body):
#     def log_message(self, format, *args):
# def build_sysinfo_page():
#     def add(key, val):
#     def __init__(self, host, port):
#     def _thread_worker(self):
#     def stop(self):
# def start(host=DEFAULT_HOST, port=DEFAULT_PORT):
# def stop():
# def url(path=None):
# def register(path, handler_fn, require_post=False):
# 
# Path: chirp/common/conf.py

# 
# Path: chirp/stream/archiver.py
# class Archiver(message.MessageConsumer):
#     ROOT_DIR = "/tmp/test-archives"
#     FILE_PREFIX = "chirp"
#     def __init__(self, src):
#     def _dirname(self, ts_ms):
#     def _open(self, ts_ms):
#     def _process_message(self, msg):
#     def _done_looping(self):
# 
# Path: chirp/stream/frame_splitter.py
# class FrameSplitter(message.MessageConsumer,
#                     message.MessageSource):
#     def __init__(self, src):
#     def _process_message(self, msg):
# 
# Path: chirp/stream/http_puller.py
# _HTTP_REQUEST = """GET %(path)s HTTP/1.1
# User-Agent: CHIRP stream_monitor
# Host: %(host)s
# Accept: */*
# Connection: Keep-Alive
# 
# """.replace("\n", "\r\n")
#     READ_TIMEOUT_S = 1
#     READ_SIZE = 2048
#     BACKOFF_S = 0.5  # i.e. 500ms
# def _parse_http_headers(header_str):
#     def __init__(self, host, post, path):
#     def _now_ms(self):
#     def _create_socket(self):
#     def _backoff(self):
#     def _start_message(self):
#     def _finish_message(self, msg, error_type=None, error=None):
#     def _connect(self):
#     def _handle_redirect(self, location):
#     def _pull_one_block(self):
#     def _loop_once(self):
#     def _done_looping(self):
# class HttpPuller(looper.Looper, message.MessageSource):
# 
# Path: chirp/stream/message.py
# CONNECTED = "Connected"        # New connection initiated.
# RESPONSE = "Response"          # HTTP response received.
# BLOCK = "Block"                # A chunk of raw data.
# FRAME = "Frame"                # An MP3 frame.
# ERROR = "Error"                # An error.
# CONNECT_TIMEOUT_ERROR = "Connect Timeout Error"
# CONNECT_ERROR = "Connect Error"
# REQUEST_ERROR = "Request Error"
# READ_TIMEOUT_ERROR = "Read Timeout Error"
# READ_ERROR = "Read Error"
# MISSING_HEADERS_ERROR = "Missing HTTP Headers Error"
# MALFORMED_HEADERS_ERROR = "Malformed HTTP Headers Error"
# BAD_REDIRECT_ERROR = "Bad Redirect Error"
# END_OF_STREAM_ERROR = "End of Stream Error"
# class Message(object):
# class MessageSource(object):
# class MessageConsumer(looper.Looper):
# class MessageTee(MessageConsumer):
#     def payload_sha1(self):
#     def payload_hex_sha1(self):
#     def __str__(self):
#     def set_error(self, error_type, err):
#     def is_error(self):
#     def is_end_of_stream(self):
#     def __init__(self):
#     def _add_message(self, msg):
#     def _add_stop_message(self):
#     def get_next_message(self, timeout=None):
#     def get_all_messages(self):
#     def wait_until_empty(self):
#     def __init__(self, src):
#     def _process_message(self, msg):
#     def _loop_once(self):
#     def __init__(self, input_src, num_outputs):
#     def _process_message(self, msg):
# 
# Path: chirp/stream/statistics.py
# class _ConnectionInfo(object):
# class Statistics(message.MessageConsumer):
#     MAX_NUM_ERRORS = 25
#     MAX_NUM_RECENT_CONNECTIONS = 20
#     def __init__(self):
#     def process(self, msg):
#     def html(self):
#         def add(key, val):
#         def add_since_ms(key, ts_ms):
#     def __init__(self, src):
#     def _process_message(self, msg):
#     def _connections_html(self, unused_request):
#     def export(self, path=None):


import sys
import time
from chirp.common import http_console_server
from chirp.common.conf import (ARCHIVER_PORT, STREAM_HOST, STREAM_PORT,
                               STREAM_PATH, ARCHIVES_DIR)
from chirp.stream import archiver
from chirp.stream import frame_splitter
from chirp.stream import http_puller
from chirp.stream import message
from chirp.stream import statistics
    arch = archiver.Archiver(fs_1)
    arch.ROOT_DIR = ARCHIVES_DIR
 
    stats = statistics.Statistics(fs_2)
    stats.export()

    all_objects = (hp, fs, fs_tee, arch, stats)
    for obj in all_objects:
        obj.loop_in_thread()

    sys.stderr.write(">>> pipeline running\n")

    # Wait indefinitely.
    try:
        while True: time.sleep(1 << 20)  # 2^20 seconds = ~12 days
    except KeyboardInterrupt:
        pass

    sys.stderr.write(">>> shutting down pipeline\n")
    hp.stop()
    for obj in all_objects:
        obj.wait()


def main():
    http_console_server.start(port=ARCHIVER_PORT)
    sys.stderr.write(">>> starting pipeline\n")
    try:
        run_pipeline()
    finally:

# Path: chirp/common/http_console_server.py
# DEFAULT_HOST = "localhost"
# DEFAULT_PORT = 9000
#     _TIMEOUT_S = 2.0
# class _RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
# class _HttpConsoleServer(object):
#     def _dispatch(self):
#     def do_GET(self):
#     def do_POST(self):
#     def _send_full_response(self, content_type, body):
#     def send_text(self, body):
#     def send_html(self, body):
#     def log_message(self, format, *args):
# def build_sysinfo_page():
#     def add(key, val):
#     def __init__(self, host, port):
#     def _thread_worker(self):
#     def stop(self):
# def start(host=DEFAULT_HOST, port=DEFAULT_PORT):
# def stop():
# def url(path=None):
# def register(path, handler_fn, require_post=False):
# 
# Path: chirp/common/conf.py

# 
# Path: chirp/stream/archiver.py
# class Archiver(message.MessageConsumer):
#     ROOT_DIR = "/tmp/test-archives"
#     FILE_PREFIX = "chirp"
#     def __init__(self, src):
#     def _dirname(self, ts_ms):
#     def _open(self, ts_ms):
#     def _process_message(self, msg):
#     def _done_looping(self):
# 
# Path: chirp/stream/frame_splitter.py
# class FrameSplitter(message.MessageConsumer,
#                     message.MessageSource):
#     def __init__(self, src):
#     def _process_message(self, msg):
# 
# Path: chirp/stream/http_puller.py
# _HTTP_REQUEST = """GET %(path)s HTTP/1.1
# User-Agent: CHIRP stream_monitor
# Host: %(host)s
# Accept: */*
# Connection: Keep-Alive
# 
# """.replace("\n", "\r\n")
#     READ_TIMEOUT_S = 1
#     READ_SIZE = 2048
#     BACKOFF_S = 0.5  # i.e. 500ms
# def _parse_http_headers(header_str):
#     def __init__(self, host, post, path):
#     def _now_ms(self):
#     def _create_socket(self):
#     def _backoff(self):
#     def _start_message(self):
#     def _finish_message(self, msg, error_type=None, error=None):
#     def _connect(self):
#     def _handle_redirect(self, location):
#     def _pull_one_block(self):
#     def _loop_once(self):
#     def _done_looping(self):
# class HttpPuller(looper.Looper, message.MessageSource):
# 
# Path: chirp/stream/message.py
# CONNECTED = "Connected"        # New connection initiated.
# RESPONSE = "Response"          # HTTP response received.
# BLOCK = "Block"                # A chunk of raw data.
# FRAME = "Frame"                # An MP3 frame.
# ERROR = "Error"                # An error.
# CONNECT_TIMEOUT_ERROR = "Connect Timeout Error"
# CONNECT_ERROR = "Connect Error"
# REQUEST_ERROR = "Request Error"
# READ_TIMEOUT_ERROR = "Read Timeout Error"
# READ_ERROR = "Read Error"
# MISSING_HEADERS_ERROR = "Missing HTTP Headers Error"
# MALFORMED_HEADERS_ERROR = "Malformed HTTP Headers Error"
# BAD_REDIRECT_ERROR = "Bad Redirect Error"
# END_OF_STREAM_ERROR = "End of Stream Error"
# class Message(object):
# class MessageSource(object):
# class MessageConsumer(looper.Looper):
# class MessageTee(MessageConsumer):
#     def payload_sha1(self):
#     def payload_hex_sha1(self):
#     def __str__(self):
#     def set_error(self, error_type, err):
#     def is_error(self):
#     def is_end_of_stream(self):
#     def __init__(self):
#     def _add_message(self, msg):
#     def _add_stop_message(self):
#     def get_next_message(self, timeout=None):
#     def get_all_messages(self):
#     def wait_until_empty(self):
#     def __init__(self, src):
#     def _process_message(self, msg):
#     def _loop_once(self):
#     def __init__(self, input_src, num_outputs):
#     def _process_message(self, msg):
# 
# Path: chirp/stream/statistics.py
# class _ConnectionInfo(object):
# class Statistics(message.MessageConsumer):
#     MAX_NUM_ERRORS = 25
#     MAX_NUM_RECENT_CONNECTIONS = 20
#     def __init__(self):
#     def process(self, msg):
#     def html(self):
#         def add(key, val):
#         def add_since_ms(key, ts_ms):
#     def __init__(self, src):
#     def _process_message(self, msg):
#     def _connections_html(self, unused_request):
#     def export(self, path=None):


# Path: chirp/stream/do_archive_stream.py
import sys
import time
from chirp.common import http_console_server
from chirp.common.conf import (ARCHIVER_PORT, STREAM_HOST, STREAM_PORT,
                               STREAM_PATH, ARCHIVES_DIR)
from chirp.stream import archiver
from chirp.stream import frame_splitter
from chirp.stream import http_puller
from chirp.stream import message
from chirp.stream import statistics

    arch = archiver.Archiver(fs_1)
    arch.ROOT_DIR = ARCHIVES_DIR
 
    stats = statistics.Statistics(fs_2)
    stats.export()

    all_objects = (hp, fs, fs_tee, arch, stats)
    for obj in all_objects:
        obj.loop_in_thread()

    sys.stderr.write(">>> pipeline running\n")

    # Wait indefinitely.
    try:
        while True: time.sleep(1 << 20)  # 2^20 seconds = ~12 days
    except KeyboardInterrupt:
        pass

    sys.stderr.write(">>> shutting down pipeline\n")
    hp.stop()
    for obj in all_objects:
        obj.wait()


def main():
    http_console_server.start(port=ARCHIVER_PORT)
    sys.stderr.write(">>> starting pipeline\n")
    try:
        run_pipeline()
    finally:

        http_console_server.stop()