Exemplo n.º 1
0
static void
register_new_client (client_t *self)
{
    self->id = zuuid_new ();
    zhashx_update (self->server->clients, zuuid_str (self->id), self);
    xrap_traffic_set_status_code (self->message, XRAP_TRAFFIC_SUCCESS);
}
Exemplo n.º 2
0
Arquivo: zhashx.c Projeto: claws/czmq
int
zhashx_load (zhashx_t *self, const char *filename)
{
    assert (self);
    zhashx_autofree (self);

    //  Whether or not file exists, we'll track the filename and last
    //  modification date (0 for unknown files), so that zhashx_refresh ()
    //  will always work after zhashx_load (), to load a newly-created
    //  file.

    //  Take copy of filename in case self->filename is same string.
    char *filename_copy = strdup (filename);
    if (filename_copy) {
        free (self->filename);
        self->filename = filename_copy;
        self->modified = zsys_file_modified (self->filename);
        FILE *handle = fopen (self->filename, "r");
        if (handle) {
            char *buffer = (char *) zmalloc (1024);
            if (buffer) {
                while (fgets (buffer, 1024, handle)) {
                    //  Skip lines starting with "#" or that do not look like
                    //  name=value data.
                    char *equals = strchr (buffer, '=');
                    if (buffer [0] == '#' || equals == buffer || !equals)
                        continue;

                    //  Buffer may end in newline, which we don't want
                    if (buffer [strlen (buffer) - 1] == '\n')
                        buffer [strlen (buffer) - 1] = 0;
                    *equals++ = 0;
                    zhashx_update (self, buffer, equals);
                }
                free (buffer);
            }
            else {
                fclose (handle);
                return -1; //  Out of memory
            }
            fclose (handle);
        }
        else
            return -1; //  Failed to open file for reading
    }
    else
        return -1; //  Out of memory

    return 0;
}
Exemplo n.º 3
0
static void
register_new_client (client_t *self)
{
    self->address = strdup (mlm_proto_address (self->message));
    //  We ignore anonymous clients, which have empty addresses
    if (*self->address) {
        //  If there's an existing client with this address, expire it
        //  The alternative would be to reject new clients with the same address
        client_t *existing = (client_t *) zhashx_lookup (
            self->server->clients, self->address);
        if (existing)
            engine_send_event (existing, expired_event);

        //  In any case, we now own this address
        zhashx_update (self->server->clients, self->address, self);
    }
    if (*self->address)
        zsys_info ("client %u address='%s' - registering", self->unique_id, self->address);
    mlm_proto_set_status_code (self->message, MLM_PROTO_SUCCESS);
}
Exemplo n.º 4
0
static void
server_accept (server_t *self, const char *key, const char *value)
{
    tuple_t *tuple = (tuple_t *) zhashx_lookup (self->tuples, key);
    if (tuple && streq (tuple->value, value))
        return;                 //  Duplicate tuple, do nothing

    //  Create new tuple
    tuple = (tuple_t *) zmalloc (sizeof (tuple_t));
    assert (tuple);
    tuple->container = self->tuples;
    tuple->key = strdup (key);
    tuple->value = strdup (value);

    //  Store new tuple
    zhashx_update (tuple->container, key, tuple);
    zhashx_freefn (tuple->container, key, tuple_free);

    //  Deliver to calling application
    zstr_sendx (self->pipe, "DELIVER", key, value, NULL);

    //  Hold in server context so we can broadcast to all clients
    self->cur_tuple = tuple;
    engine_broadcast_event (self, NULL, forward_event);

    //  Copy new tuple announcement to all remotes
    zgossip_msg_t *gossip = zgossip_msg_new ();
    zgossip_msg_set_id (gossip, ZGOSSIP_MSG_PUBLISH);
    zsock_t *remote = (zsock_t *) zlistx_first (self->remotes);
    while (remote) {
        zgossip_msg_set_key (gossip, tuple->key);
        zgossip_msg_set_value (gossip, tuple->value);
        zgossip_msg_send (gossip, remote);
        remote = (zsock_t *) zlistx_next (self->remotes);
    }
    zgossip_msg_destroy (&gossip);
}
Exemplo n.º 5
0
JNIEXPORT void JNICALL
Java_org_zeromq_czmq_Zhashx__1_1update (JNIEnv *env, jclass c, jlong self, jlong key, jlong item)
{
    zhashx_update ((zhashx_t *) (intptr_t) self, (const void *) (intptr_t) key, (void *) (intptr_t) item);
}
Exemplo n.º 6
0
///
//  Update or insert item into hash table with specified key and item. If the
//  key is already present, destroys old item and inserts new one. If you set
//  a container item destructor, this is called on the old value. If the key
//  was not already present, inserts a new item. Sets the hash cursor to the
//  new item.
void QmlZhashx::update (const void *key, void *item) {
    zhashx_update (self, key, item);
};