首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

9. Orber Stubs/Skeletons

9.1 Orber Stubs和Skeletons描述

这个例子描述了Orber存根和骨架的API和行为。

服务器启动

Orber服务器可以通过多种方式启动。选择的启动功能决定了服务器如何被访问及其行为。

使用Module_Interface:oe_create()oe_create_link()*

  • 无法传递任何初始数据。
  • 不能用作主管子启动函数。
  • 只能通过开始函数返回的对象引用访问。如果服务器死亡并重新启动,则对象引用不再有效。

使用Module_Interface:oe_create(Env)oe_create_link(Env)*

  • 初始数据可以使用Env...
  • 不能用作主管子启动函数。
  • 只能通过开始函数返回的对象引用访问。如果服务器死亡并重新启动,则对象引用不再有效。

使用Module_Interface:oe_create(Env, Options)*

  • 初始数据可以使用Env...
  • 不能用作主管子启动函数。
  • 通过启动函数返回的对象引用可访问。如果使用该选项,{regname, RegName}则即使服务器已重新启动,对象引用仍然有效。
  • 如果使用选项{persistent,true}和{regname,{global,Name}},则仅当对象以reason normal或shutdown方式终止时,对象调用的结果才是异常“OBJECT_NOT_EXIST”。 如果对象正在重新启动,则结果将为{error,Reason}或引发系统异常。
  • 选择{pseudo, true}使启动创建非服务器对象成为可能。但是,也存在一些限制,这些限制将在Pseudo objects部分。

使用Module_Interface:oe_create_link(Env, Options)*

  • 初始数据可以使用Env...
  • 如果使用了{sup_child,true}选项,可以用作主管子启动功能。
  • 通过启动函数返回的对象引用可访问。 如果使用选项{regname,RegName},则即使服务器已重新启动,对象引用仍然有效。
  • 如果使用选项{persistent,true}和{regname,{global,Name}},则仅当对象以reason normal或shutdown方式终止时,对象调用的结果才是异常“OBJECT_NOT_EXIST”。 如果对象正在重新启动,则结果将为{error,Reason}或引发系统异常。
  • 要将服务器作为主管孩子启动,应该使用选项[{persistent,true},{regname,{global,Name}},{sup_child,true}]和类型为transient的选项。 通过此配置,您可以将重新启动委派给管理员,并仍然可以使用相同的对象引用,并能够查看服务器是否永久终止。 请注意,您必须使用supervisor / stdlib-1.7或更高版本,并且它返回{ok,Pid,Object}而不仅仅是Object。
  • 使用选项{pseudo, true}与使用相同的效果oe_create/2...

警告

为了避免使用旧对象引用洪泛Orber,请使用标志-orber objectkeys_gc_time Time来启动erlang,该时间将删除与服务器在Time秒内销毁有关的所有对象引用。 为了避免额外的开销,即在没有持久对象启动时执行垃圾收集,objectkeys_gc_time默认值是无穷大。 有关更多信息,请参阅orber和corba文档。

警告

Orber仍然允许使用oe_create(Env,{Type,RegName})和oe_create_link(Env,{Type,RegName}),但可能不会在将来的版本中使用。

伪对象

本节描述Orber伪对象。

Orber存根可用于启动pseudo object,这将创建一个非服务器实现。伪对象引入了一些限制:

  • 功能oe_create_link/2等于oe_create/2,即不能或将创建任何链接。
  • BIF:s self()process_flag(trap_exit,true)不正确的行为。
  • IC选项{{impl, "M::I"}, "other_impl"}不起作用。回调函数必须在一个名为的文件中实现M_I_impl.erl
  • 回调函数必须像使用该IC选项一样执行{this, "M::I"}
  • gen_server状态更改不起作用。 用户可以通过Env启动参数提供信息,从init / 2返回的状态将是在以下调用中传递的状态。
  • 服务器回复Timeout无效。
  • 编译选项from无效。
  • 选择{pseudo, true}重写所有其他开始选项。
  • 必须执行除自定义之外的函数init/2(通过oe_create * / 2 terminate/2调用)和(通过corba:dispose / 1调用)。

通过采用上述pseudo对象的规则,我们可以使用oe_create/2创建serverpseudo对象,通过排除或包含选项{pseudo, true},而无需更改回调模块。

若要创建伪对象,请执行以下操作:

代码语言:javascript
复制
fingolfin 127> erl 
Erlang (BEAM) emulator version 4.9
 
