// overridable base class method // assumes data have already been checked for proper dictionary, name. CalibratorRequestedAction Calibrator::getRequestedAction(Datum dictionaryData) { // check what action is requested (e.g. update parameters) if (!(dictionaryData.hasKey(R_CALIBRATOR_ACTION))) { mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Request sent to calibrator %s that did not contain an action field was ignored.", uniqueCalibratorName.c_str()); return(CALIBRATOR_NO_ACTION); } Datum actionData = dictionaryData.getElement(R_CALIBRATOR_ACTION); if (!(actionData.getDataType() == M_STRING)) { // check if name field is a string mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Request sent to calibrator %s that did not contain a string in the action field was ignored.", uniqueCalibratorName.c_str()); return(CALIBRATOR_NO_ACTION); } if (actionData.getString() == R_CALIBRATOR_ACTION_SET_PARAMETERS) { // check is name field matches the name of this calibrator if (VERBOSE_EYE_CALIBRATORS>1) mprintf( "Calibrator %s successfully received request for to update its parameters to contained values.", uniqueCalibratorName.c_str()); return CALIBRATOR_ACTION_SET_PARAMS_TO_CONTAINED; } else if (actionData.getString() == R_CALIBRATOR_ACTION_SET_PARAMETERS_TO_DEFAULTS) { if (VERBOSE_EYE_CALIBRATORS>1) mprintf("Calibrator %s successfully received request for to update its parameters to defaults.", uniqueCalibratorName.c_str()); return CALIBRATOR_ACTION_SET_PARAMS_TO_DEFAULTS; } else { mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Calibrator %s received a request, but action was unknown. Request ignored", uniqueCalibratorName.c_str()); return CALIBRATOR_NO_ACTION; } return (CALIBRATOR_NO_ACTION); }
Datum ComponentRegistry::getNumber(std::string expression, GenericDataType type){ Datum value; switch(type){ case M_FLOAT: case M_INTEGER: case M_BOOLEAN: case M_STRING: value = getValue(expression, type); if (value.getDataType() == type) { return value; } break; default: throw FatalParserException("Attempt to cast a number of invalid type"); } switch (type){ case M_FLOAT: return Datum(value.getFloat()); case M_INTEGER: return Datum(value.getInteger()); case M_STRING: return Datum(value.getString()); case M_BOOLEAN: return Datum(value.getBool()); default: return value; } }
bool ExperimentUnpackager::createFile(Datum filename, Datum buffer) { if(buffer.getDataType() != M_STRING || filename.getDataType() != M_STRING) return false; boost::filesystem::path filePath(filename.getString()); boost::filesystem::create_directories(filePath.parent_path()); // create an output file. std::ofstream outFile(filePath.string().c_str()); //, ios::binary); //write the data outFile << buffer.getString(); // flush buffer outFile.flush(); // close the handle outFile.close(); return true; }
boost::python::object convert_datum_to_python(const Datum &datum) { ScopedRecursionGuard srg(" while converting MWorks datum to Python object"); if (datum.isUndefined()) { return boost::python::object(); } switch (datum.getDataType()) { case M_INTEGER: return convert_longlong_to_python(datum.getInteger()); case M_FLOAT: return manageNewRef( PyFloat_FromDouble(datum.getFloat()) ); case M_BOOLEAN: return manageNewRef( PyBool_FromLong(datum.getBool()) ); case M_STRING: { auto &str = datum.getString(); return manageNewRef( PyString_FromStringAndSize(str.c_str(), str.size()) ); } case M_LIST: { auto &listValue = datum.getList(); boost::python::object list = manageNewRef( PyList_New(listValue.size()) ); for (Py_ssize_t i = 0; i < listValue.size(); i++) { boost::python::object item = convert_datum_to_python(listValue.at(i)); // PyList_SetItem "steals" the item reference, so we need to INCREF it Py_INCREF(item.ptr()); if (PyList_SetItem(list.ptr(), i, item.ptr())) throw_error_already_set(); } return list; } case M_DICTIONARY: { boost::python::object dict = manageNewRef( PyDict_New() ); for (auto &item : datum.getDict()) { if (PyDict_SetItem(dict.ptr(), convert_datum_to_python(item.first).ptr(), convert_datum_to_python(item.second).ptr())) { throw_error_already_set(); } } return dict; } default: PyErr_Format(PyExc_TypeError, "Cannot convert Datum of unknown type (%d)", datum.getDataType()); throw_error_already_set(); return boost::python::object(); // Never reached } }
static shared_ptr<mw::Component> createRealtimeComponent(const shared_ptr<ComponentRegistry> &componentRegistry, const Datum &realtimeComponentsValue, const std::string &componentType, const std::string &defaultPluginName) { std::string pluginName(defaultPluginName); if (realtimeComponentsValue.isDictionary()) { Datum componentValue = realtimeComponentsValue.getElement(componentType); if (componentValue.isString()) { pluginName = componentValue.getString(); } } mprintf(M_SYSTEM_MESSAGE_DOMAIN, " %s:\t%s", componentType.c_str(), pluginName.c_str()); return componentRegistry->createNewObject(pluginName, map<string, string>()); }
// this routine checks that the request is a dictionary and that it contains a name that matches the calibrator bool Calibrator::checkRequest(Datum dictionaryData) { Datum data; // to hold field data for checking // check if this is a dictionary if (!(dictionaryData.getDataType() == M_DICTIONARY)) { mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Request sent to calibrator %s that was not expected dictionary type was ignored.", uniqueCalibratorName.c_str()); return(false); } // check if there is a name field and if this calibrator should respond (i.e. if it has the correct name) if (!(dictionaryData.hasKey(R_CALIBRATOR_NAME))) { mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Request sent to calibrator %s that did not contain name field was ignored.", uniqueCalibratorName.c_str()); return(false); } Datum nameData = dictionaryData.getElement(R_CALIBRATOR_NAME); if (!(nameData.getDataType() == M_STRING)) { // check if name field is a string mwarning(M_SYSTEM_MESSAGE_DOMAIN, "Request sent to calibrator %s that did not contain a string in the name field was ignored.", uniqueCalibratorName.c_str()); return(false); } if (uniqueCalibratorName == nameData.getString()) { // check is name field matches the name of this calibrator if (VERBOSE_EYE_CALIBRATORS) mprintf("Calibrator %s successfully received a properly named request.", uniqueCalibratorName.c_str()); } else { if (VERBOSE_EYE_CALIBRATORS) mprintf("Calibrator %s received a request, but name did not match.", uniqueCalibratorName.c_str()); return(false); // request not meant for this calibrator (can be normal behavior -- no warnings) } return true; }
bool ExperimentUnpackager::unpackageExperiment(Datum payload) { namespace bf = boost::filesystem; if(payload.getDataType() != M_DICTIONARY) { merror(M_NETWORK_MESSAGE_DOMAIN, "Invalid payload type for experiment package"); return false; } Datum experimentFilePackage = payload.getElement(M_PACKAGER_EXPERIMENT_STRING); if(experimentFilePackage.getNElements() != M_EXPERIMENT_PACKAGE_NUMBER_ELEMENTS_PER_UNIT || experimentFilePackage.getDataType() != M_DICTIONARY) return false; Datum experimentFileName = experimentFilePackage.getElement(M_PACKAGER_FILENAME_STRING); if(experimentFileName.getDataType() != M_STRING || experimentFileName.getString().empty()) return false; bf::path experimentName(experimentFileName.getString()); loadedExperimentFilename = prependExperimentInstallPath(removeFileExtension(experimentName.string()), experimentName.string()); bf::path experimentPath = loadedExperimentFilename.branch_path(); createExperimentInstallDirectoryStructure(experimentName.string()); // create the XML file Datum experimentFileBuffer = experimentFilePackage.getElement(M_PACKAGER_CONTENTS_STRING); if(experimentFileBuffer.getDataType() != M_STRING || experimentFileBuffer.getString().empty()) return false; if(!(createFile(Datum(loadedExperimentFilename.string().c_str()), experimentFileBuffer))) { // failed to create experiment file merror(M_FILE_MESSAGE_DOMAIN, "Failed to create server side experiment file %s", loadedExperimentFilename.string().c_str()); return false; } // create all of the other media files Datum mediaFileList = payload.getElement(M_PACKAGER_MEDIA_BUFFERS_STRING); if(mediaFileList.isList()) { for(int i=0; i<mediaFileList.getNElements(); ++i) { Datum mediaFilePackage = mediaFileList.getElement(i); if(mediaFilePackage.getDataType() != M_DICTIONARY | mediaFilePackage.getNElements() != 2) { merror(M_FILE_MESSAGE_DOMAIN, "incorrectly packaged media files"); return false; } Datum mediaFileName = mediaFilePackage.getElement(M_PACKAGER_FILENAME_STRING); Datum mediaFileBuffer = mediaFilePackage.getElement(M_PACKAGER_CONTENTS_STRING); if(mediaFileName.getDataType() != M_STRING || mediaFileName.getString().empty() || mediaFileBuffer.getDataType() != M_STRING) return false; std::string filename(mediaFileName.getString()); std::string filenameWPath = experimentPath.string() + "/" + filename; if(!(createFile(Datum(filenameWPath.c_str()), mediaFileBuffer))) { // failed to create experiment file merror(M_FILE_MESSAGE_DOMAIN, "Failed to create server side experiment file %s", filenameWPath.c_str()); return false; } } } expandRangeReplicatorItems(loadedExperimentFilename); modifyExperimentMediaPaths(loadedExperimentFilename); return true; }