src/eric7/RemoteServer/EricServer.py

branch
server
changeset 10539
4274f189ff78
parent 10531
3308e8349e4c
child 10546
300487f5f517
equal deleted inserted replaced
10531:3308e8349e4c 10539:4274f189ff78
33 @param port port to listen on (defaults to 42024) 33 @param port port to listen on (defaults to 42024)
34 @type int (optional) 34 @type int (optional)
35 @param useIPv6 flag indicating to use IPv6 protocol (defaults to False) 35 @param useIPv6 flag indicating to use IPv6 protocol (defaults to False)
36 @type bool (optional) 36 @type bool (optional)
37 """ 37 """
38 self.__requestCategoryHandlerRegistry = { 38 self.__requestCategoryHandlerRegistry = {}
39 # Dictionary containing the defined and registered request category 39 # Dictionary containing the defined and registered request category
40 # handlers. The key is the request category and the value is the respective 40 # handlers. The key is the request category and the value is the respective
41 # handler method. This method must have the signature: 41 # handler method. This method must have the signature:
42 # handler(request:str, params:dict, reqestUuid:str) -> None 42 # handler(request:str, params:dict, reqestUuid:str) -> None
43 EricRequestCategory.Debugger: None, # TODO: not implemented yet 43 self.__registerInternalHandlers()
44 EricRequestCategory.Echo: self.__handleEchoRequest,
45 EricRequestCategory.FileSystem: None, # TODO: not implemented yet
46 EricRequestCategory.Project: None, # TODO: not implemented yet
47 EricRequestCategory.Server: self.__handleServerRequest
48 }
49 44
50 self.__connection = None 45 self.__connection = None
51 46
52 address = ("", port) 47 address = ("", port)
53 if socket.has_dualstack_ipv6() and useIPv6: 48 if socket.has_dualstack_ipv6() and useIPv6:
255 break 250 break
256 251
257 return cleanExit 252 return cleanExit
258 253
259 ####################################################################### 254 #######################################################################
255 ## Methods for registering and unregistering handlers.
256 #######################################################################
257
258 def registerRequestHandler(self, requestCategory, handler):
259 """
260 Public method to register a request handler method for the given request
261 category.
262
263 @param requestCategory request category to be registered
264 @type EricRequestCategory or int (>= EricRequestCategory.UserCategory)
265 @param handler reference to the handler method. This handler must accept
266 the parameters 'request', 'params', and 'requestUuid'
267 @type function(request:str, params:dict, requestUuid:str)
268 @exception ValueError raised to signal a request category collision
269 """
270 if requestCategory in self.__requestCategoryHandlerRegistry:
271 raise ValueError(f"Request category '{requestCategory} already registered.")
272
273 self.__requestCategoryHandlerRegistry[requestCategory] = handler
274
275 def unregisterRequestHandler(self, requestCategory, ignoreError=False):
276 """
277 Public method to unregister a handler for the given request category.
278
279 Note: This method will raise a KeyError exception in case the request
280 category has not been registered and ignoreError is False (the default).
281
282 @param requestCategory request category to be unregistered
283 @type EricRequestCategory or int (>= EricRequestCategory.UserCategory)
284 @param ignoreError flag indicating to ignore errors (defaults to False)
285 @type bool (optional)
286 """
287 try:
288 del self.__requestCategoryHandlerRegistry[requestCategory]
289 except KeyError:
290 if not ignoreError:
291 raise
292
293 def __registerInternalHandlers(self):
294 """
295 Private method to register request handler categories of this class.
296 """
297 self.registerRequestHandler(EricRequestCategory.Echo, self.__handleEchoRequest)
298 self.registerRequestHandler(
299 EricRequestCategory.Server, self.__handleServerRequest
300 )
301 self.registerRequestHandler(EricRequestCategory.Error, None)
302 # Register a None handler to indicate we are not expecting a request of the
303 # 'Error' category.
304
305 #######################################################################
260 ## Request handler methods. 306 ## Request handler methods.
261 ####################################################################### 307 #######################################################################
262 308
263 def __handleRequest(self, category, request, params, reqestUuid): 309 def __handleRequest(self, category, request, params, reqestUuid):
264 """ 310 """
269 @param request request name 315 @param request request name
270 @type str 316 @type str
271 @param params request parameters 317 @param params request parameters
272 @type dict 318 @type dict
273 @param reqestUuid UUID of the associated request as sent by the eric IDE 319 @param reqestUuid UUID of the associated request as sent by the eric IDE
274 (defaults to "", i.e. no UUID received) 320 @type str
275 @type str 321 @exception ValueError raised to indicate an invalid or unsupported request
322 handler catehory
276 """ 323 """
277 try: 324 try:
278 handler = self.__requestCategoryHandlerRegistry[category] 325 handler = self.__requestCategoryHandlerRegistry[category]
279 if handler is None: 326 except KeyError:
280 raise ValueError("invalid handler function") 327 if (
328 category < EricRequestCategory.UserCategory
329 and category in EricRequestCategory
330 ):
331 # it is an internally supported category
332 if category == EricRequestCategory.FileSystem:
333 from .EricServerFileSystemRequestHandler import (
334 EricServerFileSystemRequestHandler,
335 )
336 handler = EricServerFileSystemRequestHandler(self).handleRequest
337
338 elif category == EricRequestCategory.Project:
339 # TODO: 'Project' handler not implemented yet
340 handler = None
341
342 elif category == EricRequestCategory.Debugger:
343 # TODO: 'Debugger' handler not implemented yet
344 handler = None
345
346 self.registerRequestHandler(category, handler)
347 else:
348 handler = None
349
350 if handler is not None:
281 handler(request=request, params=params, reqestUuid=reqestUuid) 351 handler(request=request, params=params, reqestUuid=reqestUuid)
282 except (KeyError, ValueError): 352 else:
283 self.sendJson( 353 self.sendJson(
284 category=EricRequestCategory.Error, 354 category=EricRequestCategory.Error,
285 reply="UnsupportedServiceCategory", 355 reply="UnsupportedServiceCategory",
286 params={"Category": category}, 356 params={"Category": category},
287 ) 357 )

eric ide

mercurial