The Client
entity calls the Ping
and Pong
methods in a varying sequence.
For more details on initialization of transport to the server, use of a proxy object and interface methods, and the purpose of the Ping.idl.h
file, see the comments to the client.c file in the echo example.
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 <Ping.idl.h>
#include <assert.h>
#define EXAMPLE_VALUE_TO_SEND 777
static struct Ping_proxy proxy;
static uint32_t ping(uint32_t value)
{
/* Request and response structures */
struct Ping_Ping_req req;
struct Ping_Ping_res res;
req.value = value;
/* Call Ping_Ping interface method.
* Server will be sent a request for calling Ping interface method
* pingComp.pingImpl with the value argument. Calling thread is locked
* until a response is received from the server. */
if (Ping_Ping(&proxy.base, &req, NULL, &res, NULL) == rcOk)
{
fprintf(stderr, "Client: Ping(%d), result = %d\n", (int) value, (int) res.result);
value = res.result;
}
else
fprintf(stderr, "Client: Ping(%d), failed\n", (int) value);
return value;
}
static uint32_t pong(uint32_t value)
{
/* Request and response structures */
struct Ping_Pong_req req;
struct Ping_Pong_res res;
req.value = value;
/* Call Ping_Pong interface method.
* Server will be sent a request for calling Pong interface method
* ping_comp.ping_impl with the value argument. Calling thread is locked
* until a response is received from the server. */
if (Ping_Pong(&proxy.base, &req, NULL, &res, NULL) == rcOk)
{
fprintf(stderr, "Client: Pong(%d), result = %d\n", (int) value, (int) res.result);
value = res.result;
}
else
fprintf(stderr, "Client: Pong(%d), failed\n", (int) value);
return value;
}
/* Client entity entry point. */
int main(int argc, const char *argv[])
{
NkKosTransport transport;
uint32_t value;
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 pingComp.pingImpl interface ID. */
nk_iid_t riid = ServiceLocatorGetRiid(handle, "pingComp.pingImpl");
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. */
Ping_proxy_init(&proxy, &transport.base, riid);
/* Test loop. */
value = EXAMPLE_VALUE_TO_SEND;
for (i = 0; i < 5; ++i)
{
value = ping(value);
value = pong(value);
}
value = ping(value);
value = ping(value);
value = pong(value);
value = pong(value);
return EXIT_SUCCESS;
}
Page top