{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Note on synchonous / asynchronous"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The R2lab library exposes 2 flavours for the sidecar client, that are\n",
    "\n",
    "* `SidecarAsyncClient`\n",
    "* `SidecarSyncClient`\n",
    "\n",
    "The core of the code lies with the former; the latter is just a convenience layer for apps that do not want to write any asynchronous code."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this notebook we go for the asynchronous version, taking advantage of **IPython7**'s ability to do `await` at the toplevel, which is so cool :) \n",
    "\n",
    "The synchronous version would not work btw in this notebook, because of conflicts with the event loop..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Import sidecar class from r2lab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from r2lab import SidecarAsyncClient as Sidecar"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Servers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default these classes join wss://r2lab.inria.fr:999/. To reach a local sidecar devel server, use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "devel_url = \"ws://localhost:10000/\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Probing for the complete nodes status"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "async with Sidecar(devel_url) as sidecar:\n",
    "    status = await sidecar.nodes_status()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "node 9 -> {'gnuradio_release': 'none', 'uname': '4.14.14-300.fc27.x86_64', 'usrp_on_off': 'off', 'os_release': 'fedora-27', 'control_ping': 'off', 'cmc_on_off': 'off', 'image_radical': 'fedora-27-new-layout', 'id': 9, 'available': 'ok', 'control_ssh': 'off', 'images_usrp': [], 'images_wifi': ['09-1.JPG', '09-2.JPG'], 'usrp_type': 'none', 'usrp_duplexer': '', 'images': ['2017-06/fit09-1.jpg', '2017-06/fit09-2.jpg', '2017-12/fit09.jpg']}\n",
      "node 28 -> {'gnuradio_release': 'none', 'available': 'ok', 'uname': '4.14.14-300.fc27.x86_64', 'usrp_on_off': 'off', 'control_ping': 'off', 'cmc_on_off': 'off', 'image_radical': 'fedora-27-new-layout', 'id': 28, 'os_release': 'fedora-27', 'control_ssh': 'off', 'usrp_type': 'usrp1', 'images_usrp': [], 'images_wifi': ['28-1.JPG', '28-2.JPG'], 'usrp_duplexer': '2-5G', 'images': ['2017-06/fit28-1.jpg', '2017-06/fit28-2.jpg', '2017-12/fit28-usrp1.jpg', '2017-12/fit28.jpg']}\n"
     ]
    }
   ],
   "source": [
    "from itertools import islice\n",
    "for id, info in islice(status.items(), 2):\n",
    "    print(f\"node {id} -> {info}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to emit a message"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Make sure your devel sidecar server is up and running, and:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "async with Sidecar(devel_url) as sidecar:\n",
    "    await sidecar.set_node_attribute(1, 'available', 'ok')\n",
    "    await sidecar.set_nodes_triples(\n",
    "        (2, 'available', 'ko'),\n",
    "        (2, 'cmc_on_off', 'off'),\n",
    "        (1, 'available', 'ko'),\n",
    "    )"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
