Commit 012158ea authored by 's avatar
Browse files

bon ya plus qu'à régler les soucis dans lamport lui même, on dirait bien que le reste marche

parent 2de157cc
...@@ -33,12 +33,13 @@ class LamportClient(threading.Thread): ...@@ -33,12 +33,13 @@ class LamportClient(threading.Thread):
self.n_clients_here = 0 self.n_clients_here = 0
self.n_clients_total = clients self.n_clients_total = clients
self.isready = False
self.queue = list(); self.queue = list();
self.times = {n:0 for n in range(1,clients+1)} self.times = {n:0 for n in range(1,clients+1)}
self.mustFree = False self.mustFree = False
self.mustRequest = True self.mustRequest = False
self.connection = pika.BlockingConnection( self.connection = pika.BlockingConnection(
pika.ConnectionParameters(host="localhost")) pika.ConnectionParameters(host="localhost"))
...@@ -76,30 +77,30 @@ class LamportClient(threading.Thread): ...@@ -76,30 +77,30 @@ class LamportClient(threading.Thread):
exchange= INIT_EXCHANGE, exchange= INIT_EXCHANGE,
routing_key='', routing_key='',
body = init_message) body = init_message)
print("saying hello to the world") print("saying hello to the world")
self.channel.start_consuming() self.channel.start_consuming()
print("INIT_DONE") self.isready = True
print("INIT_DONE\n")
while True: while True:
messages = self.channel.consume( messages = self.channel.consume(
self.reception, inactivity_timeout=TIMEOUT, no_ack=True) self.reception, inactivity_timeout=TIMEOUT,
no_ack=True)
mess = next(messages) mess = next(messages)
if mess is not None: if mess is not None:
*_, body = mess *_, body = mess
print("received : {}".format(body)) self.when_receive(body)
when_receive(body) #else:
else: #print("waiting for message")
print("waiting for message")
if self.mustFree: if self.mustFree:
print("Must Release")
self.free() self.free()
self.mustFree = False self.mustFree = False
if self.mustRequest: if self.mustRequest:
print("Must Request")
self.request() self.request()
self.mustRequest = False self.mustRequest = False
...@@ -132,7 +133,7 @@ class LamportClient(threading.Thread): ...@@ -132,7 +133,7 @@ class LamportClient(threading.Thread):
send message to server send message to server
""" """
self.channel.basic_publish( self.channel.basic_publish(
exchange = "lamport", body=message, routing_key='') exchange = EXCHANGE, body=message, routing_key='')
def request(self): def request(self):
"""request """request
...@@ -143,17 +144,20 @@ class LamportClient(threading.Thread): ...@@ -143,17 +144,20 @@ class LamportClient(threading.Thread):
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") print("{} requesting".format(self.id))
print("queue is now {}".format(self.queue))
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") print("{} freeing".format(self.id))
def validate(self): def validate(self):
self.time += 1
message = "{},V,{}".format(self.id, self.time) message = "{},V,{}".format(self.id, self.time)
print("validating") self.publish(message)
print("{} validating".format(self.id))
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
...@@ -163,25 +167,26 @@ class LamportClient(threading.Thread): ...@@ -163,25 +167,26 @@ class LamportClient(threading.Thread):
self.update_times(sender, time) self.update_times(sender, time)
self.queue.append((sender, time)) self.queue.append((sender, time))
self.times[sender] = time self.times[sender] = time
print("received request from {} at time = {}".format(sender, time))
print("queue is now {}".format(self.queue))
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") print("received release notification from {} at time = {}".format(
sender, time))
def when_validated(self, sender, time): def when_validated(self, sender, time):
self.update_times(sender, time) self.update_times(sender, time)
print("received validation from {} at time = {}".format(sender, time))
def when_receive(self, channel, method, properties, body): def when_receive(self, body):
body = str(body) body = str(body)
_,body,_ = body.split("'") _,body,_ = body.split("'")
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)
...@@ -207,10 +212,14 @@ class LamportClient(threading.Thread): ...@@ -207,10 +212,14 @@ class LamportClient(threading.Thread):
def to_free(self): def to_free(self):
self.mustFree = True self.mustFree = True
print("{} : Ressource released".format(self.id)) print("{} finished using ressource".format(self.id))
def to_acquire(self): def to_request(self):
return self.mustRequest = True
print("User needs {} to access ressource".format(self.id))
def ready(self):
return self.isready
def critical_section(client): def critical_section(client):
print("{} in critical section...".format(client.id)) print("{} in critical section...".format(client.id))
...@@ -225,13 +234,17 @@ if __name__ == "__main__": ...@@ -225,13 +234,17 @@ if __name__ == "__main__":
print("running client thread") print("running client thread")
client.start() client.start()
print("client thread lauched") print("client thread lauched")
while not client.ready():
pass
while True: while True:
time.sleep(random.randint(1,5)) time.sleep(random.randint(1,5))
client.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,5))
print("still can't acquire...") #print("still can't acquire...")
client.to_acquire() print("ready to acquire!")
critical_section() critical_section()
client.to_free() client.to_free()
......
#! usr/bin/env python3
"""main.py
Spawns <count> gnome-terminals, each running <program>.
The program will be passed a sequence of arguments, separated by spaces.
The first argument is the identifier of the program, and the following arguments
are the identifiers of the nodes, including the node itself.
Usage:
main.py <program> <count>
"""
import subprocess
import docopt
def main(args):
program = args['<program>']
count = args['<count>']
for n in nodes:
command = "{} {} {}".format(program, n, count)
subprocess.run(
["gnome-terminal",
"-e",
command])
def format_args(args):
args['<count>'] = int(args['<count>'])
args['<program>'] = "./" + args['<program>']
if __name__ == '__main__':
args = docopt.docopt(__doc__)
format_args(args)
main(args)
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