Example #1
0
    def _getReference(self, sturdyOrURL):
        if isinstance(sturdyOrURL, SturdyRef):
            sturdy = sturdyOrURL
        else:
            sturdy = SturdyRef(sturdyOrURL)
        # pb->pb: ok, requires crypto
        # pbu->pb: ok, requires crypto
        # pbu->pbu: ok
        # pb->pbu: ok, requires crypto
        if sturdy.encrypted and not crypto_available:
            e = BananaError("crypto for PB is not available, "
                            "we cannot handle encrypted PB-URLs like %s" %
                            sturdy.getURL())
            return defer.fail(e)

        if not self.running:
            # queue their request for service once the Tub actually starts
            log.msg(
                "Tub.getReference(%s) queued until Tub.startService called" %
                sturdy,
                facility="foolscap.tub")
            d = defer.Deferred()
            self._pending_getReferences.append((d, sturdy))
            return d

        name = sturdy.name
        d = self.getBrokerForTubRef(sturdy.getTubRef())
        d.addCallback(lambda b: b.getYourReferenceByName(name))
        return d
Example #2
0
    def _getReference(self, sturdyOrURL):
        if isinstance(sturdyOrURL, SturdyRef):
            sturdy = sturdyOrURL
        else:
            sturdy = SturdyRef(sturdyOrURL)
        # pb->pb: ok, requires crypto
        # pbu->pb: ok, requires crypto
        # pbu->pbu: ok
        # pb->pbu: ok, requires crypto
        if sturdy.encrypted and not crypto_available:
            e = BananaError("crypto for PB is not available, "
                            "we cannot handle encrypted PB-URLs like %s"
                            % sturdy.getURL())
            return defer.fail(e)

        if not self.running:
            # queue their request for service once the Tub actually starts
            log.msg("Tub.getReference(%s) queued until Tub.startService called"
                    % sturdy, facility="foolscap.tub")
            d = defer.Deferred()
            self._pending_getReferences.append((d, sturdy))
            return d

        name = sturdy.name
        d = self.getBrokerForTubRef(sturdy.getTubRef())
        d.addCallback(lambda b: b.getYourReferenceByName(name))
        return d
Example #3
0
 def testEndpointsURLTcpMixed(self):
     sr1 = SturdyRef("pb://%s@tcp:host=127.0.0.1:9900/name" % TUB1)
     sr2 = SturdyRef("pb://%s@tcp:127.0.0.1:port=9900/name" % TUB1)
     self.failUnlessEqual(sr1, sr2)
     self.failUnlessEqual(sr1.tubID, TUB1)
     self.failUnlessEqual(sr1.locationHints,
                          [ ("tcp", "127.0.0.1", 9900) ])
     self.failUnlessEqual(sr1.name, "name")
     self.failUnlessEqual(sr2.tubID, TUB1)
     self.failUnlessEqual(sr2.locationHints,
                          [ ("tcp", "127.0.0.1", 9900) ])
     self.failUnlessEqual(sr2.name, "name")
Example #4
0
    def testBrokenHints(self):
        # This should throw an exception
        furl = "pb://%[email protected]/name" % TUB1  # missing portnum
        f = self.failUnlessRaises(BadFURLError, SturdyRef, furl)

        def _check(f, hostname):
            self.failUnless(("bad connection hint '%s' "
                             "(hostname, but no port)" % hostname) in str(f),
                            f)

        _check(f, "127.0.0.1")

        furl = "pb://%[email protected]/name" % TUB1  # missing portnum
        f = self.failUnlessRaises(BadFURLError, SturdyRef, furl)
        _check(f, "example.com")

        furl = "pb://%s@,/name" % TUB1  # empty hints are not allowed
        f = self.failUnlessRaises(BadFURLError, SturdyRef, furl)
        _check(f, "")

        furl = "pb://%s@/name" % TUB1  # this is ok, and means "unrouteable"
        sr = SturdyRef(furl)
        self.failUnlessEqual(sr.locationHints, [])

        furl = "pb://%s/name" % TUB1  # this is not ok
        f = self.failUnlessRaises(ValueError, SturdyRef, furl)
        self.failUnless("unknown FURL prefix in " in str(f), f)
