Commit 258ec5bb authored by 's avatar
Browse files

appel par callback

parent 4b841434
......@@ -27,7 +27,7 @@ class LamportClient:
self.time = 0
self.queue = list();
self.times = {n:0 for n in range(clients)}
self.times = {n:0 for n in range(1,clients+1)}
self.channel = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost')
......@@ -46,10 +46,8 @@ class LamportClient:
print("Well connected to server")
while True:
(method, properties, body) = self.channel.basic_get(self.reception)
if body is not None:
self.when_receive(body)
self.channel.basic_consume(self.when_receive, self.reception)
if self.mustFree:
self.free()
self.mustFree = False
......@@ -58,13 +56,14 @@ class LamportClient:
self.request()
self.mustRequest = False
time.sleep(random.randint(1,5))
client.request()
while not client.can_acquire():
pass
client.acquire()
critical_section()
client.free()
self.channel.start_consuming()
#time.sleep(random.randint(1,5))
#client.request()
#while not client.can_acquire():
# pass
#client.acquire()
#critical_section()
#client.free()
def publish(self, message):
......@@ -78,18 +77,21 @@ class LamportClient:
request access to critical section
"""
self.queue.append((self.id, self.time))
self.time += 1
message = "{},R,{}".format(self.id, self.time)
self.publish(message)
print("requesting")
def free(self):
self.time += 1
message = "{},F,{}".format(self.id, self.time)
self.publish(message)
print("freeing")
def validate(self):
message = "{},V,{}".format(self.id, self.time)
print("validating")
def update_times(self, sender, time):
self.time = max(self.time, time)+1
......@@ -100,18 +102,23 @@ class LamportClient:
self.queue.append((sender, time))
self.times[sender] = time
self.validate()
print("received request")
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))
print("received free")
def when_validated(self, sender, time):
self.update_times(sender, time)
def when_receive(self, body):
def when_receive(self, channel, method, properties, body):
body = str(body)
print("received {}".format(body))
sender, message, time = body.split(',')
sender = int(sender)
time = int(time)
......
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