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
9bbdc320
Commit
9bbdc320
authored
Nov 23, 2017
by
Browse files
initialisation en place, mais ça bloque pendant
parent
258ec5bb
Changes
1
Hide whitespace changes
Inline
Side-by-side
lamportclient.py
View file @
9bbdc320
...
...
@@ -26,17 +26,27 @@ class LamportClient:
self
.
id
=
id
self
.
time
=
0
self
.
n_clients_here
=
0
self
.
n_clients_total
=
clients
self
.
queue
=
list
();
self
.
times
=
{
n
:
0
for
n
in
range
(
1
,
clients
+
1
)}
self
.
channel
=
pika
.
BlockingConnection
(
pika
.
ConnectionParameters
(
host
=
'localhost'
)
).
channel
()
self
.
channel
.
exchange_declare
(
exchange
=
"lamport"
,
exchange_type
=
"fanout"
)
self
.
channel
.
basic_qos
(
prefetch_count
=
1
)
self
.
channel
.
exchange_declare
(
exchange
=
"lamport"
,
exchange_type
=
"fanout"
)
self
.
channel
.
exchange_declare
(
exchange
=
"lamport_init"
,
exchange_type
=
"fanout"
)
self
.
reception
=
self
.
channel
.
queue_declare
(
exclusive
=
True
).
method
.
queue
self
.
channel
.
queue_bind
(
exchange
=
"lamport"
,
queue
=
self
.
reception
)
self
.
init_reception
=
self
.
channel
.
queue_declare
(
exclusive
=
True
).
method
.
queue
self
.
channel
.
queue_bind
(
exchange
=
"lamport"
,
queue
=
self
.
reception
)
self
.
channel
.
queue_bind
(
exchange
=
"lamport_init"
,
queue
=
self
.
init_reception
)
self
.
mustFree
=
False
self
.
mustRequest
=
True
...
...
@@ -44,10 +54,16 @@ class LamportClient:
print
(
"I am {} of {} sites"
.
format
(
self
.
id
,
len
(
list
(
self
.
times
.
keys
()))))
if
self
.
channel
.
connection
.
is_open
:
print
(
"Well connected to server"
)
self
.
channel
.
basic_consume
(
self
.
initializeCycle
,
self
.
init_reception
)
self
.
channel
.
basic_publish
(
exchange
=
"lamport_init"
,
body
=
"{} says : Hello World!"
.
format
(
self
.
id
),
routing_key
=
''
)
print
(
"saying hello to the world"
)
self
.
channel
.
start_consuming
()
self
.
channel
.
basic_consume
(
self
.
when_receive
,
self
.
reception
)
while
True
:
self
.
channel
.
basic_consume
(
self
.
when_receive
,
self
.
reception
)
if
self
.
mustFree
:
self
.
free
()
self
.
mustFree
=
False
...
...
@@ -65,6 +81,16 @@ class LamportClient:
#critical_section()
#client.free()
def
initializeCycle
(
self
,
channel
,
method
,
properties
,
body
):
_
,
body
,
_
=
str
(
body
).
split
(
"'"
)
arrival_id
=
int
(
body
.
split
(
" "
)[
0
])
self
.
n_clients_here
+=
1
print
(
"{} arrived!
\n
We are now {} of {}"
.
format
(
arrival_id
,
self
.
n_clients_here
,
self
.
n_clients_total
))
if
self
.
n_clients_here
==
self
.
n_clients_total
:
self
.
channel
.
stop_consuming
()
print
(
"All Ready, let's go!"
)
def
publish
(
self
,
message
):
"""publish
...
...
@@ -117,6 +143,8 @@ class LamportClient:
def
when_receive
(
self
,
channel
,
method
,
properties
,
body
):
body
=
str
(
body
)
_
,
body
,
_
=
body
.
split
(
"'"
)
print
(
"received {}"
.
format
(
body
))
sender
,
message
,
time
=
body
.
split
(
','
)
...
...
@@ -133,6 +161,8 @@ class LamportClient:
elif
message
==
'V'
:
self
.
when_validated
(
sender
,
time
)
self
.
channel
.
stop_consuming
()
def
can_acquire
(
self
):
priority_demand
=
self
.
queue
[
0
]
if
(
priority_demand
[
0
])
==
self
:
...
...
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