application.c
#include <time.h>
#include "secure_logger_common.h"
#include <rtl/string.h>
#include <rtl/stdio.h>
/* Logger interface description. */
#include <secure_logger/Write.idl.h>
#define SEND_MESSAGE_COUNT 1
#define WRITE_DELAY_SEC 2
#define WRITE_DELAY_NANOSEC 0
const struct timespec WriteDelay = {
.tv_sec = WRITE_DELAY_SEC,
.tv_nsec = WRITE_DELAY_NANOSEC
};
static const char EntityName[] = "Application";
typedef struct {
struct Write_proxy *proxy;
Write_WriteInLog_req *req;
Write_WriteInLog_res *res;
struct nk_arena *reqArena;
struct nk_arena *resArena;
} TransportDescriptor;
#define DESCR_INIT(proxyIn, reqIn, resIn, reqArenaIn, resArenaIn) \
{ \
.proxy = proxyIn, \
.req = reqIn, \
.res = resIn, \
.reqArena = reqArenaIn, \
.resArena = resArenaIn, \
}
static void WriteInLog(TransportDescriptor *desc, unsigned messageCounter)
{
int logMessageLength = 0;
char logMessage[Write_WriteInLog_req_message_elem_size];
nk_arena_reset(desc->reqArena);
nk_ptr_t *message = nk_arena_alloc(
nk_ptr_t, desc->reqArena, &desc->req->message, SEND_MESSAGE_COUNT);
if (message == RTL_NULL)
{
fprintf(
stderr,
"[%s]: Error: can`t allocate memory in arena!\n",
EntityName);
return;
}
logMessageLength = rtl_snprintf(
logMessage,
Write_WriteInLog_req_message_elem_size,
"Log message %u",
messageCounter);
if (logMessageLength < 0)
{
fprintf(
stderr,
"[%s]: Error: length of message is negative number!\n",
EntityName);
return;
}
nk_char_t *str = nk_arena_alloc(
nk_char_t, desc->reqArena, &message[0], (logMessageLength + 1));
if (str == RTL_NULL)
{
fprintf(
stderr,
"[%s]: Error: can`t allocate memory in arena!\n",
EntityName);
return;
}
rtl_strncpy(str, logMessage, (logMessageLength + 1));
if (Write_WriteInLog(
&desc->proxy->base,
desc->req,
desc->reqArena,
desc->res,
desc->resArena) == NK_EOK)
{
messageCounter++;
}
else
{
fprintf(
stderr,
"[%s]: Error: can`t send message to Logger entity!\n",
EntityName);
}
fprintf(stderr, "[%s]: write in log : %s\n", EntityName, logMessage);
}
int main(int argc, char **argv)
{
NkKosTransport transport;
struct Write_proxy proxy;
unsigned messageCounter = 0;
setvbuf(stderr, NULL, _IOLBF, 0);
/**
* Gets the client IPC handle for the connection named
* "logger_connection".
*/
Handle handle = ServiceLocatorConnect("logger_connection");
if (handle == INVALID_HANDLE)
{
fprintf(
stderr,
"[%s]: Error: can`t establish static IPC connection!\n",
EntityName);
return EXIT_FAILURE;
}
/* Initializes IPC transport for the connection with the Logger entity. */
NkKosTransport_Init(&transport, handle, NK_NULL, 0);
/* Gets the runtime implementation ID (RIID) of the interface secure_logger.Logger.write. */
nk_iid_t riid = ServiceLocatorGetRiid(
handle, "secure_logger.Logger.write");
if (riid == INVALID_RIID)
{
fprintf(
stderr,
"[%s]: Error: can`t get runtime implementation ID (RIID) of "
"interface secure_logger.Logger.write!\n",
EntityName);
return EXIT_FAILURE;
}
/**
* Initializes the proxy object. Every method of the proxy object will be
* implemented as a request to the server.
*/
Write_proxy_init(&proxy, &transport.base, riid);
/* Structures of the request and response. */
Write_WriteInLog_req req;
Write_WriteInLog_res res;
char reqBuffer[Write_WriteInLog_req_arena_size];
struct nk_arena reqArena = NK_ARENA_INITIALIZER(
reqBuffer, reqBuffer + sizeof(reqBuffer));
/**
* An application entity must not be able to read from the log. The code below
* will not get the handle of "reader_connection".
*/
if (ServiceLocatorConnect("reader_connection") == INVALID_HANDLE)
{
fprintf(
stderr,
"[%s]: Expected behavior: can not open connection with Reader\n",
EntityName);
}
else
{
fprintf(
stderr,
"[%s]: Error: something goes wrong! Somebody could get access"
" to read from log file!\n",
EntityName);
}
TransportDescriptor desc = DESCR_INIT(
&proxy, &req, &res, &reqArena, RTL_NULL);
do
{
nanosleep(&WriteDelay, NULL);
WriteInLog(&desc, messageCounter);
messageCounter++;
}
while (messageCounter != MAX_MESSAGE_COUNT);
fprintf(
stderr,
"[%s]: Stop write in log. Number of messages gets maximum count %d.\n",
EntityName,
MAX_MESSAGE_COUNT);
return EXIT_SUCCESS;
}
Page top