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