<commit_before>/*  =========================================================================
    zmtp_dealer - DEALER socket class

    Copyright (c) contributors as noted in the AUTHORS file.
    This file is part of libzmtp, the C ZMTP stack.

    This Source Code Form is subject to the terms of the Mozilla Public
    License, v. 2.0. If a copy of the MPL was not distributed with this
    file, You can obtain one at http://mozilla.org/MPL/2.0/.
    =========================================================================
*/

#include "zmtp_classes.h"

//  Structure of our class

struct _zmtp_dealer_t {
    zmtp_channel_t *channel;
};


//  --------------------------------------------------------------------------
//  Constructor

zmtp_dealer_t *
zmtp_dealer_new ()
{
    zmtp_dealer_t *self = (zmtp_dealer_t *) zmalloc (sizeof *self);
    assert (self);              //  For now, memory exhaustion is fatal

    self->channel = NULL;
    return self;
}


//  --------------------------------------------------------------------------
//  Destructor

void
zmtp_dealer_destroy (zmtp_dealer_t **self_p)
{
    assert (self_p);

    if (*self_p) {
        zmtp_dealer_t *self = *self_p;
        zmtp_channel_destroy (&self->channel);
        free (self);
        *self_p = NULL;
    }
}


//  --------------------------------------------------------------------------
//

int
zmtp_dealer_ipc_connect (zmtp_dealer_t *self, const char *path)
{
    if (!self)
        return -1;
    if (self->channel)
        return -1;
    zmtp_channel_t *channel = zmtp_channel_new ();
    if (!channel)
        return -1;
    if (zmtp_channel_ipc_connect (channel, path) == -1) {
        zmtp_channel_destroy (&channel);
        return -1;
    }
    self->channel = channel;
    return 0;
}


//  --------------------------------------------------------------------------
//

int
zmtp_dealer_tcp_connect (zmtp_dealer_t *self,
                         const char *addr, unsigned short port)
{
    if (!self)
        return -1;
    if (self->channel)
        return -1;
    zmtp_channel_t *channel = zmtp_channel_new ();
    if (!channel)
        return -1;
    if (zmtp_channel_tcp_connect (channel, addr, port) == -1) {
        zmtp_channel_destroy (&channel);
        return -1;
    }
    self->channel = channel;
    return 0;
}


//  --------------------------------------------------------------------------
//

int
zmtp_dealer_send (zmtp_dealer_t *self, zmtp_msg_t *msg)
{
    assert (self);
    assert (self->channel);

    return zmtp_channel_send (self->channel, msg);
}


//  --------------------------------------------------------------------------
//

zmtp_msg_t *
zmtp_dealer_recv (zmtp_dealer_t *self)
{
    assert (self);
    assert (self->channel);

    return zmtp_channel_recv (self->channel);
}


//  --------------------------------------------------------------------------
//  Selftest

void
zmtp_dealer_test (bool verbose)
{
    printf (" * zmtp_dealer: ");
    //  @selftest
    //  @end
    printf ("OK\n");
}<commit_msg>Don't assert on incorrect use of public API<commit_after>/*  =========================================================================
    zmtp_dealer - DEALER socket class

    Copyright (c) contributors as noted in the AUTHORS file.
    This file is part of libzmtp, the C ZMTP stack.

    This Source Code Form is subject to the terms of the Mozilla Public
    License, v. 2.0. If a copy of the MPL was not distributed with this
    file, You can obtain one at http://mozilla.org/MPL/2.0/.
    =========================================================================
*/

#include "zmtp_classes.h"

//  Structure of our class

struct _zmtp_dealer_t {
    zmtp_channel_t *channel;    //  At most one channel per socket now
};


//  --------------------------------------------------------------------------
//  Constructor

zmtp_dealer_t *
zmtp_dealer_new ()
{
    zmtp_dealer_t *self = (zmtp_dealer_t *) zmalloc (sizeof *self);
    assert (self);              //  For now, memory exhaustion is fatal

    self->channel = NULL;
    return self;
}


//  --------------------------------------------------------------------------
//  Destructor

void
zmtp_dealer_destroy (zmtp_dealer_t **self_p)
{
    assert (self_p);

    if (*self_p) {
        zmtp_dealer_t *self = *self_p;
        zmtp_channel_destroy (&self->channel);
        free (self);
        *self_p = NULL;
    }
}


//  --------------------------------------------------------------------------
//

int
zmtp_dealer_ipc_connect (zmtp_dealer_t *self, const char *path)
{
    if (!self)
        return -1;
    if (self->channel)  //  At most one channel per socket now
        return -1;
    zmtp_channel_t *channel = zmtp_channel_new ();
    if (!channel)
        return -1;
    if (zmtp_channel_ipc_connect (channel, path) == -1) {
        zmtp_channel_destroy (&channel);
        return -1;
    }
    self->channel = channel;
    return 0;
}


//  --------------------------------------------------------------------------
//

int
zmtp_dealer_tcp_connect (zmtp_dealer_t *self,
                         const char *addr, unsigned short port)
{
    if (!self)
        return -1;
    if (self->channel)  //  At most one channel per socket now
        return -1;
    zmtp_channel_t *channel = zmtp_channel_new ();
    if (!channel)
        return -1;
    if (zmtp_channel_tcp_connect (channel, addr, port) == -1) {
        zmtp_channel_destroy (&channel);
        return -1;
    }
    self->channel = channel;
    return 0;
}


//  --------------------------------------------------------------------------
//  Send a message on a socket

int
zmtp_dealer_send (zmtp_dealer_t *self, zmtp_msg_t *msg)
{
    if (!self)
        return -1;
    if (!self->channel)
        return -1;
    return zmtp_channel_send (self->channel, msg);
}


//  --------------------------------------------------------------------------
//  Receive a message from a socket

zmtp_msg_t *
zmtp_dealer_recv (zmtp_dealer_t *self)
{
    if (!self)
        return NULL;
    if (!self->channel)
        return NULL;
    return zmtp_channel_recv (self->channel);
}


//  --------------------------------------------------------------------------
//  Selftest

void
zmtp_dealer_test (bool verbose)
{
    printf (" * zmtp_dealer: ");
    //  @selftest
    //  @end
    printf ("OK\n");
}
{'fraction_non_alphanumeric': 0.30067727201134037, 'fraction_numerical': 0.005355174043156403, 'mean_word_length': 1.466977466977467, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7274730', 'n_tokens_mistral': 1955, 'n_tokens_neox': 1889, 'n_words': 482}