forked from EmbeddedRPC/erpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_server.py
More file actions
56 lines (40 loc) · 1.54 KB
/
simple_server.py
File metadata and controls
56 lines (40 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
# Copyright 2016-2025 NXP
#
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import print_function
import threading
from typing import Optional, Type
from erpc.codec import Codec
from erpc.transport import Transport, TransportError
from .client import RequestError
from .server import Server
class SimpleServer(Server):
def __init__(self, transport: Optional[Transport], codecClass: Optional[Type[Codec]]):
super(SimpleServer, self).__init__(transport, codecClass)
self._run = True
def run(self) -> None:
self._run = True
while self._run:
try:
self._receive_request()
except (RequestError, TransportError) as e:
print(f"Error while processing request: {str(e)}")
def stop(self) -> None:
self._run = False
def _receive_request(self):
assert self.transport is not None, "No Transport is set"
assert self.codec_class is not None, "No Codec class is set"
message = self.transport.receive()
codec = self.codec_class()
codec.buffer = message
self._process_request(codec)
if len(codec.buffer):
self.transport.send(codec.buffer)
class ServerThread(SimpleServer):
def __init__(self, transport: Transport, codecClass: Type[Codec]):
super(ServerThread, self).__init__(transport, codecClass)
self._thread = threading.Thread(target=self.run, name="erpc_server")
self._thread.daemon = True
def start(self):
self._thread.start()