zproto_example(3)
=================

NAME
----
zproto_example - zproto example protocol

SYNOPSIS
--------
----
//  Create a new empty zproto_example
zproto_example_t *
    zproto_example_new (void);

//  Destroy a zproto_example instance
void
    zproto_example_destroy (zproto_example_t **self_p);

//  Receive a zproto_example from the socket. Returns 0 if OK, -1 if
//  there was an error. Blocks if there is no message waiting.
int
    zproto_example_recv (zproto_example_t *self, zsock_t *input);

//  Send the zproto_example to the output socket, does not destroy it
int
    zproto_example_send (zproto_example_t *self, zsock_t *output);


//  Print contents of message to stdout
void
    zproto_example_print (zproto_example_t *self);

//  Get/set the message routing id
zframe_t *
    zproto_example_routing_id (zproto_example_t *self);
void
    zproto_example_set_routing_id (zproto_example_t *self, zframe_t *routing_id);

//  Get the zproto_example id and printable command
int
    zproto_example_id (zproto_example_t *self);
void
    zproto_example_set_id (zproto_example_t *self, int id);
const char *
    zproto_example_command (zproto_example_t *self);

//  Get/set the sequence field
uint16_t
    zproto_example_sequence (zproto_example_t *self);
void
    zproto_example_set_sequence (zproto_example_t *self, uint16_t sequence);

//  Get/set the level field
byte
    zproto_example_level (zproto_example_t *self);
void
    zproto_example_set_level (zproto_example_t *self, byte level);

//  Get/set the event field
byte
    zproto_example_event (zproto_example_t *self);
void
    zproto_example_set_event (zproto_example_t *self, byte event);

//  Get/set the node field
uint16_t
    zproto_example_node (zproto_example_t *self);
void
    zproto_example_set_node (zproto_example_t *self, uint16_t node);

//  Get/set the peer field
uint16_t
    zproto_example_peer (zproto_example_t *self);
void
    zproto_example_set_peer (zproto_example_t *self, uint16_t peer);

//  Get/set the time field
uint64_t
    zproto_example_time (zproto_example_t *self);
void
    zproto_example_set_time (zproto_example_t *self, uint64_t time);

//  Get/set the host field
const char *
    zproto_example_host (zproto_example_t *self);
void
    zproto_example_set_host (zproto_example_t *self, const char *value);

//  Get/set the data field
const char *
    zproto_example_data (zproto_example_t *self);
void
    zproto_example_set_data (zproto_example_t *self, const char *value);

//  Get/set the aliases field
zlist_t *
    zproto_example_aliases (zproto_example_t *self);
//  Get the aliases field and transfer ownership to caller
zlist_t *
    zproto_example_get_aliases (zproto_example_t *self);
//  Set the aliases field, transferring ownership from caller
void
    zproto_example_set_aliases (zproto_example_t *self, zlist_t **aliases_p);

//  Get a copy of the headers field
zhash_t *
    zproto_example_headers (zproto_example_t *self);
//  Get the headers field and transfer ownership to caller
zhash_t *
    zproto_example_get_headers (zproto_example_t *self);
//  Set the headers field, transferring ownership from caller
void
    zproto_example_set_headers (zproto_example_t *self, zhash_t **hash_p);

//  Get/set the flags field
byte *
    zproto_example_flags (zproto_example_t *self);
void
    zproto_example_set_flags (zproto_example_t *self, byte *flags);

//  Get a copy of the public_key field
zchunk_t *
    zproto_example_public_key (zproto_example_t *self);
//  Get the public_key field and transfer ownership to caller
zchunk_t *
    zproto_example_get_public_key (zproto_example_t *self);
//  Set the public_key field, transferring ownership from caller
void
    zproto_example_set_public_key (zproto_example_t *self, zchunk_t **chunk_p);

//  Get/set the identifier field
zuuid_t *
    zproto_example_identifier (zproto_example_t *self);
void
    zproto_example_set_identifier (zproto_example_t *self, zuuid_t *uuid);
//  Get the identifier field and transfer ownership to caller
zuuid_t *
    zproto_example_get_identifier (zproto_example_t *self);

//  Get a copy of the address field
zframe_t *
    zproto_example_address (zproto_example_t *self);
