]> rtime.felk.cvut.cz Git - frescor/fna.git/commitdiff
preparing to do unix fna
authorsangorrin <sangorrin@35b4ef3e-fd22-0410-ab77-dab3279adceb>
Fri, 26 Oct 2007 17:46:58 +0000 (17:46 +0000)
committersangorrin <sangorrin@35b4ef3e-fd22-0410-ab77-dab3279adceb>
Fri, 26 Oct 2007 17:46:58 +0000 (17:46 +0000)
git-svn-id: http://www.frescor.org/private/svn/frescor/fna/trunk@819 35b4ef3e-fd22-0410-ab77-dab3279adceb

config.mk_example
include/frsh_fna.h
rules.mk
src/fna_configuration.c
src_unix/unix_fna.c
src_unix/unix_fna.h
tests/tests_rtep_fna/test_c_rtep_fna.c
tests/tests_unix_fna/Makefile [new file with mode: 0644]
tests/tests_unix_fna/test_unix_fna_send_receive.c [new file with mode: 0644]
tests/tests_unix_fna/test_unix_send_receive.c [new file with mode: 0644]

index a5fee75fd5a19fec1601170129062c94a3a7df3b..257e2c6f6f4e7227b1c52d0b260b2b5cf1f45425 100644 (file)
@@ -6,8 +6,8 @@ PLATFORM_PATH = $(HOME)/marte
 EXPORT_PATH = $(HOME)/export
 
 # enable networks with their flag (change DISABLED<->ENABLED)
-RTEP_ENABLE = RTEP_FNA_ENABLED
-UNIX_ENABLE = UNIX_FNA_DISABLED
+RTEP_ENABLE = RTEP_FNA_DISABLED
+UNIX_ENABLE = UNIX_FNA_ENABLED
 
 # the gcc to use
 CC = mgcc
index bcf505663e22f15c634094898aa031c33b71b480..4c2af91c0fa00cf19368847e7b4bb9da6a703f5e 100644 (file)
@@ -72,7 +72,7 @@
 #include <stdbool.h> /* for bool */
 #include "fna_configuration.h"
 
-#ifdef RTEP
+#ifdef RTEP_FNA_ENABLED
    #include "rtep.h"
 #endif
 
  *   FNA_ERR_BAD_ARGUMENT: if pointers are NULL \n
  *
  **/
-#ifdef RTEP
-int frsh_rtep_map_network_address
-      (const frsh_resource_id_t resource_id,
-       const rtep_station_id_t in_address,
-       frsh_network_address_t *out_address);
+#ifdef RTEP_FNA_ENABLED
+int frsh_rtep_map_network_address(const frsh_resource_id_t resource_id,
+                                  const rtep_station_id_t in_address,
+                                  frsh_network_address_t *out_address);
 #endif
 
 /**
@@ -150,11 +149,10 @@ int frsh_rtep_map_network_address
  *   FNA_ERR_BAD_ARGUMENT: if pointers are NULL \n
  *
  **/
-#ifdef RTEP
-int frsh_rtep_map_stream_id
-      (const frsh_resource_id_t resource_id,
-       const rtep_channel_t in_stream,
-       frsh_stream_id_t *out_stream);
+#ifdef RTEP_FNA_ENABLED
+int frsh_rtep_map_stream_id(const frsh_resource_id_t resource_id,
+                            const rtep_channel_t in_stream,
+                            frsh_stream_id_t *out_stream);
 #endif
 
 /*@}*/
@@ -196,7 +194,7 @@ int frsh_rtep_map_stream_id
  *   FNA_ERR_REJECTED_CONTRACT : if the renegotiation fails \n
  *
  **/
-#ifdef RTEP
+#ifdef RTEP_FNA_ENABLED
 int frsh_rtep_negotiation_messages_vres_renegotiate
       (const frsh_resource_id_t resource_id,
        const struct timespec *period);