Eshell V4.9  (abort with ^G)
1> ic:gen(myDefinition, [{this, "MyModule::MyInterface"}]).
Erlang IDL compiler version 20
ok
2> make:all().
Recompile: oe_MyDefinition
Recompile: MyModule_MyInterface
Recompile: MyModule_MyInterface_impl
up_to_date
3> PseudoObj = MyModule_MyInterface:oe_create(Env, [{pseudo, true}]).
      

必须将回调函数实现为MyFunction(OE_THIS, State, Args),然后被MyModule_MyInterface:MyFunction(PseudoObj, Args)调用

回拨模块

本节提供了如何实现回调模块的示例.

参数和答复由IDL-代码决定,因此,这里不再进一步描述.

代码语言:javascript
复制
%%%-----------------------------------------------------------
%%% File    : Module_Interface_impl.erl
%%% Author  : 
%%% Purpose : 
%%% Created : 
%%%-----------------------------------------------------------
 
-module('Module_Interface_impl').
 
%%--------------- INCLUDES -----------------------------------
-include_lib("orber/include/corba.hrl").
-include_lib(".. ..").
 
%%--------------- EXPORTS-------------------------------------
%% Arity depends on IC configuration parameters and the IDL
%% specification.
-export([own_function/X]).
 
 
%%--------------- gen_server specific ------------------------
-export([init/1, terminate/2, code_change/3, handle_info/2]).
 
%%------------------------------------------------------------
%% function : server specific
%%------------------------------------------------------------
init(InitialData) ->
    %% 'trap_exit' optional (have no effect if pseudo object).
    process_flag(trap_exit,true),

    %%--- Possible replies ---
    %% Reply and await next request
    {ok, State}.

    %% Reply and if no more requests within Time the special 
    %% timeout message should be handled in the 
    %% Module_Interface_impl:handle_info/2 call-back function (use the 
    %% IC option {{handle_info, "Module::Interface"}, true}).
    {ok, State, Timeout} 

    %% Return ignore in order to inform the parent, especially if it is a 
    %% supervisor, that the server, as an example, did not start in 
    %% accordance with the configuration data. 
    ignore 
    %% If the initializing procedure fails, the reason 
    %% is supplied as StopReason.
    {stop, StopReason}

terminate(Reason, State) ->
    ok.

code_change(OldVsn, State, Extra) ->
    {ok, NewState}.

%% If use IC option {{handle_info, "Module::Interface"}, true}. 
%% (have no effect if pseudo object).
handle_info(Info, State) ->
    %%--- Possible replies ---
    %% Await the next invocation.
    {noreply, State}.
    %% Stop with Reason.
    {stop, Reason, State}.

%%--- two-way ------------------------------------------------
%% If use IC option {this, "Module:Interface"} 
%% (Required for pseudo objects)
own_function(This, State, .. Arguments ..) ->
%% IC options this and from
own_function(This, From, State, .. Arguments ..) ->
%% IC option from
own_function(From, State, .. Arguments ..) ->
    %% Send explicit reply to client.
    corba:reply(From, Reply),
    %%--- Possible replies ---
    {noreply, State}
    {noreply, State, Timeout}

 
%% If not use IC option {this, "Module:Interface"}
own_function(State, .. Arguments ..) ->
    %%--- Possible replies ---
    %% Reply and await next request
    {reply, Reply, State}

    %% Reply and if no more requests within Time the special 
    %% timeout message should be handled in the 
    %% Module_Interface_impl:handle_info/2 call-back function (use the
    %% IC option {{handle_info, "Module::Interface"}, true}).
    {reply, Reply, State, Timeout}

    %% Stop the server and send Reply to invoking object.
    {stop, StopReason, Reply, State}

    %% Stop the server and send no reply to invoking object.
    {stop, StopReason, State}

    %% Raise exception. Any changes to the internal State is lost.
    corba:raise(Exception).

%%--- one-way ------------------------------------------------
%% If use IC option {this, "Module:Interface"}
%% (Required for pseudo objects)
own_function(This, State, .. Arguments ..) ->

%% If not use IC option {this, "Module:Interface"}
own_function(State, .. Arguments ..) ->
    %%--- Possible results ---
    {noreply, State}

    %% Release and if no more requests within Time the special 
    %% timeout message should be handled in the 
    %% Module_Interface_impl:handle_info/2 call-back function (use the
    %%  IC option {{handle_info, "Module::Interface"}, true}).
    {noreply, State, Timeout}

    %% Stop the server with StopReason.
    {stop, StopReason, State}

%%--------------- END OF MODULE ------------------------------
      

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com