int main(int argc, char **argv) { int merge, interp, purge, i, gotfile; #ifdef USE_GRIB_UPDATE_TIME int update, tsset; time_t update_time; #endif /* USE_GRIB_UPDATE_TIME */ int shmid, semid; void *segmaddr; struct sembuf sem_op[2]; /* TODO add options like -merge -interp -replace */ global_vlmc_context = calloc(1, sizeof(vlmc_context)); init_context_default(global_vlmc_context); if (argc == 1) { usage(*argv); } #ifdef USE_GRIB_UPDATE_TIME gotfile = merge = interp = purge = update = tsset = 0; update_time = 0; #else gotfile = merge = interp = purge = 0; #endif /* USE_GRIB_UPDATE_TIME */ for (i=1; i<argc; i++) { #ifdef USE_GRIB_UPDATE_TIME if (!strncmp(argv[i], "-update", 8)) { update = 1; continue; } if (!strncmp(argv[i], "-set", 5)) { i++; if (i<argc) { update_time = atol(argv[i]); tsset = 1; } continue; } #endif /* USE_GRIB_UPDATE_TIME */ if (!strncmp(argv[i], "-merge", 7)) { merge = 1; continue; } if (!strncmp(argv[i], "-purge", 7)) { purge = 1; continue; } if (!strncmp(argv[i], "-interp", 8)) { interp = 1; continue; } /* unknown option */ if (*argv[i] == '-') { usage(argv[0]); } set_grib_filename(global_vlmc_context, argv[i]); break; } shmid = -1; segmaddr = NULL; /* first we read the grib before locking things */ #ifdef USE_GRIB_UPDATE_TIME if (merge||update) { /* first we need to read the grib from the segment */ /* no need to lock as we are the one to lock it when doing the update */ shmid = get_grib_shmid(0); if (shmid == -1) { fprintf(stderr, "Can't attach segment, impossible to merge data\n"); } else { segmaddr = get_shmem(shmid, 0); allocate_grib_array_from_shmem(&global_vlmc_context->windtable, segmaddr); if (merge) { merge_gribs(purge); } else if (update) { /* no need for the if there, but for sanity... */ /* we save the previous timestamp, as it's only an update and not a replacement */ update_time = get_grib_update_time(); tsset = 1; /* force setting the time stamp */ init_grib(); if (purge) { purge_gribs(); } } } } #else if (merge) { /* first we need to read the grib from the segment */ /* no need to lock as we are the one to lock it when doing the update */ shmid = get_grib_shmid(0); if (shmid == -1) { fprintf(stderr, "Can't attach segment, impossible to merge data\n"); } else { segmaddr = get_shmem(shmid, 0); allocate_grib_array_from_shmem(&global_vlmc_context->windtable, segmaddr); merge_gribs(purge); } } #endif /* USE_GRIB_UPDATE_TIME */ if (!segmaddr) { /* no merge, or failed one */ init_grib(); if (purge) { purge_gribs(); } } if (!global_vlmc_context->windtable.nb_prevs) { fprintf(stderr, "Invalid GRIB entry\n"); exit(1); } #ifdef USE_GRIB_UPDATE_TIME if (tsset) { set_grib_update_time(update_time); } #endif /* USE_GRIB_UPDATE_TIME */ semid = get_semaphore_id(); if (semid == -1) { semid = create_semaphore(); if (semid == -1) { fprintf(stderr, "Unable to create the semaphore\n"); exit(1); } } sem_op[0].sem_num = 0; sem_op[0].sem_op = 0; sem_op[0].sem_flg = SEM_UNDO; sem_op[1].sem_num = 0; sem_op[1].sem_op = 1; sem_op[1].sem_flg = SEM_UNDO|IPC_NOWAIT; if (semop(semid, sem_op, 2) == -1) { fprintf(stderr, "Fail to lock the semaphore\n"); exit(1); } if (shmid == -1) { /* uninitialized ? (we might have got it already) */ shmid = get_grib_shmid(0); } if (shmid == -1) { /* not there, we create it */ shmid = create_grib_shmid(&global_vlmc_context->windtable); if (shmid == -1) { fprintf(stderr, "Fail to create the GRIB memory segment\n"); exit(1); } } /* copy the grib */ if (!segmaddr) { /* did we got it from a merge ? */ segmaddr = get_shmem(shmid, 0); } copy_grib_array_to_shmem(shmid, &global_vlmc_context->windtable, segmaddr); shmdt(segmaddr); sem_op[0].sem_num = 0; sem_op[0].sem_op = -1; sem_op[0].sem_flg = SEM_UNDO|IPC_NOWAIT; if (semop(semid, sem_op, 1) == -1) { fprintf(stderr, "Fail to unlock the semaphore\n"); exit(1); } printf("Grib segment successfully updated\n"); return 0; }
/* //------------------------------------------------------------------------------------------------------ // Allocate shraed memory ,set semaphore,wakeup audio server // //------------------------------------------------------------------------------------------------------ */ static int preinit(sh_audio_t *sh) { #ifndef EngineMode union smc *sc; long curr_codec; #endif { #ifndef EngineMode if(sh->samplesize==2) { sh->sample_format=AF_FORMAT_S16_LE; //packing as 2 bytes }else if(sh->samplesize==1) { sh->sample_format=AF_FORMAT_U8; //packing as 1 bytes }else if(sh->samplesize==4) { sh->sample_format=AF_FORMAT_S32_LE; //packing as 4 bytes } #endif sh->audio_out_minsize = BLOCKS_PER_LOOP*2*sh->samplesize; } #ifndef EngineMode #ifdef AD_SHM_REWRITE sm_com_id = get_first_shared_mem_id(KEY_SM_COM, SM_COM_SIZE); if(sm_com_id < 0) sm_com_id = get_shared_mem_id(KEY_SM_COM, SM_COM_SIZE); //get shared memory ID if(sm_com == NULL) { if ((sm_com = allocate_share_mem(sm_com_id))==NULL) //allocate shared memory ( command ) return 0; } #else sm_com_id = get_shared_mem_id(KEY_SM_COM, SM_COM_SIZE); //get shared memory ID if ((sm_com = allocate_share_mem(sm_com_id))==NULL) //allocate shared memory ( command ) return 0; #endif id = get_semaphore_id(); //get semaphore ID if (!reset_semvalue(id, 0)){ //reset semaphore return 0; } sc = (union smc *)sm_com; sc->magic.magic_num = PLAYER_MAGIC; curr_codec = APE_ID; //inform server what kind of coded to be used. sc->magic.codec_id = curr_codec; if (!semaphore_v(id, 1)) //wakeup server! return 0; if (!wait_server(sc)){ //wait for server's response deallocate_share_mem(sm_com); #ifndef AD_SHM_REWRITE shmctl(sm_com_id, IPC_RMID, 0); #endif del_semvalue(id, 0); printf("Audio server crash \n"); return 0; } if (sc->magic.codec_id != curr_codec){ //check codec printf("Unsuppoted codec\n"); return 0; } #endif csd_avail_num = res_num = 0; return 1; }
/* //------------------------------------------------------------------------------------------------------ // Allocate shraed memory ,set semaphore,wakeup audio server // //------------------------------------------------------------------------------------------------------ */ static int preinit(sh_audio_t *sh) { aserver_pid = 0; #ifndef EngineMode union smc *sc; long curr_codec; #endif { sh->audio_out_minsize = 320; } #ifndef EngineMode #ifdef AD_SHM_REWRITE sm_com_id = get_first_shared_mem_id(KEY_SM_COM, SM_COM_SIZE); if(sm_com_id < 0) sm_com_id = get_shared_mem_id(KEY_SM_COM, SM_COM_SIZE); //get shared memory ID if(sm_com == NULL) { if ((sm_com = allocate_share_mem(sm_com_id))==NULL) //allocate shared memory ( command ) return 0; } #else sm_com_id = get_shared_mem_id(KEY_SM_COM, SM_COM_SIZE); //get shared memory ID if ((sm_com = allocate_share_mem(sm_com_id))==NULL) //allocate shared memory ( command ) return 0; #endif id = get_semaphore_id(); //get semaphore ID if (!reset_semvalue(id, 0)){ //reset semaphore return 0; } sc = (union smc *)sm_com; sc->magic.magic_num = PLAYER_MAGIC; curr_codec = AMRNB_ID; //inform server what kind of coded to be used. sc->magic.codec_id = curr_codec; if (!semaphore_v(id, 1)) //wakeup server! return 0; if (!wait_server(sc)){ //wait for server's response deallocate_share_mem(sm_com); #ifndef AD_SHM_REWRITE shmctl(sm_com_id, IPC_RMID, 0); #endif del_semvalue(id, 0); printf("Audio server crash \n"); return 0; } if (sc->magic.codec_id != curr_codec){ //check codec printf("Unsuppoted codec\n"); return 0; } #endif aserver_pid = sc->pinit.pid; return 1; }