@@ -220,7 +218,7 @@ int frsh_rtep_negotiation_messages_vres_renegotiate
  *   FNA_ERR_BAD_ARGUMENT: if pointers are NULL \n
  *
  **/
-#ifdef RTEP
+#ifdef RTEP_FNA_ENABLED
 int frsh_rtep_negotiation_messages_vres_get_period
       (const frsh_resource_id_t resource_id,
        struct timespec *period);
@@ -250,7 +248,7 @@ int frsh_rtep_negotiation_messages_vres_get_period
  *   FNA_ERR_BAD_ARGUMENT: if pointers are NULL \n
  *
  **/
-#ifdef RTEP
+#ifdef RTEP_FNA_ENABLED
 int frsh_rtep_service_thread_vres_renegotiate
    (const frsh_resource_id_t resource_id,
     const struct timespec *budget,
@@ -276,7 +274,7 @@ int frsh_rtep_service_thread_vres_renegotiate
  *   FNA_ERR_BAD_ARGUMENT: if pointers are NULL \n
  *
  **/
-#ifdef RTEP
+#ifdef RTEP_FNA_ENABLED
 int frsh_rtep_service_thread_vres_get_budget_and_period
       (const frsh_resource_id_t resource_id,
        struct timespec *budget,
index f7567a7bc063568d119d2867067a11dc6968fa69..b5e8191e30a69ac4e2056b5b54895cada1e651f7 100644 (file)
--- a/rules.mk
+++ b/rules.mk
@@ -1,4 +1,4 @@
-.PHONY: none clean
+.PHONY: none clean libfna.a
 
 none:
        @exec echo -e "\nPlease introduce a target, i.e: 'make test_dtm_xxx.exe'";
@@ -7,7 +7,7 @@ none:
        @exec echo -e "\n>> Building $@: ";
        @if [ -f $< ]; \
        then \
-               $(CC) $(CFLAGS) $< $(LDFLAGS) &> /dev/null; \
+               $(CC) $(CFLAGS) $< $(LDFLAGS); \
        fi; # -o $@ (fix marte mgcc)
        @exec echo ">> End Building $@";
 
index 8884178ab21e60778f5161056e0c939f023b5ff2..935fd3097278f0f94d4faa8759a3ce32c0db0ace 100644 (file)
@@ -81,8 +81,8 @@
 
 
 fna_operations_t  *fna_operations[FNA_MAX_NETWORKS] = {
-        &rtep_fna_operations,
-        // &unix_fna_operations,
+        // &rtep_fna_operations,
+        &unix_fna_operations,
         // NULL, // resource_id 0
         NULL, // resource_id 1
         NULL, // resource_id 2
index 779fe21dbafaa83d56eb2118b140fb5deaddc8e2..8ffca6d2172cbd3d67ed6cfa67f21fdec1db3009 100644 (file)
@@ -65,7 +65,7 @@
 #include <malloc.h>   /* for malloc and free */
 #include "unix_fna.h" /* function prototypes */
 
-#if 0
+#if 1
 #include <stdio.h>
 #define DEBUG(x,args...) printf("%s: " x, __func__ , ##args)
 #else
 //           INITIALIZATION
 //////////////////////////////////////////////////////////////////////
 
+char *the_unix_stream_ids[MX_UNIX_STREAM_IDS] = {
+        "/tmp/stream00",
+        "/tmp/stream01",
+        "/tmp/stream02",
+        "/tmp/stream03",
+        "/tmp/stream04",
+        "/tmp/stream05"
+};
+
 /**
  * unix_fna_init()
  *
@@ -83,8 +92,8 @@
 
 int unix_fna_init(const frsh_resource_id_t resource_id)
 {
-    DEBUG("starting unix fna\n");
-    return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 ///////////////////////////////////////////////////////////////////
@@ -100,7 +109,8 @@ int unix_fna_contract_negotiate(const frsh_resource_id_t resource_id,
                                 const frsh_contract_t *contract,
                                 fna_vres_id_t *vres)
 {
-    return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 /**
@@ -112,7 +122,8 @@ int unix_fna_contract_renegotiate_sync(const frsh_resource_id_t resource_id,
                                        const fna_vres_id_t vres,
                                        const frsh_contract_t *new_contract)
 {
-    return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 /**
@@ -126,7 +137,8 @@ int unix_fna_contract_renegotiate_async(const frsh_resource_id_t resource_id,
                                         frsh_signal_t signal_to_notify,
                                         frsh_signal_info_t signal_info)
 {
-   return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 /**
@@ -138,7 +150,8 @@ int unix_fna_vres_get_renegotiation_status(const frsh_resource_id_t resource_id,
                                            const fna_vres_id_t vres,
                                            frsh_renegotiation_status_t *renegotiation_status)
 {
-   return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 /**
@@ -149,7 +162,8 @@ int unix_fna_vres_get_renegotiation_status(const frsh_resource_id_t resource_id,
 int unix_fna_vres_destroy(const frsh_resource_id_t resource_id,
                           const fna_vres_id_t vres)
 {
-    return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 /**
@@ -161,7 +175,8 @@ int unix_fna_vres_get_contract(const frsh_resource_id_t resource_id,
                                const fna_vres_id_t vres,
                                frsh_contract_t *contract)
 {
-   return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 /**
@@ -173,7 +188,8 @@ int unix_fna_vres_get_usage(const frsh_resource_id_t resource_id,
                             const fna_vres_id_t vres,
                             struct timespec *usage)
 {
-   return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 /**
@@ -185,7 +201,8 @@ int unix_fna_vres_get_remaining_budget(const frsh_resource_id_t resource_id,
                                        const fna_vres_id_t vres,
                                        struct timespec *remaining_budget)
 {
-   return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 /**
@@ -198,7 +215,8 @@ int unix_fna_vres_get_budget_and_period(const frsh_resource_id_t resource_id,
                                         struct timespec *budget,
                                         struct timespec *period)
 {
-   return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 ///////////////////////////////////////////////////////////////////
@@ -214,7 +232,8 @@ int unix_fna_resource_get_capacity(const frsh_resource_id_t resource_id,
                                    const int importance,
                                    uint32_t *capacity)
 {
-   return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 /**
@@ -226,7 +245,8 @@ int unix_fna_resource_get_total_weight(const frsh_resource_id_t resource_id,
                                        const int importance,
                                        int *total_weight)
 {
-   return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 /**
@@ -239,7 +259,8 @@ int unix_fna_vres_decrease_capacity(const frsh_resource_id_t resource_id,
                                     const struct timespec new_budget,
                                     const struct timespec new_period)
 {
-   return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 ///////////////////////////////////////////////////////////////////
@@ -255,7 +276,8 @@ int unix_fna_send_sync(const fna_endpoint_data_t *endpoint,
                        const void *msg,
                        const size_t size)
 {
-   return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 /**
@@ -267,7 +289,8 @@ int unix_fna_send_async(const fna_endpoint_data_t *endpoint,
                         const void *msg,
                         const size_t size)
 {
-    return 0;
+        DEBUG("send async\n");
+        return 0;
 }
 
 /**
@@ -281,7 +304,8 @@ int unix_fna_receive_sync(const fna_endpoint_data_t *endpoint,
                           size_t *received_bytes,
                           frsh_network_address_t *from)
 {
-    return 0;
+        DEBUG("receive sync\n");
+        return 0;
 }
 
 /**
@@ -295,7 +319,8 @@ int unix_fna_receive_async(const fna_endpoint_data_t *endpoint,
                            size_t *received_bytes,
                            frsh_network_address_t *from)
 {
-   return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 /**
@@ -308,7 +333,8 @@ int unix_fna_send_endpoint_get_status(const fna_endpoint_data_t *endpoint,
                                       frsh_endpoint_network_status_t *network_status,
                                       frsh_protocol_status_t *protocol_status)
 {
-   return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 /**
@@ -317,7 +343,8 @@ int unix_fna_send_endpoint_get_status(const fna_endpoint_data_t *endpoint,
  **/
 int unix_fna_receive_endpoint_created(const fna_endpoint_data_t *endpoint)
 {
-   return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 /**
@@ -330,7 +357,8 @@ int unix_fna_receive_endpoint_get_status(const fna_endpoint_data_t *endpoint,
                                          frsh_endpoint_network_status_t *network_status,
                                          frsh_protocol_status_t *protocol_status)
 {
-   return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 //////////////////////////////////////////////////////////////////////
@@ -346,7 +374,8 @@ int unix_fna_network_get_max_message_size(const frsh_resource_id_t resource_id,
                                           const frsh_network_address_t destination,
                                           size_t *max_size)
 {
-    return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 /**
@@ -358,7 +387,8 @@ int unix_fna_network_bytes_to_budget(const frsh_resource_id_t resource_id,
                                      const size_t nbytes,
                                      struct timespec *budget)
 {
-    return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 /**
@@ -370,7 +400,8 @@ int unix_fna_network_budget_to_bytes(const frsh_resource_id_t resource_id,
                                      const struct timespec *budget,
                                      size_t *nbytes)
 {
-    return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 /**
@@ -381,34 +412,35 @@ int unix_fna_network_budget_to_bytes(const frsh_resource_id_t resource_id,
 int unix_fna_network_get_min_eff_budget(const frsh_resource_id_t resource_id,
                                         struct timespec *budget)
 {
-   return 0;
+        DEBUG("NOT IMPLEMENTED\n");
+        return 0;
 }
 
 // GLOBAL variable to install the network protocol in FRESCOR
 
 fna_operations_t unix_fna_operations = {
-    .fna_init = unix_fna_init,
-    .fna_contract_negotiate = unix_fna_contract_negotiate,
-    .fna_contract_renegotiate_sync = unix_fna_contract_renegotiate_sync,
-    .fna_contract_renegotiate_async = unix_fna_contract_renegotiate_async,
-    .fna_vres_get_renegotiation_status = unix_fna_vres_get_renegotiation_status,
-    .fna_vres_destroy = unix_fna_vres_destroy,
-    .fna_vres_get_contract = unix_fna_vres_get_contract,
-    .fna_vres_get_usage = unix_fna_vres_get_usage,
-    .fna_vres_get_remaining_budget = unix_fna_vres_get_remaining_budget,
-    .fna_vres_get_budget_and_period = unix_fna_vres_get_budget_and_period,
-    .fna_resource_get_capacity = unix_fna_resource_get_capacity,
-    .fna_resource_get_total_weight = unix_fna_resource_get_total_weight,
-    .fna_vres_decrease_capacity = unix_fna_vres_decrease_capacity,
-    .fna_send_sync = unix_fna_send_sync,
-    .fna_send_async = unix_fna_send_async,
-    .fna_receive_sync = unix_fna_receive_sync,
-    .fna_receive_async = unix_fna_receive_async,
-    .fna_send_endpoint_get_status = unix_fna_send_endpoint_get_status,
-    .fna_receive_endpoint_created = unix_fna_receive_endpoint_created,
-    .fna_receive_endpoint_get_status = unix_fna_receive_endpoint_get_status,
-    .fna_network_get_max_message_size = unix_fna_network_get_max_message_size,
-    .fna_network_bytes_to_budget = unix_fna_network_bytes_to_budget,
-    .fna_network_budget_to_bytes = unix_fna_network_budget_to_bytes,
-    .fna_network_get_min_eff_budget = unix_fna_network_get_min_eff_budget
+        .fna_init = unix_fna_init,
+        .fna_contract_negotiate = unix_fna_contract_negotiate,
+        .fna_contract_renegotiate_sync = unix_fna_contract_renegotiate_sync,
+        .fna_contract_renegotiate_async = unix_fna_contract_renegotiate_async,
+        .fna_vres_get_renegotiation_status = unix_fna_vres_get_renegotiation_status,
+        .fna_vres_destroy = unix_fna_vres_destroy,
+        .fna_vres_get_contract = unix_fna_vres_get_contract,
+        .fna_vres_get_usage = unix_fna_vres_get_usage,
+        .fna_vres_get_remaining_budget = unix_fna_vres_get_remaining_budget,
+        .fna_vres_get_budget_and_period = unix_fna_vres_get_budget_and_period,
+        .fna_resource_get_capacity = unix_fna_resource_get_capacity,
+        .fna_resource_get_total_weight = unix_fna_resource_get_total_weight,
+        .fna_vres_decrease_capacity = unix_fna_vres_decrease_capacity,
+        .fna_send_sync = unix_fna_send_sync,
+        .fna_send_async = unix_fna_send_async,
+        .fna_receive_sync = unix_fna_receive_sync,
+        .fna_receive_async = unix_fna_receive_async,
+        .fna_send_endpoint_get_status = unix_fna_send_endpoint_get_status,
+        .fna_receive_endpoint_created = unix_fna_receive_endpoint_created,
+        .fna_receive_endpoint_get_status = unix_fna_receive_endpoint_get_status,
+        .fna_network_get_max_message_size = unix_fna_network_get_max_message_size,
+        .fna_network_bytes_to_budget = unix_fna_network_bytes_to_budget,
+        .fna_network_budget_to_bytes = unix_fna_network_budget_to_bytes,
+        .fna_network_get_min_eff_budget = unix_fna_network_get_min_eff_budget
 };
index 7d890ca5a4e2d6c2c93c9aa538ec7ace7bc4c096..07db3d790f3f997f40ca1641a2d4cf9e2e0a0847 100644 (file)
 
 extern fna_operations_t unix_fna_operations;
 
+/**
+ *  unix addresses:
+ *
+ *  As we are in the same node the network address will be always 0.
+ *  The stream_ids 0 .. MX_UNIX_STREAM_IDS-1 are statically mapped in the
+ *  following table to UNIX pathnames.
+ *
+ **/
+#define MX_UNIX_STREAM_IDS 6
+extern char *the_unix_stream_ids[MX_UNIX_STREAM_IDS];
+
 #endif // _UNIX_FNA_H_
index 5b7ee428e135b10c2123b9757dfdd8b5f95f3466..c9e162d3f1c1b278e15addf5f489bdb59596b913 100644 (file)
@@ -71,7 +71,7 @@
 #include <time.h> // for timespec
 
 #include "frsh_core.h" // for frsh_contract_set_xxx
-#include "frsh_core_types.h" // for FRSH_RESOURCE_ID_DEFAULT
+#include "frsh_core_types.h" // for FRSH_NETWORK_ID_DEFAULT
 #include "frsh_distributed_types.h" // for frsh_network_address_t, frsh_stream_id_t
 
 #include "fna.h" // for fna_vres_id_t
@@ -99,7 +99,7 @@ int main ()
     printf("--------------------------------------------------\n");
     printf("1.- fna_init\n");
     printf("--------------------------------------------------\n");
-    err = rtep_fna_operations.fna_init(FRSH_RESOURCE_ID_DEFAULT);
+    err = rtep_fna_operations.fna_init(FRSH_NETWORK_ID_DEFAULT);
     assert(err == 0);
 
     printf("--------------------------------------------------\n");
diff --git a/tests/tests_unix_fna/Makefile b/tests/tests_unix_fna/Makefile
new file mode 100644 (file)
index 0000000..7d3abec
--- /dev/null
@@ -0,0 +1,5 @@
+include ../../config.mk
+include ../../rules.mk
+
+CFLAGS += -I$(FNA_PATH)/src_unix/
+LDFLAGS += -L$(FNA_PATH)/lib -lunixfna -L$(FRSH_PATH)/lib -lfrsh -L$(FOSA_PATH)/lib -lfosa_$(PLATFORM) -lm -lfna
\ No newline at end of file
diff --git a/tests/tests_unix_fna/test_unix_fna_send_receive.c b/tests/tests_unix_fna/test_unix_fna_send_receive.c
new file mode 100644 (file)
index 0000000..7843077
--- /dev/null
@@ -0,0 +1,120 @@
+#include <stdio.h>
+#include <stdlib.h> /* atoi */
+#include <unistd.h> /* sleep */
+#include <assert.h>
+#include <string.h> /* memset */
+
+#include <sys/socket.h>
+#include <sys/un.h> /* struct sockaddr_un */
+
+#include "frsh_core.h" // for frsh_contract_set_xxx
+#include "frsh_core_types.h" // for FRSH_NETWORK_ID_DEFAULT
+#include "frsh_distributed_types.h" // for frsh_network_address_t, frsh_stream_id_t
+
+#include "fna.h" // for fna_vres_id_t
+#include "unix_fna.h" // for unix_fna_operations
+
+
+void sender(void);
+void receiver(void);
+
+int main (int argc, char **argv)
+{
+        int err;
+
+        if (argc != 2) {
+                printf("please provide the node to execute:\n");
+                printf("  $./mprogram 0 (sender)\n");
+                printf("  $./mprogram 1 (receiver)\n");
+                return -1;
+        }
+
+        err = unix_fna_operations.fna_init(FRSH_NETWORK_ID_DEFAULT);
+        assert(err == 0);
+
+        switch(atoi(argv[1])) {
+        case 0:
+                sender();
+                break;
+        case 1:
+                receiver();
+                break;
+        default:
+                printf("wrong node number\n");
+                return -1;
+        }
+
+        unlink(the_unix_stream_ids[0]);
+        unlink(the_unix_stream_ids[1]);
+
+        return 0;
+}
+
+void sender(void)
+{
+        int err, i;
+
+        fna_endpoint_data_t endpoint;
+        frsh_stream_id_t receiver_port;
+        char buffer[100];
+        size_t size;
+
+        printf("I am the sender\n");
+
+        receiver_port = 0; /* statically configured to the_unix_stream_ids[0] */
+
+        endpoint.endpoint_type  = FRSH_SEND_ENDPOINT_TYPE;
+        endpoint.vres           = 0;
+        endpoint.is_bound       = true;
+        endpoint.destination    = 0; /* only for send_endpoints */
+        endpoint.resource_id    = FRSH_NETWORK_ID_DEFAULT;
+        endpoint.stream_id      = receiver_port;
+
+        for(i=0; i<10; i++) {
+                sleep(1);
+                size = snprintf(buffer, sizeof(buffer), "hello world %d", i);
+
+                err = unix_fna_operations.fna_send_async(&endpoint,
+                                                         buffer,
+                                                         size);
+                assert (err == 0);
+
+                printf("sender : sent %s\n", buffer);
+        }
+
+        printf("Sender done\n");
+}
+
+void receiver(void)
+{
+        int err, i;
+        fna_endpoint_data_t endpoint;
+        frsh_network_address_t sender_addr;
+        frsh_stream_id_t sender_port;
+        char buffer[100];
+        size_t received_bytes;
+
+        printf("I am the receiver\n");
+
+        sender_port = 1;
+
+        endpoint.endpoint_type  = FRSH_RECEIVE_ENDPOINT_TYPE;
+        endpoint.is_bound       = false;
+        endpoint.resource_id    = FRSH_NETWORK_ID_DEFAULT;
+        endpoint.stream_id      = sender_port;
+
+        for(i=0; i<10; i++) {
+                err = unix_fna_operations.fna_receive_sync(&endpoint,
+                                                           buffer,
+                                                           100,
+                                                           &received_bytes,
+                                                           &sender_addr);
+                assert (err == 0);
+
+                buffer[received_bytes] = '\0';
+
+                printf("receiver : received %s\n", buffer);
+        }
+
+        printf("Receiver done\n");
+}
diff --git a/tests/tests_unix_fna/test_unix_send_receive.c b/tests/tests_unix_fna/test_unix_send_receive.c
new file mode 100644 (file)
index 0000000..f09297d
--- /dev/null
@@ -0,0 +1,126 @@
+#include <stdio.h>
+#include <stdlib.h> /* atoi */
+#include <unistd.h> /* sleep */
+#include <assert.h>
+#include <string.h> /* memset */
+
+#include <sys/socket.h>
+#include <sys/un.h> /* struct sockaddr_un */
+
+#define SENDER_PATH     "/tmp/sender_addr"
+#define RECEIVER_PATH   "/tmp/receiver_addr"
+
+void sender(void);
+void receiver(void);
+
+int main (int argc, char **argv)
+{
+        if (argc != 2) {
+                printf("please provide the node to execute:\n");
+                printf("  $./mprogram 0 (sender)\n");
+                printf("  $./mprogram 1 (receiver)\n");
+                return -1;
+        }
+
+        switch(atoi(argv[1])) {
+        case 0:
+                sender();
+                break;
+        case 1:
+                receiver();
+                break;
+        default:
+                printf("wrong node number\n");
+                return -1;
+        }
+
+        unlink(SENDER_PATH);
+        unlink(RECEIVER_PATH);
+
+        return 0;
+}
+
+void sender(void)
+{
+        int sock, err, i;
+        struct sockaddr_un sender_addr, receiver_addr;
+        char buffer[100];
+        size_t size;
+        ssize_t sent_bytes;
+
+        printf("I am the sender\n");
+
+        err = sock = socket(AF_UNIX, SOCK_DGRAM, 0);
+        assert(err >= 0);
+
+        memset(&sender_addr, 0, sizeof(sender_addr));
+        sender_addr.sun_family = AF_UNIX;
+        strcpy(sender_addr.sun_path, SENDER_PATH);
+
+        err = bind(sock, (struct sockaddr *)&sender_addr, sizeof(sender_addr));
+        assert(err >= 0);
+
+        memset(&receiver_addr, 0, sizeof(receiver_addr));
+        receiver_addr.sun_family = AF_UNIX;
+        strcpy(receiver_addr.sun_path, RECEIVER_PATH);
+
+        for(i=0; i<10; i++) {
+                sleep(1);
+                size = snprintf(buffer, sizeof(buffer), "hello world %d", i);
+                sent_bytes = sendto(sock,
+                                    buffer,
+                                    size,
+                                    0,
+                                    (struct sockaddr *) &receiver_addr,
+                                    sizeof(receiver_addr));
+
+                assert(sent_bytes >= 0);
+
+                printf("sender : sent %s\n", buffer);
+        }
+
+        close(sock);
+        printf("Sender done\n");
+}
+
+void receiver(void)
+{
+        int sock, err, i;
+        struct sockaddr_un sender_addr, receiver_addr;
+        char buffer[100];
+        ssize_t received_bytes;
+        socklen_t from_len;
+
+        printf("I am the receiver\n");
+
+        err = sock = socket(AF_UNIX, SOCK_DGRAM, 0);
+        assert(err >= 0);
+
+        memset(&receiver_addr, 0, sizeof(receiver_addr));
+        receiver_addr.sun_family = AF_UNIX;
+        strcpy(receiver_addr.sun_path, RECEIVER_PATH);
+
+        err = bind(sock, (struct sockaddr *)&receiver_addr, sizeof(receiver_addr));
+        assert(err >= 0);
+
+        for(i=0; i<10; i++) {
+
+                sleep(5);
+
+                from_len = sizeof(sender_addr);
+                received_bytes = recvfrom(sock,
+                                          buffer,
+                                          sizeof(buffer),
+                                          0,
+                                          (struct sockaddr *)&sender_addr,
+                                          &from_len);
+
+                assert(received_bytes >= 0);
+
+                buffer[received_bytes] = '\0';
+                printf("receiver : received %s\n", buffer);
+        }
+
+        close(sock);
+        printf("receiver done\n");
+}