Commit 4850f887 authored by Felix Hamann's avatar Felix Hamann

first c client implementation (sparse options)

parent a836a004
......@@ -2,20 +2,23 @@
ACLOCAL_AMFLAGS = -I m4
AM_CFLAGS = -g -Werror -Wall -Wextra -pedantic -std=gnu99
samwise_LDFLAGS = \
-lzmq \
lib_LTLIBRARIES = src/libsamwise.la
include_HEADERS = include/samwise.h
src_libsamwise_la_SOURCES = \
include/samwise.h \
src/samwise.c
src_libsamwise_la_LDFLAGS = \
-lzmq \
-lczmq
#
# BINARIES
#
bin_PROGRAMS = samwise
bin_PROGRAMS = samcli
#
# samd
#
samwise_SOURCES = \
include/samwise.h \
src/samwise.c
samcli_SOURCES = src/samcli.c
samcli_LDADD = src/libsamwise.la
samcli_LDFLAGS = -static
......@@ -11,6 +11,7 @@ AM_INIT_AUTOMAKE([subdir-objects])
AC_CONFIG_MACRO_DIR([m4])
AM_PROG_CC_C_O
AC_PROG_INSTALL
LT_INIT
# Checks for libraries.
# FIXME: Replace `main' with a function in `-lczmq':
......
......@@ -22,13 +22,60 @@
#define __SAMWISE_H__
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define UU __attribute__((unused))
typedef struct samwise_t samwise_t;
/// publishing request options
typedef struct samwise_pub_t {
char *exchange;
char *routing_key;
char *msg;
size_t size;
} samwise_pub_t;
// --------------------------------------------------------------------------
/// @brief Publish a message to samd
/// @param self A samwise instance
/// @param pub Publishing options
/// @return 0 if success, -1 otherwise
int
samwise_publish (
samwise_t *self,
samwise_pub_t *pub);
// --------------------------------------------------------------------------
/// @brief Send a ping to samwise
/// @param self A samwise instance
/// @return 0 if success, -1 otherwise
int
samwise_ping (
samwise_t *self);
// --------------------------------------------------------------------------
/// @brief Create a new samwise instance
/// @return The newly created instance
samwise_t *
samwise_new ();
// --------------------------------------------------------------------------
/// @brief Destroy a samwise instance
/// @param self Pointer to be free'd and nullified
void
samwise_destroy (
samwise_t **self);
......
File added
/* =========================================================================
samwise - best effort store and forward message publishing
This Source Code Form is subject to the terms of the MIT
License. If a copy of the MIT License was not distributed with
this file, You can obtain one at http://opensource.org/licenses/MIT
=========================================================================
*/
/**
@brief samwise c client
@file samwise.c
*/
#include "../include/samwise.h"
// --------------------------------------------------------------------------
/// Print usage information.
static void
usage ()
{
printf (
"samcli CMD\n"
"CMD = ping|publish\n\n");
}
// --------------------------------------------------------------------------
/// Publish n messages to samd.
static void
publish (
samwise_t *sam,
int amount)
{
assert (amount > 0);
int count = 1;
while (count <= amount) {
char buf [64];
snprintf (buf, 64, "message no %d", count);
samwise_pub_t pub = {
.exchange = "amq.direct",
.routing_key = "",
.size = strlen (buf),
.msg = buf
};
printf ("publishing message %d\n", count);
int rc = samwise_publish (sam, &pub);
if (rc) {
fprintf (stderr, "publishing failed\n");
}
count += 1;
}
}
// --------------------------------------------------------------------------
/// Entry point.
int
main (
int arg_c,
char **arg_v)
{
arg_c -= 1;
arg_v += 1;
if (arg_c < 1) {
usage ();
return 2;
}
samwise_t *sam = samwise_new ();
if (!sam) {
fprintf (stderr, "an error occured, exiting\n");
return 2;
}
if (!strcmp (arg_v [0], "ping")) {
samwise_ping (sam);
}
else if (!strcmp (arg_v [0], "publish")) {
publish (sam, 2);
}
else {
usage ();
}
samwise_destroy (&sam);
return 0;
}
......@@ -10,7 +10,7 @@
*/
/**
@brief samwise c client
@brief samwise c client library
@file samwise.c
......@@ -21,37 +21,130 @@
#include <czmq.h>
/// samwise state
struct samwise_t {
int TMP;
zsock_t *req; ///< request socket communicating with samd
};
// --------------------------------------------------------------------------
/// Read the response from samd and print any errors that may arise.
static int
handle_response (
samwise_t *self)
{
int code;
char *msg;
zsock_recv (self->req, "is", &code, &msg);
if (code) {
fprintf (stderr, "received error '%d': %s\n", code, msg);
}
free (msg);
return code;
}
// --------------------------------------------------------------------------
/// Creates an empty message and appends the protocol version.
static zmsg_t *
create_msg ()
{
zmsg_t *msg = zmsg_new ();
zmsg_addstr (msg, "100");
return msg;
}
// --------------------------------------------------------------------------
/// Publish a message to samd.
int
samwise_publish (
samwise_t *self,
samwise_pub_t *pub)
{
zmsg_t *msg = create_msg ();
zmsg_addstr (msg, "publish");
zmsg_addstr (msg, "round robin");
zmsg_addstr (msg, pub->exchange);
zmsg_addstr (msg, pub->routing_key);
zmsg_addstr (msg, "0"); // mandatory
zmsg_addstr (msg, "0"); // immediate
// options
zmsg_addstr (msg, "12");
zmsg_addmem (msg, NULL, 0);
zmsg_addmem (msg, NULL, 0);
zmsg_addmem (msg, NULL, 0);
zmsg_addmem (msg, NULL, 0);
zmsg_addmem (msg, NULL, 0);
zmsg_addmem (msg, NULL, 0);
zmsg_addmem (msg, NULL, 0);
zmsg_addmem (msg, NULL, 0);
zmsg_addmem (msg, NULL, 0);
zmsg_addmem (msg, NULL, 0);
zmsg_addmem (msg, NULL, 0);
zmsg_addmem (msg, NULL, 0);
// header
zmsg_addstr (msg, "0");
// payload
zmsg_addmem (msg, pub->msg, pub->size);
printf ("publishing message of size %zu\n", pub->size);
zmsg_send (&msg, self->req);
return handle_response (self);
}
// --------------------------------------------------------------------------
/// Ping samd.
int
samwise_ping (
samwise_t *self)
{
printf ("send ping request\n");
zmsg_t *msg = create_msg ();
zmsg_addstr (msg, "ping");
zmsg_send (&msg, self->req);
return handle_response (self);
}
// --------------------------------------------------------------------------
/// Create a new samwise instance and connect to samd's endpoint.
samwise_t *
samwise_new ()
{
samwise_t *self = malloc (sizeof (samwise_t));
assert (self);
self->TMP = 0;
self->req = zsock_new_req ("ipc://../../sam_ipc");
if (!self->req) {
fprintf (stderr, "could not connect to endpoint\n");
return NULL;
}
return self;
}
// --------------------------------------------------------------------------
/// Destroy a samwise instance.
void
samwise_destroy (
samwise_t **self)
{
assert (*self);
zsock_destroy (&(*self)->req);
free (*self);
*self = NULL;
}
int
main (void)
{
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment