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):
self.n_clients_here = 0
self.n_clients_total = clients
self.isready = False
self.queue = list();
self.times = {n:0 for n in range(1,clients+1)}
self.mustFree = False
self.mustRequest = True
self.mustRequest = False
self.connection = pika.BlockingConnection(
pika.ConnectionParameters(host="localhost"))
......@@ -76,30 +77,30 @@ class LamportClient(threading.Thread):
exchange= INIT_EXCHANGE,
routing_key='',
body = init_message)
print("saying hello to the world")
self.channel.start_consuming()
print("INIT_DONE")
self.isready = True
print("INIT_DONE\n")
while True:
messages = self.channel.consume(
self.reception, inactivity_timeout=TIMEOUT, no_ack=True)
self.reception, inactivity_timeout=TIMEOUT,
no_ack=True)
mess = next(messages)
if mess is not None:
*_, body = mess
print("received : {}".format(body))
when_receive(body)
else:
print("waiting for message")
self.when_receive(body)
#else:
#print("waiting for message")
if self.mustFree:
print("Must Release")
self.free()
self.mustFree = False
if self.mustRequest:
print("Must Request")
self.request()
self.mustRequest = False
......@@ -132,7 +133,7 @@ class LamportClient(threading.Thread):
send message to server
"""
self.channel.basic_publish(
exchange = "lamport", body=message, routing_key='')
exchange = EXCHANGE, body=message, routing_key='')
def request(self):
"""request
......@@ -143,17 +144,20 @@ class LamportClient(threading.Thread):
self.time += 1
message = "{},R,{}".format(self.id, self.time)
self.publish(message)
print("requesting")
print("{} requesting".format(self.id))
print("queue is now {}".format(self.queue))
def free(self):
self.time += 1
message = "{},F,{}".format(self.id, self.time)
self.publish(message)
print("freeing")
print("{} freeing".format(self.id))
def validate(self):
self.time += 1
message = "{},V,{}".format(self.id, self.time)
print("validating")
self.publish(message)
print("{} validating".format(self.id))
def update_times(self, sender, time):
self.time = max(self.time, time)+1
......@@ -163,25 +167,26 @@ class LamportClient(threading.Thread):
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()
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")
print("received release notification from {} at time = {}".format(
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, channel, method, properties, body):
def when_receive(self, body):
body = str(body)
_,body,_ = body.split("'")
print("received {}".format(body))
sender, message, time = body.split(',')
sender = int(sender)
time = int(time)
......@@ -207,10 +212,14 @@ class LamportClient(threading.Thread):
def to_free(self):
self.mustFree = True
print("{} : Ressource released".format(self.id))
print("{} finished using ressource".format(self.id))
def to_request(self):
self.mustRequest = True
print("User needs {} to access ressource".format(self.id))
def to_acquire(self):
return
def ready(self):
return self.isready
def critical_section(client):
print("{} in critical section...".format(client.id))
......@@ -225,13 +234,17 @@ if __name__ == "__main__":
print("running client thread")
client.start()
print("client thread lauched")
while not client.ready():
pass
while True:
time.sleep(random.randint(1,5))
client.request()
client.to_request()
while not client.can_acquire():
time.sleep(random.randint(1,5))
print("still can't acquire...")
client.to_acquire()
#print("still can't acquire...")
print("ready to acquire!")
critical_section()
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