int main (int argc, char *argv[]) { string config_path; vector<string> overrides; string method; string type; string rfmt; string hfmt; uint32_t db; bool raw; bool content; bool verbose; SearchRequest search; namespace po = boost::program_options; po::options_description desc("Allowed options"); desc.add_options() ("help,h", "produce help message.") ("config", po::value(&config_path)->default_value("donkey.xml"), "") ("override,D", po::value(&overrides), "override configuration.") ("method", po::value(&method)->default_value("ping"), "") ("db", po::value(&db)->default_value(0), "") ("feature", "") ("content", "") ("type", po::value(&type), "") (",K", po::value(&search.K)->default_value(-1), "") (",R", po::value(&search.R)->default_value(NAN), "") ("hint_K", po::value(&search.hint_K)->default_value(-1), "") ("hint_R", po::value(&search.hint_R)->default_value(NAN), "") ("rfmt", po::value(&rfmt)->default_value("%k\t%s\t%m"), "response format") ("hfmt", po::value(&hfmt)->default_value("%K => %k\t%s\t%m"), "hit format") ("embed", "") ("no-sync", "") ("no-reindex", "") ("verbose,v", "") ; po::positional_options_description p; p.add("method", 1); po::variables_map vm; po::store(po::command_line_parser(argc, argv). options(desc).positional(p).run(), vm); po::notify(vm); if (vm.count("help")) { cout << "Usage:" << endl; cout << "\tserver ..." << endl; cout << desc; cout << endl; return 0; } raw = !vm.count("feature"); content = vm.count("content"); verbose = vm.count("verbose"); Config config; LoadConfig(config_path, &config); OverrideConfig(overrides, &config); setup_logging(config); Service *client = nullptr; if (method == "extract") { ; } else if (vm.count("embed")) { client = new Server(config); } else { try { client = make_client(config); } catch (exception const &e) { cerr << "Failed to create client: " << e.what() << endl; return -1; } } if (method == "ping") { PingResponse resp; client->ping(&resp); cout << resp.restart_count << endl; cout << resp.last_start_time << endl; cout << resp.first_start_time << endl; } else if (method == "extract") { Tasks tasks; Extractor xtor(config); #pragma omp parallel { #pragma omp for schedule(dynamic, 1) for (unsigned i = 0; i < tasks.size(); ++i) { Task const &task = tasks[i]; Object object; xtor.extract_path(task.url, type, &object); if (task.meta.size()) { ofstream os(task.meta, std::ios::binary); object.write(os); } } } } else if (method == "insert") { Tasks tasks; vector<InsertRequest> reqs(tasks.size()); vector<InsertResponse> resps(tasks.size()); #pragma omp parallel { Service *th_client = client; int th = omp_get_thread_num(); if ((vm.count("embed") == 0) && (th != 0)) { // create clients for new threads #pragma omp critical th_client = make_client(config); } #pragma omp for schedule(dynamic, 1) for (unsigned i = 0; i < tasks.size(); ++i) { Task const &task = tasks[i]; InsertRequest &req = reqs[i]; InsertResponse &resp = resps[i]; req.db = db; req.raw = raw; req.key = task.key; req.type = type; req.url = task.url; req.meta = task.meta; if (content) { ReadURL(req.url, &req.content); req.url.clear(); } try { th_client->insert(req, &resp); } catch (Error const &e) { cerr << "Error " << e.code() << ": " << e.what() << endl; } req.content.clear(); } if (th_client != client) { delete th_client; } } for (unsigned i = 0; i < tasks.size(); ++i) { auto &req = reqs[i]; auto &resp = resps[i]; cout << req.key; if (verbose) { cout << '\t' << resp.time << '\t' << resp.load_time << '\t' << resp.journal_time << '\t' << resp.index_time << endl; } cout << endl; } { MiscRequest req; MiscResponse resp; req.db = db; if (vm.count("no-reindex") == 0) { req.method = "reindex"; client->misc(req, &resp); } if (vm.count("no-sync") == 0) { req.method = "sync"; //client->misc(req, &resp); client->misc(req, &resp); } } } else if (method == "search") { Tasks tasks; vector<SearchRequest> reqs(tasks.size(), search); vector<SearchResponse> resps(tasks.size()); #pragma omp parallel { Service *th_client = client; int th = omp_get_thread_num(); if ((vm.count("embed") == 0) && (th != 0)) { // create clients for new threads #pragma omp critical th_client = make_client(config); } #pragma omp for schedule(dynamic, 1) for (unsigned i = 0; i < tasks.size(); ++i) { Task const &task = tasks[i]; SearchRequest &req = reqs[i]; SearchResponse &resp = resps[i]; req.db = db; req.raw = raw; req.type = type; if (content) { ReadFile(task.url, &req.content); } else { req.url = task.url; } try { th_client->search(req, &resp); } catch (Error const &e) { cerr << "Error " << e.code() << ": " << e.what() << endl; } req.content.clear(); } if (th_client != client) { delete th_client; } } for (unsigned i = 0; i < tasks.size(); ++i) { Task const &task = tasks[i]; auto &resp = resps[i]; /* if (verbose) { cout << task.key << ": " << resp.time << '\t' << resp.load_time << '\t' << resp.filter_time << '\t' << resp.rank_time << endl; } */ for (auto const &h: resp.hits) { cout << format_hit(hfmt, task, h) << endl; } } } else if (method == "stat") { StatRequest req; StatResponse resp; req.db = db; client->stat(req, &resp); cout << "size: " << resp.size << endl; cout << "last: " << endl; for (auto const &s: resp.last) { cout << '\t' << s << endl; } } else { MiscRequest req; MiscResponse resp; req.method = method; req.db = db; client->misc(req, &resp); cout << "code: " << resp.code << endl; cout << "text: " << resp.text << endl; } if (client) delete client; cleanup_logging(); return 0; }
/* open description: open the dat file, checking if it exists and is valid, and determine the type of task input: string file - the file name to open vector<TaskType> - an array of the possible task types from the ini file output: TaskType - the task type that this function determines the file belongs to */ bool analysis::open(string file, Tasks &taskTypes) { //first try to open the file using the BCI2000 file reader function fName = file; int nTasks = 0; thisTask.taskName = "Unknown"; droppedSamples = 0; dFile = new BCI2000FileReader(fName.c_str()); if (!dFile->IsOpen()) { cout <<"Could not open \""<<fName<<"\" as a BCI2000 data file."<<endl; mIsOpen = false; thisTask.taskName = ""; return false; } mIsOpen = true; //get some task parameters nSamples = dFile->NumSamples(); nChannels = dFile->SignalProperties().Channels(); sampleRate = dFile->SamplingRate(); SignalType dataType = dFile->SignalProperties().Type(); //read in the signal ----------------------- signal = new double *[nChannels]; for (int ch = 0; ch < nChannels; ch++) { signal[ch] = new double[nSamples]; for (int s = 0; s < nSamples; s++) { signal[ch][s] = dFile->RawValue(ch, s); } } //get states ----------------------------- nStates = dFile->States()->Size(); for (int i = 0; i < nStates; i++) { string tmpStr = (*dFile->States())[i].Name(); states[tmpStr] = new double[nSamples]; stateNames.push_back(tmpStr); } const StateVector* stateVector = dFile->StateVector(); //const StateList& curStateList = stateVector->StateList(); for (int s = 0; s < nSamples; s++) { dFile->ReadStateVector(s); for (int i = 0; i < nStates; i++) { states[stateNames[i]][s] = stateVector->StateValue(stateNames[i]); } } //get useful parameters //get the data location, whos parameter may be different depending on the version //i.e., FileInitials is v1 and DataDirectory is v2 blockSize = dFile->Parameter("SampleBlockSize"); if (dFile->Parameters()->Exists("FileInitials")) FileInitials = dFile->Parameter("FileInitials"); else if (dFile->Parameters()->Exists("DataDirectory")) FileInitials = dFile->Parameter("DataDirectory"); SubjectSession = dFile->Parameter("SubjectSession"); SubjectName = dFile->Parameter("SubjectName"); SubjectRun = dFile->Parameter("SubjectRun"); //cleanup //delete stateVector; //handled by delete dFile; bool statesFound = false; bool parmsFound = false; bool taskFound = false; bool nameFound = false; //go through every possible task type, and compare configurations for (int i = 0; i < taskTypes.size() && !taskFound; i++) { //search through the task types until one is found statesFound = true; parmsFound = true; nameFound = false; //first, check to see if the task folder and subject name are the same //if not, we should not continue at all nameFound = (taskTypes[i].taskFolder == SubjectName); if (!nameFound) continue; //if states are specified in the ini file, check to see that those //states exist; otherwise, they have no effect on the task classification for (int s = 0; s < taskTypes[i].states.size(); s++) { statesFound = (isMember(stateNames, taskTypes[i].states[s]) && statesFound); } //at some point, we can also add the parm check, //but for now it probably is not necessary //if the name is found, the states are found (true by def), //and the parms are found (true by def), then we have successfuly classified //this file as the current task type if (statesFound && parmsFound && nameFound) { //set thisTask as the current taskType, and set the parameterss thisTask = (taskTypes[i]); thisTask.sampleRate = sampleRate; thisTask.blockSize = blockSize; taskFound = true; nTasks++; } } //if the task is not classified, say so if (!taskFound) { thisTask.taskName = "Unknown"; return false; } return true; }