Skip to content
Snippets Groups Projects
Select Git revision
  • c60745cf27edef74de07d082777c5a7a139de6a0
  • master default protected
  • dev
  • appimage
  • fix_gitlab
  • fixappveyor
  • gitlab
  • fix_ci
  • fix_dbus_error
  • fix_ci_osx
  • sakia020
  • fix_travis#1105
  • feature/backend
  • check_uniq_node_by_endpoints
  • qt5.7
  • feature/agent_architecture
  • translations
  • pyqt5.6
  • qtwebengine
  • pyinstaller
  • landscape
  • 0.53.2
  • 0.53.1
  • 0.53.0
  • 0.52.0
  • 0.51.1
  • 0.51.0
  • 0.50.5
  • 0.50.4
  • 0.50.3
  • 0.50.2
  • 0.50.1
  • 0.50.0
  • 0.33.0rc7
  • 0.33.0rc6
  • 0.33.0rc5
  • 0.33.0rc4
  • 0.33.0rc3
  • 0.33.0rc2
  • 0.33.0rc1
  • 0.32.10post1
41 results

peer.py

Blame
  • peer.py 2.05 KiB
    '''
    Created on 2 déc. 2014
    
    @author: inso
    '''
    
    import re
    
    from . import Document
    from .. import PROTOCOL_VERSION, MANAGED_API
    
    
    class Peer(Document):
        """
        Version: VERSION
        Type: Peer
        Currency: CURRENCY_NAME
        PublicKey: NODE_PUBLICKEY
        Block: BLOCK
        Endpoints:
        END_POINT_1
        END_POINT_2
        END_POINT_3
        [...]
        """
    
        def __init__(self, currency, pubkey, blockid, endpoints):
            self.currency = currency
            self.pubkey = pubkey
            self.blockid = blockid
            self.endpoints = endpoints
            
        @classmethod
        def from_raw(cls, raw):
            #TODO : Parsing
            return cls()
    
        def content(self):
            doc = """
    Version: {0}
    Type: Peer
    Currency: {1}
    PublicKey: {2}
    Block: {3}
    Endpoints:
    """.format(PROTOCOL_VERSION, self.currency, self.pubkey, self.blockid)
    
            for endpoint in self.endpoints:
                doc += "{0}\n".format(endpoint.inline())
            return doc
    
    
    class Endpoint():
        """
        Describing endpoints
        """
    
        @staticmethod
        def from_inline(inline):
            for api in MANAGED_API:
                if (inline.startswith(api)):
                    return Endpoint.parse_line(inline, api)
    
        @staticmethod
        def parse_line(self, inline, api):
            if (api == "BASIC_MERKLED_API"):
                bma_endpoints = re.compile('^BASIC_MERKLED_API( ([a-z_][a-z0-9-_.]+))?( ([0-9.]+))?( ([0-9a-f:]+))?( ([0-9]+))$')
                m = bma_endpoints.match(inline)
                server = m.group(2)
                ipv4 = m.group(4)
                ipv6 = m.group(6)
                port = int(m.group(8))
                return BMAEndpoint(server, ipv4, ipv6, port)
            return None
    
    
    class BMAEndpoint(Endpoint):
        def __init__(self, server, ipv4, ipv6, port):
            self.server = server
            self.ipv4 = ipv4
            self.ipv6 = ipv6
            self.port = port
    
        def inline(self):
            return "BASIC_MERKLED_API {DNS} {IPv4} {IPv6} {PORT}" \
                        .format(DNS=self.server,
                                IPv4=self.ipv4,
                                IPv6=self.ipv6,
                                PORT=self.port)