Ejemplo n.º 1
0
static void
client_destroy (client_t **self_p)
{
    assert (self_p);
    if (*self_p) {
        client_t *self = *self_p;
        fmq_config_destroy (&self->config);
        fmq_msg_destroy (&self->request);
        fmq_msg_destroy (&self->reply);
        //  Destroy subscriptions                         
        while (zlist_size (self->subs)) {                 
            sub_t *sub = (sub_t *) zlist_pop (self->subs);
            sub_destroy (&sub);                           
        }                                                 
        zlist_destroy (&self->subs);                      
        free (self);
        *self_p = NULL;
    }
}
Ejemplo n.º 2
0
static void
client_destroy (client_t **self_p)
{
    assert (self_p);
    if (*self_p) {
        client_t *self = *self_p;
        fmq_config_destroy (&self->config);
        int server_nbr;
        for (server_nbr = 0; server_nbr < self->nbr_servers; server_nbr++) {
            server_t *server = self->servers [server_nbr];
            server_destroy (&server);
        }
        //  Destroy subscriptions                         
        while (zlist_size (self->subs)) {                 
            sub_t *sub = (sub_t *) zlist_pop (self->subs);
            sub_destroy (&sub);                           
        }                                                 
        zlist_destroy (&self->subs);                      
        free (self);
        *self_p = NULL;
    }
}
Ejemplo n.º 3
0
static void
control_message (client_t *self)
{
    zmsg_t *msg = zmsg_recv (self->pipe);
    char *method = zmsg_popstr (msg);
    if (streq (method, "SUBSCRIBE")) {
        char *path = zmsg_popstr (msg);
        //  Store subscription along with any previous ones         
        //  Check we don't already have a subscription for this path
        sub_t *sub = (sub_t *) zlist_first (self->subs);            
        while (sub) {                                               
            if (streq (path, sub->path))                            
                return;                                             
            sub = (sub_t *) zlist_next (self->subs);                
        }                                                           
        //  Subscription path must start with '/'                   
        //  We'll do better error handling later                    
        assert (*path == '/');                                      
                                                                    
        //  New subscription, so store it for later replay          
        sub = sub_new (self, path);                                 
        zlist_append (self->subs, sub);                             
                                                                    
        //  If we're connected, then also send to server            
        if (self->connected) {                                      
            fmq_msg_path_set (self->request, path);                 
            self->next_event = subscribe_event;                     
        }                                                           
        free (path);
    }
    else
    if (streq (method, "CONNECT")) {
        char *endpoint = zmsg_popstr (msg);
        client_restart (self, endpoint);
        free (endpoint);
    }
    else
    if (streq (method, "CONFIG")) {
        char *config_file = zmsg_popstr (msg);
        fmq_config_destroy (&self->config);
        self->config = fmq_config_load (config_file);
        if (self->config)
            client_apply_config (self);
        else {
            printf ("E: cannot load config file '%s'\n", config_file);
            self->config = fmq_config_new ("root", NULL);
        }
        free (config_file);
    }
    else
    if (streq (method, "SETOPTION")) {
        char *path = zmsg_popstr (msg);
        char *value = zmsg_popstr (msg);
        fmq_config_path_set (self->config, path, value);
        free (path);
        free (value);
    }
    else
    if (streq (method, "STOP")) {
        zstr_send (self->pipe, "OK");
        self->stopped = true;
    }
    free (method);
    zmsg_destroy (&msg);

    if (self->next_event)
        client_execute (self, self->next_event);
}
Ejemplo n.º 4
0
//  Process message from pipe
static void
client_control_message (client_t *self)
{
    zmsg_t *msg = zmsg_recv (self->pipe);
    char *method = zmsg_popstr (msg);
    if (streq (method, "SUBSCRIBE")) {
        char *path = zmsg_popstr (msg);
        //  Store subscription along with any previous ones                       
        //  Check we don't already have a subscription for this path              
        self->sub = (sub_t *) zlist_first (self->subs);                           
        while (self->sub) {                                                       
            if (streq (path, self->sub->path))                                    
                return;                                                           
            self->sub = (sub_t *) zlist_next (self->subs);                        
        }                                                                         
        //  Subscription path must start with '/'                                 
        //  We'll do better error handling later                                  
        assert (*path == '/');                                                    
                                                                                  
        //  New subscription, store it for later replay                           
        char *inbox = fmq_config_resolve (self->config, "client/inbox", ".inbox");
        self->sub = sub_new (self, inbox, path);                                  
        zlist_append (self->subs, self->sub);                                     
        free (path);
    }
    else
    if (streq (method, "SET INBOX")) {
        char *path = zmsg_popstr (msg);
        fmq_config_path_set (self->config, "client/inbox", path);
        free (path);
    }
    else
    if (streq (method, "SET RESYNC")) {
        char *enabled_string = zmsg_popstr (msg);
        long enabled = atoi (enabled_string);
        free (enabled_string);
        //  Request resynchronization from server                              
        fmq_config_path_set (self->config, "client/resync", enabled? "1" :"0");
    }
    else
    if (streq (method, "CONFIG")) {
        char *config_file = zmsg_popstr (msg);
        fmq_config_destroy (&self->config);
        self->config = fmq_config_load (config_file);
        if (self->config)
            client_apply_config (self);
        else {
            printf ("E: cannot load config file '%s'\n", config_file);
            self->config = fmq_config_new ("root", NULL);
        }
        free (config_file);
    }
    else
    if (streq (method, "SETOPTION")) {
        char *path = zmsg_popstr (msg);
        char *value = zmsg_popstr (msg);
        fmq_config_path_set (self->config, path, value);
        client_config_self (self);
        free (path);
        free (value);
    }
    else
    if (streq (method, "STOP")) {
        zstr_send (self->pipe, "OK");
        self->stopped = true;
    }
    else
    if (streq (method, "CONNECT")) {
        char *endpoint = zmsg_popstr (msg);
        if (self->nbr_servers < MAX_SERVERS) {
            server_t *server = server_new (self->ctx, endpoint);
            self->servers [self->nbr_servers++] = server;
            self->dirty = true;
            client_server_execute (self, server, initialize_event);
        }
        else
            printf ("E: too many server connections (max %d)\n", MAX_SERVERS);
            
        free (endpoint);
    }
    free (method);
    zmsg_destroy (&msg);
}