# 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/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):


import threading
import time
import socket
import unittest
from chirp.stream import message
from chirp.stream import http_puller
        hp = MockHttpPuller("host", 1729, "/foo")
        hp.next_socket.simulate_connect_error = True
        hp._loop_once()
        # The puller should not have a saved socket.
        self.assertTrue(hp._sock is None)
        # A CONNECT_ERROR error message should be put onto the message
        # queue.
        msg = hp.get_next_message()
        self.assertMessageError(message.CONNECT_ERROR, msg)
        # We should have backed off after the error.
        self.assertEqual(1, hp.backoff_count)

        # Simulate a problem sending the request.
        hp = MockHttpPuller("host", 1729, "/foo")
        hp.next_socket.simulate_sendall_error = True
        hp._loop_once()
        # The puller should not have a saved socket.
        self.assertTrue(hp._sock is None)
        # A REQUEST_ERROR error message should be put onto the message
        # queue.
        msg = hp.get_next_message()
        self.assertMessageError(message.REQUEST_ERROR, msg)
        # We should have backed off after the error.
        self.assertEqual(1, hp.backoff_count)

    def get_connected_http_puller(self):
        # Simulate successfully connecting to the remote host.
        hp = MockHttpPuller("host", 1729, "/foo")
        hp._loop_once()
        msg = hp.get_next_message()

# 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/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/http_puller_test.py
import threading
import time
import socket
import unittest
from chirp.stream import message
from chirp.stream import http_puller

        hp = MockHttpPuller("host", 1729, "/foo")
        hp.next_socket.simulate_connect_error = True
        hp._loop_once()
        # The puller should not have a saved socket.
        self.assertTrue(hp._sock is None)
        # A CONNECT_ERROR error message should be put onto the message
        # queue.
        msg = hp.get_next_message()
        self.assertMessageError(message.CONNECT_ERROR, msg)
        # We should have backed off after the error.
        self.assertEqual(1, hp.backoff_count)

        # Simulate a problem sending the request.
        hp = MockHttpPuller("host", 1729, "/foo")
        hp.next_socket.simulate_sendall_error = True
        hp._loop_once()
        # The puller should not have a saved socket.
        self.assertTrue(hp._sock is None)
        # A REQUEST_ERROR error message should be put onto the message
        # queue.
        msg = hp.get_next_message()
        self.assertMessageError(message.REQUEST_ERROR, msg)
        # We should have backed off after the error.
        self.assertEqual(1, hp.backoff_count)

    def get_connected_http_puller(self):
        # Simulate successfully connecting to the remote host.
        hp = MockHttpPuller("host", 1729, "/foo")
        hp._loop_once()
        msg = hp.get_next_message()

        self.assertEqual(message.CONNECTED, msg.message_type)