comparison libervia/backend/plugins/plugin_misc_remote_control.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 898db6daf0d0
children
comparison
equal deleted inserted replaced
4269:64a85ce8be70 4270:0d7bb4df2343
45 C.PI_DEPENDENCIES: ["XEP-0166", "XEP-0167"], 45 C.PI_DEPENDENCIES: ["XEP-0166", "XEP-0167"],
46 C.PI_MAIN: "RemoteControl", 46 C.PI_MAIN: "RemoteControl",
47 C.PI_HANDLER: "yes", 47 C.PI_HANDLER: "yes",
48 C.PI_DESCRIPTION: _("""Remote control devices with Jingle."""), 48 C.PI_DESCRIPTION: _("""Remote control devices with Jingle."""),
49 } 49 }
50
51 50
52 51
53 class RemoteControl(BaseApplicationHandler): 52 class RemoteControl(BaseApplicationHandler):
54 53
55 def __init__(self, host): 54 def __init__(self, host):
82 extra_s: str, 81 extra_s: str,
83 profile: str, 82 profile: str,
84 ) -> defer.Deferred[str]: 83 ) -> defer.Deferred[str]:
85 client = self.host.get_client(profile) 84 client = self.host.get_client(profile)
86 extra = data_format.deserialise(extra_s) 85 extra = data_format.deserialise(extra_s)
87 d = defer.ensureDeferred(self.remote_control_start( 86 d = defer.ensureDeferred(
88 client, 87 self.remote_control_start(
89 jid.JID(peer_jid_s), 88 client,
90 extra, 89 jid.JID(peer_jid_s),
91 )) 90 extra,
91 )
92 )
92 d.addCallback(data_format.serialise) 93 d.addCallback(data_format.serialise)
93 return d 94 return d
94 95
95 async def remote_control_start( 96 async def remote_control_start(
96 self, 97 self, client: SatXMPPEntity, peer_jid: jid.JID, extra: dict
97 client: SatXMPPEntity,
98 peer_jid: jid.JID,
99 extra: dict
100 ) -> dict: 98 ) -> dict:
101 """Start a remote control session. 99 """Start a remote control session.
102 100
103 @param peer_jid: destinee jid 101 @param peer_jid: destinee jid
104 @return: progress id 102 @return: progress id
105 """ 103 """
106 if not extra: 104 if not extra:
107 raise exceptions.DataError( 105 raise exceptions.DataError('"extra" must be set.')
108 '"extra" must be set.'
109 )
110 # webrtc is always used for remote control 106 # webrtc is always used for remote control
111 extra["webrtc"] = True 107 extra["webrtc"] = True
112 content = { 108 content = {
113 "app_ns": NS_REMOTE_CONTROL, 109 "app_ns": NS_REMOTE_CONTROL,
114 # XXX: for now only unidirectional device exist, but future extensions mays be 110 # XXX: for now only unidirectional device exist, but future extensions mays be
125 121
126 metadata = self._rtp.parse_call_data(call_data) 122 metadata = self._rtp.parse_call_data(call_data)
127 try: 123 try:
128 application_data = call_data["application"] 124 application_data = call_data["application"]
129 except KeyError: 125 except KeyError:
130 raise exceptions.DataError( 126 raise exceptions.DataError('"call_data" must have an application media.')
131 '"call_data" must have an application media.'
132 )
133 try: 127 try:
134 content["transport_data"] = { 128 content["transport_data"] = {
135 "sctp-port": metadata["sctp-port"], 129 "sctp-port": metadata["sctp-port"],
136 "max-message-size": metadata.get("max-message-size", 65536), 130 "max-message-size": metadata.get("max-message-size", 65536),
137 "local_ice_data": { 131 "local_ice_data": {
138 "ufrag": metadata["ice-ufrag"], 132 "ufrag": metadata["ice-ufrag"],
139 "pwd": metadata["ice-pwd"], 133 "pwd": metadata["ice-pwd"],
140 "candidates": application_data.pop("ice-candidates"), 134 "candidates": application_data.pop("ice-candidates"),
141 "fingerprint": application_data.pop("fingerprint", {}), 135 "fingerprint": application_data.pop("fingerprint", {}),
142 } 136 },
143 } 137 }
144 name = application_data.get("id") 138 name = application_data.get("id")
145 if name: 139 if name:
146 content["name"] = name 140 content["name"] = name
147 except KeyError as e: 141 except KeyError as e:
206 @raises exceptions.CancelError: If the user doesn't accept the incoming call. 200 @raises exceptions.CancelError: If the user doesn't accept the incoming call.
207 """ 201 """
208 session_id = session["id"] 202 session_id = session["id"]
209 peer_jid = session["peer_jid"] 203 peer_jid = session["peer_jid"]
210 204
211 is_in_roster, confirm_msg, confirm_title = self._get_confirm_msg( 205 is_in_roster, confirm_msg, confirm_title = self._get_confirm_msg(client, peer_jid)
212 client, peer_jid
213 )
214 if is_in_roster: 206 if is_in_roster:
215 action_type = C.META_TYPE_CONFIRM 207 action_type = C.META_TYPE_CONFIRM
216 else: 208 else:
217 action_type = C.META_TYPE_NOT_IN_ROSTER_LEAK 209 action_type = C.META_TYPE_NOT_IN_ROSTER_LEAK
218 210
225 accepted = await xml_tools.defer_confirm( 217 accepted = await xml_tools.defer_confirm(
226 self.host, 218 self.host,
227 confirm_msg, 219 confirm_msg,
228 confirm_title, 220 confirm_title,
229 profile=client.profile, 221 profile=client.profile,
230 action_extra=action_extra 222 action_extra=action_extra,
231 ) 223 )
232 if accepted: 224 if accepted:
233 session["pre_accepted"] = True 225 session["pre_accepted"] = True
234 return accepted 226 return accepted
235 227
246 self, client: SatXMPPEntity, session: dict, cancel_error: exceptions.CancelError 238 self, client: SatXMPPEntity, session: dict, cancel_error: exceptions.CancelError
247 ) -> None: 239 ) -> None:
248 """The remote control has been rejected""" 240 """The remote control has been rejected"""
249 241
250 def jingle_session_init( 242 def jingle_session_init(
251 self, 243 self, client: SatXMPPEntity, session: dict, content_name: str, extra: dict
252 client: SatXMPPEntity,
253 session: dict,
254 content_name: str,
255 extra: dict
256 ) -> domish.Element: 244 ) -> domish.Element:
257 """Initializes a jingle session. 245 """Initializes a jingle session.
258 246
259 @param client: The client instance. 247 @param client: The client instance.
260 @param session: Jingle session. 248 @param session: Jingle session.
303 # We are the controlled entity. 291 # We are the controlled entity.
304 return await self._remote_control_request_conf( 292 return await self._remote_control_request_conf(
305 client, session, content_data, content_name 293 client, session, content_data, content_name
306 ) 294 )
307 else: 295 else:
308 raise exceptions.InternalError( 296 raise exceptions.InternalError(f"Invalid role {role!r}")
309 f"Invalid role {role!r}"
310 )
311 297
312 async def _remote_control_request_conf( 298 async def _remote_control_request_conf(
313 self, 299 self,
314 client: SatXMPPEntity, 300 client: SatXMPPEntity,
315 session: dict, 301 session: dict,
338 return await xml_tools.defer_confirm( 324 return await xml_tools.defer_confirm(
339 self.host, 325 self.host,
340 confirm_msg, 326 confirm_msg,
341 confirm_title, 327 confirm_title,
342 profile=client.profile, 328 profile=client.profile,
343 action_extra=action_extra 329 action_extra=action_extra,
344 ) 330 )
345 331
346 async def jingle_handler(self, client, action, session, content_name, desc_elt): 332 async def jingle_handler(self, client, action, session, content_name, desc_elt):
347 content_data = session["contents"][content_name] 333 content_data = session["contents"][content_name]
348 application_data = content_data["application_data"] 334 application_data = content_data["application_data"]