Permalink
Browse files

implement check_write_read.c (to be fixed)

  • Loading branch information...
1 parent 1a0ea1d commit 3069a21f8f27327653c086955a52fa1868db506a @aalex aalex committed Mar 20, 2012
Showing with 119 additions and 2 deletions.
  1. +2 −1 tests/Makefile.am
  2. +117 −1 tests/check_write_read.c
View
@@ -1,6 +1,7 @@
AM_CFLAGS = -I$(top_srcdir)
LDADD = \
- $(top_builddir)/shmdata/libshmdata-@LIBSHMDATA_API_VERSION@.la
+ $(top_builddir)/shmdata/libshmdata-@LIBSHMDATA_API_VERSION@.la \
+ $(top_builddir)/shmdata/libshmdata-any-@LIBSHMDATA_API_VERSION@.la
check_PROGRAMS = \
check_write_read
@@ -1,7 +1,123 @@
+/*
+ * Copyright (C) 2012 Nicolas Bouillot (http://www.nicolasbouillot.net)
+ * Copyright (C) 2012 Alexandre Quessy
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ */
+
+/**
+ * @file Checks if it can send/receive ASCII data.
+ * The main() function calls check_read_write() which starts a writer and a reader. When on_data is called, it checks if the string received matches what has been sent, and sets the value of the success variable accordingly.
+ */
+
+#include "shmdata/any-data-reader.h"
+#include "shmdata/any-data-writer.h"
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h> //sleep
+
+// types:
+typedef enum _bool
+{
+ no = 0,
+ yes = 1
+} Bool;
+
+// constants:
+static const Bool VERBOSE = yes;
+static const char message[21] = "helloworldhelloworld";
+
+// variables:
+static Bool success = no;
+static Bool keep_going;
+
+// Function signatures:
+static void data_not_required_anymore (void *priv);
+static void on_data (shmdata_any_reader_t * reader, void *shmbuf, void *data, int data_size, unsigned long long timestamp, const char *type_description, void *user_data);
+static int check_read_write ();
+
+// Implementations:
+static void
+data_not_required_anymore (void *priv)
+{
+ // FIXME: what is this?
+}
+
+void
+on_data (shmdata_any_reader_t * reader,
+ void *shmbuf,
+ void *data,
+ int data_size,
+ unsigned long long timestamp,
+ const char *type_description, void *user_data)
+{
+ if (VERBOSE == yes)
+ {
+ printf ("data %p, data size %d, timestamp %llu, type descr %s\n",
+ data, data_size, timestamp, type_description);
+ printf ("user_data: %s\n", (const char *) user_data);
+ }
+
+ if (strcmp(data, message) == 0)
+ success = yes;
+ //free the data, can also be called later
+ shmdata_any_reader_free (shmbuf);
+ keep_going = no;
+}
+
int
check_read_write ()
{
- return 0;
+ shmdata_any_reader_t *reader;
+ shmdata_any_writer_t *writer;
+
+ const char *my_user_data = "You can pass a pointer to the reader's data handler function.";
+ const char *SOCKET_PATH = "/tmp/shmdata-test-check-write-read";
+
+ reader = shmdata_any_reader_init ();
+ shmdata_any_reader_set_debug (reader, SHMDATA_ENABLE_DEBUG);
+ shmdata_any_reader_set_on_data_handler (reader, &on_data,
+ (void *) my_user_data);
+ shmdata_any_reader_set_data_type (reader, "application/helloworld_");
+ shmdata_any_reader_start (reader, SOCKET_PATH);
+
+ writer = shmdata_any_writer_init ();
+ shmdata_any_writer_set_debug (writer, SHMDATA_ENABLE_DEBUG);
+ shmdata_any_writer_set_data_type (writer, "application/helloworld_");
+ shmdata_any_writer_start (writer, SOCKET_PATH);
+
+ unsigned long long myclock = 0;
+ unsigned long long nsecPeriod = 30000000;
+
+ keep_going = yes;
+ while (keep_going == yes)
+ {
+ //data should be serialized if network is involved
+ shmdata_any_writer_push_data (writer,
+ message,
+ sizeof (message),
+ myclock,
+ &data_not_required_anymore, message);
+ usleep (nsecPeriod / 1000);
+ myclock += nsecPeriod;
+ }
+
+ shmdata_any_writer_close (writer);
+ shmdata_any_reader_close (reader);
+ if (success == yes)
+ return 0;
+ else
+ return 1;
}
int

0 comments on commit 3069a21

Please sign in to comment.