src/siri/admin/client.c
/*
 * Called when database.dat is received.
 */
Called when database.dat is received.
Called when database . dat is received .
static void CLIENT_on_file_database(
        siri_admin_client_t * adm_client,
        sirinet_pkg_t * pkg)
{
    FILE * fp;
    qp_unpacker_t unpacker;
    qp_obj_t qp_uuid;
    siridb_t * siridb;
    int rc;
    char fn[strlen(adm_client->dbpath) + 13]; 
    sprintf(fn, "%sdatabase.dat", adm_client->dbpath);
    qp_unpacker_init(&unpacker, pkg->data, pkg->len);
    if (qp_is_array(qp_next(&unpacker, NULL)) &&
        qp_next(&unpacker, NULL) == QP_INT64 &&
        qp_next(&unpacker, &qp_uuid) == QP_RAW &&
        qp_uuid.len == 16)
    {
        memcpy(unpacker.pt - 16, &adm_client->uuid, 16);
    }
    else
    {
        CLIENT_err(adm_client, "invalid database file received");
        return;
    }
    fp = fopen(fn, "w");
    if (fp == NULL)
    {
        CLIENT_err(adm_client, "cannot write or create file: %s", fn);
        return;
    }
    rc = fwrite(pkg->data, pkg->len, 1, fp);
    if (fclose(fp) || rc != 1)
    {
        CLIENT_err(adm_client, "cannot write or create file: %s", fn);
        return;
    }
    siridb = siridb_new(adm_client->dbpath, LOCK_QUIT_IF_EXIST);
    if (siridb == NULL)
    {
        CLIENT_err(adm_client, "error loading database");
        return;
    }
    adm_client->flags |= CLIENT_FLAGS_NO_ROLLBACK;
    siridb->server->flags |= SERVER_FLAG_RUNNING;
    siri_heartbeat_force();
    sirinet_pkg_t * package;
    qp_packer_t * packer = sirinet_packer_new(512);
    if (packer == NULL)
    {
        CLIENT_err(adm_client, "memory allocation error");
        return;
    }
    adm_client->request = CLIENT_REQUEST_REGISTER_SERVER;
    if (qp_add_type(packer, QP_ARRAY4) ||
        qp_add_raw(packer, (const unsigned char *) &adm_client->uuid, 16) ||
        qp_add_string(packer, siri.cfg->server_address) ||
        qp_add_int32(packer, (int32_t) siri.cfg->listen_backend_port) ||
        qp_add_int32(packer, (int32_t) adm_client->pool))
    {
        qp_packer_free(packer);
        CLIENT_err(adm_client, "memory allocation error");
        return;
    }
    package = sirinet_packer2pkg(packer, 0, CPROTO_REQ_REGISTER_SERVER);
    CLIENT_send_pkg(adm_client, package);
}
static void CLIENT_on_file_database ( siri_admin_client_t * adm_client , sirinet_pkg_t * pkg ) { FILE * fp ; qp_unpacker_t unpacker ; qp_obj_t qp_uuid ; siridb_t * siridb ; int rc ; char fn [ strlen ( adm_client -> dbpath ) + 13 ] ; sprintf ( fn , " " , adm_client -> dbpath ) ; qp_unpacker_init ( & unpacker , pkg -> data , pkg -> len ) ; if ( qp_is_array ( qp_next ( & unpacker , NULL ) ) && qp_next ( & unpacker , NULL ) == QP_INT64 && qp_next ( & unpacker , & qp_uuid ) == QP_RAW && qp_uuid . len == 16 ) { memcpy ( unpacker . pt - 16 , & adm_client -> uuid , 16 ) ; } else { CLIENT_err ( adm_client , " " ) ; return ; } fp = fopen ( fn , " " ) ; if ( fp == NULL ) { CLIENT_err ( adm_client , " " , fn ) ; return ; } rc = fwrite ( pkg -> data , pkg -> len , 1 , fp ) ; if ( fclose ( fp ) || rc != 1 ) { CLIENT_err ( adm_client , " " , fn ) ; return ; } siridb = siridb_new ( adm_client -> dbpath , LOCK_QUIT_IF_EXIST ) ; if ( siridb == NULL ) { CLIENT_err ( adm_client , " " ) ; return ; } adm_client -> flags |= CLIENT_FLAGS_NO_ROLLBACK ; siridb -> server -> flags |= SERVER_FLAG_RUNNING ; siri_heartbeat_force ( ) ; sirinet_pkg_t * package ; qp_packer_t * packer = sirinet_packer_new ( 512 ) ; if ( packer == NULL ) { CLIENT_err ( adm_client , " " ) ; return ; } adm_client -> request = CLIENT_REQUEST_REGISTER_SERVER ; if ( qp_add_type ( packer , QP_ARRAY4 ) || qp_add_raw ( packer , ( const unsigned char * ) & adm_client -> uuid , 16 ) || qp_add_string ( packer , siri . cfg -> server_address ) || qp_add_int32 ( packer , ( int32_t ) siri . cfg -> listen_backend_port ) || qp_add_int32 ( packer , ( int32_t ) adm_client -> pool ) ) { qp_packer_free ( packer ) ; CLIENT_err ( adm_client , " " ) ; return ; } package = sirinet_packer2pkg ( packer , 0 , CPROTO_REQ_REGISTER_SERVER ) ; CLIENT_send_pkg ( adm_client , package ) ; }
Called when database.dat is received.
Called when database . dat is received .
// 13 = strlen("database.dat")+1 /* schema check is not required at this moment but can be done here */ /* roll-back is not possible anymore */ /* Force one heart-beat */