-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathB.py
More file actions
85 lines (68 loc) · 2.54 KB
/
B.py
File metadata and controls
85 lines (68 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# B.py (Broker with Measurements)
import concore
import time
# --- Fallback Definitions for Direct Execution ---
# These values are normally injected by copy_with_port_portname.py during study generation.
# When running this script directly, safe defaults are used instead.
_STANDALONE_MODE = False
if 'PORT_NAME_F1_F2' not in globals():
PORT_NAME_F1_F2 = "F1_F2"
_STANDALONE_MODE = True
if 'PORT_F1_F2' not in globals():
PORT_F1_F2 = "5555"
_STANDALONE_MODE = True
if 'PORT_NAME_F2_F3' not in globals():
PORT_NAME_F2_F3 = "F2_F3"
_STANDALONE_MODE = True
if 'PORT_F2_F3' not in globals():
PORT_F2_F3 = "5556"
_STANDALONE_MODE = True
if _STANDALONE_MODE:
print("Warning: Port variables not injected. Running in standalone mode with default values.")
print(" For full study behavior, run via study generation (makestudy).")
# --- ZMQ Initialization ---
# This REP socket binds and waits for requests from Node A
concore.init_zmq_port(
port_name=PORT_NAME_F1_F2,
port_type="bind",
address="tcp://*:" + PORT_F1_F2,
socket_type_str="REP"
)
# This REQ socket connects to Node C
concore.init_zmq_port(
port_name=PORT_NAME_F2_F3,
port_type="connect",
address="tcp://localhost:" + PORT_F2_F3,
socket_type_str="REQ"
)
print("Node B broker started. Waiting for requests...")
# --- Measurement Initialization ---
start_time = time.monotonic()
messages_routed = 0
while True:
# 1. Wait for a request from Node A
value_from_a = concore.read(PORT_NAME_F1_F2, "value", [0.0])
received_value = value_from_a[0]
print(f"Node B: Received {received_value:.2f} from Node A. Forwarding to C...")
# 2. Send the received value as a new request to Node C
concore.write(PORT_NAME_F2_F3, "value", [received_value])
# 3. Wait for the reply from Node C
value_from_c = concore.read(PORT_NAME_F2_F3, "value", [0.0])
processed_value = value_from_c[0]
print(f"Node B: Received {processed_value:.2f} from Node C. Replying to A...")
# 4. Send the processed value back as a reply to Node A
concore.write(PORT_NAME_F1_F2, "value", [processed_value])
messages_routed += 1
# 5. Check termination condition
if processed_value >= 100:
break
# --- Finalize and Report Measurements ---
end_time = time.monotonic()
duration = end_time - start_time
print("\n" + "="*30)
print("--- NODE B: RESULTS ---")
print(f"Total messages routed: {messages_routed}")
print(f"Total execution time: {duration:.4f} seconds")
print("="*30)
print("\nNode B: Terminating.")
concore.terminate_zmq()