Exemple #1
0
class LBRYStdoutDownloader():
    """This class downloads a live stream from the network and outputs it to standard out."""
    def __init__(self, dht_node_port, known_dht_nodes,
                 stream_info_manager_class=DBLiveStreamMetadataManager, blob_manager_class=TempBlobManager):
        """
        @param dht_node_port: the network port on which to listen for DHT node requests

        @param known_dht_nodes: a list of (ip_address, dht_port) which will be used to join the DHT network

        """

        self.session = LBRYSession(blob_manager_class=blob_manager_class,
                                   stream_info_manager_class=stream_info_manager_class,
                                   dht_node_class=Node, dht_node_port=dht_node_port, known_dht_nodes=known_dht_nodes,
                                   use_upnp=False)
        self.payment_rate_manager = BaseLiveStreamPaymentRateManager()

    def start(self):
        """Initialize the session"""
        d = self.session.setup()
        return d

    def read_sd_file(self, sd_blob):
        reader = BlobStreamDescriptorReader(sd_blob)
        return save_sd_info(self.stream_info_manager, reader, ignore_duplicate=True)

    def download_sd_file_from_hash(self, sd_hash):
        downloader = StandaloneBlobDownloader(sd_hash, self.session.blob_manager,
                                              self.session.peer_finder, self.session.rate_limiter,
                                              self.session.wallet)
        d = downloader.download()
        return d

    def start_download(self, sd_hash):
        """Start downloading the stream from the network and outputting it to standard out"""
        d = self.download_sd_file_from_hash(sd_hash)
        d.addCallbacks(self.read_sd_file)

        def start_stream(stream_hash):
            consumer = LBRYLiveStreamDownloader(stream_hash, self.session.peer_finder,
                                                self.session.rate_limiter, self.session.blob_manager,
                                                self.stream_info_manager, self.payment_rate_manager,
                                                self.session.wallet)
            return consumer.start()

        d.addCallback(start_stream)
        return d

    def shut_down(self):
        """End the session"""
        d = self.session.shut_down()
        return d
class LBRYStdinUploader():
    """This class reads from standard in, creates a stream, and makes it available on the network."""
    def __init__(self, peer_port, dht_node_port, known_dht_nodes,
                 stream_info_manager_class=DBLiveStreamMetadataManager, blob_manager_class=TempBlobManager):
        """
        @param peer_port: the network port on which to listen for peers

        @param dht_node_port: the network port on which to listen for nodes in the DHT

        @param known_dht_nodes: a list of (ip_address, dht_port) which will be used to join the DHT network
        """
        self.peer_port = peer_port
        self.lbry_server_port = None
        self.session = LBRYSession(blob_manager_class=blob_manager_class,
                                   stream_info_manager_class=stream_info_manager_class,
                                   dht_node_class=Node, dht_node_port=dht_node_port,
                                   known_dht_nodes=known_dht_nodes, peer_port=self.peer_port,
                                   use_upnp=False)
        self.payment_rate_manager = BaseLiveStreamPaymentRateManager()

    def start(self):
        """Initialize the session and start listening on the peer port"""
        d = self.session.setup()
        d.addCallback(lambda _: self._start())

        return d

    def _start(self):
        self._start_server()
        return True

    def _start_server(self):
        query_handler_factories = [
            CryptBlobInfoQueryHandlerFactory(self.stream_info_manager, self.session.wallet,
                                             self.payment_rate_manager),
            BlobAvailabilityHandlerFactory(self.session.blob_manager),
            BlobRequestHandlerFactory(self.session.blob_manager, self.session.wallet,
                                      self.payment_rate_manager),
            self.session.wallet.get_wallet_info_query_handler_factory()
        ]

        self.server_factory = ServerProtocolFactory(self.session.rate_limiter,
                                                    query_handler_factories,
                                                    self.session.peer_manager)
        from twisted.internet import reactor
        self.lbry_server_port = reactor.listenTCP(self.peer_port, self.server_factory)

    def start_live_stream(self, stream_name):
        """Create the stream and start reading from stdin

        @param stream_name: a string, the suggested name of this stream
        """
        stream_creator_helper = StdOutLiveStreamCreator(stream_name, self.session.blob_manager,
                                                        self.stream_info_manager)
        d = stream_creator_helper.create_and_publish_stream_descriptor()

        def print_sd_hash(sd_hash):
            print "Stream descriptor hash:", sd_hash

        d.addCallback(print_sd_hash)
        d.addCallback(lambda _: stream_creator_helper.start_streaming())
        return d

    def shut_down(self):
        """End the session and stop listening on the server port"""
        d = self.session.shut_down()
        d.addCallback(lambda _: self._shut_down())
        return d

    def _shut_down(self):
        if self.lbry_server_port is not None:
            d = defer.maybeDeferred(self.lbry_server_port.stopListening)
        else:
            d = defer.succeed(True)
        return d