+++ /dev/null
-/*
- File name : morep_test.c
- Projet : MERLIN
- Date of creation : 2025/03/18
- Version : 1.0
- Copyright : Thales SIX
- Author : Laurent Mazet <laurent.mazet@thalesgroup.com>
-
- Description : Test programm for MOREP library
-
- History :
- - initial version
-*/
-
-/* depend: */
-/* cflags: */
-/* linker: morep.o */
-/* winlnk: morep.o */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <assert.h>
-#include <pthread.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "morep.h"
-#include "verbose.h"
-
-#define UNUSED __attribute__ ((unused))
-
-int MOREP_Accept_NONBLOCK_OK = 0;
-int MOREP_Receive_NONBLOCK_OK = 0;
-int MOREP_Send_OK = 0;
-int MOREP_Echo_OK = 0;
-
-int test_client (char *local_address, char *remote_address)
-{
- VERBOSE (morep, TRACE, PRINTF ("test_client\n"));
-
- int count;
- unsigned char tx_data[10][8192];
- unsigned long tx_msgtype[10];
- int tx_len[10];
-
- VERBOSE (morep, INFO, PRINTF ("%s started\n", remote_address));
-
- int morep = MOREP_Connect (local_address, remote_address);
- if (morep < 0) {
- VERBOSE (morep, ERROR, PRINTF ("%s MOREP_Connect error\n", remote_address));
- return -1;
- }
-
- VERBOSE (morep, INFO, PRINTF ("%s connected to server, trying to exchange 10 messages\n", remote_address));
-
- for (count = 0; count < 10; count++) {
- int i, rc;
-
- /* to valid non block */
- if (count == 5)
- sleep (1);
-
- tx_len[count] = rand () % sizeof (tx_data[count]);
- tx_msgtype[count] = rand ();
- for (i = 0; i < tx_len[count]; i++)
- tx_data[count][i] = rand () % 256;
-
- rc = MOREP_Send (morep, tx_msgtype[count], tx_data[count], tx_len[count]);
- if (rc < 0) {
- VERBOSE (morep, ERROR, PRINTF ("%s MOREP_Send tx error\n", remote_address));
- return -1;
-
- }
- if (tx_len[count] != rc) {
- VERBOSE (morep, ERROR, PRINTF ("%s MOREP_Send tx len error tx=%d rc=%d\n", remote_address, tx_len[count], rc));
- return -1;
- }
- }
-
- for (count = 0; count < 10; count++) {
- int rx_len;
- unsigned char rx_data[32768];
- unsigned long rx_msgtype;
-
-
- do {
- rx_len = MOREP_Receive (morep, &rx_msgtype, rx_data, sizeof (rx_data));
- if (rx_len == 0)
- sleep (1);
- } while (rx_len == 0);
-
- VERBOSE (morep, INFO, PRINTF ("%s echo received count=%d len=%d, morep=%d\n", remote_address, count, rx_len, morep));
-
- if (tx_len[count] != rx_len) {
- VERBOSE (morep, WARNING, PRINTF ("%s MOREP_Receive rx len error : tx=%d / rx=%d\n", remote_address, tx_len[count], rx_len));
- return -1;
- }
-
- if (tx_msgtype[count] != rx_msgtype) {
- VERBOSE (morep, WARNING, PRINTF ("%s MOREP_Receive rx msgtype error : tx=%08lX / rx=%08lX\n", remote_address, tx_msgtype[count], rx_msgtype));
- return -1;
- }
-
- if (memcmp (rx_data, tx_data[count], tx_len[count]) != 0 ) {
- VERBOSE (morep, WARNING, PRINTF ("%s MOREP_Receive rx corrupted data\n",remote_address));
- return -1;
- }
- }
-
- MOREP_Close (morep);
- VERBOSE (morep, INFO, PRINTF ("%s end of test, %d echo processed\n", remote_address, count));
- return 0;
-}
-
-void *rep_server (UNUSED void *dummy)
-{
- VERBOSE (morep, TRACE, PRINTF ("rep_server\n"));
-
- VERBOSE (morep, INFO, PRINTF ("rep_server started\n"));
-
- int morep = MOREP_Connect ("rep://01:02:03:04:05:06/2064", "rep://07:08:08:0a:0b:0C/2065"); /* 0x0810 / 0x0811 */
- if (morep < 0) {
- VERBOSE (morep, ERROR, PRINTF ("MOREP_Connect (%d)", errno));
- exit (1);
- }
-
- VERBOSE (morep, INFO, PRINTF ("rep_server waiting for data\n"));
-
- while (1) {
- unsigned char data[1024];
- unsigned long msgtype;
-
- int rx_len = MOREP_Receive (morep, &msgtype, data, sizeof (data));
- if (rx_len == 0) {
- MOREP_Receive_NONBLOCK_OK = 1;
- sleep (1);
- continue;
- }
- if (rx_len < 0) {
- MOREP_Close (morep);
- break;
- }
-
- VERBOSE (morep, INFO, PRINTF ("rep_server receive data len=%d, sending echo\n", rx_len));
-
- int tx_len = MOREP_Send (morep, msgtype, data, rx_len);
- if (tx_len == rx_len)
- MOREP_Send_OK = 1;
- }
-
- return NULL;
-}
-
-void *rep_client (UNUSED void *dummy)
-{
- int i;
-
- for (i = 0; i < 5; i++) {
- int rc = test_client ("rep://07:08:08:0a:0b:0C/2065", "rep://01:02:03:04:05:06/2064");
- if (rc)
- pthread_exit (&MOREP_Echo_OK);
- }
-
- MOREP_Echo_OK = 1;
- pthread_exit (&MOREP_Echo_OK);
- return NULL;
-}
-
-/**
- Dump status macro
-*/
-#define DUMP_STATUS(x) do { \
- printf (#x " %s\n", (x) ? "OK" : "BAD"); \
- if (!(x)) \
- failed = 1; \
- } while (0) \
-
-/**
- Verbose level
-*/
-DECLARE_VERBOSE_LEVEL (morep, INFO);
-
-int main (int argc, char **argv)
-{
-
- /* process arguments */
- if (argc > 1) {
- CHANGE_VERBOSE_LEVEL (morep, atoi (argv[1]));
- argc--;
- }
- if (argc > 1) {
- printf ("usage: %s [verbose level]\n", argv[0]);
- exit (1);
- }
-
- printf ("*** STARTING TEST SEQUENCE ***\n");
-
- pthread_t rep_thread;
-
- pthread_create (&rep_thread, NULL, rep_server, NULL);
-
- /* Give some delay to server to setup there MOREP */
- sleep (1);
-
- pthread_t rep_client_thread;
-
- pthread_create (&rep_client_thread, NULL, rep_client, NULL);
-
- pthread_join (rep_client_thread, NULL);
-
- printf ("*** END OF TEST SEQUENCE ***\n");
-
- int failed = 0;
- DUMP_STATUS (MOREP_Accept_NONBLOCK_OK);
- DUMP_STATUS (MOREP_Receive_NONBLOCK_OK);
- DUMP_STATUS (MOREP_Send_OK);
- DUMP_STATUS (MOREP_Echo_OK);
-
- return failed ? EXIT_FAILURE : EXIT_SUCCESS;
-}
-
-/* test: morep_test */
-/* test: morep_test 1 */
-
-/* vi:set tabstop=4 expandtab shiftwidth=4: this line set vi mode*/