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
e0d3123f
Commit
e0d3123f
authored
Nov 23, 2017
by
Browse files
début
parent
d4af81ca
Changes
1
Hide whitespace changes
Inline
Side-by-side
lamportclient.py
0 → 100644
View file @
e0d3123f
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
()
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