Skip to content

Commit 379a126

Browse files
committed
Add documentation for the simple client-server example using the new SDK
1 parent 844acb6 commit 379a126

5 files changed

Lines changed: 99 additions & 4 deletions

File tree

examples/distributed/teleport/run.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
# Check if SimulaQron is already running
44
if [ ! -f ~/.simulaqron_pids/simulaqron_network_default.pid ]; then
5-
simulaqron start --nodes=Alice,Bob --network-config-file ./networkConfig.json
5+
# If not, start simulaqron backend for both nodes
6+
simulaqron start --nodes=Alice,Bob --network-config-file simulaqron_network.json
67
fi
78

89
python3 teleport-bob.py &
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Classical Client-Server example
2+
3+
Very simple example to demonstrate how to write client-server applications which exchange
4+
classical messages.
5+
6+
7+
# How to run
8+
9+
There are two ways to run this example:
10+
11+
12+
## On a single machine
13+
14+
The simplest way to run this example is on a single machine. To do this, you can use the
15+
provided bash script:
16+
17+
```shell
18+
./run.sh
19+
```
20+
21+
Alternatively, you can manually start the client and teh server in two different terminals.
22+
First, start the server on one terminal
23+
24+
```shell
25+
python example_server_alice.py
26+
```
27+
28+
which start the server and keeps listening for new incoming connections. Then start the client:
29+
30+
```shell
31+
python example_client_bob.py
32+
```
33+
34+
which will connect to the server and send a message that will be sent back by the server.
35+
36+
37+
## On different machines
38+
39+
To run this example on different machines, it is necessary that both machines can reach each
40+
other via a network (or the internet). Additionally, you need to know the IP addresses of both
41+
machines.
42+
43+
Assuming that the server (alice) will run on a machine with IP `192.168.0.1` and the client (bob)
44+
will run on the machine with IP `192.168.0.2`, modify the `simulaqron_network.json` file *on both*
45+
the server and the client to look like this:
46+
47+
```json
48+
[
49+
{
50+
"name": "default",
51+
"nodes": [
52+
{
53+
"Alice": {
54+
"app_socket": ["192.168.0.1", 8821],
55+
"qnodeos_socket": ["192.168.0.1", 8822],
56+
"vnode_socket": ["192.168.0.1", 8823]
57+
}
58+
},
59+
{
60+
"Bob": {
61+
"app_socket": ["192.168.0.2", 9831],
62+
"qnodeos_socket": ["192.168.0.2", 9832],
63+
"vnode_socket": ["192.168.0.2", 9833]
64+
}
65+
}
66+
],
67+
"topology": null
68+
}
69+
]
70+
```
71+
72+
Note that the `localhost` entries from Alice were changed to `192.168.0.1`. Similarly, the
73+
`localhost` entries from Bob were changed to `192.168.0.2`.
74+
75+
After these modifications, start the server and client in their respective terminals as described
76+
before.

examples/new-sdk/classical-client-server/example_client.py renamed to examples/new-sdk/classical-client-server/example_client_bob.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
async def client_code(reader: StreamReader, writer: StreamWriter):
1010
data = "Hello World!".encode("utf-8")
1111
writer.write(data)
12-
print(f"Sent message '{data.decode("utf-8")}'")
12+
print(f"Client sent message '{data.decode("utf-8")}'")
1313
result = await reader.read(255)
14-
print(f"Received message: '{result.decode("utf-8")}'")
14+
print(f"Client received message: '{result.decode("utf-8")}'")
1515
writer.close()
1616

1717
if __name__ == "__main__":
18+
# This is "Bob" - the client
1819
# Load the file network configuration file
1920
network_config_file = Path("simulaqron_network.json")
2021
network_config.read_from_file(network_config_file)

examples/new-sdk/classical-client-server/example_server.py renamed to examples/new-sdk/classical-client-server/example_server_alice.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88

99
async def connection_handler(reader: StreamReader, writer: StreamWriter):
1010
result = await reader.read(255)
11-
print(f"Received message: '{result.decode("utf-8")}'")
11+
print(f"Server received message: '{result.decode("utf-8")}'")
1212
writer.write(result)
13+
print(f"Server send message: '{result.decode("utf-8")}'")
1314
writer.close()
1415

1516
if __name__ == "__main__":
17+
# This is "Alice" - the server
1618
# Load the file network configuration file
1719
network_config_file = Path("simulaqron_network.json")
1820
network_config.read_from_file(network_config_file)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
3+
# Running SimulaQron backend is not needed for this example.
4+
## Check if SimulaQron is already running
5+
#if [ ! -f ~/.simulaqron_pids/simulaqron_network_default.pid ]; then
6+
# # If not, start simulaqron backend for both nodes
7+
# simulaqron start --nodes=Alice,Bob --network-config-file simulaqron_network.json
8+
#fi
9+
10+
# Run the server
11+
python3 example_server_alice.py &
12+
# Wait 1 second for the server to fully start
13+
sleep 1
14+
# Run the client
15+
python3 example_client_bob.py &

0 commit comments

Comments
 (0)