Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 1 | # Copyright (C) 2019 Garmin Ltd. |
| 2 | # |
| 3 | # SPDX-License-Identifier: GPL-2.0-only |
| 4 | # |
| 5 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 6 | import asyncio |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 7 | import json |
| 8 | import logging |
| 9 | import socket |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 10 | import os |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame^] | 11 | import bb.asyncrpc |
| 12 | from . import create_async_client |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 13 | |
| 14 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 15 | logger = logging.getLogger("hashserv.client") |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 16 | |
| 17 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame^] | 18 | class AsyncClient(bb.asyncrpc.AsyncClient): |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 19 | MODE_NORMAL = 0 |
| 20 | MODE_GET_STREAM = 1 |
| 21 | |
| 22 | def __init__(self): |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame^] | 23 | super().__init__('OEHASHEQUIV', '1.1', logger) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 24 | self.mode = self.MODE_NORMAL |
| 25 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame^] | 26 | async def setup_connection(self): |
| 27 | await super().setup_connection() |
| 28 | cur_mode = self.mode |
| 29 | self.mode = self.MODE_NORMAL |
| 30 | await self._set_mode(cur_mode) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 31 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 32 | async def send_stream(self, msg): |
| 33 | async def proc(): |
| 34 | self.writer.write(("%s\n" % msg).encode("utf-8")) |
| 35 | await self.writer.drain() |
| 36 | l = await self.reader.readline() |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 37 | if not l: |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame^] | 38 | raise ConnectionError("Connection closed") |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 39 | return l.decode("utf-8").rstrip() |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 40 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 41 | return await self._send_wrapper(proc) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 42 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 43 | async def _set_mode(self, new_mode): |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 44 | if new_mode == self.MODE_NORMAL and self.mode == self.MODE_GET_STREAM: |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 45 | r = await self.send_stream("END") |
| 46 | if r != "ok": |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame^] | 47 | raise ConnectionError("Bad response from server %r" % r) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 48 | elif new_mode == self.MODE_GET_STREAM and self.mode == self.MODE_NORMAL: |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 49 | r = await self.send_message({"get-stream": None}) |
| 50 | if r != "ok": |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame^] | 51 | raise ConnectionError("Bad response from server %r" % r) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 52 | elif new_mode != self.mode: |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 53 | raise Exception( |
| 54 | "Undefined mode transition %r -> %r" % (self.mode, new_mode) |
| 55 | ) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 56 | |
| 57 | self.mode = new_mode |
| 58 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 59 | async def get_unihash(self, method, taskhash): |
| 60 | await self._set_mode(self.MODE_GET_STREAM) |
| 61 | r = await self.send_stream("%s %s" % (method, taskhash)) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 62 | if not r: |
| 63 | return None |
| 64 | return r |
| 65 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 66 | async def report_unihash(self, taskhash, method, outhash, unihash, extra={}): |
| 67 | await self._set_mode(self.MODE_NORMAL) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 68 | m = extra.copy() |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 69 | m["taskhash"] = taskhash |
| 70 | m["method"] = method |
| 71 | m["outhash"] = outhash |
| 72 | m["unihash"] = unihash |
| 73 | return await self.send_message({"report": m}) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 74 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 75 | async def report_unihash_equiv(self, taskhash, method, unihash, extra={}): |
| 76 | await self._set_mode(self.MODE_NORMAL) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 77 | m = extra.copy() |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 78 | m["taskhash"] = taskhash |
| 79 | m["method"] = method |
| 80 | m["unihash"] = unihash |
| 81 | return await self.send_message({"report-equiv": m}) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 82 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 83 | async def get_taskhash(self, method, taskhash, all_properties=False): |
| 84 | await self._set_mode(self.MODE_NORMAL) |
| 85 | return await self.send_message( |
| 86 | {"get": {"taskhash": taskhash, "method": method, "all": all_properties}} |
| 87 | ) |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 88 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 89 | async def get_outhash(self, method, outhash, taskhash): |
| 90 | await self._set_mode(self.MODE_NORMAL) |
| 91 | return await self.send_message( |
| 92 | {"get-outhash": {"outhash": outhash, "taskhash": taskhash, "method": method}} |
| 93 | ) |
| 94 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 95 | async def get_stats(self): |
| 96 | await self._set_mode(self.MODE_NORMAL) |
| 97 | return await self.send_message({"get-stats": None}) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 98 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 99 | async def reset_stats(self): |
| 100 | await self._set_mode(self.MODE_NORMAL) |
| 101 | return await self.send_message({"reset-stats": None}) |
| 102 | |
| 103 | async def backfill_wait(self): |
| 104 | await self._set_mode(self.MODE_NORMAL) |
| 105 | return (await self.send_message({"backfill-wait": None}))["tasks"] |
| 106 | |
| 107 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame^] | 108 | class Client(bb.asyncrpc.Client): |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 109 | def __init__(self): |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame^] | 110 | super().__init__() |
| 111 | self._add_methods( |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 112 | "connect_tcp", |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 113 | "close", |
| 114 | "get_unihash", |
| 115 | "report_unihash", |
| 116 | "report_unihash_equiv", |
| 117 | "get_taskhash", |
| 118 | "get_stats", |
| 119 | "reset_stats", |
| 120 | "backfill_wait", |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame^] | 121 | ) |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 122 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame^] | 123 | def _get_async_client(self): |
| 124 | return AsyncClient() |