src/siri/admin/client.c
/*
 * Called when servers.dat is received.
 */
Called when servers.dat is received.
Called when servers . dat is received .
static void CLIENT_on_file_servers(
        siri_admin_client_t * adm_client,
        sirinet_pkg_t * pkg)
{
    FILE * fp;
    qp_unpacker_t unpacker;
    qp_types_t tp;
    int rc, n, close_num;
    char fn[strlen(adm_client->dbpath) + 12]; 
    sprintf(fn, "%sservers.dat", adm_client->dbpath);
    fp = fopen(fn, "w");
    if (fp == NULL)
    {
        CLIENT_err(adm_client, "cannot write or create file: %s", fn);
        return;
    }
    qp_unpacker_init(&unpacker, pkg->data, pkg->len);
    tp = qp_next(&unpacker, NULL);
    if (tp >= QP_ARRAY0 && tp <= QP_ARRAY5)
    {
        pkg->data[0] = QP_ARRAY_OPEN;
    }
    else if (tp != QP_ARRAY_OPEN)
    {
        CLIENT_err(adm_client, "invalid server status response");
        return;
    }
    qp_next(&unpacker, NULL);
    tp = qp_next(&unpacker, NULL);
    if (!qp_is_array(tp))
    {
        CLIENT_err(adm_client, "invalid server status response");
        return;
    }
    close_num = (tp == QP_ARRAY_OPEN) ? 1 : 0;
    for (n = pkg->len;
         (unsigned char) pkg->data[n - 1] == QP_ARRAY_CLOSE;
         n--);
    rc = (fwrite(pkg->data, n, 1, fp) == 1) ? 0 : EOF;
    if (close_num)
    {
        rc += qp_fadd_type(fp, QP_ARRAY_CLOSE);
    }
    rc += qp_fadd_type(fp, QP_ARRAY4);
    rc += qp_fadd_raw(fp, (const unsigned char *) &adm_client->uuid, 16);
    rc += qp_fadd_string(fp, siri.cfg->server_address);
    rc += qp_fadd_int32(fp, (int32_t) siri.cfg->listen_backend_port);
    rc += qp_fadd_int32(fp, (int32_t) adm_client->pool);
    rc += fclose(fp);
    if (rc)
    {
        CLIENT_err(adm_client, "cannot write or create file: %s", fn);
    }
    else
    {
        sirinet_pkg_t * package;
        adm_client->request = CLIENT_REQUEST_FILE_DATABASE;
        package = sirinet_pkg_new(0, 0, CPROTO_REQ_FILE_DATABASE, NULL);
        if (package == NULL)
        {
            CLIENT_err(adm_client, "memory allocation error");
        }
        else
        {
            CLIENT_send_pkg(adm_client, package);
        }
    }
}
static void CLIENT_on_file_servers ( siri_admin_client_t * adm_client , sirinet_pkg_t * pkg ) { FILE * fp ; qp_unpacker_t unpacker ; qp_types_t tp ; int rc , n , close_num ; char fn [ strlen ( adm_client -> dbpath ) + 12 ] ; sprintf ( fn , " " , adm_client -> dbpath ) ; fp = fopen ( fn , " " ) ; if ( fp == NULL ) { CLIENT_err ( adm_client , " " , fn ) ; return ; } qp_unpacker_init ( & unpacker , pkg -> data , pkg -> len ) ; tp = qp_next ( & unpacker , NULL ) ; if ( tp >= QP_ARRAY0 && tp <= QP_ARRAY5 ) { pkg -> data [ 0 ] = QP_ARRAY_OPEN ; } else if ( tp != QP_ARRAY_OPEN ) { CLIENT_err ( adm_client , " " ) ; return ; } qp_next ( & unpacker , NULL ) ; tp = qp_next ( & unpacker , NULL ) ; if ( ! qp_is_array ( tp ) ) { CLIENT_err ( adm_client , " " ) ; return ; } close_num = ( tp == QP_ARRAY_OPEN ) ? 1 : 0 ; for ( n = pkg -> len ; ( unsigned char ) pkg -> data [ n - 1 ] == QP_ARRAY_CLOSE ; n -- ) ; rc = ( fwrite ( pkg -> data , n , 1 , fp ) == 1 ) ? 0 : EOF ; if ( close_num ) { rc += qp_fadd_type ( fp , QP_ARRAY_CLOSE ) ; } rc += qp_fadd_type ( fp , QP_ARRAY4 ) ; rc += qp_fadd_raw ( fp , ( const unsigned char * ) & adm_client -> uuid , 16 ) ; rc += qp_fadd_string ( fp , siri . cfg -> server_address ) ; rc += qp_fadd_int32 ( fp , ( int32_t ) siri . cfg -> listen_backend_port ) ; rc += qp_fadd_int32 ( fp , ( int32_t ) adm_client -> pool ) ; rc += fclose ( fp ) ; if ( rc ) { CLIENT_err ( adm_client , " " , fn ) ; } else { sirinet_pkg_t * package ; adm_client -> request = CLIENT_REQUEST_FILE_DATABASE ; package = sirinet_pkg_new ( 0 , 0 , CPROTO_REQ_FILE_DATABASE , NULL ) ; if ( package == NULL ) { CLIENT_err ( adm_client , " " ) ; } else { CLIENT_send_pkg ( adm_client , package ) ; } } }
Called when servers.dat is received.
Called when servers . dat is received .
// 12 = strlen("servers.dat") + 1 /* schema checking is not required at this moment but can be done here */ /* trim closing */