/*-----------------------------------------------------------------------* * Main routine which takes a string as input and returns a parse string * *-----------------------------------------------------------------------*/ Gal_Frame send_to_parse(Gal_Frame f, void *server_data) { int i, path_score; char *in_string, *out_string, *sysid; Gal_Frame f_new = Gal_MakeFrame("main", GAL_CLAUSE); /* get the input string to parse from the key's value */ in_string = Gal_GetString(f, ":input_string"); sysid = Gal_GetString(f, ":sysid"); path_score = Gal_GetInt(f, ":path_score"); if (in_string == NULL) in_string = Gal_GetString(f, ":parse_input"); /* strip out punctuation, comments, etc, to uppercase */ strip_line(in_string); /* Call Phoenix Parse function */ parse(in_string, gram); /* print parses to buffer */ if( num_parses > MaxParses ) num_parses= MaxParses; if( num_parses < 1 ) { strcpy(outbuf, "No Parse"); } else { out_string= outbuf; for(i= 0; i < num_parses; i++ ) { sprintf(out_string, "PARSE_%d:\n", i); out_string += strlen(out_string); print_parse(i, out_string, extract, gram); out_string += strlen(out_string); sprintf(out_string, "END_PARSE\n"); out_string += strlen(out_string); } sprintf(out_string, "\n"); out_string= outbuf; } /* clear parser temps */ reset(num_nets); /* create a new frame containing the parse */ Gal_SetProp(f_new, ":parse_input", Gal_StringObject(in_string)); Gal_SetProp(f_new, ":parse_output", Gal_StringObject(outbuf)); if (sysid != NULL) Gal_SetProp(f_new, ":sysid", Gal_StringObject(sysid)); if (path_score != 0) Gal_SetProp(f_new, ":path_score", Gal_IntObject(path_score)); /* write parse output frame to HUB */ GalSS_EnvWriteFrame((GalSS_Environment *) server_data, f_new, 0); return(f); }
Gal_Frame hub_debug_prompt(Gal_Frame frame, void *server_data) { Gal_Frame reply_frame = Gal_MakeFrame("hub_debug_cmd", GAL_CLAUSE); int buf_size = 256; char cmd[buf_size]; char *help_msg = "C: disable debug and continue\nc: continue\nd: session DB\ne: exit Hub\ng: globals\nh: help\nl: locks\nm: message\nr: server\ns: session\nt: token\n"; /* Get the debug command. */ printf("%s", help_msg); printf("--> "); fflush(stdout); fgets(cmd, buf_size, stdin); Gal_SetProp(reply_frame, ":debug_cmd", Gal_StringObject(cmd)); return reply_frame; }
Gal_Frame reinitialize(Gal_Frame f, void *server_data) { GalSS_Environment *env = (GalSS_Environment *) server_data; GalIO_CommStruct *gcomm = GalSS_EnvComm(env); AudioPkg *p = (AudioPkg *) GalIO_GetCommServerData(gcomm); Gal_Frame session_f; GalSS_EnvMaintainInLocation(gcomm, GalSS_EnvGetSessionID(env), &(p->env)); /* Create the session. */ session_f = Gal_MakeFrame("OpenAudioSession", GAL_CLAUSE); GalSS_EnvWriteFrame(env, session_f, 0); Gal_FreeFrame(session_f); /* Don't enable the audio device yet - only after the greeting. */ return (Gal_Frame) NULL; }
static Gal_Frame prepare_audio_frame(char *filename) { Gal_Frame f = Gal_MakeFrame("main", GAL_CLAUSE); FILE *fp; size_t count; int total; char *buf; if (!filename) { fprintf(stderr, "No filename provided\n"); exit(1); } fp = fopen(filename, "rb"); if (!fp) { fprintf(stderr, "Couldn't open %s\n", filename); exit(1); } /* If we're the sending direction, we read binary data from the file until we find EOF, and then we build a binary data structure. Once we do that, we transmit the data over a socket by transmitting the size of the memory buffer, and then transmitting the contents of the memory buffer. */ buf = (char *) malloc(BLOCKSIZE * sizeof(char)); count = fread(buf, sizeof(char), BLOCKSIZE, fp); total = count; while (count == BLOCKSIZE) { buf = (char *) realloc(buf, total + BLOCKSIZE); count = fread(buf + total, sizeof(char), BLOCKSIZE, fp); total += count; } fclose(fp); /* Now that we have the audio, we add a binary element. */ /* GlobalBuf = buf; */ Gal_SetProp(f, ":audio_data", Gal_BinaryObject((void *) buf, total)); free(buf); return f; }
static void __AudioPoll(AudioDevice *a, void *data, int num_samples) { AudioPkg *p = (AudioPkg *) a->client_data; Gal_Frame output_f; printf("[Previous device state is %s.]\n[Current device state is %s.]\n", AudioDeviceStateName(a->previous_device_state), AudioDeviceStateName(a->current_device_state)); fflush(stdout); switch (a->current_device_state) { case AUDIO_IDLE: switch (a->previous_device_state) { case AUDIO_RECORDING: /* We have data and this is the last chunk. */ if (!p->out_proxy) { GalUtil_Warn("No out proxy to send audio"); } else { GalSS_ProxyArrayAdd(p->out_proxy, data, num_samples); printf("[Audio data from user (%d samples).]\n", num_samples); fflush(stdout); GalSS_ProxyDone(p->out_proxy); GalSS_FreeBrokerProxy(p->out_proxy); p->out_proxy = (GalSS_BrokerProxy *) NULL; printf("[Audio data from user done.]\n"); fflush(stdout); } break; } break; case AUDIO_RECORDING: switch (a->previous_device_state) { case AUDIO_IDLE: /* We have data and this is the first chunk. */ if (p->out_proxy) { GalUtil_Warn("Out proxy already exists"); } else { output_f = Gal_MakeFrame("FromAudio", GAL_CLAUSE); /* Set up the outgoing broker connection. */ p->out_proxy = GalSS_ProxifyObjectType(p->env, GAL_INT_16, 0, 10); if (p->out_proxy) { /* Send the first chunk. */ GalSS_ProxyArrayAdd(p->out_proxy, data, num_samples); printf("[Audio data from user (%d samples).]\n", num_samples); fflush(stdout); /* Notify the Hub that data is coming. */ Gal_SetProp(output_f, ":sample_rate", Gal_IntObject(a->sample_rate)); Gal_SetProp(output_f, ":encoding_format", Gal_StringObject(a->encoding_format)); Gal_SetProp(output_f, ":proxy", Gal_CreateProxyObject(p->out_proxy, 0)); GalSS_EnvWriteFrame(p->env, output_f, 0); } Gal_FreeFrame(output_f); } break; case AUDIO_RECORDING: /* We have data and this is an intermediate chunk. */ if (!p->out_proxy) { GalUtil_Warn("No out proxy to send audio"); } else { GalSS_ProxyArrayAdd(p->out_proxy, data, num_samples); printf("[Audio data from user (%d samples).]\n", num_samples); fflush(stdout); } break; } break; case AUDIO_PLAYING: break; case AUDIO_UNAVAILABLE: /* The device shut down. End the session and exit. */ /* We ran out of inputs. Disconnect. */ printf("Audio no longer available. Disconnecting.\n"); fflush(stdout); /* First, end the session. */ output_f = Gal_MakeFrame("Builtin.end_session", GAL_CLAUSE); GalSS_EnvWriteFrame(p->env, output_f, 0); Gal_FreeFrame(output_f); /* Next, shut down the connection. This ought to reset the poll. */ GalIO_SetCommDone(GalSS_EnvComm(p->env)); /* DON'T DESTROY THE CONNECTION. In batch mode, we'll be inside a poll for that connection, and all sorts of bad things will happen. It's enough to set it to be done; that will force it to be destroyed when the current poll is finished. */ break; } if (data) free(data); }
static Gal_Frame prepare_audio_frame(GalSS_Environment *env, char *filename, int use_stream) { Gal_Frame f = Gal_MakeFrame("main", GAL_CLAUSE); FILE *fp; size_t count = 0; int total = 0; char *buf = (char *) NULL; Gal_Object proxy; GalIO_BrokerStruct *b; OutData *o = (OutData *) NULL; if (!filename) { fprintf(stderr, "No filename provided\n"); exit(1); } fp = fopen(filename, "rb"); if (!fp) { fprintf(stderr, "Couldn't open %s\n", filename); exit(1); } /* If we're the sending direction, we read binary data from the file until we find EOF, and then we build a binary data structure. Once we do that, we transmit the data over a socket by transmitting the size of the memory buffer, and then transmitting the contents of the memory buffer. If we're streaming, we do it bit by bit; otherwise, we do it all now. */ if (use_stream) { o = (OutData *) calloc(1, sizeof(OutData)); o->fp = fp; } else { buf = (char *) malloc(BLOCKSIZE * sizeof(char)); count = fread(buf, sizeof(char), BLOCKSIZE, fp); total = count; while (count == BLOCKSIZE) { buf = (char *) realloc(buf, total + BLOCKSIZE); count = fread(buf + total, sizeof(char), BLOCKSIZE, fp); total += count; } fclose(fp); } switch (BrokerMethod) { case BROKER_ORIGINAL_ENV: case BROKER_ORIGINAL_GCOMM: b = GalIO_BrokerDataOutInit(GalSS_EnvComm(env), 0, 10); if (b && (GalIO_GetBrokerListenPort(b) > 0)) { GalIO_BrokerPopulateFrame(b, f, ":binary_host", ":binary_port"); if (use_stream) { o->b = b; Gal_AddTask(__write_data, (void *) o, 10, 0, NULL); } else { GalIO_BrokerWriteBinary(b, buf, total); GalIO_BrokerDataOutDone(b); } } break; case BROKER_PROXY_OBJ: case BROKER_PROXY_STREAM: case BROKER_PROXY_ORIGINAL: /* Now that we have the audio, we write the binary data through the broker. */ proxy = GalSS_ObjProxifyObjectType(env, GAL_BINARY, 0, 10); if (proxy) { if (use_stream) { o->proxy = proxy; /* The frame will be freed on write, so we have to copy it to make sure the proxy survives, since we plan on writing to it. */ Gal_SetProp(f, ":binary_proxy", Gal_CopyObject(proxy)); Gal_AddTask(__write_data, (void *) o, 10, 0, NULL); } else { GalSS_ObjProxyArrayAdd(proxy, buf, total); GalSS_ObjProxyDone(proxy); Gal_SetProp(f, ":binary_proxy", proxy); } } break; } free(buf); return f; }