Commit a344de1e authored by 's avatar
Browse files

dernier commit ce soir : IT WORKSgit add *! avnt ça marchait qu'à deux, en...

dernier commit ce soir : IT WORKSgit add *! avnt ça marchait qu'à deux, en fait... Là c'est bon, normalement. Par contre du coup c'est plus une horloge logique, mais le temps système que j'utilise, et je trie la queue en fonction du temps de départ des messages, pas de leur arrivée, parce que j'ai l'impression que ce très cher rabbitmq est pas FIFO (mais je fatigue ptet)
parent 67b72aee
......@@ -18,6 +18,7 @@ import random
import pika
import sys
import threading
import operator
EXCHANGE = "Lamport"
INIT_EXCHANGE = "Init"
......@@ -29,7 +30,6 @@ class LamportClient(threading.Thread):
super().__init__()
self.id = id
self.time = 0
self.n_clients_here = 0
self.n_clients_total = clients
......@@ -134,40 +134,35 @@ class LamportClient(threading.Thread):
"""
self.channel.basic_publish(
exchange = EXCHANGE, body=message, routing_key='')
self.times[self.id] = self.time
self.times[self.id] = time.time()
def request(self):
"""request
request access to critical section
"""
self.queue.append((self.id, self.time))
self.time += 1
message = "{},R,{}".format(self.id, self.time)
mtime = time.time()
self.queue.append((self.id, mtime))
message = "{},R,{}".format(self.id, mtime)
self.publish(message)
print("{} requesting".format(self.id))
print("{} requesting at time {}".format(self.id, mtime))
print("queue is now {}".format(self.queue))
def free(self):
self.time += 1
self.queue.pop(0)
message = "{},F,{}".format(self.id, self.time)
message = "{},F,{}".format(self.id, time.time())
self.publish(message)
print("{} freeing".format(self.id))
def validate(self):
self.time += 1
message = "{},V,{}".format(self.id, self.time)
message = "{},V,{}".format(self.id, time.time())
self.publish(message)
print("{} validating".format(self.id))
def update_times(self, sender, time):
self.time = max(self.time, time)+1
self.times[sender] = time
def when_requested(self, sender, time):
self.queue.append((sender, time))
print("received request from {} at time = {}".format(sender, time))
print("received request from {} from time = {}".format(sender, time))
self.queue.sort(key = operator.itemgetter(1))
print("queue is now {}".format(self.queue))
self.validate()
......@@ -175,30 +170,32 @@ class LamportClient(threading.Thread):
for (previousSender, previousTime) in self.queue:
if (previousSender == sender):
self.queue.remove((previousSender, previousTime))
print("received release notification from {} at time = {}".format(
print("received release notification from {} from time = {}".format(
sender, time))
def update_time(self, sender, time):
self.times[sender] = time
def when_validated(self, sender, time):
print("received validation from {} at time = {}".format(sender, time))
print("received validation from {} from time = {}".format(sender, time))
def when_receive(self, body):
body = str(body)
_,body,_ = body.split("'")
sender, message, time = body.split(',')
sender, message, mtime = body.split(',')
sender = int(sender)
time = int(time)
mtime = float(mtime)
if sender == self.id:
return
self.update_times(sender, time)
self.update_time(sender, mtime)
if message == 'R':
self.when_requested(sender, time)
self.when_requested(sender, mtime)
elif message == 'F':
self.when_freed(sender, time)
self.when_freed(sender, mtime)
elif message == 'V':
self.when_validated(sender, time)
self.when_validated(sender, mtime)
def can_acquire(self):
if len(self.queue) > 0:
......@@ -231,7 +228,8 @@ class LamportClient(threading.Thread):
def critical_section(client):
print("{} in critical section...".format(client.id))
for i in range(5):
print(" "*(5-i)+"."*i)
j= i+1
print(" "*(5-j)+"."*2*j)
time.sleep(1)
if __name__ == "__main__":
......
#! usr/bin/env python3
"""main.py
Spawns <count> gnome-terminals, each running <program>.
......@@ -19,8 +18,8 @@ def main(args):
program = args['<program>']
count = args['<count>']
for n in nodes:
command = "{} {} {}".format(program, n, count)
for n in range(count):
command = "{} {} {}".format(program, n+1, count)
subprocess.run(
["gnome-terminal",
"-e",
......
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