示例#1
0
文件: zsys.c 项目: wysman/czmq
void
zsys_set_interface (const char *value)
{
    zsys_init ();
    free (s_interface);
    s_interface = strdup (value);
}
示例#2
0
void
zargs_test (bool verbose)
{
    zsys_init ();
    printf (" * zargs: ");

    //  @selftest
    //  Simple create/destroy test
    
    char *argv1[] = {"progname", "--named1", "-n1", "val1", "positional1", "--with", "value", "--with2=value2", "-W3value3", "--", "--thisis", "considered", "positional", NULL};

    zargs_t *self = zargs_new (13, argv1);
    assert (self);

    assert (streq (zargs_progname (self), "progname"));
    assert (streq (zargs_first (self), "positional1"));
    assert (streq (zargs_next (self), "--thisis"));
    assert (streq (zargs_next (self), "considered"));
    assert (streq (zargs_next (self), "positional"));
    assert (!zargs_next (self));

    assert (zargs_param_empty (zargs_param_lookup (self, "--named1")));
    assert (!zargs_param_empty (zargs_param_lookup (self, "-n1")));
    assert (streq (zargs_param_lookupx (self, "--not at all", "-n1", NULL), "val1"));
    // TODO: this does not look like an easy hack w/o allocating extra memory
    //       ???
    //assert (streq (zargs_param_lookup (self, "--with", NULL), "value2"));

    zargs_destroy (&self);
    //  @end
    printf ("OK\n");
}
示例#3
0
文件: zsys.c 项目: wysman/czmq
void
zsys_set_ipv6 (int ipv6)
{
    zsys_init ();
    ZMUTEX_LOCK (s_mutex);
    s_ipv6 = ipv6;
    ZMUTEX_UNLOCK (s_mutex);
}
示例#4
0
文件: zsys.c 项目: wysman/czmq
void
zsys_set_rcvhwm (size_t rcvhwm)
{
    zsys_init ();
    ZMUTEX_LOCK (s_mutex);
    s_rcvhwm = rcvhwm;
    ZMUTEX_UNLOCK (s_mutex);
}
示例#5
0
文件: zsys.c 项目: wysman/czmq
void
zsys_set_pipehwm (size_t pipehwm)
{
    zsys_init ();
    ZMUTEX_LOCK (s_mutex);
    s_pipehwm = pipehwm;
    ZMUTEX_UNLOCK (s_mutex);
}
示例#6
0
文件: zsys.c 项目: wysman/czmq
void
zsys_set_sndhwm (size_t sndhwm)
{
    zsys_init ();
    ZMUTEX_LOCK (s_mutex);
    s_sndhwm = sndhwm;
    ZMUTEX_UNLOCK (s_mutex);
}
示例#7
0
文件: zsys.c 项目: wysman/czmq
void
zsys_set_linger (size_t linger)
{
    zsys_init ();
    ZMUTEX_LOCK (s_mutex);
    s_linger = linger;
    ZMUTEX_UNLOCK (s_mutex);
}
示例#8
0
文件: zsys.c 项目: wysman/czmq
void
zsys_set_logsystem (bool logsystem)
{
    zsys_init ();
    s_logsystem = logsystem;
#if defined (__UNIX__)
    if (s_logsystem)
        openlog (s_logident, LOG_PID, LOG_USER);
#elif defined (__WINDOWS__)
    //  TODO: hook into Windows event log
#endif
}
示例#9
0
文件: zsys.c 项目: wysman/czmq
void
zsys_set_max_sockets (size_t max_sockets)
{
    zsys_init ();
    ZMUTEX_LOCK (s_mutex);
    //  If the app is misusing this method, burn it with fire
    if (s_open_sockets)
        zsys_error ("zsys_max_sockets() is not valid after creating sockets");
    assert (s_open_sockets == 0);
    s_max_sockets = max_sockets ? max_sockets : zsys_socket_limit ();
    ZMUTEX_UNLOCK (s_mutex);
}
示例#10
0
文件: zsys.c 项目: wysman/czmq
void
zsys_set_logident (const char *value)
{
    zsys_init ();
    free (s_logident);
    s_logident = strdup (value);
#if defined (__UNIX__)
    if (s_logsystem)
        openlog (s_logident, LOG_PID, LOG_USER);
#elif defined (__WINDOWS__)
    //  TODO: hook in Windows event log for Windows
#endif
}
int graylog_forwarder_run_controller_loop(zconfig_t* config, zlist_t* devices, zlist_t *subscriptions, int rcv_hwm, int send_hwm)
{
    set_thread_name("controller");

    zsys_init();

    controller_state_t state = {.config = config};
    bool start_up_complete = controller_create_actors(&state, devices, subscriptions, rcv_hwm, send_hwm);

    if (!start_up_complete)
        goto exit;

    // set up event loop
    zloop_t *loop = zloop_new();
    assert(loop);
    zloop_set_verbose(loop, 0);

    // send tick commands every second
    int rc = zloop_timer(loop, 1000, 1, send_tick_commands, &state);
    assert(rc != -1);

    // run the loop
    // when running under the google profiler, zmq_poll terminates with EINTR
    // so we keep the loop running in this case
    if (!zsys_interrupted) {
        bool should_continue_to_run = getenv("CPUPROFILE") != NULL;
        do {
            rc = zloop_start(loop);
            should_continue_to_run &= errno == EINTR && !zsys_interrupted;
            log_zmq_error(rc, __FILE__, __LINE__);
        } while (should_continue_to_run);
    }
    printf("[I] controller: shutting down\n");

    // shutdown
    zloop_destroy(&loop);
    assert(loop == NULL);

 exit:
    printf("[I] controller: destroying actor threads\n");
    controller_destroy_actors(&state);
    printf("[I] controller: calling zsys_shutdown\n");
    zsys_shutdown();

    printf("[I] controller: terminated\n");
    return 0;
}
示例#12
0
文件: zsys.c 项目: wysman/czmq
void
zsys_set_io_threads (size_t io_threads)
{
    zsys_init ();
    ZMUTEX_LOCK (s_mutex);
    if (s_open_sockets)
        zsys_error ("zsys_io_threads() is not valid after creating sockets");
    assert (s_open_sockets == 0);
    zmq_term (s_process_ctx);
    s_io_threads = io_threads;
    s_process_ctx = zmq_init ((int) s_io_threads);
#if (ZMQ_VERSION >= ZMQ_MAKE_VERSION (3, 2, 0))
    //  TODO: this causes TravisCI to break; libzmq does not return a
    //  valid socket on zmq_socket(), after this...
    zmq_ctx_set (s_process_ctx, ZMQ_MAX_SOCKETS, s_max_sockets);
#endif
    ZMUTEX_UNLOCK (s_mutex);
}
示例#13
0
文件: zsys.c 项目: wysman/czmq
void
zsys_set_logsender (const char *endpoint)
{
    zsys_init ();
    if (endpoint) {
        //  Create log sender if needed
        if (!s_logsender) {
            s_logsender = zsys_socket (ZMQ_PUB, NULL, 0);
            assert (s_logsender);
        }
        //  Bind to specified endpoint
        int rc = zmq_bind (s_logsender, endpoint);
        assert (rc == 0);
    }
    else
    if (s_logsender) {
        zsys_close (s_logsender, NULL, 0);
        s_logsender = NULL;
    }
}
示例#14
0
文件: zsys.c 项目: wysman/czmq
void *
zsys_socket (int type, const char *filename, size_t line_nbr)
{
    //  First time initialization; if the application is mixing
    //  its own threading calls with zsock, this may fail if two
    //  threads try to create sockets at the same time. In such
    //  apps, they MUST create a socket in the main program before
    //  starting any threads. If the app uses zactor for its threads
    //  then we can guarantee this to always be safe.
    zsys_init ();
    ZMUTEX_LOCK (s_mutex);
    void *handle = zmq_socket (s_process_ctx, type);
    //  Configure socket with process defaults
    zsocket_set_linger (handle, (int) s_linger);
#if (ZMQ_VERSION_MAJOR == 2)
    //  For ZeroMQ/2.x we use sndhwm for both send and receive
    zsocket_set_hwm (handle, s_sndhwm);
#else
    //  For later versions we use separate SNDHWM and RCVHWM
    zsocket_set_sndhwm (handle, (int) s_sndhwm);
    zsocket_set_rcvhwm (handle, (int) s_rcvhwm);
#   if defined (ZMQ_IPV6)
    zsocket_set_ipv6 (handle, s_ipv6);
#   else
    zsocket_set_ipv4only (handle, s_ipv6 ? 0 : 1);
#   endif
#endif
    //  Add socket to reference tracker so we can report leaks; this is
    //  done only when the caller passes a filename/line_nbr
    if (filename) {
        s_sockref_t *sockref = (s_sockref_t *) malloc (sizeof (s_sockref_t));
        sockref->handle = handle;
        sockref->type = type;
        sockref->filename = filename;
        sockref->line_nbr = line_nbr;
        zlist_append (s_sockref_list, sockref);
    }
    s_open_sockets++;
    ZMUTEX_UNLOCK (s_mutex);
    return handle;
}
示例#15
0
文件: zs.c 项目: bgruenefeld/zs
int main (int argc, char *argv [])
{
    int argn = 1;
    bool verbose = false;
    if (argc > argn && streq (argv [argn], "-v")) {
        verbose = true;
        argn++;
    }
    if (argc > argn && streq (argv [argn], "-h")) {
        puts ("Usage: zs [ -v ]");
        return 0;
    }
    //  Main thread is read/parse/execute input text
    zsys_init ();
    zs_repl_t *repl = zs_repl_new ();
    zs_repl_verbose (repl, verbose);

    while (!zctx_interrupted) {
        char input [1024 + 2];          //  1024 chars + LF + null
        if (!fgets (input, 1026, stdin))
            break;

        if (zs_repl_execute (repl, input) == 0) {
            if (zs_repl_completed (repl)) {
                char *results = zs_repl_results (repl);
                printf ("[%s]\n", results);
                zstr_free (&results);
            }
        }
        else {
            printf ("%*c\n", zs_repl_offset (repl), '^');
            puts ("Syntax error");
        }
    }
    zs_repl_destroy (&repl);
    return 0;
}
示例#16
0
文件: QmlZsys.cpp 项目: taotetek/czmq
///
//  Initialize CZMQ zsys layer; this happens automatically when you create
//  a socket or an actor; however this call lets you force initialization
//  earlier, so e.g. logging is properly set-up before you start working.
//  Not threadsafe, so call only from main thread. Safe to call multiple
//  times. Returns global CZMQ context.
void *QmlZsysAttached::init () {
    return zsys_init ();
};
示例#17
0
文件: zsys.c 项目: wysman/czmq
void
zsys_set_logstream (FILE *stream)
{
    zsys_init ();
    s_logstream = stream;
}
示例#18
0
int main(int argc, char * const *argv)
{
    int rc = 0;
    process_arguments(argc, argv);

    setvbuf(stdout, NULL, _IOLBF, 0);
    setvbuf(stderr, NULL, _IOLBF, 0);

    if (!quiet)
        printf("[I] started %s\n"
               "[I] sub-port:    %d\n"
               "[I] push-port:   %d\n"
               "[I] io-threads:  %lu\n"
               "[I] rcv-hwm:  %d\n"
               "[I] snd-hwm:  %d\n"
               , argv[0], pull_port, pub_port, io_threads, rcv_hwm, snd_hwm);

    // load config
    config_file_exists = zsys_file_exists(config_file_name);
    if (config_file_exists) {
        config_file_init();
        config = zconfig_load((char*)config_file_name);
    }

    // set global config
    zsys_init();
    zsys_set_rcvhwm(10000);
    zsys_set_sndhwm(10000);
    zsys_set_pipehwm(1000);
    zsys_set_linger(100);
    zsys_set_io_threads(io_threads);

    // create socket to receive messages on
    zsock_t *receiver = zsock_new(ZMQ_SUB);
    assert_x(receiver != NULL, "sub socket creation failed", __FILE__, __LINE__);
    zsock_set_rcvhwm(receiver, rcv_hwm);

    // bind externally
    char* host = zlist_first(hosts);
    while (host) {
        if (!quiet)
            printf("[I] connecting to: %s\n", host);
        rc = zsock_connect(receiver, "%s", host);
        assert_x(rc == 0, "sub socket connect failed", __FILE__, __LINE__);
        host = zlist_next(hosts);
    }
    tracker = device_tracker_new(hosts, receiver);

    // create socket for publishing
    zsock_t *publisher = zsock_new(ZMQ_PUSH);
    assert_x(publisher != NULL, "pub socket creation failed", __FILE__, __LINE__);
    zsock_set_sndhwm(publisher, snd_hwm);

    rc = zsock_bind(publisher, "tcp://%s:%d", "*", pub_port);
    assert_x(rc == pub_port, "pub socket bind failed", __FILE__, __LINE__);

    // create compressor sockets
    zsock_t *compressor_input = zsock_new(ZMQ_PUSH);
    assert_x(compressor_input != NULL, "compressor input socket creation failed", __FILE__, __LINE__);
    rc = zsock_bind(compressor_input, "inproc://compressor-input");
    assert_x(rc==0, "compressor input socket bind failed", __FILE__, __LINE__);

    zsock_t *compressor_output = zsock_new(ZMQ_PULL);
    assert_x(compressor_output != NULL, "compressor output socket creation failed", __FILE__, __LINE__);
    rc = zsock_bind(compressor_output, "inproc://compressor-output");
    assert_x(rc==0, "compressor output socket bind failed", __FILE__, __LINE__);

    // create compressor agents
    zactor_t *compressors[MAX_COMPRESSORS];
    for (size_t i = 0; i < num_compressors; i++)
        compressors[i] = message_decompressor_new(i);

    // set up event loop
    zloop_t *loop = zloop_new();
    assert(loop);
    zloop_set_verbose(loop, 0);

    // calculate statistics every 1000 ms
    int timer_id = zloop_timer(loop, 1000, 0, timer_event, NULL);
    assert(timer_id != -1);

    // setup handler for the receiver socket
    publisher_state_t publisher_state = {
        .receiver = zsock_resolve(receiver),
        .publisher = zsock_resolve(publisher),
        .compressor_input = zsock_resolve(compressor_input),
        .compressor_output = zsock_resolve(compressor_output),
    };

    // setup handler for compression results
    rc = zloop_reader(loop, compressor_output, read_zmq_message_and_forward, &publisher_state);
    assert(rc == 0);
    zloop_reader_set_tolerant(loop, compressor_output);

    // setup handdler for messages incoming from the outside or rabbit_listener
    rc = zloop_reader(loop, receiver, read_zmq_message_and_forward, &publisher_state);
    assert(rc == 0);
    zloop_reader_set_tolerant(loop, receiver);

    // initialize clock
    global_time = zclock_time();

    // setup subscriptions
    if (subscriptions == NULL || zlist_size(subscriptions) == 0) {
        if (!quiet)
            printf("[I] subscribing to all log messages\n");
        zsock_set_subscribe(receiver, "");
    } else {
        char *subscription = zlist_first(subscriptions);
        while (subscription) {
            if (!quiet)
                printf("[I] subscribing to %s\n", subscription);
            zsock_set_subscribe(receiver, subscription);
            subscription = zlist_next(subscriptions);
        }
        zsock_set_subscribe(receiver, "heartbeat");
    }

    // run the loop
    if (!zsys_interrupted) {
        if (verbose)
            printf("[I] starting main event loop\n");
        bool should_continue_to_run = getenv("CPUPROFILE") != NULL;
        do {
            rc = zloop_start(loop);
            should_continue_to_run &= errno == EINTR && !zsys_interrupted;
            log_zmq_error(rc, __FILE__, __LINE__);
        } while (should_continue_to_run);
        if (verbose)
            printf("[I] main event zloop terminated with return code %d\n", rc);
    }

    zloop_destroy(&loop);
    assert(loop == NULL);

    if (!quiet) {
        printf("[I] received %zu messages\n", received_messages_count);
        printf("[I] shutting down\n");
    }

    zlist_destroy(&hosts);
    zlist_destroy(&subscriptions);
    zsock_destroy(&receiver);
    zsock_destroy(&publisher);
    zsock_destroy(&compressor_input);
    zsock_destroy(&compressor_output);
    device_tracker_destroy(&tracker);
    for (size_t i = 0; i < num_compressors; i++)
        zactor_destroy(&compressors[i]);
    zsys_shutdown();

    if (!quiet)
        printf("[I] terminated\n");

    return rc;
}
示例#19
0
int main (int argc, char *argv [])
{
    puts (PRODUCT);
    puts (COPYRIGHT);
    puts (NOWARRANTY);

    int argn = 1;
    bool verbose = false;
    bool force_foreground = false;
    if (argc > argn && streq (argv [argn], "-v")) {
        verbose = true;
        argn++;
    }
    if (argc > argn && streq (argv [argn], "-f")) {
        force_foreground = true;
        argn++;
    }
    if (argc > argn && streq (argv [argn], "-h")) {
        puts ("Usage: malamute [ -v ] [ -f ] [ -h | config-file ]");
        puts ("  Default config-file is 'malamute.cfg'");
        return 0;
    }
    //  Collect configuration file name
    const char *config_file = "malamute.cfg";
    if (argc > argn) {
        config_file = argv [argn];
        argn++;
    }
    //  Send logging to system facility as well as stdout
    zsys_init ();
    zsys_set_logsystem (true);
    zsys_set_pipehwm (0);
    zsys_set_sndhwm (0);
    zsys_set_rcvhwm (0);

    //  Load config file for our own use here
    zsys_info ("starting Malamute using config in '%s'", config_file);
    zconfig_t *config = zconfig_load (config_file);
    if (config) {
        //  Do we want to run broker in the background?
        int as_daemon = !force_foreground && atoi (zconfig_resolve (config, "server/background", "0"));
        const char *workdir = zconfig_resolve (config, "server/workdir", ".");
        if (as_daemon) {
            zsys_info ("switching Malamute to background...");
            if (zsys_daemonize (workdir))
                return -1;
        }
        //  Switch to user/group to run process under, if any
        if (zsys_run_as (
            zconfig_resolve (config, "server/lockfile", NULL),
            zconfig_resolve (config, "server/group", NULL),
            zconfig_resolve (config, "server/user", NULL)))
            return -1;
    }
    else {
        zsys_error ("cannot load config file '%s'\n", config_file);
        return 1;
    }
    //  Install authenticator (NULL or PLAIN)
    zactor_t *auth = zactor_new (zauth, NULL);
    assert (auth);

    if (verbose || atoi (zconfig_resolve (config, "server/auth/verbose", "0"))) {
        zstr_sendx (auth, "VERBOSE", NULL);
        zsock_wait (auth);
    }
    //  Do PLAIN password authentication if requested
    const char *passwords = zconfig_resolve (config, "server/auth/plain", NULL);
    if (passwords) {
        zstr_sendx (auth, "PLAIN", passwords, NULL);
        zsock_wait (auth);
    }
    //  Start Malamute server instance
    zactor_t *server = zactor_new (mlm_server, "Malamute");
    if (verbose)
        zstr_send (server, "VERBOSE");
    zstr_sendx (server, "LOAD", config_file, NULL);

    //  Accept and print any message back from server
    while (true) {
        char *message = zstr_recv (server);
        if (message) {
            puts (message);
            free (message);
        }
        else {
            puts ("interrupted");
            break;
        }
    }
    //  Shutdown all services
    zactor_destroy (&server);
    zactor_destroy (&auth);

    //  Destroy config tree
    zconfig_destroy (&config);
    return 0;
}
示例#20
0
///
//  Initialize CZMQ zsys layer; this happens automatically when you create
//  a socket or an actor; however this call lets you force initialization
//  earlier, so e.g. logging is properly set-up before you start working.
//  Not threadsafe, so call only from main thread. Safe to call multiple
//  times. Returns global CZMQ context.
void * QZsys::init ()
{
    void * rv = zsys_init ();
    return rv;
}
示例#21
0
JNIEXPORT jlong JNICALL
Java_org_zeromq_czmq_Zsys__1_1init (JNIEnv *env, jclass c)
{
    jlong init_ = (jlong) (intptr_t) zsys_init ();
    return init_;
}
示例#22
0
int main (int argc, char *argv [])
{
    puts (PRODUCT);
    puts (COPYRIGHT);
    puts (NOWARRANTY);

    int argn = 1;
    bool verbose = false;
    bool force_foreground = false;
    if (argc > argn && streq (argv [argn], "-v")) {
        verbose = true;
        argn++;
    }
    if (argc > argn && streq (argv [argn], "-f")) {
        force_foreground = true;
        argn++;
    }
    if (argc > argn && streq (argv [argn], "-h")) {
        puts ("Usage: malamute [ -v ] [ -f ] [ -h | config-file ]");
        puts ("  Default config-file is 'malamute.cfg'");
        return 0;
    }
    //  Collect configuration file name
    const char *config_file = "malamute.cfg";
    if (argc > argn) {
        config_file = argv [argn];
        argn++;
    }
    zsys_init ();
    // Keep old behavior unless specified otherwise.
    if (!getenv ("ZSYS_LOGSYSTEM")) {
        zsys_set_logsystem(true);
    }
    zsys_set_pipehwm (0);
    zsys_set_sndhwm (0);
    zsys_set_rcvhwm (0);

    //  Load config file for our own use here
    zsys_info ("loading configuration from '%s'...", config_file);
    zconfig_t *config = zconfig_load (config_file);
    if (!config) {
        zsys_info ("'%s' is missing, creating with defaults:", config_file);
        config = zconfig_new ("root", NULL);
        zconfig_put (config, "server/timeout", "5000");
        zconfig_put (config, "server/background", "0");
        zconfig_put (config, "server/workdir", ".");
        zconfig_put (config, "server/verbose", "0");
        zconfig_put (config, "mlm_server/security/mechanism", "null");
        zconfig_put (config, "mlm_server/bind/endpoint", MLM_DEFAULT_ENDPOINT);
        zconfig_print (config);
        zconfig_save (config, config_file);
    }
    //  Do we want to run broker in the background?
    int as_daemon = !force_foreground && atoi (zconfig_resolve (config, "server/background", "0"));
    const char *workdir = zconfig_resolve (config, "server/workdir", ".");
    if (as_daemon) {
        zsys_info ("switching Malamute to background...");
        if (zsys_daemonize (workdir))
            return -1;
    }
    //  Switch to user/group to run process under, if any
    if (zsys_run_as (
        zconfig_resolve (config, "server/lockfile", NULL),
        zconfig_resolve (config, "server/group", NULL),
        zconfig_resolve (config, "server/user", NULL)))
        return -1;

    //  Install authenticator (NULL or PLAIN)
    zactor_t *auth = zactor_new (zauth, NULL);
    assert (auth);

    if (verbose || atoi (zconfig_resolve (config, "server/auth/verbose", "0"))) {
        zstr_sendx (auth, "VERBOSE", NULL);
        zsock_wait (auth);
    }
    //  Do PLAIN password authentication if requested
    const char *passwords = zconfig_resolve (config, "server/auth/plain", NULL);
    if (passwords) {
        zstr_sendx (auth, "PLAIN", passwords, NULL);
        zsock_wait (auth);
    }
    //  Start Malamute server instance
    zactor_t *server = zactor_new (mlm_server, "Malamute");
    if (verbose)
        zstr_send (server, "VERBOSE");
    zstr_sendx (server, "LOAD", config_file, NULL);

    //  Accept and print any message back from server
    while (true) {
        char *message = zstr_recv (server);
        if (message) {
            puts (message);
            free (message);
        }
        else {
            puts ("interrupted");
            break;
        }
    }
    //  Shutdown all services
    zactor_destroy (&server);
    zactor_destroy (&auth);

    //  Destroy config tree
    zconfig_destroy (&config);

#if defined (__WINDOWS__)
    zsys_shutdown ();
#endif

    return 0;
}