示例#1
0
文件: client.py 项目: tpltnt/foolscap
def run_command(config):
    c = dispatch_table[config.subCommand]()
    tub = Tub()
    try:
        from twisted.internet import reactor
        from twisted.internet.endpoints import clientFromString
        from foolscap.connections import tor
        CONTROL = os.environ.get("FOOLSCAP_TOR_CONTROL_PORT", "")
        SOCKS = os.environ.get("FOOLSCAP_TOR_SOCKS_PORT", "")
        if CONTROL:
            h = tor.control_endpoint(clientFromString(reactor, CONTROL))
            tub.addConnectionHintHandler("tor", h)
        elif SOCKS:
            h = tor.socks_endpoint(clientFromString(reactor, SOCKS))
            tub.addConnectionHintHandler("tor", h)
        #else:
        #    h = tor.default_socks()
        #    tub.addConnectionHintHandler("tor", h)
    except ImportError:
        pass
    d = defer.succeed(None)
    d.addCallback(lambda _ign: tub.startService())
    d.addCallback(lambda _ign: tub.getReference(config.furl))
    d.addCallback(c.run, config.subOptions) # might provide tub here
    d.addBoth(lambda res: tub.stopService().addCallback(lambda _ign: res))
    return d
示例#2
0
    def test_wait_for_brokers(self):
        """
        The L{Deferred} returned by L{Tub.stopService} fires only after the
        L{Broker} connections belonging to the L{Tub} have disconnected.
        """
        tub = Tub()
        tub.startService()

        another_tub = Tub()
        another_tub.startService()

        brokers = list(tub.brokerClass(None) for i in range(3))
        for n, b in enumerate(brokers):
            b.makeConnection(StringTransport())
            ref = SturdyRef(encode_furl(another_tub.tubID, [], str(n)))
            tub.brokerAttached(ref, b, isClient=(n % 2) == 1)

        stopping = tub.stopService()
        d = flushEventualQueue()

        def event(ignored):
            self.assertNoResult(stopping)
            for b in brokers:
                b.connectionLost(failure.Failure(Exception("Connection lost")))
            return flushEventualQueue()

        d.addCallback(event)

        def connectionsLost(ignored):
            self.successResultOf(stopping)

        d.addCallback(connectionsLost)

        return d
示例#3
0
    def test_wait_for_brokers(self):
        """
        The L{Deferred} returned by L{Tub.stopService} fires only after the
        L{Broker} connections belonging to the L{Tub} have disconnected.
        """
        tub = Tub()
        tub.startService()

        another_tub = Tub()
        another_tub.startService()

        brokers = list(tub.brokerClass(None) for i in range(3))
        for n, b in enumerate(brokers):
            b.makeConnection(StringTransport())
            ref = SturdyRef(encode_furl(another_tub.tubID, [], str(n)))
            tub.brokerAttached(ref, b, isClient=(n % 2)==1)

        stopping = tub.stopService()
        d = flushEventualQueue()

        def event(ignored):
            self.assertNoResult(stopping)
            for b in brokers:
                b.connectionLost(failure.Failure(Exception("Connection lost")))
            return flushEventualQueue()
        d.addCallback(event)

        def connectionsLost(ignored):
            self.successResultOf(stopping)
        d.addCallback(connectionsLost)

        return d
示例#4
0
文件: client.py 项目: public/foolscap
def run_command(config):
    c = dispatch_table[config.subCommand]()
    tub = Tub()
    d = defer.succeed(None)
    d.addCallback(lambda _ign: tub.startService())
    d.addCallback(lambda _ign: tub.getReference(config.furl))
    d.addCallback(c.run, config.subOptions)  # might provide tub here
    d.addBoth(lambda res: tub.stopService().addCallback(lambda _ign: res))
    return d
示例#5
0
def run_command(config):
    c = dispatch_table[config.subCommand]()
    tub = Tub()
    d = defer.succeed(None)
    d.addCallback(lambda _ign: tub.startService())
    d.addCallback(lambda _ign: tub.getReference(config.furl))
    d.addCallback(c.run, config.subOptions)  # might provide tub here
    d.addBoth(lambda res: tub.stopService().addCallback(lambda _ign: res))
    return d
