Commit e0d3123f authored by 's avatar
Browse files

début

parent d4af81ca
import random
import pika
import sys
import critical_section
class LamportClient:
def __init__(self, id, clients):
self.id = id
self.time = 0
self.queue = list();
self.channel = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost')
).channel()
self.channel.exchange_declare(exchange="lamport", exchange_type="fanout")
def publish(self, message):
"""publish
send message to server
"""
self.channel.basic_publish(exchange = "lamport", body=message)
def request(self):
self.queue.append((self.id, self.time))
self.time += 1
message = "{},R,{}".format(self.id, self.time)
self.publish(message)
def free(self):
self.time += 1
message = "{},F,{}".format(self.id, self.time)
self.publish(message)
def validate(self):
message = "{},V,{}".format(self.id, self.time)
def when_requested(self, sender, time):
self.time = max(self.time, time) + 1
self.validate()
def when_freed(self, sender, time):
self.time = max(self.time, time) +1
def when_validated(self, sender, time):
self.time = max(self.time, time) +1
def when_receive(self, body):
body = str(body)
sender, message, time = body.split(',')
sender = int(sender)
time = int(time)
if sender == self.id:
return
if message == 'R':
self.when_requested(sender, time)
elif message == 'F':
self.when_freed(sender, time)
elif message == 'V':
self.when_validated(sender, time)
if __name__ == "__main__":
id = int(sys.argv[1])
clients = range(argv[2])
client = LamportClient(id, clients)
client.start()
while True:
time.sleep(random.randint(1,5))
client.request_ressource()
while not client.can_acquire():
pass
client.acquire()
critical_section.acquired_by(client)
client.free()
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