Example #1
0
class Barrier():
    def __init__(self, count=1):
        self.count = int(count)
        self.current = 0
        self.condition = DCondition()

    def wait(self, _concoord_command):
        self.condition.acquire(_concoord_command)
        self.current += 1
        if self.current != self.count:
            self.condition.wait(_concoord_command)
        else:
            self.current = 0
            self.condition.notifyAll(_concoord_command)
        self.condition.release(_concoord_command)

    def __str__(self):
        return "<%s object>" % (self.__class__.__name__)
Example #2
0
class Barrier():
    def __init__(self, count=1):
        self.count = int(count)
        self.current = 0
        self.condition = DCondition()

    def wait(self, _concoord_command):
        self.condition.acquire(_concoord_command)
        self.current += 1
        if self.current != self.count:
            self.condition.wait(_concoord_command)
        else:
            self.current = 0
            self.condition.notifyAll(_concoord_command)
        self.condition.release(_concoord_command)

    def __str__(self):
        return "<%s object>" % (self.__class__.__name__)
Example #3
0
class Barrier():
    def __init__(self, count=1, **kwargs):
        self.count = int(count)
        self.current = 0
        self.condition = DCondition()

    def wait(self, **kwargs):
        self.condition.acquire(kwargs)
        self.current += 1
        if self.current != self.count:
            self.condition.wait(kwargs)
        else:
            self.current = 0
            self.condition.notifyAll(kwargs)
        self.condition.release(kwargs)
        
    def __str__(self, **kwargs):
        return "<%s object>" % (self.__class__.__name__)
Example #4
0
class Condition():
    """Lock object that supports following functions:
    - acquire: locks the object
    - release: unlocks the object
    """
    def __init__(self, lock=None, **kwargs):
        self.condition = DCondition()

    def __repr__(self, **kwargs):
        return repr(self.condition)

    def acquire(self, **kwargs):
        try:
            self.condition.acquire(kwargs)
        except Exception as e:
            raise e
        
    def release(self, **kwargs):
        try:
            self.condition.release(kwargs)
        except Exception as e:
            raise e
        
    def wait(self, **kwargs):
        try:
            self.condition.wait(kwargs)
        except Exception as e:
            raise e
        
    def notify(self, **kwargs):
        try:
            self.condition.notify(kwargs)
        except Exception as e:
            raise e

    def notifyAll(self, **kwargs):
        try:
            self.condition.notifyAll(kwargs)
        except Exception as e:
            raise e

    def __str__(self, **kwargs):
        return str(self.condition)
Example #5
0
class Condition():
    def __init__(self, lock=None):
        self.condition = DCondition()

    def __repr__(self):
        return repr(self.condition)

    def acquire(self, _concoord_command):
        try:
            self.condition.acquire(_concoord_command)
        except Exception as e:
            raise e

    def release(self, _concoord_command):
        try:
            self.condition.release(_concoord_command)
        except Exception as e:
            raise e

    def wait(self, _concoord_command):
        try:
            self.condition.wait(_concoord_command)
        except Exception as e:
            raise e

    def notify(self, _concoord_command):
        try:
            self.condition.notify(_concoord_command)
        except Exception as e:
            raise e

    def notifyAll(self, _concoord_command):
        try:
            self.condition.notifyAll(_concoord_command)
        except Exception as e:
            raise e

    def __str__(self):
        return str(self.condition)
Example #6
0
class Condition():
    def __init__(self, lock=None):
        self.condition = DCondition()

    def __repr__(self):
        return repr(self.condition)

    def acquire(self, _concoord_command):
        try:
            self.condition.acquire(_concoord_command)
        except Exception as e:
            raise e

    def release(self, _concoord_command):
        try:
            self.condition.release(_concoord_command)
        except Exception as e:
            raise e

    def wait(self, _concoord_command):
        try:
            self.condition.wait(_concoord_command)
        except Exception as e:
            raise e

    def notify(self, _concoord_command):
        try:
            self.condition.notify(_concoord_command)
        except Exception as e:
            raise e

    def notifyAll(self, _concoord_command):
        try:
            self.condition.notifyAll(_concoord_command)
        except Exception as e:
            raise e

    def __str__(self):
        return str(self.condition)
Example #7
0
class Pinger():
    def __init__(self):
        self.members = set()
        self.membership_condition = DCondition()

        self.liveness = {}

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        myaddr = socket.gethostbyname(socket.gethostname())
        myport = self.socket.getsockname()[1]
        self.me = "%s:%d", (myaddr, myport)

        comm_thread = Thread(target=self.ping_members, name='PingThread')
        comm_thread.start()

    def add(self, member):
        if member not in self.members:
            self.members.add(member)
            self.liveness[member] = 0
        # Notify nodes of membership change
        self.membership_condition.notifyAll()

    def remove(self, member):
        if member in self.members:
            self.members.remove(member)
            del self.liveness[member]
        else:
            raise KeyError(member)
        # Notify nodes of membership change
        self.membership_condition.notifyAll()

    def get_members(self):
        return self.members

    def ping_members(self):
        while True:
            for member in self.members:
                print "Sending PING to %s" % str(member)
                pingmessage = PingMessage(self.me)
                success = self.send(pingmessage, peer=peer)
                if success < 0:
                    print "Node not responding, marking."
                    self.liveness[member] += 1

            time.sleep(PING_DELAY)

    def send(self, msg):
        messagestr = pickle.dumps(msg)
        message = struct.pack("I", len(messagestr)) + messagestr
        try:
            while len(message) > 0:
                try:
                    bytesent = self.thesocket.send(message)
                    message = message[bytesent:]
                except IOError, e:
                    if isinstance(e.args, tuple):
                        if e[0] == errno.EAGAIN:
                            continue
                        else:
                            raise e
                except AttributeError, e:
                    raise e