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 | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 86 | async def get_outhash(self, method, outhash, taskhash): |
| 87 | await self._set_mode(self.MODE_NORMAL) |
| 88 | return await self.send_message( |
| 89 | {"get-outhash": {"outhash": outhash, "taskhash": taskhash, "method": method}} |
| 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 | |
| 104 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 105 | class Client(bb.asyncrpc.Client): |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 106 | def __init__(self): |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 107 | super().__init__() |
| 108 | self._add_methods( |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 109 | "connect_tcp", |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 110 | "close", |
| 111 | "get_unihash", |
| 112 | "report_unihash", |
| 113 | "report_unihash_equiv", |
| 114 | "get_taskhash", |
| 115 | "get_stats", |
| 116 | "reset_stats", |
| 117 | "backfill_wait", |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 118 | ) |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 119 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 120 | def _get_async_client(self): |
| 121 | return AsyncClient() |