Skip to content

Instantly share code, notes, and snippets.

@vyskocilm
Last active May 9, 2016
Embed
What would you like to do?
#!/bin/sh
set -x
gcc -std=c99 -Werror -ggdb demo.c -lczmq -lmlm -o test \
&& ./test
#include <malamute.h>
static const char *endpoint = "inproc://@/malamute-demo";
static const char *stream = "METRICS";
int main ()
{
zactor_t *server = zactor_new (mlm_server, "mlm_server_test");
zstr_sendx (server, "BIND", endpoint, NULL);
mlm_client_t *producer1 = mlm_client_new ();
mlm_client_connect (producer1, endpoint, 1000, "producer1");
mlm_client_set_producer (producer1, stream);
mlm_client_t *consumer1 = mlm_client_new ();
mlm_client_connect (consumer1, endpoint, 1000, "consumer1");
mlm_client_set_consumer (consumer1, stream, ".*");
mlm_client_t *consumer2 = mlm_client_new ();
mlm_client_connect (consumer2, endpoint, 1000, "consumer2");
mlm_client_set_consumer (consumer2, stream, ".*");
// PUB
mlm_client_sendx (producer1, "UPS1", "input.current", "12", "A", NULL);
// MAILBOX
mlm_client_sendtox (producer1, "consumer1", "[SUBJECT]", "hello", "MAILBOX!", NULL);
//RECEIVE
zmsg_t *msg = mlm_client_recv (consumer1);
zsys_debug ("sender=%s, command=%s\n", mlm_client_sender (consumer1), mlm_client_command (consumer1));
zmsg_print (msg);
zmsg_destroy (&msg);
msg = mlm_client_recv (consumer1);
zsys_debug ("sender=%s, command=%s\n", mlm_client_sender (consumer1), mlm_client_command (consumer1));
zmsg_print (msg);
zmsg_destroy (&msg);
//RECEIVE 2
msg = mlm_client_recv (consumer2);
zsys_debug ("sender=%s\n", mlm_client_sender (consumer2));
zsys_debug ("subject=%s\n", mlm_client_subject (consumer2));
zmsg_print (msg);
zmsg_destroy (&msg);
mlm_client_destroy (&consumer2);
mlm_client_destroy (&consumer1);
mlm_client_destroy (&producer1);
zactor_destroy (&server);
}
#include <czmq.h>
typedef struct _test_t {
char *msg;
uint64_t time;
} test_t;
/*
[static] typ navratove hodnoty
jmeno_funkce (typ1 jmeno1, typ2 jmeno2)
{
telo;
}
*/
test_t *
test_new (uint64_t time)
{
test_t *self = (test_t *) zmalloc (1 * sizeof(test_t));
assert (self);
self -> time = time;
return self;
}
void
test_destroy (test_t **self_p) {
assert(self_p);
if (*self_p){
test_t *self = *self_p;
zstr_free (&self->msg);
free(self);
*self_p = NULL;
}
}
// test_t (msg='hello', time=42)
void
test_print (test_t *self)
{
assert(self);
printf("msg: %s, time: %u \n", self -> msg, self -> time);
}
void
test_set_msg (test_t *self, const char *msg)
{
assert (self);
zstr_free (&self->msg);
self->msg = strdup (msg);
}
void
test_set_time (test_t *self, uint64_t time)
{
assert(self);
self -> time = time;
}
int main ()
{
// heap
// Test t = new Test ();
// t = Test ()
test_t *t = test_new (42);
// t.setMsg ("hello");
// t.msg = "hello"
test_set_msg (t, "hello");
test_print(t);
test_set_time(t,13);
test_print(t);
test_destroy (&t);
test_destroy (&t);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment