app_manager: add a function to request to load the server application

this is similar to handler.register_service but for client-server
style applications.  register_service is not appropriate for such
applications becuase normally the client does not consume
(in the sense of set_ev_cls) asynchronous events from the server.

note: this automatically load the server only if "api" module is
directly loaded from the module defining the client application.

Signed-off-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
YAMAMOTO Takashi 2014-02-21 17:14:00 +09:00 committed by FUJITA Tomonori
parent 07c3aa1bf8
commit cd615a2f04
2 changed files with 16 additions and 1 deletions

View File

@ -1,4 +1,4 @@
# Copyright (C) 2011, 2012 Nippon Telegraph and Telephone Corporation.
# Copyright (C) 2011-2014 Nippon Telegraph and Telephone Corporation.
# Copyright (C) 2011 Isaku Yamahata <yamahata at valinux co jp>
#
# Licensed under the Apache License, Version 2.0 (the "License");
@ -48,6 +48,19 @@ def unregister_app(app):
SERVICE_BRICKS.pop(app.name)
def require_app(app_name):
"""
Request the application to be loaded.
This is used for "api" style modules, which is imported by a client
application, to automatically load the corresponding server application.
"""
frm = inspect.stack()[2] # skip a frame for "api" module
m = inspect.getmodule(frm[0]) # client module
m._REQUIRED_APP = getattr(m, '_REQUIRED_APP', [])
m._REQUIRED_APP.append(app_name)
class RyuApp(object):
"""
The base class for Ryu applications.

View File

@ -90,6 +90,8 @@ def get_dependent_services(cls):
if cls.__module__ != service:
services.append(service)
m = sys.modules[cls.__module__]
services.extend(getattr(m, '_REQUIRED_APP', []))
services = list(set(services))
return services