Commit 258ec5bb authored by 's avatar
Browse files

appel par callback

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