Commit 67b72aee authored by 's avatar
Browse files

bon dieu on dirait que ça marche!

parent 012158ea
...@@ -21,7 +21,7 @@ import threading ...@@ -21,7 +21,7 @@ import threading
EXCHANGE = "Lamport" EXCHANGE = "Lamport"
INIT_EXCHANGE = "Init" INIT_EXCHANGE = "Init"
TIMEOUT = 0.5 TIMEOUT = 0.1
class LamportClient(threading.Thread): class LamportClient(threading.Thread):
def __init__(self, id, clients): def __init__(self, id, clients):
...@@ -134,6 +134,7 @@ class LamportClient(threading.Thread): ...@@ -134,6 +134,7 @@ class LamportClient(threading.Thread):
""" """
self.channel.basic_publish( self.channel.basic_publish(
exchange = EXCHANGE, body=message, routing_key='') exchange = EXCHANGE, body=message, routing_key='')
self.times[self.id] = self.time
def request(self): def request(self):
"""request """request
...@@ -149,6 +150,7 @@ class LamportClient(threading.Thread): ...@@ -149,6 +150,7 @@ class LamportClient(threading.Thread):
def free(self): def free(self):
self.time += 1 self.time += 1
self.queue.pop(0)
message = "{},F,{}".format(self.id, self.time) message = "{},F,{}".format(self.id, self.time)
self.publish(message) self.publish(message)
print("{} freeing".format(self.id)) print("{} freeing".format(self.id))
...@@ -161,18 +163,15 @@ class LamportClient(threading.Thread): ...@@ -161,18 +163,15 @@ class LamportClient(threading.Thread):
def update_times(self, sender, time): def update_times(self, sender, time):
self.time = max(self.time, time)+1 self.time = max(self.time, time)+1
self.times[sender-1] = time self.times[sender] = time
def when_requested(self, sender, time): def when_requested(self, sender, time):
self.update_times(sender, time)
self.queue.append((sender, time)) self.queue.append((sender, time))
self.times[sender] = time
print("received request from {} at time = {}".format(sender, time)) print("received request from {} at time = {}".format(sender, time))
print("queue is now {}".format(self.queue)) print("queue is now {}".format(self.queue))
self.validate() self.validate()
def when_freed(self, sender, time): def when_freed(self, sender, time):
self.update_times(sender, time)
for (previousSender, previousTime) in self.queue: for (previousSender, previousTime) in self.queue:
if (previousSender == sender): if (previousSender == sender):
self.queue.remove((previousSender, previousTime)) self.queue.remove((previousSender, previousTime))
...@@ -180,7 +179,6 @@ class LamportClient(threading.Thread): ...@@ -180,7 +179,6 @@ class LamportClient(threading.Thread):
sender, time)) sender, time))
def when_validated(self, sender, time): def when_validated(self, sender, time):
self.update_times(sender, time)
print("received validation from {} at time = {}".format(sender, time)) print("received validation from {} at time = {}".format(sender, time))
def when_receive(self, body): def when_receive(self, body):
...@@ -194,6 +192,7 @@ class LamportClient(threading.Thread): ...@@ -194,6 +192,7 @@ class LamportClient(threading.Thread):
if sender == self.id: if sender == self.id:
return return
self.update_times(sender, time)
if message == 'R': if message == 'R':
self.when_requested(sender, time) self.when_requested(sender, time)
elif message == 'F': elif message == 'F':
...@@ -202,13 +201,21 @@ class LamportClient(threading.Thread): ...@@ -202,13 +201,21 @@ class LamportClient(threading.Thread):
self.when_validated(sender, time) self.when_validated(sender, time)
def can_acquire(self): def can_acquire(self):
priority_demand = self.queue[0] if len(self.queue) > 0:
if(priority_demand[0]) == self: priority_demand = self.queue[0]
can = True print("next is {}".format(priority_demand))
for time in self.times: if(priority_demand[0]) == self.id:
if (time <= priority_demand[1]): print("I am next... can I go?")
can = False can = True
return can print("I received messages at times {}".format(self.times))
for time in self.times.values():
if (time <= priority_demand[1]):
can = False
return can
else:
print("waiting for my turn...")
else:
return False
def to_free(self): def to_free(self):
self.mustFree = True self.mustFree = True
...@@ -225,7 +232,7 @@ def critical_section(client): ...@@ -225,7 +232,7 @@ def critical_section(client):
print("{} in critical section...".format(client.id)) print("{} in critical section...".format(client.id))
for i in range(5): for i in range(5):
print(" "*(5-i)+"."*i) print(" "*(5-i)+"."*i)
time.sleep(random.randint(1,5)) time.sleep(1)
if __name__ == "__main__": if __name__ == "__main__":
id = int(sys.argv[1]) id = int(sys.argv[1])
...@@ -239,13 +246,13 @@ if __name__ == "__main__": ...@@ -239,13 +246,13 @@ if __name__ == "__main__":
pass pass
while True: while True:
time.sleep(random.randint(1,5)) time.sleep(random.randint(1,2))
client.to_request() client.to_request()
while not client.can_acquire(): while not client.can_acquire():
time.sleep(random.randint(1,5)) time.sleep(random.randint(1,2))
#print("still can't acquire...") #print("still can't acquire...")
print("ready to acquire!") print("ready to acquire!")
critical_section() critical_section(client)
client.to_free() client.to_free()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment