Services Communication Sample
The Services Communication sample demonstrates how to establish communication between an RO SDK server application and a Hydra SDK plugin. You will learn how to share a session managed between server and plugin, but the same techniques can be used for similar tasks.
Please note that you will need to have the RO SDK for Delphi and the Hydra RO SDK integration package installed in order to build and run this application.
Examine the Server
The server application is a regular RemObjects SDK VCL server application that exposes two services - a simple test service and a login service. If you need more information on how to create an RO SDK server, please refer to the RemObjects SDK docs.
Let's examine the most important part of the server:
initialization
TServerForm = class(TForm, ISessionProvider)
[...]
ModuleManager: THYModuleManager;
RODLReader: THYRODLReader;
[...]
end;
procedure TServerForm.FormCreate(Sender: TObject);
begin
ModuleManager.LoadModules('*.dll');
ROServer.Active := true;
end;
procedure TServerForm.ROServerGetRODLReader(Sender: TROServer;
var aRODLReader: TROCustomRODLReader);
begin
aRODLReader := RODLReader;
end;
As you can see, the code is similar to what we have in a regular server application. The important parts are:
- FormCreate method - Using module manager, we can load all existing dll's in the server directory and activate our server.
- OnGetRODLReader event handler - Assigns Hydra's THYRODLReader component as a custom RODL reader. This component allows our server to merge RODLs from plugins into the main RODL.
ServerInterfaces.pas
The ServerInterfaces.pas file holds a definition of our custom interface and is shared between the server and the plugin.
ISessionProvider = interface
['{3B50E543-506B-461E-8C47-F49294B969F1}']
function GetSession: TROCustomSessionManager;
property Session: TROCustomSessionManager read GetSession;
end;
This interface is implemented by our server and will allow the plugin to get access to the server session manager.
Services
Our server contains two services: One is a regular test service that is added by the SDK wizard. This service is set up to check for a valid session to be able to perform client calls.
To do this, we've set the RequiresSession property to true and assigned a session manager that is located in the server's data module.
The second service is a login service that allows the client application to authorize itself and register sessions.
function TLoginService.Login(const UserName: UnicodeString; const Password: UnicodeString): Boolean;
begin
Result := UserName = Password;
if Result then
CreateSession;
end;
As you can see, it simply compares the user name and password, and if they match, it creates a new session.
Examine the Plugin
Our plugin is a Hydra module that holds an RO SDK service. If you need more information on how to create an RO SDK server, please refer to this article.
Like the sample service in the server, it requires a user session to be able to process client calls. In order to do this, we can use our custom interface to communicate with the server application.
Unlike regular Hydra plugins, the RO SDK plugins do not have a Host property, so in order to access the host, we need to use our module controller. The module controller is a singleton object that is created when the plugin is loaded by the host so you can use to get access to the host instance.
uses
hcPluginController, ServerInterfaces;
procedure TPluginService.RORemoteDataModuleCreate(Sender: TObject);
var
Provider: ISessionProvider;
begin
if Supports(PluginController.Host, ISessionProvider, Provider) then
SessionManager := Provider.Session;
end;
The Supports method checks if the Host implements the interface, and if it does, it will assign a reference to the Provider variable that we declared above. Now we can set the SessionManager property and our service is ready to process client calls.
Putting Everything Together
Now that we examined all major parts of the sample, we can build both the plugin and the host. After we launch the sample, we should get the following results:
If we authorized first, we will get a proper response from all our services:
If we try to access services without a proper session, it will shows us an exception: