File simulatie/rabbit/connection.py changed (mode: 100644) (index 0ff3b76..edaf55a) |
... |
... |
import threading |
5 |
5 |
creds = pika.PlainCredentials(model.uname, model.passwd) |
creds = pika.PlainCredentials(model.uname, model.passwd) |
6 |
6 |
connection = pika.BlockingConnection(pika.ConnectionParameters(host=model.host, virtual_host=model.vhost, credentials=creds)) |
connection = pika.BlockingConnection(pika.ConnectionParameters(host=model.host, virtual_host=model.vhost, credentials=creds)) |
7 |
7 |
channel = connection.channel() |
channel = connection.channel() |
|
8 |
|
recieve = connection.channel() |
8 |
9 |
|
|
9 |
10 |
channel.exchange_declare( |
channel.exchange_declare( |
10 |
11 |
exchange='1234', |
exchange='1234', |
11 |
12 |
exchange_type='direct' |
exchange_type='direct' |
12 |
13 |
) |
) |
13 |
14 |
|
|
14 |
|
result = channel.queue_declare(exclusive=True) |
|
15 |
|
queuename = result.method.queue |
|
16 |
|
|
|
17 |
|
channel.queue_bind( |
|
18 |
|
exchange='1234', |
|
19 |
|
queue=queuename, |
|
20 |
|
routing_key=model.vhost |
|
21 |
|
) |
|
22 |
|
|
|
23 |
15 |
def callback(ch, method, prop, body): |
def callback(ch, method, prop, body): |
24 |
|
print('%r ontvangen' % body) |
|
|
16 |
|
print(model.recieveQueue + ': ') |
|
17 |
|
print(body.decode('utf-8', 'ignore')) |
25 |
18 |
|
|
26 |
|
channel.basic_consume ( |
|
|
19 |
|
recieve.basic_consume ( |
27 |
20 |
callback, |
callback, |
28 |
|
queue=queuename, |
|
|
21 |
|
queue=model.recieveQueue, |
29 |
22 |
no_ack=True |
no_ack=True |
30 |
23 |
) |
) |
31 |
24 |
|
|
32 |
25 |
def sendMsg(msg): |
def sendMsg(msg): |
33 |
26 |
channel.basic_publish ( |
channel.basic_publish ( |
34 |
|
exchange='1234', |
|
35 |
|
routing_key=model.vhost, |
|
|
27 |
|
exchange='', |
|
28 |
|
routing_key=model.sendQueue, |
36 |
29 |
body=msg |
body=msg |
37 |
30 |
) |
) |
38 |
31 |
print('Msg sent.') |
print('Msg sent.') |
39 |
|
|
|
|
32 |
|
|
40 |
33 |
def listen(): |
def listen(): |
41 |
34 |
print('Waiting for a sign..') |
print('Waiting for a sign..') |
42 |
|
channel.start_consuming() |
|
|
35 |
|
recieve.start_consuming() |
43 |
36 |
|
|
44 |
37 |
thread = threading.Thread(target=listen) |
thread = threading.Thread(target=listen) |
45 |
38 |
thread.setDaemon(True) |
thread.setDaemon(True) |
File simulatie/rabbit/test/connection.py added (mode: 100644) (index 0000000..72c16dd) |
|
1 |
|
import pika |
|
2 |
|
import model |
|
3 |
|
import threading |
|
4 |
|
|
|
5 |
|
creds = pika.PlainCredentials(model.uname, model.passwd) |
|
6 |
|
connection = pika.BlockingConnection(pika.ConnectionParameters(host=model.host, virtual_host=model.vhost, credentials=creds)) |
|
7 |
|
channel = connection.channel() |
|
8 |
|
recieve = connection.channel() |
|
9 |
|
|
|
10 |
|
channel.exchange_declare( |
|
11 |
|
exchange='1234', |
|
12 |
|
exchange_type='direct' |
|
13 |
|
) |
|
14 |
|
|
|
15 |
|
def callback(ch, method, prop, body): |
|
16 |
|
print(model.recieveQueue + ': ') |
|
17 |
|
print(body.decode('utf-8', 'ignore')) |
|
18 |
|
|
|
19 |
|
#recieve.queue_declare( |
|
20 |
|
# queue=model.recieveQueue |
|
21 |
|
#) |
|
22 |
|
|
|
23 |
|
#channel.queue_declare( |
|
24 |
|
# queue=model.sendQueue, |
|
25 |
|
# auto_delete=True |
|
26 |
|
#) |
|
27 |
|
|
|
28 |
|
#result = channel.queue_declare(exclusive=True) |
|
29 |
|
#queuename = result.method.queue |
|
30 |
|
|
|
31 |
|
#channel.queue_bind( |
|
32 |
|
# exchange='1234', |
|
33 |
|
# queue='simulator', |
|
34 |
|
# routing_key=model.vhost |
|
35 |
|
#) |
|
36 |
|
|
|
37 |
|
#recieve.queue_bind( |
|
38 |
|
# exchange='1234', |
|
39 |
|
# queue='controller', |
|
40 |
|
# routing_key=model.vhost |
|
41 |
|
#) |
|
42 |
|
|
|
43 |
|
#channel.basic_consume ( |
|
44 |
|
# callback, |
|
45 |
|
# queue=recieve, |
|
46 |
|
# no_ack=True |
|
47 |
|
#) |
|
48 |
|
|
|
49 |
|
recieve.basic_consume ( |
|
50 |
|
callback, |
|
51 |
|
queue=model.recieveQueue, |
|
52 |
|
no_ack=True |
|
53 |
|
) |
|
54 |
|
|
|
55 |
|
def sendMsg(msg): |
|
56 |
|
channel.basic_publish ( |
|
57 |
|
exchange='', |
|
58 |
|
routing_key=model.sendQueue, |
|
59 |
|
body=msg |
|
60 |
|
) |
|
61 |
|
print('Msg sent.') |
|
62 |
|
|
|
63 |
|
def listen(): |
|
64 |
|
print('Waiting for a sign..') |
|
65 |
|
recieve.start_consuming() |
|
66 |
|
|
|
67 |
|
thread = threading.Thread(target=listen) |
|
68 |
|
thread.setDaemon(True) |
|
69 |
|
thread.start() |
|
70 |
|
thread.join(0) |
File simulatie/rabbit/test/traffic.py copied from file simulatie/rabbit/traffic.py (similarity 94%) (mode: 100644) (index 2fabb42..9437387) |
... |
... |
def update(index, amountOfCars, dirRequests): |
17 |
17 |
|
|
18 |
18 |
msg = json.dumps({ |
msg = json.dumps({ |
19 |
19 |
"TrafficUpdate": { |
"TrafficUpdate": { |
20 |
|
"LightId": lights[index][0], |
|
|
20 |
|
"LightId": '500', |
21 |
21 |
"Count": lights[index][1], |
"Count": lights[index][1], |
22 |
22 |
"DirectionRequests": lights[index][2] |
"DirectionRequests": lights[index][2] |
23 |
23 |
} |
} |