class TubFailures(ExamineFailuresMixin, ShouldFailMixin, unittest.TestCase): def setUp(self): self.s = service.MultiService() self.s.startService() self.target_tub = UnauthenticatedTub() self.target_tub.setServiceParent(self.s) l = self.target_tub.listenOn("tcp:0:interface=127.0.0.1") self.target_tub.setLocation("127.0.0.1:%d" % l.getPortnum()) self.source_tub = UnauthenticatedTub() self.source_tub.setServiceParent(self.s) def tearDown(self): return self.s.stopService() def setupTarget(self, target): furl = self.target_tub.registerReference(target) d = self.source_tub.getReference(furl) return d def test_raise_not_exposed(self): self.source_tub.setOption("expose-remote-exception-types", False) d = self.setupTarget(TargetWithoutInterfaces()) d.addCallback(lambda rr: self.shouldFail(RemoteException, "one", None, rr.callRemote, "fail")) d.addCallback(self._examine_raise, True) return d def test_raise_yes_exposed(self): self.source_tub.setOption("expose-remote-exception-types", True) d = self.setupTarget(TargetWithoutInterfaces()) d.addCallback(lambda rr: self.shouldFail(ValueError, "one", None, rr.callRemote, "fail")) d.addCallback(self._examine_raise, False) return d def test_raise_default(self): # current default is to expose exceptions. This may change in the # future. d = self.setupTarget(TargetWithoutInterfaces()) d.addCallback(lambda rr: self.shouldFail(ValueError, "one", None, rr.callRemote, "fail")) d.addCallback(self._examine_raise, False) return d
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()
class ReferenceCounting(ShouldFailMixin, unittest.TestCase): def setUp(self): self.s = service.MultiService() self.s.startService() self.target_tub = UnauthenticatedTub() self.target_tub.setServiceParent(self.s) l = self.target_tub.listenOn("tcp:0:interface=127.0.0.1") self.target_tub.setLocation("127.0.0.1:%d" % l.getPortnum()) self.source_tub = UnauthenticatedTub() self.source_tub.setServiceParent(self.s) def tearDown(self): return self.s.stopService() def setupTarget(self, target): furl = self.target_tub.registerReference(target) d = self.source_tub.getReference(furl) return d def test_reference_counting(self): self.source_tub.setOption("expose-remote-exception-types", True) target = HelperTarget() d = self.setupTarget(target) def _stash(rref): # to exercise bug #104, we need to trigger remote Violations, so # we tell the sending side to not use a RemoteInterface. We do # this by reaching inside the RemoteReference and making it # forget rref.tracker.interfaceName = None rref.tracker.interface = None self.rref = rref d.addCallback(_stash) # the first call causes an error, which discards all remaining # tokens, including the OPEN tokens for the arguments. The #104 bug # is that this causes the open-count to get out of sync, by -2 (one # for the arguments sequence, one for the list inside it). d.addCallback(lambda ign: self.shouldFail(Violation, "one", None, self.rref.callRemote, "bogus", ["one list"])) #d.addCallback(lambda ign: # self.rref.callRemote("set", ["one list"])) # a method call that has no arguments (specifically no REFERENCE # sequences) won't notice the loss of sync d.addCallback(lambda ign: self.rref.callRemote("set", 42)) def _check_42(ign): self.failUnlessEqual(target.obj, 42) d.addCallback(_check_42) # but when the call takes shared arguments, sync matters l = ["list", 1, 2] s = set([3,4]) t = ("tuple", 5, 6) d.addCallback(lambda ign: self.rref.callRemote("set", [t, l, s, t])) def _check_shared(ign): # the off-by-two bug would cause the second tuple shared-ref to # point at the set instead of the first tuple self.failUnlessEqual(type(target.obj), list) one, two, three, four = target.obj self.failUnlessEqual(type(one), tuple) self.failUnlessEqual(one, t) self.failUnlessEqual(type(two), list) self.failUnlessEqual(two, l) self.failUnlessEqual(type(three), set) self.failUnlessEqual(three, s) self.failUnlessEqual(type(four), tuple) # this is where it fails self.failUnlessEqual(four, t) self.failUnlessIdentical(one, four) d.addCallback(_check_shared) return d
#! /usr/bin/python from twisted.internet import reactor from foolscap.api import Referenceable, UnauthenticatedTub class MathServer(Referenceable): def remote_add(self, a, b): return a+b def remote_subtract(self, a, b): return a-b myserver = MathServer() tub = UnauthenticatedTub() tub.listenOn("tcp:12345") tub.setLocation("localhost:12345") url = tub.registerReference(myserver, "math-service") print "the object is available at:", url tub.startService() reactor.run()