//  Get the address field and transfer ownership to caller
zframe_t *
    zproto_example_get_address (zproto_example_t *self);
//  Set the address field, transferring ownership from caller
void
    zproto_example_set_address (zproto_example_t *self, zframe_t **frame_p);

//  Get a copy of the content field
zmsg_t *
    zproto_example_content (zproto_example_t *self);
//  Get the content field and transfer ownership to caller
zmsg_t *
    zproto_example_get_content (zproto_example_t *self);
//  Set the content field, transferring ownership from caller
void
    zproto_example_set_content (zproto_example_t *self, zmsg_t **msg_p);

//  Get/set the client_forename field
const char *
    zproto_example_client_forename (zproto_example_t *self);
void
    zproto_example_set_client_forename (zproto_example_t *self, const char *value);

//  Get/set the client_surname field
const char *
    zproto_example_client_surname (zproto_example_t *self);
void
    zproto_example_set_client_surname (zproto_example_t *self, const char *value);

//  Get/set the client_mobile field
const char *
    zproto_example_client_mobile (zproto_example_t *self);
void
    zproto_example_set_client_mobile (zproto_example_t *self, const char *value);

//  Get/set the client_email field
const char *
    zproto_example_client_email (zproto_example_t *self);
void
    zproto_example_set_client_email (zproto_example_t *self, const char *value);

//  Get/set the supplier_forename field
const char *
    zproto_example_supplier_forename (zproto_example_t *self);
void
    zproto_example_set_supplier_forename (zproto_example_t *self, const char *value);

//  Get/set the supplier_surname field
const char *
    zproto_example_supplier_surname (zproto_example_t *self);
void
    zproto_example_set_supplier_surname (zproto_example_t *self, const char *value);

//  Get/set the supplier_mobile field
const char *
    zproto_example_supplier_mobile (zproto_example_t *self);
void
    zproto_example_set_supplier_mobile (zproto_example_t *self, const char *value);

//  Get/set the supplier_email field
const char *
    zproto_example_supplier_email (zproto_example_t *self);
void
    zproto_example_set_supplier_email (zproto_example_t *self, const char *value);

//  Self test of this class
void
    zproto_example_test (bool verbose);
----

DESCRIPTION
-----------

zproto_example - zproto example protocol


