Commit 67b72aee authored by 's avatar
Browse files

bon dieu on dirait que ça marche!

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