Client entity implementation

The code of the client entity uses the transport types and methods that will be generated during the solution build by the NK compiler based on the IDL description of the IPing interface.

However, to obtain the descriptions of types and signatures of methods required for implementing the entity, you can use the NK compiler immediately after creating an EDL description of the entity, CDL descriptions of components and IDL descriptions of the interfaces used for interaction. As a result, the required types and signatures of methods will be declared in the generated *.h files.

In the client entity implementation, the following is required:

  1. Get the client IPC handle of the connection (channel) by using the ServiceLocatorConnect() function.

    Input the name of the IPC server_connection predefined in the init.yaml file.

  2. Initialize NkKosTransport by passing the obtained IPC handle to the NkKosTransport_Init() function.
  3. Obtain the ID of the required interface (RIID) by using the ServiceLocatorGetRiid() function.

    Input the full name of the IPing interface implementation. It consists of the names of the component instance (ping_comp) and interface (ping_impl) that were previously defined in server.edl and ping.cdl.

  4. Initialize the proxy object by passing the transport and interface ID to the IPing_proxy_init() function.
  5. Prepare the request and response structures.
  6. Call the IPing_Ping() interface method by passing the proxy object and the pointers to the request and response.

client.c

#include <stdio.h>

#include <stdlib.h>

#include <stdint.h>

#include <assert.h>

/* Header file for implementation of transport level for KasperskyOS. */

#include <coresrv/nk/transport-kos.h>

/* Header file of Service Locator. */

#include <coresrv/sl/sl_api.h>

/* The ping.idl.h file will be generated from ping.idl when building the solution.

* The ping.idl.h file contains the generated methods and types

* required for calling the IPing interface: IPing_proxy, IPing_proxy_init,

* IPing_Ping_req, IPing_Ping_res, IPing_Ping. */

#include "ping.idl.h"

int main(int argc, const char *argv[])

{

NkKosTransport transport;

struct IPing_proxy proxy;

int i;

fprintf(stderr, "Hello I'm client\n");

/* Get client IPC handle of

* "server_connection". */

Handle handle = ServiceLocatorConnect("server_connection");

assert(handle != INVALID_HANDLE);

/* Initialize transport to server. */

NkKosTransport_Init(&transport, handle, NK_NULL, 0);

/* Get ping_comp.ping_impl interface ID. Here

* ping_comp is the name of the ping component instance,

* ping_impl is the name of the IPing interface implementation. */

nk_iid_t riid = ServiceLocatorGetRiid(handle, "ping_comp.ping_impl");

assert(riid != INVALID_RIID);

/* Initialize proxy object by specifying transport to server entity

* (&transport) and ID of the server interface (riid). Each method

* of the proxy object will be implemented by sending a request to the server. */

IPing_proxy_init(&proxy, &transport.base, riid);

/* Request and response structures */

struct IPing_Ping_req req;

struct IPing_Ping_res res;

/* Initialize the request: assign the value 777 to the value argument

* (value is the input argument of the Ping method). */

req.value = 777;

for (i = 0; i < 10; ++i)

{

/* Call the IPing_Ping interface method by passing the proxy object,

* and the pointers to the request and response. The server will receive

* a request for calling a Ping interface method

* ping_comp.ping_impl with the value argument. As the Ping method

* has no sequence type arguments, the arena is not being used. Instead,

* NULL is sent.

* The thread is locked until a response is received from the server. */

if (IPing_Ping(&proxy.base, &req, NULL, &res, NULL) == rcOk)

{

/* Print "result" value from response

* (result is the output argument of the Ping method). */

fprintf(stderr, "result = %d\n", (int) res.result);

/* Include received "result" value into "value" argument. */

req.value = res.result;

}

else

fprintf(stderr, "Call failed\n");

}

return EXIT_SUCCESS;

}

Page top