From 3a95a6ecea8168f89a2a95d1cc1b19e11ca827a6 Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Thu, 15 Sep 2016 23:08:08 +0200
Subject: [PATCH] first cbor test programs added
---
.gitignore | 1 +
README.markdown | 12 ++++++
misc/Makefile | 4 +-
misc/cbor-examples/Makefile | 19 +++++++++
misc/cbor-examples/read-serialized-data.c | 36 ++++++++++++++++
misc/cbor-examples/serializing-data.c | 29 +++++++++++++
misc/cbor-examples/streaming-parser.c | 50 +++++++++++++++++++++++
misc/list.c | 3 ++
misc/msg.c | 37 ++++++++++++-----
misc/open-read-close-fifo.c | 4 ++
misc/open-write-close-fifo.c | 2 +
pingpong/Makefile | 8 ++--
pubsub/Makefile | 4 +-
13 files changed, 191 insertions(+), 18 deletions(-)
create mode 100644 misc/cbor-examples/Makefile
create mode 100644 misc/cbor-examples/read-serialized-data.c
create mode 100644 misc/cbor-examples/serializing-data.c
create mode 100644 misc/cbor-examples/streaming-parser.c
diff --git a/.gitignore b/.gitignore
index 97302fa..9bed793 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@
*.swp
*.out
*.o
+*.bin
diff --git a/README.markdown b/README.markdown
index f07bfdc..c2b0cff 100644
--- a/README.markdown
+++ b/README.markdown
@@ -24,6 +24,13 @@ First of all, the application will send a message to the service's pipe in **pla
In order to communicate between the application and the service, we use the [CBOR format (RFC 7049)][cbor].
This will be used with some conventions.
+## CBOR install, programming, debug
+
+[libcbor][libcbor] is used in the provided implementations.
+It is an [extensively documented][libcbor-doc] library, easy to install and to work with.
+
+We also strongly encourage the use of the [cbor-diag][cbor-diag] to ensure that you send and receive correctly formatted messages.
+
## overview
The format will be "type : value".
@@ -40,5 +47,10 @@ The type will be a simple byte :
index | abbreviation | semantic
0 | close | to close the communication between the application and the service
+ 1 | message | to send data
+ 2 | error | to send an error message
[cbor]: https://tools.ietf.org/html/rfc7049
+[cbor-diag]: https://github.com/cabo/cbor-diag
+[libcbor]: https://github.com/PJK/libcbor
+[libcbor-doc]: https://github.com/PJK/libcbor
diff --git a/misc/Makefile b/misc/Makefile
index 41eafb8..4d99ddf 100644
--- a/misc/Makefile
+++ b/misc/Makefile
@@ -10,7 +10,7 @@ TESTS=$(addsuffix .test, $(EXEC))
all: $(SOURCES) $(EXEC)
$(EXEC): $(OBJECTS) $(CFILES)
- $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@
+ $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -lcbor -o $@.bin
.c.o:
$(CC) -c $(CFLAGS) $< -o $@
@@ -19,4 +19,4 @@ clean:
-rm $(OBJECTS)
mrproper: clean
- rm $(EXEC)
+ rm *.bin
diff --git a/misc/cbor-examples/Makefile b/misc/cbor-examples/Makefile
new file mode 100644
index 0000000..c7173e0
--- /dev/null
+++ b/misc/cbor-examples/Makefile
@@ -0,0 +1,19 @@
+CC=gcc
+CFLAGS=-Wall -g -Wextra
+LDFLAGS= -pthread -lcbor
+CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change
+EXEC=$(basename $(wildcard *.c))
+SOURCES=$(wildcard ../lib/*.c)
+OBJECTS=$(SOURCES:.c=.o)
+TESTS=$(addsuffix .test, $(EXEC))
+
+all: $(SOURCES) $(EXEC)
+
+$(EXEC): $(OBJECTS) $(CFILES)
+ $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -lcbor -o $@.bin
+
+.c.o:
+ $(CC) -c $(CFLAGS) $< -o $@
+
+mrproper:
+ rm *.bin
diff --git a/misc/cbor-examples/read-serialized-data.c b/misc/cbor-examples/read-serialized-data.c
new file mode 100644
index 0000000..872d015
--- /dev/null
+++ b/misc/cbor-examples/read-serialized-data.c
@@ -0,0 +1,36 @@
+#include "cbor.h"
+#include
+
+/*
+ * Reads data from a file. Example usage:
+ * $ ./examples/readfile examples/data/nested_array.cbor
+ */
+
+int main(int argc, char * argv[])
+{
+ (void) argc;
+ (void) argv;
+
+ if (argc != 2) {
+ fprintf (stderr, "usage: %s file\n", argv[0]);
+ exit (1);
+ }
+
+ FILE * f = fopen(argv[1], "rb");
+ fseek(f, 0, SEEK_END);
+ size_t length = (size_t)ftell(f);
+ fseek(f, 0, SEEK_SET);
+ unsigned char * buffer = malloc(length);
+ fread(buffer, length, 1, f);
+
+ /* Assuming `buffer` contains `info.st_size` bytes of input data */
+ struct cbor_load_result result;
+ cbor_item_t * item = cbor_load(buffer, length, &result);
+ /* Pretty-print the result */
+ cbor_describe(item, stdout);
+ fflush(stdout);
+ /* Deallocate the result */
+ cbor_decref(&item);
+
+ fclose(f);
+}
diff --git a/misc/cbor-examples/serializing-data.c b/misc/cbor-examples/serializing-data.c
new file mode 100644
index 0000000..b721064
--- /dev/null
+++ b/misc/cbor-examples/serializing-data.c
@@ -0,0 +1,29 @@
+#include "cbor.h"
+#include
+
+int main(int argc, char * argv[])
+{
+ (void) argc;
+ (void) argv;
+
+ /* Preallocate the map structure */
+ cbor_item_t * root = cbor_new_definite_map(2);
+ /* Add the content */
+ cbor_map_add(root, (struct cbor_pair) {
+ .key = cbor_move(cbor_build_string("Is CBOR awesome?")),
+ .value = cbor_move(cbor_build_bool(true))
+ });
+ cbor_map_add(root, (struct cbor_pair) {
+ .key = cbor_move(cbor_build_uint8(42)),
+ .value = cbor_move(cbor_build_string("Is the answer"))
+ });
+ /* Output: `length` bytes of data in the `buffer` */
+ unsigned char * buffer;
+ size_t buffer_size, length = cbor_serialize_alloc(root, &buffer, &buffer_size);
+
+ fwrite(buffer, 1, length, stdout);
+ free(buffer);
+
+ fflush(stdout);
+ cbor_decref(&root);
+}
diff --git a/misc/cbor-examples/streaming-parser.c b/misc/cbor-examples/streaming-parser.c
new file mode 100644
index 0000000..af9c3a1
--- /dev/null
+++ b/misc/cbor-examples/streaming-parser.c
@@ -0,0 +1,50 @@
+#include "cbor.h"
+#include
+#include
+
+/*
+ * * Illustrates how one might skim through a map (which is assumed to have
+ * * string keys and values only), looking for the value of a specific key
+ * *
+ * * Use the examples/data/map.cbor input to test this.
+ * */
+
+const char * key = "a secret key";
+bool key_found = false;
+
+void find_string(void * _ctx, cbor_data buffer, size_t len)
+{
+ (void) _ctx;
+ if (key_found) {
+ printf("Found the value: %*s\n", (int) len, buffer);
+ key_found = false;
+ } else if (len == strlen(key)) {
+ key_found = (memcmp(key, buffer, len) == 0);
+ }
+}
+
+int main(int argc, char * argv[])
+{
+ (void) argc;
+ (void) argv;
+
+ FILE * f = fopen(argv[1], "rb");
+ fseek(f, 0, SEEK_END);
+ size_t length = (size_t)ftell(f);
+ fseek(f, 0, SEEK_SET);
+ unsigned char * buffer = malloc(length);
+ fread(buffer, length, 1, f);
+
+ struct cbor_callbacks callbacks = cbor_empty_callbacks;
+ struct cbor_decoder_result decode_result;
+ size_t bytes_read = 0;
+ callbacks.string = find_string;
+ while (bytes_read < length) {
+ decode_result = cbor_stream_decode(buffer + bytes_read,
+ length - bytes_read,
+ &callbacks, NULL);
+ bytes_read += decode_result.read;
+ }
+
+ fclose(f);
+}
diff --git a/misc/list.c b/misc/list.c
index aea2341..71dbda0 100644
--- a/misc/list.c
+++ b/misc/list.c
@@ -13,6 +13,9 @@ struct node {
int main(int argc, char * argv[])
{
+ (void) argc;
+ (void) argv;
+
// the list
struct mlist *list;
list = malloc (sizeof(struct mlist));
diff --git a/misc/msg.c b/misc/msg.c
index 4d1dbe7..d978013 100644
--- a/misc/msg.c
+++ b/misc/msg.c
@@ -1,7 +1,12 @@
#include "../lib/pubsubd.h"
+#include "cbor.h"
#include
#include
+#define PKT_CLOSE 0
+#define PKT_MSG 1
+#define PKT_ERROR 2
+
void
ohshit(int rvalue, const char* str) {
fprintf (stderr, "\033[31merr: %s\033[00m\n", str);
@@ -16,16 +21,6 @@ void usage (char **argv)
printf ( " This sends a CBOR msg [ 1, \"data\" ]\n");
}
-/*
- * implemented types:
- * bstr_t (default)
- * tstr_t
- * int_t
- *
- * future types:
- * nint_t
- */
-
int
main(int argc, char **argv)
{
@@ -34,5 +29,27 @@ main(int argc, char **argv)
exit (1);
}
+ unsigned char buf[BUFSIZ];
+ memset (buf, 0, BUFSIZ);
+
+ ssize_t buflen = read (0, buf, BUFSIZ);
+
+ /* Preallocate the map structure */
+ cbor_item_t * root = cbor_new_definite_map(1);
+ /* Add the content */
+ cbor_map_add(root, (struct cbor_pair) {
+ .key = cbor_move(cbor_build_uint8(PKT_MSG)),
+ .value = cbor_move(cbor_build_bytestring(buf, buflen))
+ });
+ /* Output: `length` bytes of data in the `buffer` */
+ unsigned char * buffer;
+ size_t buffer_size, length = cbor_serialize_alloc (root, &buffer, &buffer_size);
+
+ fwrite(buffer, 1, length, stdout);
+ free(buffer);
+
+ fflush(stdout);
+ cbor_decref(&root);
+
return EXIT_SUCCESS;
}
diff --git a/misc/open-read-close-fifo.c b/misc/open-read-close-fifo.c
index 919c225..ca2f396 100644
--- a/misc/open-read-close-fifo.c
+++ b/misc/open-read-close-fifo.c
@@ -5,6 +5,10 @@
int main(int argc, char * argv[])
{
+
+ (void) argc;
+ (void) argv;
+
char *fifopathin = "/tmp/123000-1-in";
size_t msize = 100;
diff --git a/misc/open-write-close-fifo.c b/misc/open-write-close-fifo.c
index dcf3e94..ca4c950 100644
--- a/misc/open-write-close-fifo.c
+++ b/misc/open-write-close-fifo.c
@@ -5,6 +5,8 @@
int main(int argc, char * argv[])
{
+ (void) argc;
+ (void) argv;
char *fifopathin = "/tmp/123000-1-in";
size_t msize;
diff --git a/pingpong/Makefile b/pingpong/Makefile
index 22d244e..17b1352 100644
--- a/pingpong/Makefile
+++ b/pingpong/Makefile
@@ -1,6 +1,6 @@
CC=gcc
CFLAGS=-Wall -g
-LDFLAGS=
+LDFLAGS= -pthread
CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change
EXEC=$(basename $(wildcard *.c))
SOURCES=$(wildcard ../lib/*.c)
@@ -10,13 +10,13 @@ TESTS=$(addsuffix .test, $(EXEC))
all: $(SOURCES) $(EXEC)
$(EXEC): $(OBJECTS) $(CFILES)
- $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@
+ $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@.bin
.c.o:
$(CC) -c $(CFLAGS) $< -o $@
clean:
- -rm $(OBJECTS)
+ @-rm $(OBJECTS)
mrproper: clean
- rm $(EXEC)
+ @-rm *.bin
diff --git a/pubsub/Makefile b/pubsub/Makefile
index 332cf7c..c30898e 100644
--- a/pubsub/Makefile
+++ b/pubsub/Makefile
@@ -10,7 +10,7 @@ TESTS=$(addsuffix .test, $(EXEC))
all: $(SOURCES) $(EXEC)
$(EXEC): $(OBJECTS) $(CFILES)
- $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@
+ $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@.bin
.c.o:
$(CC) -c $(CFLAGS) $< -o $@
@@ -22,4 +22,4 @@ clean:
@-rm $(OBJECTS)
mrproper: clean
- @-rm $(EXEC)
+ @-rm *.bin