Commit 4b841434 authored by 's avatar
Browse files

ça tourne, mais ça fait rien

parent cec64f85
"""lamportclient.py
author: Francois Neron
email: francois.neron@telecom-bretagne.eu
Lamport Algorithm put to the test.
Launch with :
-the identifier of the site represented by this instance,
-the number of sites in total.
as follow : lamportclient.py 1 4
where identifiers of the other sites would be 2,3, and 4
"""
import time
import random
import pika
import sys
TIMEOUT = 0.1
class LamportClient:
def __init__(self, id, clients):
......@@ -13,19 +27,56 @@ class LamportClient:
self.time = 0
self.queue = list();
self.times = {n:0 for n in range(clients)}
self.channel = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost')
).channel()
self.channel.exchange_declare(exchange="lamport", exchange_type="fanout")
self.reception = self.channel.queue_declare(exclusive=True).method.queue
self.channel.queue_bind(exchange = "lamport", queue = self.reception)
self.mustFree = False
self.mustRequest = True
def run(self):
print("I am {} of {} sites".format(self.id, len(list(self.times.keys()))))
if self.channel.connection.is_open:
print("Well connected to server")
while True:
(method, properties, body) = self.channel.basic_get(self.reception)
if body is not None:
self.when_receive(body)
if self.mustFree:
self.free()
self.mustFree = False
if self.mustRequest:
self.request()
self.mustRequest = False
time.sleep(random.randint(1,5))
client.request()
while not client.can_acquire():
pass
client.acquire()
critical_section()
client.free()
def publish(self, message):
"""publish
send message to server
"""
self.channel.basic_publish(exchange = "lamport", body=message)
self.channel.basic_publish(exchange = "lamport", body=message, routing_key='')
def request(self):
"""request
request access to critical section
"""
self.queue.append((self.id, self.time))
self.time += 1
......@@ -40,15 +91,24 @@ class LamportClient:
def validate(self):
message = "{},V,{}".format(self.id, self.time)
def update_times(self, sender, time):
self.time = max(self.time, time)+1
self.times[sender-1] = time
def when_requested(self, sender, time):
self.time = max(self.time, time) + 1
self.update_times(sender, time)
self.queue.append((sender, time))
self.times[sender] = time
self.validate()
def when_freed(self, sender, time):
self.time = max(self.time, time) +1
self.update_times(sender, time)
for (previousSender, previousTime) in self.queue:
if (previousSender == sender):
self.queue.remove((previousSender, previousTime))
def when_validated(self, sender, time):
self.time = max(self.time, time) +1
self.update_times(sender, time)
def when_receive(self, body):
body = str(body)
......@@ -66,22 +126,25 @@ class LamportClient:
elif message == 'V':
self.when_validated(sender, time)
def can_acquire(self):
priority_demand = self.queue[0]
if(priority_demand[0]) == self:
can = True
for time in self.times:
if (time <= priority_demand[1]):
can = False
return can
def critical_section(client):
print("{} in critical section...".format(client.id))
if __name__ == "__main__":
id = int(sys.argv[1])
clients = range(argv[2])
clients = int(sys.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()
client.run()
......
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