Commit 9bbdc320 authored by 's avatar
Browse files

initialisation en place, mais ça bloque pendant

parent 258ec5bb
......@@ -26,17 +26,27 @@ class LamportClient:
self.id = id
self.time = 0
self.n_clients_here = 0
self.n_clients_total = clients
self.queue = list();
self.times = {n:0 for n in range(1,clients+1)}
self.channel = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost')
).channel()
self.channel.exchange_declare(exchange="lamport", exchange_type="fanout")
self.channel.basic_qos(prefetch_count=1)
self.channel.exchange_declare(exchange="lamport", exchange_type="fanout")
self.channel.exchange_declare(exchange="lamport_init", exchange_type="fanout")
self.reception = self.channel.queue_declare(exclusive=True).method.queue
self.channel.queue_bind(exchange = "lamport", queue = self.reception)
self.init_reception = self.channel.queue_declare(exclusive=True).method.queue
self.channel.queue_bind(exchange = "lamport", queue = self.reception)
self.channel.queue_bind(exchange = "lamport_init", queue = self.init_reception)
self.mustFree = False
self.mustRequest = True
......@@ -44,10 +54,16 @@ class LamportClient:
print("I am {} of {} sites".format(self.id, len(list(self.times.keys()))))
if self.channel.connection.is_open:
print("Well connected to server")
self.channel.basic_consume(self.initializeCycle, self.init_reception)
self.channel.basic_publish(exchange="lamport_init", body = "{} says : Hello World!".format(self.id), routing_key='')
print("saying hello to the world")
self.channel.start_consuming()
self.channel.basic_consume(self.when_receive, self.reception)
while True:
self.channel.basic_consume(self.when_receive, self.reception)
if self.mustFree:
self.free()
self.mustFree = False
......@@ -65,6 +81,16 @@ class LamportClient:
#critical_section()
#client.free()
def initializeCycle(self, channel, method, properties, body):
_,body,_ = str(body).split("'")
arrival_id = int(body.split(" ")[0])
self.n_clients_here += 1
print("{} arrived!\nWe are now {} of {}".format(arrival_id, self.n_clients_here, self.n_clients_total))
if self.n_clients_here == self.n_clients_total:
self.channel.stop_consuming()
print ("All Ready, let's go!")
def publish(self, message):
"""publish
......@@ -117,6 +143,8 @@ class LamportClient:
def when_receive(self, channel, method, properties, body):
body = str(body)
_,body,_ = body.split("'")
print("received {}".format(body))
sender, message, time = body.split(',')
......@@ -133,6 +161,8 @@ class LamportClient:
elif message == 'V':
self.when_validated(sender, time)
self.channel.stop_consuming()
def can_acquire(self):
priority_demand = self.queue[0]
if(priority_demand[0]) == self:
......
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