{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#returns the type of the given item of NestedList\n", "from struct import *\n", "import asyncio\n", "import nest_asyncio\n", "nest_asyncio.apply()\n", "import import_ipynb\n", "\n", "from libs_pkg.nested_list import *\n", "from libs_pkg.exception_handler import *\n", " \n", "def binary_type(item):\n", " \"\"\"\n", " The function binary_type() returns the aquivalent binary type of the item in a \n", " nested list according to the data types of the nested lists in Secondo.\n", " :param item: An element in nested list.\n", " :return: The binary data type.\n", " \"\"\"\n", " itemType = item_type(item)\n", " if itemType == \"string\":\n", " length = len(item)\n", " if length < 256:\n", " return 'BIN_SHORTSTRING'\n", " if length < 65536:\n", " return 'BIN_STRING'\n", " return 'BIN_LONGSTRING'\n", " if itemType == \"symbol\":\n", " length = len(item)\n", " if length < 256:\n", " return 'BIN_SHORTSYMBOL'\n", " if length < 65536:\n", " return 'BIN_SYMBOL'\n", " return 'BIN_LONGSYMBOL'\n", " if itemType == \"integer\":\n", " if item in range(-128, 128):\n", " return 'BIN_BYTE'\n", " if item in range(-32768, 32768):\n", " return 'BIN_SHORTINT'\n", " return 'BIN_INTEGER'\n", " if itemType == \"real\":\n", " return 'BIN_DOUBLE'\n", " if itemType == \"boolean\":\n", " return 'BIN_BOOLEAN'\n", " if itemType == \"text\":\n", " length = len(item)\n", " if length < 256:\n", " return 'BIN_SHORTTEXT'\n", " if length < 65536:\n", " return 'BIN_TEXT'\n", " return 'BIN_LONGTEXT'\n", " if itemType == \"list\":\n", " length = len(item)\n", " if length < 256:\n", " return 'BIN_SHORTLIST'\n", " if length < 65536:\n", " return 'BIN_LIST'\n", " return 'BIN_LONGLIST'\n", " \n", " #main function \n", "async def write_binary_header(writer, NL):\n", " \"\"\"\n", " The function write_binary_header() writes the binary header which contains\n", " signature and version to the stream writer of Secondo object which is \n", " connected to Secondo server and converts each element of the nested list\n", " to binary format by calling the function binary_encode().\n", " \n", " :param writer: Stream writer of the Secondo object.\n", " :param NL: The nested list to be converted.\n", " :return: True if the conversion is succesful Flase when not.\n", " \"\"\"\n", " \n", " if not writer:\n", " writer.close()\n", " raise SecondoAPI_ERROR('Connection to secondo reset.')\n", " \n", " sig = bytearray(\"bnl\")\n", " major = pack('>H', 1)\n", " minor = pack('>H', 2)\n", " writer.write(sig)\n", " writer.write(major)\n", " writer.write(minor)\n", " await writer.drain()\n", " ok = await binary_encode(writer, NL)\n", " await writer.drain()\n", " return ok\n", " \n", " \n", "async def binary_encode(writer, NL):\n", " \"\"\"\n", " The function binary_encode() converts each element of the nested list\n", " to binary format and writes them to the stream writer of Secondo object \n", " which is connected to Secondo server.\n", " \n", " :param writer: Stream writer of the Secondo object.\n", " :param NL: The nested list to be converted.\n", " :return: True if the conversion is succesful and Flase when not.\n", " \"\"\"\n", " \n", " if not writer:\n", " writer.close()\n", " raise SecondoAPI_ERROR('Connection to secondo reset.')\n", " \n", " Type_dict = {\"BIN_LONGLIST\": 0,\n", " \"BIN_INTEGER\": 1,\n", " \"BIN_REAL\": 2,\n", " \"BIN_BOOLEAN\": 3,\n", " \"BIN_LONGSTRING\": 4,\n", " \"BIN_LONGSYMBOL\": 5,\n", " \"BIN_LONGTEXT\": 6,\n", " \"BIN_LIST\": 10,\n", " \"BIN_SHORTLIST\": 11,\n", " \"BIN_SHORTINT\": 12,\n", " \"BIN_BYTE\":13,\n", " \"BIN_STRING\": 14,\n", " \"BIN_SHORTSTRING\": 15,\n", " \"BIN_SYMBOL\": 16,\n", " \"BIN_SHORTSYMBOL\": 17,\n", " \"BIN_TEXT\": 18,\n", " \"BIN_SHORTTEXT\": 19,\n", " \"BIN_DOUBLE\": 20}\n", " for item in NL:\n", " btype = binary_type(item)\n", " typeCode = None\n", " for typ, val in Type_dict.items():\n", " if btype == typ:\n", " typeCode = val\n", " break\n", " \n", " if btype == 'BIN_BOOLEAN':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " val = 1 if item else 0\n", " writer.write(pack('?', val)[0])\n", " await writer.drain()\n", " return True\n", " \n", " if btype == 'BIN_INTEGER':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(pack('>I', item))\n", " await writer.drain()\n", " return True\n", " if btype == 'BIN_SHORTINT':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(pack('>H', item))\n", " await writer.drain()\n", " return True\n", " if btype == 'BIN_BYTE':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(item.to_bytes(1, byteorder='big'))\n", " await writer.drain()\n", " return True\n", " \n", " if btype == 'BIN_REAL':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(pack('>f', item)[0])\n", " await writer.drain()\n", " return True\n", " \n", " if btype == 'BIN_DOUBLE':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(pack('>d', item)[0])\n", " await writer.drain()\n", " return True\n", " \n", " if btype == 'BIN_DOUBLE':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(pack('>d', item)[0])\n", " await writer.drain()\n", " return True\n", " \n", " if btype == 'BIN_SHORTSTRING':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(len(item).to_bytes(1, byteorder='big'))\n", " writer.write(item.encode())\n", " await writer.drain()\n", " return True\n", "\n", " if btype == 'BIN_STRING':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(pack('>H', len(item)))\n", " writer.write(item.encode())\n", " await writer.drain()\n", " return True\n", " \n", " if btype == 'BIN_LONGSTRING':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(pack('>I', len(item)))\n", " writer.write(item.encode())\n", " await writer.drain()\n", " return True\n", "\n", " if btype == 'BIN_SHORTSYMBOL':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(len(item).to_bytes(1, byteorder='big'))\n", " writer.write(item.encode())\n", " await writer.drain()\n", " return True\n", "\n", " if btype == 'BIN_SYMBOL':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(pack('>H', len(item)))\n", " writer.write(item.encode())\n", " await writer.drain()\n", " return True\n", " \n", " if btype == 'BIN_LONGSYMBOL':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(pack('>I', len(item)))\n", " writer.write(item.encode())\n", " await writer.drain()\n", " return True\n", "\n", " if btype == 'BIN_SHORTTEXT':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(len(item).to_bytes(1, byteorder='big'))\n", " writer.write(item.encode())\n", " await writer.drain()\n", " return True\n", " \n", " if btype == 'BIN_TEXT':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(pack('>H', len(item)))\n", " writer.write(item.encode())\n", " await writer.drain()\n", " return True\n", "\n", " if btype == 'BIN_LONGTEXT':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(pack('>I', len(item)))\n", " writer.write(item.encode())\n", " await writer.drain()\n", " return True\n", "\n", " if btype == 'BIN_SHORTLIST':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(len(item).to_bytes(1, byteorder='big'))\n", " for i in range(len(item)):\n", " res = await binary_encode(writer, item[i])\n", " if not res:\n", " return False\n", " await writer.drain()\n", " return True\n", " \n", " if btype == 'BIN_LIST':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(pack('>H', len(item)))\n", " for i in range(len(item)):\n", " res = await binary_encode(writer, item[i])\n", " if not res:\n", " return False\n", " await writer.drain()\n", " return True\n", " \n", " if btype == 'BIN_LONGLIST':\n", " writer.write(typeCode.to_bytes(1, byteorder='big'))\n", " writer.write(pack('>I', len(item)))\n", " for i in range(len(item)):\n", " res = await binary_encode(writer, item[i])\n", " if not res:\n", " return False\n", " await writer.drain()\n", " return True\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.4" } }, "nbformat": 4, "nbformat_minor": 2 }