diff options
author | Joshua Watt <JPEWhacker@gmail.com> | 2023-11-03 08:26:20 -0600 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2023-11-09 17:33:02 +0000 |
commit | 2484bd893189bc24ac210edf2de3b7e1115eebd3 (patch) | |
tree | be9433e53a5bb645816f5b78730830a73bc6ee10 /bitbake/lib/bb/asyncrpc/connection.py | |
parent | 8f8501ed403dec27acbe780b936bc087fc5006d0 (diff) | |
download | poky-2484bd893189bc24ac210edf2de3b7e1115eebd3.tar.gz |
bitbake: hashserv: Add websocket connection implementation
Adds support to the hash equivalence client and server to communicate
over websockets. Since websockets are message orientated instead of
stream orientated, and new connection class is needed to handle them.
Note that websocket support does require the 3rd party websockets python
module be installed on the host, but it should not be required unless
websockets are actually being used.
(Bitbake rev: 56dd2fdbfb6350a9eef43a12aa529c8637887a7e)
Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/bb/asyncrpc/connection.py')
-rw-r--r-- | bitbake/lib/bb/asyncrpc/connection.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/bitbake/lib/bb/asyncrpc/connection.py b/bitbake/lib/bb/asyncrpc/connection.py index c4fd24754c..a10628f75a 100644 --- a/bitbake/lib/bb/asyncrpc/connection.py +++ b/bitbake/lib/bb/asyncrpc/connection.py | |||
@@ -93,3 +93,47 @@ class StreamConnection(object): | |||
93 | if self.writer is not None: | 93 | if self.writer is not None: |
94 | self.writer.close() | 94 | self.writer.close() |
95 | self.writer = None | 95 | self.writer = None |
96 | |||
97 | |||
98 | class WebsocketConnection(object): | ||
99 | def __init__(self, socket, timeout): | ||
100 | self.socket = socket | ||
101 | self.timeout = timeout | ||
102 | |||
103 | @property | ||
104 | def address(self): | ||
105 | return ":".join(str(s) for s in self.socket.remote_address) | ||
106 | |||
107 | async def send_message(self, msg): | ||
108 | await self.send(json.dumps(msg)) | ||
109 | |||
110 | async def recv_message(self): | ||
111 | m = await self.recv() | ||
112 | return json.loads(m) | ||
113 | |||
114 | async def send(self, msg): | ||
115 | import websockets.exceptions | ||
116 | |||
117 | try: | ||
118 | await self.socket.send(msg) | ||
119 | except websockets.exceptions.ConnectionClosed: | ||
120 | raise ConnectionClosedError("Connection closed") | ||
121 | |||
122 | async def recv(self): | ||
123 | import websockets.exceptions | ||
124 | |||
125 | try: | ||
126 | if self.timeout < 0: | ||
127 | return await self.socket.recv() | ||
128 | |||
129 | try: | ||
130 | return await asyncio.wait_for(self.socket.recv(), self.timeout) | ||
131 | except asyncio.TimeoutError: | ||
132 | raise ConnectionError("Timed out waiting for data") | ||
133 | except websockets.exceptions.ConnectionClosed: | ||
134 | raise ConnectionClosedError("Connection closed") | ||
135 | |||
136 | async def close(self): | ||
137 | if self.socket is not None: | ||
138 | await self.socket.close() | ||
139 | self.socket = None | ||