From 1aa331780e62c9a500a3d48733ae61c8e519d679 Mon Sep 17 00:00:00 2001
From: Millian Poquet <millian.poquet@irit.fr>
Date: Sun, 13 Nov 2022 20:41:33 +0100
Subject: [PATCH] python client

---
 client-py/client-py                   |  1 +
 client-py/client.py                   | 35 +++++++++++++++++++++++++++
 client-py/setup.py                    | 24 ++++++++++++++++++
 default.nix                           | 18 ++++++++++++++
 test/simulator-py-client.expected.err |  9 +++++++
 test/simulator-py-client.sh           | 25 +++++++++++++++++++
 6 files changed, 112 insertions(+)
 create mode 120000 client-py/client-py
 create mode 100755 client-py/client.py
 create mode 100644 client-py/setup.py
 create mode 100644 test/simulator-py-client.expected.err
 create mode 100755 test/simulator-py-client.sh

diff --git a/client-py/client-py b/client-py/client-py
new file mode 120000
index 0000000..59b9c19
--- /dev/null
+++ b/client-py/client-py
@@ -0,0 +1 @@
+client.py
\ No newline at end of file
diff --git a/client-py/client.py b/client-py/client.py
new file mode 100755
index 0000000..1b527b4
--- /dev/null
+++ b/client-py/client.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+import zmq
+import protocol.hello_pb2 as proto
+
+def main():
+    context = zmq.Context()
+    socket = context.socket(zmq.REP)
+    socket.bind("tcp://*:28000")
+
+    # Round 1: hello
+    msg = socket.recv()
+
+    order = proto.Order()
+    order.hello.name = 'client-py'
+    msg_str = order.SerializeToString()
+    socket.send(msg_str)
+
+    # Round2: compute flops
+    msg = socket.recv()
+
+    order = proto.Order()
+    order.compute_flops.flop_amount = 1e9
+    msg_str = order.SerializeToString()
+    socket.send(msg_str)
+
+    # Round3: bye
+    msg = socket.recv()
+
+    order = proto.Order()
+    order.bye.SetInParent()
+    msg_str = order.SerializeToString()
+    socket.send(msg_str)
+
+if __name__ == '__main__':
+    main()
diff --git a/client-py/setup.py b/client-py/setup.py
new file mode 100644
index 0000000..5e7563d
--- /dev/null
+++ b/client-py/setup.py
@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+from distutils.core import setup
+
+setup(name='client-py',
+    version='0.1.0',
+    py_modules=['client'],
+    scripts=['client-py'],
+
+    python_requires='>=3.6',
+    install_requires=[
+        'pyzmq>=22.3.0',
+        'protobuf>=3.19.4',
+    ],
+
+    description="Example python client for DZ2 toy CI prototype.",
+    author='Millian Poquet',
+    author_email='millian.poquet@irit.fr',
+    url='https://gitlab.irit.fr/sepia/datazero/ci-prototype',
+    license='MIT',
+    classifiers=[
+        "Programming Language :: Python :: 3",
+        "License :: OSI Approved :: MIT License",
+    ],
+)
diff --git a/default.nix b/default.nix
index 75e049a..48de43e 100644
--- a/default.nix
+++ b/default.nix
@@ -77,6 +77,18 @@ let self = rec {
       zeromq
     ];
   };
+  client-py = pythonPackages.buildPythonPackage {
+    name = "client-py-0.1.0";
+    src = pkgs.lib.sourceByRegex ./client-py [
+      "setup\.py"
+      "client\.py"
+    ];
+    propagatedBuildInputs = [
+      protocol-python
+      pythonPackages.protobuf
+      pythonPackages.pyzmq
+    ];
+  };
 
   integration_shell_cpp = pkgs.mkShell {
     buildInputs = [
@@ -84,6 +96,12 @@ let self = rec {
       client-cpp
     ];
   };
+  integration_shell_py = pkgs.mkShell {
+    buildInputs = [
+      simgrid-simulator
+      client-py
+    ];
+  };
 };
 in
   self
diff --git a/test/simulator-py-client.expected.err b/test/simulator-py-client.expected.err
new file mode 100644
index 0000000..ee33178
--- /dev/null
+++ b/test/simulator-py-client.expected.err
@@ -0,0 +1,9 @@
+[Fafard:orchestrator:(1) 0.000000] [orchestrator/INFO] started!
+[Fafard:orchestrator:(1) 0.000000] [orchestrator/INFO] connecting to endpoint 'tcp://localhost:28000'...
+[Fafard:orchestrator:(1) 0.000000] [orchestrator/INFO] connected!
+[Fafard:orchestrator:(1) 0.000000] [orchestrator/INFO] sending what you wanna do
+[Fafard:orchestrator:(1) 0.000000] [orchestrator/INFO] received hello from client-py
+[Fafard:orchestrator:(1) 0.000000] [orchestrator/INFO] sending what you wanna do
+[Fafard:orchestrator:(1) 0.000000] [orchestrator/INFO] computing 1e+09 flops
+[Fafard:orchestrator:(1) 13.106847] [orchestrator/INFO] sending what you wanna do
+[Fafard:orchestrator:(1) 13.106847] [orchestrator/INFO] received bye
diff --git a/test/simulator-py-client.sh b/test/simulator-py-client.sh
new file mode 100755
index 0000000..6f68883
--- /dev/null
+++ b/test/simulator-py-client.sh
@@ -0,0 +1,25 @@
+#!/usr/bin/env sh
+set -eu
+
+simulator simgrid-simulator/platforms/small_platform.xml 1>simulator.out 2>simulator.err &
+simulator_pid=$!
+
+client-py 1>client-py.out 2>client-py.err &
+client_pid=$!
+
+wait ${client_pid}
+wait ${simulator_pid}
+
+script_dir=$(dirname "$0")
+diff simulator.err "${script_dir}/simulator-py-client.expected.err" > simulator.err.diff || true
+
+if [ -s simulator.err.diff ]; then
+    # file not empty, there is a diff!
+    echo "simulation output is not the expected one, aborting!"
+    cat simulator.err.diff
+    exit 1
+else
+    # file is empty, everything is fine
+    echo "simulation output is the expected one"
+    exit 0
+fi
-- 
GitLab