Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
NERON
TPmqRabbit
Commits
4b841434
Commit
4b841434
authored
Nov 23, 2017
by
Browse files
ça tourne, mais ça fait rien
parent
cec64f85
Changes
2
Hide whitespace changes
Inline
Side-by-side
LagoLamport/.lamport_node_exchange.py.swp
0 → 100644
View file @
4b841434
File added
lamportclient.py
View file @
4b841434
"""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
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment