Exemple #1
0
 def connecttobootstrap(self):
     connected = False
     for boottuple in self.bootstraplist:
         try:
             self.socket.close()
             self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
             self.socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
             self.socket.connect(boottuple)
             self.conn = Connection(self.socket)
             self.conn.settimeout(CLIENTRESENDTIMEOUT)
             self.bootstrap = boottuple
             connected = True
             if self.debug: print "Connected to new bootstrap: ", boottuple
             break
         except socket.error, e:
             if self.debug: print "Socket.Error: ", e
             continue
 def connecttobootstrap(self):
     connected = False
     for boottuple in self.bootstraplist:
         try:
             self.socket.close()
             self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
             self.socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
             self.socket.connect(boottuple)
             self.conn = Connection(self.socket)
             self.conn.settimeout(CLIENTRESENDTIMEOUT)
             self.bootstrap = boottuple
             connected = True
             if self.debug: print "Connected to new bootstrap: ", boottuple
             break
         except socket.error, e:
             if self.debug: print "Socket.Error: ", e
             continue
class ClientProxy():
    def __init__(self, bootstrap, timeout=60, debug=False, token=None):
        self.debug = debug
        self.timeout = timeout
        self.domainname = None
        self.token = token
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        self.socket.setblocking(1)
        self.writelock = Lock()

        self.bootstraplist = self.discoverbootstrap(bootstrap)
        if len(self.bootstraplist) == 0:
            raise ConnectionError("No bootstrap found")
        if not self.connecttobootstrap():
            raise ConnectionError("Cannot connect to any bootstrap")
        myaddr = findOwnIP()
        myport = self.socket.getsockname()[1]
        self.me = Peer(myaddr, myport, NODE_CLIENT)
        self.commandnumber = random.randint(1, sys.maxint)

        # synchronization
        self.lock = Lock()
        self.pendingops = {}  # pending requests indexed by commandnumber
        self.doneops = {}  # requests that are finalized, indexed by command number

        # spawn thread, invoke recv_loop
        try:
            recv_thread = Thread(target=self.recv_loop, name='ReceiveThread')
            recv_thread.daemon = True
            recv_thread.start()
        except (KeyboardInterrupt, SystemExit):
            self._graceexit()

    def _getipportpairs(self, bootaddr, bootport):
        for node in socket.getaddrinfo(bootaddr, bootport, socket.AF_INET, socket.SOCK_STREAM):
            yield (node[4][0],bootport)

    def getbootstrapfromdomain(self, domainname):
        tmpbootstraplist = []
        try:
            answers = dns.resolver.query('_concoord._tcp.'+domainname, 'SRV')
            for rdata in answers:
                for peer in self._getipportpairs(str(rdata.target), rdata.port):
                    if peer not in tmpbootstraplist:
                        tmpbootstraplist.append(peer)
        except (dns.resolver.NXDOMAIN, dns.exception.Timeout):
            if self.debug: print "Cannot resolve name"
        return tmpbootstraplist

    def discoverbootstrap(self, givenbootstrap):
        tmpbootstraplist = []
        try:
            for bootstrap in givenbootstrap.split(","):
                bootstrap = bootstrap.strip()
                # The bootstrap list is read only during initialization
                if bootstrap.find(":") >= 0:
                    bootaddr,bootport = bootstrap.split(":")
                    for peer in self._getipportpairs(bootaddr, int(bootport)):
                        if peer not in tmpbootstraplist:
                            tmpbootstraplist.append(peer)
                else:
                    self.domainname = bootstrap
                    tmpbootstraplist = self.getbootstrapfromdomain(self.domainname)
        except ValueError:
            if self.debug: print "bootstrap usage: ipaddr1:port1,ipaddr2:port2 or domainname"
            self._graceexit()
        return tmpbootstraplist

    def connecttobootstrap(self):
        connected = False
        for boottuple in self.bootstraplist:
            try:
                self.socket.close()
                self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
                self.socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
                self.socket.connect(boottuple)
                self.conn = Connection(self.socket)
                self.conn.settimeout(CLIENTRESENDTIMEOUT)
                self.bootstrap = boottuple
                connected = True
                if self.debug: print "Connected to new bootstrap: ", boottuple
                break
            except socket.error, e:
                if self.debug: print "Socket.Error: ", e
                continue
        return connected
Exemple #4
0
class ClientProxy():
    def __init__(self, bootstrap, timeout=60, debug=False, token=None):
        self.debug = debug
        self.timeout = timeout
        self.domainname = None
        self.token = token
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        self.writelock = Lock()

        self.bootstraplist = self.discoverbootstrap(bootstrap)
        if len(self.bootstraplist) == 0:
            raise ConnectionError("No bootstrap found")
        if not self.connecttobootstrap():
            raise ConnectionError("Cannot connect to any bootstrap")
        myaddr = findOwnIP()
        myport = self.socket.getsockname()[1]
        self.me = Peer(myaddr, myport, NODE_CLIENT)
        self.commandnumber = random.randint(1, sys.maxint)

    def _getipportpairs(self, bootaddr, bootport):
        for node in socket.getaddrinfo(bootaddr, bootport, socket.AF_INET,
                                       socket.SOCK_STREAM):
            yield (node[4][0], bootport)

    def getbootstrapfromdomain(self, domainname):
        tmpbootstraplist = []
        try:
            answers = dns.resolver.query('_concoord._tcp.' + domainname, 'SRV')
            for rdata in answers:
                for peer in self._getipportpairs(str(rdata.target),
                                                 rdata.port):
                    if peer not in tmpbootstraplist:
                        tmpbootstraplist.append(peer)
        except (dns.resolver.NXDOMAIN, dns.exception.Timeout):
            if self.debug: print("Cannot resolve name")
        return tmpbootstraplist

    def discoverbootstrap(self, givenbootstrap):
        tmpbootstraplist = []
        try:
            for bootstrap in givenbootstrap.split(","):
                bootstrap = bootstrap.strip()
                # The bootstrap list is read only during initialization
                if bootstrap.find(":") >= 0:
                    bootaddr, bootport = bootstrap.split(":")
                    for peer in self._getipportpairs(bootaddr, int(bootport)):
                        if peer not in tmpbootstraplist:
                            tmpbootstraplist.append(peer)
                else:
                    self.domainname = bootstrap
                    tmpbootstraplist = self.getbootstrapfromdomain(
                        self.domainname)
        except ValueError:
            if self.debug:
                print(
                    "bootstrap usage: ipaddr1:port1,ipaddr2:port2 or domainname"
                )
            self._graceexit()
        return tmpbootstraplist

    def connecttobootstrap(self):
        connected = False
        if self.debug: print("connecttobootstrap called.")
        for boottuple in self.bootstraplist:
            try:
                self.socket.close()
                self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
                                       1)
                self.socket.connect(boottuple)
                self.conn = Connection(self.socket)
                self.conn.settimeout(CLIENTRESENDTIMEOUT)
                self.bootstrap = boottuple
                connected = True
                if self.debug: print("Connected to new bootstrap: ", boottuple)
                break
            except socket.error, e:
                if self.debug: print("Socket.Error: ", e)
                continue
        return connected