Intercepting Invocations
CORBA brokers provide support for interception of GIOP requests and replies both in the server and client sides. Although the interception model is heavily influenced by the CORBA Portable Interceptors specification, OiL does not stick to it. Like other aspects of the OiL's API, it priories a simpler model more adequate to Lua than the defined by the CORBA standard.
Interceptors
Invocation interception is done by interceptor objects, which can be register in the broker as a client interceptor or a server interceptor. Client interceptors intercept invocations issued to remote servants, while server interceptors intercept invocations received from remote applications.
A client interceptor is an object that provides either methods interceptor:sendrequest
or interceptor:receivereply
.
The first method is called before an invocation request is sent to a remote servant, and the second is called after a reply is received from a remote servant.
On the other hand, a server interceptor is an object that provides either methods interceptor:receiverequest
or interceptor:sendreply
, which are called before an invocation request is processed by the server, and after a reply is sent to the corresponding invoker, respectively.
Each one of these methods receives a single parameter, which is a table representing the invocation.
The structure and information contained in this table varies accordingly to the method being called.
However, for a single invocation issued or received, the table received by method interceptor:sendrequest
and interceptor:receivereply
are always the same.
The same is true for methods interceptor:receiverequest
and interceptor:sendreply
, which means that the parameter received by the former method is the same parameter passed to the corresponding invocation of the later.
This also means that any field stored in this table during the execution of the first method will be available during the execution of the other for a single invocation.
For an example, consider the code below that measures the time each invocation takes to conclude.
local socket = require "socket" TimeProfiler = {} function TimeProfiler:receiverequest(request) request.start = socket.gettime() end function TimeProfiler:sendreply(reply) local time = socket.gettime() - reply.start print(string.format("opreation '%s' lasted %d seconds", reply.operation, time)) end
Adding Information to Invocations
GIOP messages for requests and replies has the special field service_context
, which can be used to store additional information to be send with the invocation.
Basically, the service_context
is a table mapping a tag number to a octet sequence that is used to store arbitrary data.
Interceptors can define the value of field service_context
in the parameter of methods sendrequest
or sendreply
.
Similarly, the value of field service_context
can be accessed through the parameter of methods receiverequest
or receivereply
.
To promote interoperability with other CORBA ORBs, the information contained in field service_context
is usually encoded using CORBA's CDR.
To help this, OiL brokers provide methods newencoder
()
and newdecoder
(stream)
.
The example below, illustrates how to use interceptors to add authentication information in an invocation.
require "oil" oil.main(function() local broker = oil.init() print("Type the your username") local Username = io.read() print("Type the password") local Password = io.read() local AuthID = broker:loadidl"struct AuthID { string user; string pswd; };" local encoder = broker:newencoder() encoder:put({user=UserName,pswd=Password}, AuthID) local AuthData = encoder:getdata() local Authenticator = {} function Authenticator:sendrequest(request) request.service_context[404] = AuthData end broker:setinterceptor(Authenticator, "client") ... end)Server
require "oil" oil.main(function() local broker = oil.init() local AuthID = broker:loadidl[[ struct AuthID { string user; string pswd; }; ]] local Validator = {} function Validator:receiverequest(request) local valid local data = request.service_context[404] if data ~= nil then local decoder = broker:newdecoder(data) local authid = decoder:get(AuthID) valid = (authid.user == "admin") and (authid.pswd == "admin") end if not valid then -- cancel request with an exception request.success = false request[1] = broker:newexcept{ "CORBA::NO_PERMISSION", minor_code_value = 0, } end end broker:setinterceptor(Validator, "server") ... end)
Multithreading Issues
OiL guarantees that for each single invocation both methods of the interceptor is invoked in the same thread.
Moreover, this thread is the same that initiated the invocation (in case of a client interceptor) or the thread that will execute the invocation (in case of a server interceptor).
However, while an invocation is being executed, others can be issued and the interceptor be invoked again, but it will execute in a different thread.
Therefore, you can use the current thread (coroutine.running ()
) to identify an invocation.
On the server side, the ORB may reuse a thread that executed a previous invocation that finished to execute other invocations.
Multiple Interceptors
Although CORBA brokers only allow one interceptor to be defined on the client side and one on the server side, if you need to define a chain of interceptors, you can register an interceptor that delegates interception to a chain of interceptors.
The following example illustrates the implementation of a interception chain that does not guarantee the order the interceptors are invoked.
The implementation use class loop.object.Publisher
of LOOP library.
require "oil" require "loop.object.Publisher" oil.main(function() local broker = oil.init() InterceptionChain = loop.object.Publisher{ Interceptor_01, Interceptor_02, Interceptor_03, ... } broker:setinterceptor(InterceptionChain, "server") ... end)