@@ -240,3 +240,72 @@ async def listen(self) -> None:
240240 await self ._handle_msg (msg )
241241 except KeyboardInterrupt :
242242 self ._logger .info ('exiting...' )
243+
244+
245+ class AsyncIODBusServer (_JeepneyServerBase ):
246+ def __init__ (self , bus : str , name : str ) -> None :
247+ '''
248+ Async DBus server built on top of Jeepney+asyncio
249+
250+ :param bus: DBus bus (hint: usually SESSION or SYSTEM)
251+ :param name: DBus name
252+ '''
253+ super ().__init__ (bus , name )
254+ self ._logger = logging .getLogger (self .__class__ .__name__ )
255+ # TODO: support signals
256+ # self.emit_signal_callback = self.emit_signal
257+
258+ # We can't have an async __init__ method, so we use this as an alternative.
259+ @classmethod
260+ async def new (cls , bus : str , name : str ) -> AsyncIODBusServer :
261+ inst = cls (bus , name )
262+ await inst ._conn_start ()
263+ return inst
264+
265+ async def _conn_start (self ) -> None :
266+ '''
267+ Start DBus connection
268+ '''
269+ import jeepney .io .asyncio
270+
271+ self ._conn = await jeepney .io .asyncio .open_dbus_connection (self ._bus )
272+ async with jeepney .io .asyncio .DBusRouter (self ._conn ) as router :
273+ bus_proxy = jeepney .io .asyncio .Proxy (jeepney .message_bus , router )
274+ await bus_proxy .RequestName (self ._name )
275+
276+ async def _handle_msg (self , msg : jeepney .Message ) -> None :
277+ '''
278+ Handle message
279+
280+ :param msg: message to handle
281+ '''
282+ return_msg = self ._jeepney_handle_msg (msg )
283+ if return_msg :
284+ await self ._conn .send (return_msg )
285+
286+ async def emit_signal (self , signal : dbus_objects ._DBusSignal , path : str , body : Any ) -> None :
287+ self ._logger .debug (f'emitting signal: { signal .name } { body } ' )
288+ await self ._conn .send_message (self ._get_signal_msg (signal , path , body ))
289+
290+ async def close (self ) -> None :
291+ '''
292+ Close the DBus connection
293+ '''
294+ await self ._conn .close ()
295+
296+ async def listen (self ) -> None :
297+ '''
298+ Start listening and handling messages
299+ '''
300+ self ._log_topology ()
301+ try :
302+ while True :
303+ try :
304+ msg = await self ._conn .receive ()
305+ except ConnectionResetError :
306+ self ._logger .debug ('connection reset abruptly, restarting...' )
307+ await self ._conn_start ()
308+ else :
309+ await self ._handle_msg (msg )
310+ except KeyboardInterrupt :
311+ self ._logger .info ('exiting...' )
0 commit comments