EXAMPLE
-------
.From zproto_example_test method
----
    //  Simple create/destroy test
    zproto_example_t *self = zproto_example_new ();
    assert (self);
    zproto_example_destroy (&self);
    //  Create pair of sockets we can send through
    //  We must bind before connect if we wish to remain compatible with ZeroMQ < v4
    zsock_t *output = zsock_new (ZMQ_DEALER);
    assert (output);
    int rc = zsock_bind (output, "inproc://selftest-zproto_example");
    assert (rc == 0);

    zsock_t *input = zsock_new (ZMQ_ROUTER);
    assert (input);
    rc = zsock_connect (input, "inproc://selftest-zproto_example");
    assert (rc == 0);


    //  Encode/send/decode and verify each message type
    int instance;
    self = zproto_example_new ();
    zproto_example_set_id (self, ZPROTO_EXAMPLE_LOG);

    zproto_example_set_sequence (self, 123);
    zproto_example_set_level (self, 2);
    zproto_example_set_event (self, 3);
    zproto_example_set_node (self, 45536);
    zproto_example_set_peer (self, 65535);
    zproto_example_set_time (self, 1427261426);
    zproto_example_set_host (self, "localhost");
    zproto_example_set_data (self, "This is the message to log");
    //  Send twice
    zproto_example_send (self, output);
    zproto_example_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        zproto_example_recv (self, input);
        assert (zproto_example_routing_id (self));
        assert (zproto_example_sequence (self) == 123);
        assert (zproto_example_level (self) == 2);
        assert (zproto_example_event (self) == 3);
        assert (zproto_example_node (self) == 45536);
        assert (zproto_example_peer (self) == 65535);
        assert (zproto_example_time (self) == 1427261426);
        assert (streq (zproto_example_host (self), "localhost"));
        assert (streq (zproto_example_data (self), "This is the message to log"));
    }
    zproto_example_set_id (self, ZPROTO_EXAMPLE_STRUCTURES);

    zproto_example_set_sequence (self, 123);
    zlist_t *structures_aliases = zlist_new ();
    zlist_append (structures_aliases, "First alias");
    zlist_append (structures_aliases, "Second alias");
    zlist_append (structures_aliases, "Third alias");
    zproto_example_set_aliases (self, &structures_aliases);
    zhash_t *structures_headers = zhash_new ();
    zhash_insert (structures_headers, "endpoint", "tcp://localhost:5665");
    zproto_example_set_headers (self, &structures_headers);
    //  Send twice
    zproto_example_send (self, output);
    zproto_example_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        zproto_example_recv (self, input);
        assert (zproto_example_routing_id (self));
        assert (zproto_example_sequence (self) == 123);
        zlist_t *aliases = zproto_example_get_aliases (self);
        assert (aliases);
        assert (streq ((char *) zlist_first (aliases), "First alias"));
        assert (streq ((char *) zlist_next (aliases), "Second alias"));
        assert (streq ((char *) zlist_next (aliases), "Third alias"));
        zlist_destroy (&aliases);
        zlist_destroy (&structures_aliases);
        zhash_t *headers = zproto_example_get_headers (self);
        assert (streq ((char *) zhash_first (headers), "tcp://localhost:5665"));
        assert (streq ((char *) zhash_cursor (headers), "endpoint"));
        zhash_destroy (&headers);
        if (instance == 1)
            zhash_destroy (&structures_headers);
    }
    zproto_example_set_id (self, ZPROTO_EXAMPLE_BINARY);

    zproto_example_set_sequence (self, 123);
    byte flags_data [ZPROTO_EXAMPLE_FLAGS_SIZE] = "b38c";
    zproto_example_set_flags (self, flags_data);
    zchunk_t *binary_public_key = zchunk_new ("89f5ffe70d747869dfe8", 20);
    zproto_example_set_public_key (self, &binary_public_key);
    zuuid_t *binary_identifier = zuuid_new ();
    zuuid_set_str (binary_identifier, "3a60e6850a1e4cc15f3bfd4b42bc6b3e");
    zproto_example_set_identifier (self, binary_identifier);
    zframe_t *binary_address = zframe_new ("0206f99f6137d9fe380f", 20);
    zproto_example_set_address (self, &binary_address);
    zmsg_t *binary_content = zmsg_new ();
    zproto_example_set_content (self, &binary_content);
    zmsg_addstr (zproto_example_content (self), "728a92c6749235ba7002");
    //  Send twice
    zproto_example_send (self, output);
    zproto_example_send (self, output);

    for (instance = 0; instance < 2; instance++) {
        zproto_example_recv (self, input);
        assert (zproto_example_routing_id (self));
        assert (zproto_example_sequence (self) == 123);
        assert (memcmp (zproto_example_flags (self), "b38c", ZPROTO_EXAMPLE_FLAGS_SIZE) == 0);
        assert (memcmp (zchunk_data (zproto_example_public_key (self)), "89f5ffe70d747869dfe8", 20) == 0);
        if (instance == 1)
            zchunk_destroy (&binary_public_key);
        assert (zuuid_eq (binary_identifier, zuuid_data (zproto_example_identifier (self))));
        if (instance == 1)
            zuuid_destroy (&binary_identifier);
        assert (zframe_streq (zproto_example_address (self), "0206f99f6137d9fe380f"));
        if (instance == 1)
            zframe_destroy (&binary_address);
        assert (zmsg_size (zproto_example_content (self)) == 1);
        char *content = zmsg_popstr (zproto_example_content (self));
        assert (streq (content, "728a92c6749235ba7002"));
        zstr_free (&content);
        if (instance == 1)
            zmsg_destroy (&binary_content);
    }
    zproto_example_set_id (self, ZPROTO_EXAMPLE_TYPES);

    zproto_example_set_sequence (self, 123);
    zproto_example_set_client_forename (self, "Lucius Junius");
    zproto_example_set_client_surname (self, "Brutus");
    zproto_example_set_client_mobile (self, "01234567890");
----

SEE ALSO
--------
link:zproto[7]
