Code Examples.
More...
Code Examples.
The following example shows how to initialze, create a message queue, sending and receiving messages on it and finaly deleting it.
As a first step, the application has to register itself with the Message Service. For this the application has to call saMsgInitialize() API. This API will return a message handle for the client. And all the subsequent communication with Message Service will be needing this message handle.
SaAisErrorT retCode = SA_AIS_OK;
SaMsgHandleT msgHandle = 0;
SaMsgCallbacksT msgCallback = {
.saMsgQueueOpenCallback = NULL,
.saMsgQueueGroupTrackCallback = NULL,
.saMsgMessageDeliveredCallback = NULL,
.saMsgMessageReceivedCallback = NULL
};
SaVersionT msgVersion = {
.releaseCode = 'B',
.majorVersion = 0x1,
.minorVersion = 0x1
};
retCode = saMsgInitialize(&msgHandle, &msgCallback, &msgVersion);
if( retCode != SA_AIS_OK)
{
}
Now opening a Message Queue. The saMsgInitialize() must be called before calling any of the SAF Message Service APIs. This is needed by the Message Server for identifying the client. And for the same reason the Message Handle needs to be passed to saMsgQueueOpen() API.
SaNameT queueName = {
.length = strlen("queueName"),
.value = "queueName"
};
SaMsgQueueCreationAttributesT creationAttributes = {
.creationFlags = 0,
.size = {10, 10, 10, 10},
.retentionTime = 0
};
SaMsgQueueHandleT queueHandle;
retCode = saMsgQueueOpen(
msgHandle,
&queueName,
&creationAttributes,
SA_MSG_QUEUE_CREATE,
SA_TIME_MAX,
&queueHandle);
if(msgError != SA_AIS_OK)
{
}
Here we can see how a message can be sent to a destination. For this the application must initialize the Message Service client.
SaNameT senderName = {
.length = strlen("senderQ"),
.value = "senderQ"
};
SaNameT receiverName = {
.length = strlen("receiverQ"),
.value = "receiverQ"
};
char sendData[20] = "Hello World";
SaMsgMessageT senderMessage = {
.type = 0,
.version = 0,
.size = 20,
.senderName = &senderName,
.data = sendData,
.priority = SA_MSG_MESSAGE_HIGHEST_PRIORITY
};
retCode = saMsgMessageSend(
msgHandle,
&receiverName,
&senderMessage,
SA_TIME_ONE_SECOND
);
{
}
On the receiver side, the receiver must have opened the "receiverQ". Only then it can receive the message sent to it by a sender.
char receiveData[20] = {0};
SaMsgMessageT receiverMessage = {
.type = 0,
.version = 0,
.size = 20,
.senderName = &senderName,
.data = receiveData,
.priority = SA_MSG_MESSAGE_HIGHEST_PRIORITY
};
retCode = saMsgMessageGet(
recvQueueHandle,
&recevierMessage,
NULL,
&senderId,
SA_TIME_TEN_SECOND);
if(retCode != SA_AIS_OK)
{
}
Closing the Message Queue close will make the queue handle unusable. So no subsequent operations on the queue can be performed. The queue continues to exist for the "retentionTime" nano-seconds, if the queue is a non-persistent queue. And for the persistent ones it will exist till the queue is unlinked using the saMsgQueueUnlink() API.
retCode = saMsgQueueClose(queueHandle);
{
}
The saMsgFinalize() will close the application's Message handle and the Message Service will do all the cleanups releated to that client.
retCode = saMsgFinalize(msgHandle);
{
}