main() { INT status, size, trans, run_number; char host_name[256], str[32]; INT event_id, request_id; DWORD last_time; BOOL via_callback; /* get parameters */ printf("ID of event to request: "); ss_gets(str, 32); event_id = atoi(str); printf("Host to connect: "); ss_gets(host_name, 256); printf("Get all events (0/1): "); ss_gets(str, 32); all_flag = atoi(str); printf("Receive via callback ([y]/n): "); ss_gets(str, 32); via_callback = str[0] != 'n'; /* connect to experiment */ status = cm_connect_experiment(host_name, "", all_flag ? "Power Consumer" : "Consumer", NULL); if (status != CM_SUCCESS) return 1; /* open the "system" buffer, 1M size */ bm_open_buffer("SYSTEM", EVENT_BUFFER_SIZE, &hBufEvent); /* set the buffer cache size */ bm_set_cache_size(hBufEvent, 100000, 0); /* place a request for a specific event id */ bm_request_event(hBufEvent, (WORD) event_id, TRIGGER_ALL, all_flag ? GET_ALL : GET_SOME, &request_id, via_callback ? process_event : NULL); /* place a request for system messages */ cm_msg_register(process_message); /* place a request for transition notification */ cm_register_transition(TR_START, via_callback? transition : NULL); last_time = 0; do { if (via_callback) status = cm_yield(1000); else { /* receive event "manually" and call receive_event */ size = sizeof(event_buffer); status = bm_receive_event(hBufEvent, event_buffer, &size, ASYNC); if (status == BM_SUCCESS) process_event(hBufEvent, request_id, (EVENT_HEADER *) event_buffer, (void *) (((EVENT_HEADER *) event_buffer) + 1)); /* receive transitions "manually" */ if (cm_query_transition(&trans, &run_number, NULL)) transition(run_number, NULL); /* call yield once every 100 ms */ if (ss_millitime() - last_time > 100) { last_time = ss_millitime(); status = cm_yield(0); } } } while (status != RPC_SHUTDOWN && status != SS_ABORT); cm_disconnect_experiment(); return 1; }
int main(int argc, char *argv[]) { BOOL daemon = FALSE; INT status, i, ch; char host_name[HOST_NAME_LENGTH]; char exp_name[NAME_LENGTH]; char *speech_program = SPEECH_PROGRAM; /* get default from environment */ cm_get_environment(host_name, sizeof(host_name), exp_name, sizeof(exp_name)); #ifdef OS_DARWIN strlcpy(mtTalkStr, "afplay $MIDASSYS/utils/notify.wav", sizeof(mtTalkStr)); strlcpy(mtUserStr, "afplay $MIDASSYS/utils/notify.wav", sizeof(mtTalkStr)); #endif /* parse command line parameters */ for (i = 1; i < argc; i++) { if (argv[i][0] == '-' && argv[i][1] == 'D') daemon = TRUE; else if (argv[i][0] == '-') { if (i + 1 >= argc || argv[i + 1][0] == '-') goto usage; if (argv[i][1] == 'e') strcpy(exp_name, argv[++i]); else if (argv[i][1] == 'h') strcpy(host_name, argv[++i]); else if (argv[i][1] == 'c') speech_program = argv[++i]; else if (argv[i][1] == 't') strcpy(mtTalkStr, argv[++i]); else if (argv[i][1] == 'u') strcpy(mtUserStr, argv[++i]); else if (argv[i][1] == 's') shutupTime = atoi(argv[++i]); else { usage: printf ("usage: mlxspeaker [-h Hostname] [-e Experiment] [-c command] [-D] daemon\n"); printf(" [-t mt_talk] Specify the mt_talk alert command\n"); printf(" [-u mt_user] Specify the mt_user alert command\n"); printf(" [-s shut up time] Specify the min time interval between alert [s]\n"); printf(" The -t & -u switch require a command equivalent to:\n"); printf(" '-t play --volume=0.3 file.wav'\n"); printf(" [-c command] Used to start the speech synthesizer,\n"); printf(" which should read text from it's standard input.\n"); printf(" eg: mlxspeaker -c 'festival --tts -'\n\n"); return 0; } } } if (daemon) { printf("Becoming a daemon...\n"); ss_daemon_init(FALSE); } /* Handle SIGPIPE signals generated from errors on the pipe */ signal(SIGPIPE, sigpipehandler); signal(SIGINT, siginthandler); fp = popen(speech_program, "w"); if (fp == NULL) { cm_msg(MERROR, "Speaker", "Unable to start \"%s\": %s\n", speech_program, strerror(errno)); cm_disconnect_experiment(); exit(2); } /* now connect to server */ status = cm_connect_experiment(host_name, exp_name, "Speaker", NULL); if (status != CM_SUCCESS) return 1; cm_msg_register(receive_message); printf("Midas Message Speaker connected to %s. Press \"!\" to exit.\n", host_name[0] ? host_name : "local host"); /* initialize terminal */ ss_getchar(0); do { status = cm_yield(1000); while (ss_kbhit()) { ch = ss_getchar(0); if (ch == -1) ch = getchar(); if (ch == '!') status = RPC_SHUTDOWN; } } while (status != SS_ABORT && status != RPC_SHUTDOWN); /* reset terminal */ ss_getchar(TRUE); pclose(fp); cm_disconnect_experiment(); return 1; }