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 Ping
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:
ServiceLocatorConnect()
function.Input the name of the IPC server_connection
predefined in the init.yaml file.
NkKosTransport
by passing the obtained IPC handle to the NkKosTransport_Init()
function.RIID
) by using the ServiceLocatorGetRiid()
function.Ping
interface implementation. It consists of the names of the component instance (Echo.ping
) and interface (ping
) that were previously defined in Server.edl and Ping.cdl.Ping_proxy_init()
function.Ping_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>
/* Files required for transport initialization. */
#include <coresrv/nk/transport-kos.h>
#include <coresrv/sl/sl_api.h>
/* Description of the server interface used by the client entity. */
#include <echo/Ping.idl.h>
#include <assert.h>
#define EXAMPLE_VALUE_TO_SEND 777
/* Client entity entry point. */
int main(int argc, const char *argv[])
{
NkKosTransport transport;
struct echo_Ping_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 IPC transport for interaction with the server entity. */
NkKosTransport_Init(&transport, handle, NK_NULL, 0);
/* Get Runtime Interface ID (RIID) for interface echo.Ping.ping.
* Here ping is the name of the echo.Ping component instance,
* echo.Ping.ping is the name of the Ping interface implementation. */
nk_iid_t riid = ServiceLocatorGetRiid(handle, "echo.Ping.ping");
assert(riid != INVALID_RIID);
/* Initialize proxy object by specifying transport (&transport)
* and ID of the server interface (riid). Each method
* of the proxy object will be implemented by sending a request to the server. */
echo_Ping_proxy_init(&proxy, &transport.base, riid);
/* Request and response structures */
echo_Ping_Ping_req req;
echo_Ping_Ping_res res;
/* Test loop. */
req.value = EXAMPLE_VALUE_TO_SEND;
for (i = 0; i < 10; ++i)
{
/* Call Ping interface method.
* Server will be sent a request for calling Ping interface method
* ping_comp.ping_impl with the value argument. Calling thread is locked
* until a response is received from the server. */
if (echo_Ping_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
* to resend to server in next iteration. */
req.value = res.result;
}
else
fprintf(stderr, "Failed to call echo.Ping.Ping()\n");
}
return EXIT_SUCCESS;
}
Page top