Exemplo n.º 1
0
 def __init__(self, server=False, sipaddr=('127.0.0.1', 5062)):
     '''Initialize the P2P-SIP agent'''
     AbstractAgent.__init__(self, sipaddr=sipaddr)
     self.p2p = ServerSocket(
         server=server)  # for initial testing start as bootstrap server
     self.location = None  # to prevent accidental access to location dictionary
Exemplo n.º 2
0
 def __init__(self, server=False, sipaddr=('127.0.0.1', 5062), port=0):
     '''Initialize the P2P-SIP agent'''
     AbstractAgent.__init__(self, sipaddr=sipaddr)
     self.p2p = ServerSocket(server=server, port=port) # for initial testing start as bootstrap server
     self.location = None # to prevent accidental access to location dictionary
Exemplo n.º 3
0
class Agent(AbstractAgent):
    '''An adaptor for P2P-SIP, that maps between local SIP user agent and Internet SIP network,
    and uses P2P module for lookup and storage. This is based on the data mode. A similar class
    can be implemented that does service mode, with advanced features such as presence aggregation
    and dynamic call routing.'''
    def __init__(self, server=False, sipaddr=('127.0.0.1', 5062)):
        '''Initialize the P2P-SIP agent'''
        AbstractAgent.__init__(self, sipaddr=sipaddr)
        self.p2p = ServerSocket(
            server=server)  # for initial testing start as bootstrap server
        self.location = None  # to prevent accidental access to location dictionary

    def start(self):
        '''Start the Agent'''
        self.p2p.start()
        AbstractAgent.start(self)
        return self

    def stop(self):
        '''Stop the Agent'''
        AbstractAgent.stop(self)
        self.p2p.stop()
        return self

    def p2preceiver(self, p2p):
        '''Receive packets or connections from p2p socket server.'''
        def p2phandler(
                self,
                sock):  # Handle the messages on the given P2P connection.
            while True:
                data = yield sock.recv()

        while True:
            sock = yield p2p.accept()
            if hasattr(self, 'identity') and self.identity:
                multitask.add(p2phandler(sock))

    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)

    def locate(self, uri):
        '''Return all saved contacts for the given uri from P2P storage.'''
        existing = yield self.p2p.get(H(uri))
        now = time.time()
        result = []
        for value, nonce, Kp, expires in existing:
            c = Header(value, 'Contact')
            c['expires'] = str(int(expires - now))
            result.append(c)
        if _debug: print 'locate', uri, result
        raise StopIteration(result)
Exemplo n.º 4
0
class Agent(AbstractAgent):
    '''An adaptor for P2P-SIP, that maps between local SIP user agent and Internet SIP network,
    and uses P2P module for lookup and storage. This is based on the data mode. A similar class
    can be implemented that does service mode, with advanced features such as presence aggregation
    and dynamic call routing.'''
    def __init__(self, server=False, sipaddr=('127.0.0.1', 5062), port=0):
        '''Initialize the P2P-SIP agent'''
        AbstractAgent.__init__(self, sipaddr=sipaddr)
        self.p2p = ServerSocket(server=server, port=port) # for initial testing start as bootstrap server
        self.location = None # to prevent accidental access to location dictionary
    def start(self, servers=None):
        '''Start the Agent'''
        self.p2p.start(servers=servers); AbstractAgent.start(self); return self
    def stop(self):
        '''Stop the Agent'''
        AbstractAgent.stop(self); self.p2p.stop(); return self
    def p2preceiver(self, p2p):
        '''Receive packets or connections from p2p socket server.'''
        def p2phandler(self, sock): # Handle the messages on the given P2P connection.
            while True: 
                data = yield sock.recv()
        while True:
            sock = yield p2p.accept()
            if hasattr(self, 'identity') and self.identity: multitask.add(p2phandler(sock))
            
    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)
    
    def locate(self, uri):
        '''Return all saved contacts for the given uri from P2P storage.'''
        existing = yield self.p2p.get(H(uri))
        now = time.time()
        result = []
        for value, nonce, Kp, expires in existing:
            c = Header(value, 'Contact')
            c['expires'] = str(int(expires - now))
            result.append(c)
        if _debug: print 'locate', uri, result
        raise StopIteration(result)