Example #5
0
    def registerReference(self, ref, name=None, furlFile=None):
        """Make a Referenceable available to the outside world. A URL is
        returned which can be used to access this object. This registration
        will remain in effect (and the Tub will retain a reference to the
        object to keep it meaningful) until explicitly unregistered, or the
        Tub is shut down.

        @type  name: string (optional)
        @param name: if provided, the object will be registered with this
                     name. If not, a random (unguessable) string will be
                     used.

        @param furlFile: if provided, get the name from this file (if
                         it exists), and write the new FURL to this file.
                         If 'name=' is also provided, it is used for the
                         name, but the FURL is still written to this file.

        @rtype: string
        @return: the URL which points to this object. This URL can be passed
        to Tub.getReference() in any Tub on any host which can reach this
        one.
        """

        if not self.locationHints:
            raise NoLocationError("you must setLocation() before "
                                  "you can registerReference()")
        oldfurl = None
        if furlFile:
            try:
                oldfurl = open(furlFile, "r").read().strip()
            except EnvironmentError:
                pass
        if oldfurl:
            sr = SturdyRef(oldfurl)
            if name is None:
                name = sr.name
            if self.tubID != sr.tubID:
                raise WrongTubIdError(
                    "I cannot keep using the old FURL from %s"
                    " because it does not have the same"
                    " TubID as I do (%s)" % (furlFile, self.tubID))
            if name != sr.name:
                raise WrongNameError("I cannot keep using the old FURL from %s"
                                     " because you called registerReference"
                                     " with a new name (%s)" %
                                     (furlFile, name))
        name = self._assignName(ref, name)
        assert name
        if ref not in self.strongReferences:
            self.strongReferences.append(ref)
        furl = self.buildURL(name)
        if furlFile:
            need_to_chmod = not os.path.exists(furlFile)
            f = open(furlFile, "w")
            f.write(furl + "\n")
            f.close()
            if need_to_chmod:
                # XXX: open-to-chmod race here
                os.chmod(furlFile, 0600)
        return furl
Example #6
0
 def unregisterURL(self, url):
     sturdy = SturdyRef(url)
     name = sturdy.name
     ref = self.nameToReference[name]
     del self.nameToReference[name]
     del self.referenceToName[ref]
     self.revokeReference(ref)
Example #7
0
 def testLocationHints(self):
     url = "pb://%[email protected]:9900,remote:8899/name" % TUB1
     sr = SturdyRef(url)
     self.assertEqual(sr.tubID, TUB1)
     self.assertEqual(sr.locationHints,
                      ["127.0.0.1:9900", "remote:8899"])
     self.assertEqual(sr.name, "name")
Example #8
0
 def testLocationHints(self):
     url = "pb://%[email protected]:9900,remote:8899/name" % TUB1
     sr = SturdyRef(url)
     self.failUnlessEqual(sr.tubID, TUB1)
     self.failUnlessEqual(sr.locationHints, [("ipv4", "127.0.0.1", 9900),
                                             ("ipv4", "remote", 8899)])
     self.failUnlessEqual(sr.name, "name")
Example #9
0
 def run(self, target_furl):
     target_tubid = SturdyRef(target_furl).getTubRef().getTubID()
     d = fireEventually(target_furl)
     d.addCallback(self.start, target_tubid)
     d.addErrback(self._error)
     print("starting..")
     reactor.run()
Example #10
0
    def _getReference(self, sturdyOrURL):
        if isinstance(sturdyOrURL, SturdyRef):
            sturdy = sturdyOrURL
        else:
            sturdy = SturdyRef(sturdyOrURL)

        if not self.running:
            # queue their request for service once the Tub actually starts
            log.msg("Tub.getReference(%s) queued until Tub.startService called"
                    % sturdy, facility="foolscap.tub")
            d = defer.Deferred()
            self._pending_getReferences.append((d, sturdy))
            return d

        name = sturdy.name
        d = self.getBrokerForTubRef(sturdy.getTubRef())
        d.addCallback(lambda b: b.getYourReferenceByName(name))
        return d
Example #11
0
    def _getReference(self, sturdyOrURL):
        if isinstance(sturdyOrURL, SturdyRef):
            sturdy = sturdyOrURL
        else:
            sturdy = SturdyRef(sturdyOrURL)

        if not self.running:
            # queue their request for service once the Tub actually starts
            log.msg("Tub.getReference(%s) queued until Tub.startService called"
                    % sturdy, facility="foolscap.tub")
            d = defer.Deferred()
            self._pending_getReferences.append((d, sturdy))
            return d

        name = sturdy.name
        d = self.getBrokerForTubRef(sturdy.getTubRef())
        d.addCallback(lambda b: b.getYourReferenceByName(name))
        return d
Example #12
0
    def testLocationHintExtensions(self):
        furl = "pb://%[email protected]:9900,udp:127.0.0.1:7700/name" % TUB1
        sr = SturdyRef(furl)
        self.failUnlessEqual(sr.locationHints, [("ipv4", "127.0.0.1", 9900)])
        self.failUnlessEqual(sr.getURL(), furl)

        furl = "pb://%s@udp:127.0.0.1:7700/name" % TUB1
        sr = SturdyRef(furl)
        self.failUnlessEqual(sr.locationHints, [])
        self.failUnlessEqual(sr.getURL(), furl)

        furl = "pb://%[email protected]:7700:postextension/name" % TUB1
        sr = SturdyRef(furl)
        self.failUnlessEqual(sr.locationHints, [])
        self.failUnlessEqual(sr.getURL(), furl)
Example #13
0
 def unregisterReference(self, ref):
     name = self.referenceToName[ref]
     url = self.buildURL(name)
     sturdy = SturdyRef(url)
     name = sturdy.name
     del self.nameToReference[name]
     del self.referenceToName[ref]
     if ref in self.strongReferences:
         self.strongReferences.remove(ref)
     self.revokeReference(ref)