示例#6
0
 def test_doublestop(self):
     tub = Tub()
     tub.startService()
     d = tub.stopService()
     d.addCallback(lambda res: self.shouldFail(
         RuntimeError, "test_doublestop_startService",
         "Sorry, but Tubs cannot be restarted", tub.startService))
     d.addCallback(lambda res: self.shouldFail(
         RuntimeError, "test_doublestop_getReference",
         "Sorry, but this Tub has been shut down", tub.getReference, "furl")
                   )
     d.addCallback(lambda res: self.shouldFail(
         RuntimeError, "test_doublestop_connectTo",
         "Sorry, but this Tub has been shut down", tub.connectTo, "furl",
         None))
     return d
示例#7
0
 def test_doublestop(self):
     tub = Tub()
     tub.startService()
     d = tub.stopService()
     d.addCallback(lambda res:
                   self.shouldFail(RuntimeError,
                                   "test_doublestop_startService",
                                   "Sorry, but Tubs cannot be restarted",
                                   tub.startService))
     d.addCallback(lambda res:
                   self.shouldFail(RuntimeError,
                                   "test_doublestop_getReference",
                                   "Sorry, but this Tub has been shut down",
                                   tub.getReference, "furl"))
     d.addCallback(lambda res:
                   self.shouldFail(RuntimeError,
                                   "test_doublestop_connectTo",
                                   "Sorry, but this Tub has been shut down",
                                   tub.connectTo, "furl", None))
     return d
示例#8
0
文件: app.py 项目: UfSoft/afm
class Application(BaseApplication):

    def prepare_application(self):
        eventmanager.register_event_handler("ApplicationLoaded",
                                            self.load_sources)
        self.setup_tubs()
        self.setup_gstreamer()
        eventmanager.emit(events.ApplicationLoaded())

    def load_config(self):
        self.config.load_core_config()

    def setup_tubs(self):
        self.infotub = UnauthenticatedTub()
        self.infotub.listenOn('tcp:%d' % self.config.core.info_port)
        self.infotub.setLocation("localhost:%d" % self.config.core.info_port)
#        self.infotub.setLocationAutomatically()
        info_server = InfoTub()
        info_url = self.infotub.registerReference(info_server, 'info')
        logging.getLogger(__name__).debug('Info url: %s', info_url)
#        self.infotub.startService()
        core_server = CoreTub()
        self.coretub = Tub()
        self.coretub.listenOn('tcp:%d' % (self.config.core.core_port))
        self.coretub.setLocation("localhost:%d" % (self.config.core.core_port))
#        self.coretub.setLocationAutomatically()
        core_info_url = self.coretub.registerReference(core_server, 'core')
        logging.getLogger(__name__).debug('Core Info url: %s', core_info_url)
        eventmanager.emit(events.CoreUrlGenerated(core_info_url))
#        self.coretub.startService()

    def setup_gstreamer(self):
        import gobject
        gobject.threads_init()
        import pygst
        pygst.require("0.10")
        import gst
#        import pygtk
#        pygtk.require('2.0')
#        import gtk
        # GST Debugging
#        gst.debug_set_active(True)
#        gst.debug_set_default_threshold(gst.LEVEL_INFO)
#        gst.debug_set_colored(True)


    def load_sources(self):
        from afm.sources import Source
        log = logging.getLogger(__name__)
        available_sources = self.config.sources.keys()
        available_sources.sort()
        for source_name in available_sources:
            source_config = self.config.sources[source_name]
            if not source_config.active:
                log.debug("Skipping %s. Not Active.", source_config)
                continue
            log.debug("%s active. Loading...", source_config)
            source = Source(source_config)
            source.prepare_source()

    def start_services(self):
        self.infotub.startService()
        self.coretub.startService()

    def stop_services(self):
        self.infotub.stopService()
        self.coretub.stopService()