Exemple #1
0
    def save(self, msg, uri, defaultExpires=3600):
        '''Save the contacts from REGISTER or PUBLISH msg to P2P storage.'''
        expires = int(msg.Expires.value if msg.Expires else defaultExpires)

        existing = yield self.p2p.get(H(uri))
        if msg.Contact and msg.first(
                'Contact').value == '*':  # single contact: * header
            if msg.Expires and msg.Expires.value == '0':  # unregistration msg
                if existing:
                    for value, nonce, Kp, expires in existing:
                        yield self.p2p.remove(H(uri), value, nonce,
                                              expires + 1)
                existing = []
        else:  # handle individual contact headers in the msg
            existing = dict([(str(Header(str(x[0]), 'Contact').value.uri), x)
                             for x in existing])
            now, remove, update = time.time(), [], []
            for c in msg.all('Contact'):  # for all contacts in the new msg
                e = now + (expires if 'expires' not in c else int(c.expires)
                           )  # expiration for this contact.
                if e <= now: remove.append(c)
                else: update.insert(0, (c, e))
            for c in remove:
                if c.value.uri in existing:
                    value, nonce, Kp, expires = existing[c.value.uri]
                    yield self.p2p.remove(H(uri), value, nonce, expires + 1)
            for c, e in update:
                if c.value.uri in existing:
                    value, nonce, Kp, expires = existing[c.value.uri]
                    yield self.p2p.put(H(uri), value, nonce, now + e)
                else:
                    yield self.p2p.put(H(uri), str(c.value), dht.randomNonce(),
                                       now + e)
        if _debug: print 'save', self.location, 'returning True'
        raise StopIteration(True)
Exemple #2
0
 def save(self, msg, uri, defaultExpires=3600):
     '''Save the contacts from REGISTER or PUBLISH msg to P2P storage.'''
     expires = int(msg.Expires.value if msg.Expires else defaultExpires)
     
     existing = yield self.p2p.get(H(uri))
     if msg.Contact and msg.first('Contact').value == '*': # single contact: * header
         if msg.Expires and msg.Expires.value == '0': # unregistration msg
             if existing:
                 for value, nonce, Kp, expires in existing:
                     yield self.p2p.remove(H(uri), value, nonce, expires+1)
             existing = []
     else: # handle individual contact headers in the msg
         existing = dict([(str(Header(str(x[0]), 'Contact').value.uri), x) for x in existing])
         now, remove, update = time.time(), [], []
         for c in msg.all('Contact'): # for all contacts in the new msg
             e = now + (expires if 'expires' not in c else int(c.expires)) # expiration for this contact.
             if e<=now: remove.append(c)
             else: update.insert(0, (c, e))
         for c in remove:
             if c.value.uri in existing:
                 value, nonce, Kp, expires = existing[c.value.uri]
                 yield self.p2p.remove(H(uri), value, nonce, expires+1)
         for c, e in update:
             if c.value.uri in existing:
                 value, nonce, Kp, expires = existing[c.value.uri]
                 yield self.p2p.put(H(uri), value, nonce, now+e)
             else:
                 yield self.p2p.put(H(uri), str(c.value), dht.randomNonce(), now+e)
     if _debug: print 'save', self.location, 'returning True'
     raise StopIteration(True)
Exemple #3
0
 def bind(self, identity, interval=3600):
     """Bind the server socket to the given identity."""
     if hasattr(self, "identity"):
         raise Exception("socket already bound")
     self.identity, self.nonce, self.expires = identity, dht.randomNonce(), time.time() + interval
     result = yield self.put(guid=H(identity), value=self.net.node.guid, nonce=self.nonce, expires=self.expires)
     raise StopIteration(result)
Exemple #4
0
 def bind(self, identity, interval=3600):
     '''Bind the server socket to the given identity.'''
     if hasattr(self, 'identity'): raise Exception('socket already bound')
     self.identity, self.nonce, self.expires = identity, dht.randomNonce(
     ), time.time() + interval
     result = yield self.put(guid=H(identity),
                             value=self.net.node.guid,
                             nonce=self.nonce,
                             expires=self.expires)
     raise StopIteration(result)