Example #14
0
    def testBrokenHints(self):
        furl = "pb://%s@,/name" % TUB1 # empty hints are not allowed
        f = self.assertRaises(BadFURLError, SturdyRef, furl)
        self.assertTrue("no connection hint may be empty" in str(f), f)

        furl = "pb://%s@/name" % TUB1 # this is ok, and means "unrouteable"
        sr = SturdyRef(furl)
        self.assertEqual(sr.locationHints, [])

        furl = "pb://%s/name" % TUB1 # this is not ok
        f = self.assertRaises(ValueError, SturdyRef, furl)
        self.assertTrue("unknown FURL prefix in " in str(f), f)
Example #15
0
    def testBrokenHints(self):
        furl = "pb://%s@,/name" % TUB1 # empty hints are not allowed
        f = self.failUnlessRaises(BadFURLError, SturdyRef, furl)
        self.failUnless("empty string" in str(f), f)

        furl = "pb://%s@/name" % TUB1 # this is ok, and means "unrouteable"
        sr = SturdyRef(furl)
        self.failUnlessEqual(sr.locationHints, [])

        furl = "pb://%s/name" % TUB1 # this is not ok
        f = self.failUnlessRaises(ValueError, SturdyRef, furl)
        self.failUnless("unknown FURL prefix in " in str(f), f)
Example #16
0
 def testCompare(self):
     sr1 = SturdyRef("pb://%[email protected]:9900/name" % TUB1)
     sr2 = SturdyRef("pb://%[email protected]:9999/name" % TUB1)
     # only tubID and name matter
     self.assertEqual(sr1, sr2)
     sr1 = SturdyRef("pb://%[email protected]:9900/name" % TUB2)
     sr2 = SturdyRef("pb://%[email protected]:9900/name" % TUB1)
     self.assertNotEqual(sr1, sr2)
     sr1 = SturdyRef("pb://%[email protected]:9900/name1" % TUB1)
     sr2 = SturdyRef("pb://%[email protected]:9900/name2" % TUB1)
     self.assertNotEqual(sr1, sr2)
Example #17
0
    def testLocationHintExtensions(self):
        furl = "pb://%[email protected]:9900,udp:127.0.0.1:7700/name" % TUB1
        sr = SturdyRef(furl)
        self.failUnlessEqual(sr.locationHints, [("tcp", "127.0.0.1", 9900)])
        self.failUnlessEqual(sr.getURL(), furl)

        furl = "pb://%s@udp:127.0.0.1:7700/name" % TUB1
        sr = SturdyRef(furl)
        self.failUnlessEqual(sr.locationHints, [])
        self.failUnlessEqual(sr.getURL(), furl)

        furl = "pb://%[email protected]:7700:postextension/name" % TUB1
        sr = SturdyRef(furl)
        self.failUnlessEqual(sr.locationHints, [])
        self.failUnlessEqual(sr.getURL(), furl)
Example #18
0
 def testURL(self):
     sr = SturdyRef("pb://%[email protected]:9900/name" % TUB1)
     self.failUnlessEqual(sr.tubID, TUB1)
     self.failUnlessEqual(sr.locationHints, [("ipv4", "127.0.0.1", 9900)])
     self.failUnlessEqual(sr.name, "name")
Example #19
0
 def testURL(self):
     sr = SturdyRef("pb://%[email protected]:9900/name" % TUB1)
     self.assertEqual(sr.tubID, TUB1)
     self.assertEqual(sr.locationHints, ["127.0.0.1:9900"])
     self.assertEqual(sr.name, "name")
Example #20
0
 def testURLTcp(self):
     sr = SturdyRef("pb://%s@tcp:host=127.0.0.1:port=9900/name" % TUB1)
     self.failUnlessEqual(sr.tubID, TUB1)
     self.failUnlessEqual(sr.locationHints, [("tcp", "127.0.0.1", 9900)])
     self.failUnlessEqual(sr.name, "name")
Example #21
0
 def testTubIDExtensions(self):
     sr = SturdyRef("pb://%s,[email protected]:9900/name" % TUB1)
     self.assertEqual(sr.tubID, TUB1)
     self.assertRaises(BadFURLError,
                       SturdyRef,
                       "pb://badstuff,%[email protected]:9900/name" % TUB1)
Example #22
0
 def testEndpointsURLTcpCompact(self):
     sr = SturdyRef("pb://%s@tcp:127.0.0.1:9900/name" % TUB1)
     self.failUnlessEqual(sr.tubID, TUB1)
     self.failUnlessEqual(sr.locationHints,
                          [ ("tcp", "127.0.0.1", 9900) ])
     self.failUnlessEqual(sr.name, "name")
Example #23
0
 def getReferenceForURL(self, url):
     # TODO: who should this be used by?
     sturdy = SturdyRef(url)
     assert sturdy.tubID == self.tubID
     return self.getReferenceForName(sturdy.name)
Example #24
0
 def getConnectionInfoForFURL(self, furl):
     try:
         tubref = SturdyRef(furl).getTubRef()
     except (ValueError, BadFURLError):
         return None  # unparseable FURL
     return self._getConnectionInfoForTubRef(tubref)