void Node::run() { // declare variables const int BUFFER_SIZE = 2048; ad_rec_t *reader; size_t read_samples; int16 read_buffer[BUFFER_SIZE]; cmd_ln_t *read_decoder_config; ps_decoder_t *read_decoder; // open audio record reader = ad_open_dev(NULL, 16000); if (reader == NULL) { throw NodeException("Failed to open read audio device"); } // start audio record if (ad_start_rec(reader) < 0) { throw NodeException("Failed to start read audio device"); } // instance audio decoder read_decoder_config = cmd_ln_init( NULL, ps_args(), TRUE, "-logfn", "/dev/null", // turn off console log "-hmm", "/usr/local/share/pocketsphinx/model/en-us/en-us", "-lm", "/usr/local/share/pocketsphinx/model/en-us/en-us.lm.dmp", "-dict", "/usr/local/share/pocketsphinx/model/en-us/cmudict-en-us.dict", NULL ); read_decoder = ps_init(read_decoder_config); if (read_decoder == NULL) { throw NodeException("Failed to initialize audio decoder"); } // start utterance if (ps_start_utt(read_decoder) < 0) { throw NodeException("Failed to start reader utterance"); } bool utt_started = false; bool in_speech = false; char const *hypothesis; while (true) { try { // read from input device read_samples = ad_read( reader, read_buffer, BUFFER_SIZE ); if (read_samples < 0) { throw NodeException("Failed to read audio"); } // add data buffered from device to read decoder ps_process_raw( read_decoder, read_buffer, read_samples, false, false ); // check device in speech in_speech = ps_get_in_speech(read_decoder); if (in_speech && !utt_started) { utt_started = true; } // check device in speech then add data to decoder if (!in_speech && utt_started) { ps_end_utt(read_decoder); hypothesis = ps_get_hyp(read_decoder, NULL); if (hypothesis != NULL) { cout<<"ENV: "<<hypothesis<<endl; Command core_command = this->spec_.first("node-core"); int port = atoi(core_command.option("--port").c_str()); SocketWriter socket(core_command.option("--host"), port); string response = socket.write_to(hypothesis); cout << "IN: " << response << endl; } if (ps_start_utt(read_decoder) < 0) { throw NodeException("Failed to start reader utterance"); } utt_started = false; } sleep_milli_seconds(100); } catch (NodeException e) { cout<<"ERROR: "<<e.what()<<endl; } } }
void SocketReader::on_write(function<string(string content)> handler) { // declare variables int socket_fd, new_socket_fd; socklen_t client; struct sockaddr_in server_address, client_address; // create new socket socket_fd = socket(AF_INET, SOCK_STREAM, 0); if (socket_fd < 0) { throw NodeException(strerror(errno)); } // bind socket to port bzero((char *) &server_address, sizeof(server_address)); server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = INADDR_ANY; server_address.sin_port = htons(this->port_); int bind_result = bind(socket_fd, (struct sockaddr *) &server_address, sizeof(server_address)); if (bind_result < 0) { throw NodeException(strerror(errno)); } // start listen signal to socket listen(socket_fd, 5); client = sizeof(client_address); // handle signal to socket // todo how to break this loop and close socket while (true) { try { // accept signal new_socket_fd = accept(socket_fd, (struct sockaddr *) &client_address, &client); if (new_socket_fd < 0) { throw NodeException(strerror(errno)); } // read request content char request_content[256]; bzero(request_content, 255); if (read(new_socket_fd, request_content, 255) < 0) { throw NodeException(strerror(errno)); } // call handler, get result string handler_result = handler(request_content); // write to request socket ssize_t write_result = write(new_socket_fd, handler_result.c_str(), handler_result.length()); if (write_result < 0) { throw NodeException(strerror(errno)); } // close new socket close(new_socket_fd); } catch (NodeException e) { cout<<"ERROR: "<<e.what()<<endl; } } }
Addresses Addresses::fromFile(string path) { Addresses result; ifstream fs(path); if (fs.fail()) { throw NodeException("Error reading node address file"); } try { while (!fs.eof()) { string line; getline(fs, line); if (fs.fail()) { throw NodeException("Error reading node addresses."); } else if (line.length() == 0) { break; } int ipStart = line.find(" "); string str = line.substr(0, ipStart); uint id = strtoul(str.c_str(), null, 10); if (id == 0 || errno == ERANGE) { throw NodeException("Error reading node addresses."); } string ip = line.substr(ipStart + 1, line.find(":") - ipStart - 1); int portStart = line.find(":", ipStart + 1) + 1; ushort port = (ushort) strtoul( line.substr(portStart, line.length() - portStart - 1).c_str(), null, 10); if (port == 0 || errno == ERANGE) { throw NodeException("Error reading node addresses."); } char type = line.at(line.length() - 1); result.addresses[id].ip = ip; result.addresses[id].port = port; switch (type) { case 'C': case 'c': result.addresses[id].type = Application::AppType::CUSTOMER; break; case 'B': case 'b': result.addresses[id].type = Application::AppType::BUSINESS; break; default: throw NodeException("Unexcepted node type."); } } } catch (const exception& e) { //catch all exceptions for the sake of simplicity throw NodeException("Error reading node addresses."); } return result; }