//-------------------------------------- // ParseStrainFile //-------------------------------------- EStrainType ProcessStrainType( const vector< std::string > & Line, int nLineNumber) { if( Line.size() < 2 ) { std::cerr << "Line " << nLineNumber << std::endl << "StrainOpt: Strain Type expects an arguments \n [ Diag | Sym | Asym ]" << std::endl; RUNTIME_ASSERT( 0, " Parsing Strain Optimization File Failed "); } EStrainType Method; for( Size_Type j = 0; j < eNumStrainTypes; j++ ) { if ( StrainTypeKeywords[j] == Line[1] ) Method = static_cast<EStrainType>( j ); } if( Method >= eNumStrainTypes ) { std::cerr << "Line " << nLineNumber << std::endl << "StrainOpt: Unknown strain type, choices are [ Diag | Sym | Asym ]" << std::endl; RUNTIME_ASSERT( 0, " Parsing Strain Optimization File Failed "); } return Method; }
//------------------------------------------------------------------------------------- // // FindBlocks // //------------------------------------------------------------------------------------- Bool FindBlocks( vector<string> &vBlocks, const string & sBuf ) { Size_Type nSearchPos = 0; string::const_iterator pStringIt = sBuf.begin(); while ( nSearchPos < sBuf.size() ) { Size_Type nStartPos = sBuf.find_first_of( '{', nSearchPos ); Size_Type nEndPos = sBuf.find_first_of( '}', nSearchPos ); RUNTIME_ASSERT( !( nStartPos == string::npos && nEndPos != string::npos ), "[DetectorFile] ERROR: Invalid Detector Block: { expected\n"); RUNTIME_ASSERT( !( nStartPos != string::npos && nEndPos == string::npos ), "[DetectorFile] ERROR: Invalid Detector Block: } expected\n"); // if neither are found, then job's finished here if ( nStartPos == string::npos && nEndPos == string::npos ) { break; } else { nSearchPos = nEndPos + 1; // advance search position string sToInsert( pStringIt + nStartPos + 1, pStringIt + nEndPos ); vBlocks.push_back( sToInsert ); } } return ( vBlocks.size() > 0 ); }
//-------------------------------------- // ParseStrainFile //-------------------------------------- EOptimizationMethod ProcessOptMethod( const vector< std::string > & Line, int nLineNumber ) { if( Line.size() < 2 ) { std::cerr << "Line " << nLineNumber << std::endl << "StrainOpt: Optimization Method expects an argument" << std::endl << "[ MonteCarlo | ConjugateGradient ]" << std::endl; RUNTIME_ASSERT( 0, " Parsing Strain Optimization File Failed "); } EOptimizationMethod Method; for( Size_Type j = 0; j < eNumOptimizationMethods; j++ ) { if ( OptMethodKeywords[j] == Line[1] ) Method = static_cast<EOptimizationMethod>( j ); } if( Method >= eNumOptimizationMethods ) { std::cerr << "Line " << nLineNumber << std::endl << "StrainOpt: Unknown strain optimization method, choices are [ MonteCarlo | ConjugateGradient ]" << std::endl; RUNTIME_ASSERT( 0, " Parsing Strain Optimization File Failed "); } return Method; }
//-------------------------------------- // ParseStrainFile //-------------------------------------- std::pair<EConvergenceMethod, Float> ProcessConvergenceMethod( const vector< std::string > & Line, int nLineNumber) { if( Line.size() < 3 ) { std::cerr << "Line " << nLineNumber << std::endl << "StrainOpt: ConvergenceMethod expects two arguments " << std::endl << "[ Quality fQ | HitRatio fH | Differential fTol ]" << std::endl << std::endl; RUNTIME_ASSERT( 0, " Parsing Strain Optimization File Failed "); } EConvergenceMethod Method; for( Size_Type j = 0; j < eNumConvergenceMethods; j++ ) { if ( ConvergenceMethodKeywords[j] == Line[1] ) Method = static_cast<EConvergenceMethod>( j ); } if( Method >= eNumConvergenceMethods ) { std::cerr << "Line " << nLineNumber << std::endl << "StrainOpt: Unknown ConvergenceMethod, choices are [ Quality fQ | HitRatio fH | Differential fTol ]" << std::endl << std::endl; RUNTIME_ASSERT( 0, " Parsing Strain Optimization File Failed "); } return std::make_pair( Method, InitFileIO::ExtractReal( Line, nLineNumber ) ); }
static size_t fill_buffer_from_fd(const int fd, void * const dst, const size_t buf_size) { char *buf = dst; size_t pos = 0; RUNTIME_ASSERT(buf); RUNTIME_ASSERT(buf_size > 0); RUNTIME_ASSERT((size_t) -1 != buf_size); while (pos < buf_size) { ssize_t ret; size_t size; size = buf_size - pos; ret = read(fd, &buf[pos], size); if ((ssize_t) -1 == ret) { if (!is_temporary_error(errno) || wait_for_fd(fd) < 0) { return -1; } } else if (0 == ret) { if (pos != 0) { errno = EIO; return -1; } return 0; /* EOF */ } else { RUNTIME_ASSERT((size_t) ret <= size); pos += (size_t) ret; } } return 1; }
static void hash_item_free(hashlist_t *hl, hash_item_t *item) { RUNTIME_ASSERT(hl != NULL); RUNTIME_ASSERT(item != NULL); mem_chunk_free(item, sizeof *item); }
static int http_send_str(http_t *ctx, const char *s) { RUNTIME_ASSERT(ctx); RUNTIME_ASSERT(s); return http_send(ctx, s, strlen(s)); }
void ev_watcher_source_closed(ev_watcher_t *w, ev_source_t *evs) { ev_watcher_check(w); ev_source_check(evs); RUNTIME_ASSERT(ev_source_get_watcher(evs) == w); RUNTIME_ASSERT(w->num_sources > 0); }
static int http_send(http_t *ctx, const void *data, size_t size) { RUNTIME_ASSERT(ctx); RUNTIME_ASSERT(ctx->output); RUNTIME_ASSERT(ctx->output->send); return ctx->output->send(ctx->output, data, size); }
/** * Retrieves the payload pointer of the current item. This must not be * used for empty lists. * * @param iter a valid hashlist_iter. * @return the value of the item at the current position. */ void * hashlist_iter_get_value(hashlist_iter_t *iter) { HASHLIST_ITER_CHECK(iter); RUNTIME_ASSERT(iter->item != NULL); RUNTIME_ASSERT(&iter->removed != iter->item); return ((hash_item_t *) iter->item)->value; }
static int http_handle_referer_header(http_t *ctx, const char *name, char *value) { RUNTIME_ASSERT(ctx); RUNTIME_ASSERT(name); RUNTIME_ASSERT(value); ACCLOG_SET(ctx->acclog, referer, value); return 0; }
static int http_handle_proxy_header(http_t *ctx, const char *name, char *value) { RUNTIME_ASSERT(ctx); RUNTIME_ASSERT(name); RUNTIME_ASSERT(value); ctx->proxied = true; return 0; }
int http_send_line(http_t *ctx, const char *header) { RUNTIME_ASSERT(ctx != NULL); RUNTIME_ASSERT(header != NULL); if (0 != http_send_str(ctx, header) || 0 != http_send_str(ctx, "\r\n")) return -1; return 0; }
//-------------------------------------------------------------------------------------------------------- // // Private: ReadExperimentalData // //-------------------------------------------------------------------------------------------------------- Bool ReconstructionSetup::ReadExperimentalData( CSimulationData & oExpData, bool bServerMode ) { const vector< SIntRange > & vFileRangeList = oExpSetup.GetFileRangeList(); const vector< SRange > & vOmegaRangeList = oExpSetup.GetOmegaRangeList(); const vector< CDetector > & vDetectorList = oExpSetup.GetDetectorList(); RUNTIME_ASSERT( vFileRangeList.size() == vDetectorList.size() && vDetectorList.size() > 0, "ERROR: File range not specified for some detector(s) \n" ); // // TODO: This needs to be more robust // oExpData.Initialize( vOmegaRangeList.size(), vDetectorList.size(), vDetectorList[0].GetNumCols(), vDetectorList[0].GetNumRows() ); for ( Size_Type nDetector = 0; nDetector < vFileRangeList.size(); nDetector ++ ) { Size_Type nNumFile = vFileRangeList[ nDetector ].nHigh - vFileRangeList[ nDetector ].nLow + 1; RUNTIME_ASSERT( nNumFile == vOmegaRangeList.size(), "ERROR: Number of files does not match specified number of omega inteval\n"); // Using -- openmp -- TODO - limit number of threads? #pragma omp parallel for for ( Int nFileNum = vFileRangeList[nDetector].nLow; nFileNum <= vFileRangeList[nDetector].nHigh; nFileNum ++ ) { Int nCurrentFileIndex = nFileNum - vFileRangeList[nDetector].nLow; stringstream ssFilename; ssFilename << InputParameters().InFileBasename << InitFileIO::NumToSuffix( nFileNum, InputParameters().InFileSerialLength ) << "." << InputParameters().InFileExt << nDetector + oExpSetup.GetBCDetectorOffset(); Bool bSuccess = false; switch( InputParameters().InFileType ) { case eASCII: bSuccess= oExpData.mImageMap[ nCurrentFileIndex ][ nDetector ].ReadCXDMSimulationDataFile( ssFilename.str() ); break; case eBin: bSuccess= oExpData.mImageMap[ nCurrentFileIndex ][ nDetector ].ReadCXDMSimulationUFFFile( ssFilename.str(), bServerMode ); break; case eTif: RUNTIME_ASSERT( 0, "Error: Tif input format not yet supported\n" ); break; default: RUNTIME_ASSERT( 0, "Error: Input File Type Not Recognized" ); } DEBUG_ALERT( 0, ssFilename.str() + "\n" ); RUNTIME_ASSERT( bSuccess, "Error! Input data files cannot be read, please check file format and location\n" + ssFilename.str() ); } } return true; }
static int http_handle_if_modified_since_header(http_t *ctx, const char *name, char *value) { RUNTIME_ASSERT(ctx); RUNTIME_ASSERT(name); RUNTIME_ASSERT(value); ctx->if_modified_since = 0 == http_parse_date(value, &ctx->ims_buf.tm) ? &ctx->ims_buf.tm : NULL; return 0; }
static int http_handle_range_header(http_t *ctx, const char *name, char *value) { RUNTIME_ASSERT(ctx); RUNTIME_ASSERT(name); RUNTIME_ASSERT(value); if (0 == http_parse_range(value, &ctx->range_set)) { DBUG("Ignoring unparsable Range header"); } return 0; }
int dnslookup(dnslookup_ctx_t *ctx, const char *host, int *error) { RUNTIME_ASSERT(ctx); RUNTIME_ASSERT(host); RUNTIME_ASSERT(error); ctx->i = 0; ctx->he = gethostbyname(host); if (!ctx->he) { *error = h_errno; } return (ctx->he && ctx->he->h_addrtype == AF_INET) ? 0 : -1; }
//------------------------------------------------------------------------ // InitializeWithDataFiles // // Purpose: This is the central point to read from all data/initialization // file. // Note: This function or InitializeWithRestore must be called beore // any reconstruction! //------------------------------------------------------------------------ void ReconstructionSetup::InitializeWithDataFiles( const CConfigFile & oConfigFile, bool bServerMode ) { oExpSetup.SetConfigFile( oConfigFile ); std::cerr << "Before initialization " << std::endl; oExpSetup.InitializeExperiment( ); // read in FZ files, omega files std::cerr << "Before reading experimental data " << std::endl; ReadExperimentalData( oExpData, bServerMode ); // read in experimental data std::cerr << "After reading experimental data " << std::endl; vector<SVector3> oFZEulerAngleList; bool bSuccess = InitFileIO::ReadFundamentalZoneFile( oFZEulerAngleList, InputParameters().FundamentalZoneFilename ); RUNTIME_ASSERT( bSuccess, "InitializeWithDataFiles: Fundamental Zone File Not Found!\n"); for ( Size_Type i = 0; i < oFZEulerAngleList.size(); i ++) { SMatrix3x3 oOrient; oOrient.BuildActiveEulerMatrix( oFZEulerAngleList[i].m_fX, oFZEulerAngleList[i].m_fY, oFZEulerAngleList[i].m_fZ ); oFZOrientationList.push_back( oOrient ); } const vector< CDetector > & vDetectorList = oExpSetup.GetDetectorList(); oExpSetup.InitializeSample( oReconstructionLayer, vDetectorList[ 0 ] ); // binding CurrentLayer to the first detector's limit // // TODO: Initialization should become uniform (or group together somewhere) // FPeakFilter.fMinEta = - InputParameters().fEtaLimit; FPeakFilter.fMaxEta = InputParameters().fEtaLimit; FPeakIntensityAccept.fMinEta = - InputParameters().fEtaLimit; FPeakIntensityAccept.fMaxEta = InputParameters().fEtaLimit; pPartialResult = MicIOFactory::Create( InputParameters().nMicGridType ); // initialize partial result // Read partial result if( InputParameters().bUsePartialResult ) { const string & filename = InputParameters().PartialResultFilename; std::cout << "before reading partial result " << std::endl; // select out the correct result RUNTIME_ASSERT( pPartialResult->Read( filename ), "Failed to read partial result at: " + filename ); } }
/////////////////////////////////////////////////////////// // // // ReadRotationIntervalFile (a.k.a "omegafiles") // // Given a filename, read in the range of data collection interval and // the experimental file range // // TODO: Create a more flexible format to replace the omega files // bool ReadRotationIntervalFiles( vector<SRange> &oRotationRange, vector<SIntRange> &oFileRange, Size_Type nNumFileRange, const string & filename ) { char *pBuffer = NULL; Size_Type nBufSize = 0; vector< vector<string> > vsTokens; pBuffer = ReadFileToBuf(nBufSize, filename); if(pBuffer == NULL){ return false; } string sBuffStr( pBuffer, nBufSize ); GeneralLib::Tokenize( vsTokens, sBuffStr, ",# \t\n"); RUNTIME_ASSERT( vsTokens.size() >= 4, "[ReadRotationIntervalFile] ERROR: Invalid file type (too short)\n"); // conform to existing omega file format, the first line is ignored for ( Size_Type i = 1; i <= nNumFileRange; i ++) { SIntRange s; s.nLow = atoi( vsTokens[i][0].c_str() ); s.nHigh = atoi( vsTokens[i][1].c_str() ); oFileRange.push_back( s ); } // // Get the omega ranges // for(Size_Type i = oFileRange.size() + 1; i < vsTokens.size(); i ++){ RUNTIME_ASSERT( (vsTokens[i].size() >= 2), "[ReadRotationIntervalFiles] ERROR: Invalid file range\n"); SRange s; s.fLow = DEGREE_TO_RADIAN( atof( vsTokens[i][0].c_str() ) ); s.fHigh = DEGREE_TO_RADIAN( atof( vsTokens[i][1].c_str() ) ); oRotationRange.push_back( s ); } delete [] pBuffer; return true; }
/** * Inserts a new node to the list before the current position. The nodes * payload pointer will be set to ``ptr''. * * @param iter a valid list_iter. * @param ptr the payload pointer for the new node. * @return false if the item could not be prepended. Returns true on success. */ bool list_iter_prepend(list_iter_t *iter, void *ptr) { node_t *n; LIST_ITER_CHECK(iter); /* Items must not be prepended to removed items! */ RUNTIME_ASSERT(&iter->removed != iter->node); n = node_new(ptr); if (!n) return false; n->prev = iter->node ? iter->node->prev : NULL; n->next = iter->node; if (!iter->l->last) iter->l->last = n; if (iter->l->first == iter->node) iter->l->first = n; if (iter->node) iter->node->prev = n; iter->stamp++; iter->l->stamp++; iter->l->length++; LIST_CHECK(iter->l); LIST_ITER_CHECK(iter); return true; }
static void ev_watcher_collect_garbage(ev_watcher_t *w) { struct timeval tv; size_t i; ev_watcher_check(w); compat_mono_time(&tv); if (DIFFTIMEVAL(&tv, &w->last_gc) < 500) { return; } w->last_gc = tv; i = w->size_sources; while (i-- > 0) { ev_source_t *evs; evs = w->sources[i]; if (evs) { ev_source_check(evs); if (ev_source_is_closed(evs)) { ev_source_unref(evs); w->sources[i] = NULL; RUNTIME_ASSERT(w->num_sources > 0); w->num_sources--; } else { /* Checks and delivers timeout */ ev_source_event(evs, EVT_NONE, &tv); } } } }
bool dnslookup_next(dnslookup_ctx_t *ctx, uint32_t *addr) { const void *p; RUNTIME_ASSERT(ctx); RUNTIME_ASSERT(addr); *addr = 0; /* Just in case */ p = ctx->he->h_addr_list[ctx->i]; if (p) { memcpy(addr, p, sizeof *addr); ctx->i++; return true; } return false; }
int http_send_status_line(http_t *ctx, unsigned int code, const char *msg) { char buf[256], *p = buf; size_t size = sizeof buf; RUNTIME_ASSERT(code >= 100 && code <= 999); ACCLOG_SET(ctx->acclog, code, code); p = append_string(p, &size, "HTTP/1.1 "); p = append_uint(p, &size, code); p = append_char(p, &size, ' '); p = append_string(p, &size, msg); #if 0 DBUG("Sending HTTP response \"%s\"", buf); #endif p = APPEND_CRLF(p, &size); p = APPEND_STATIC_CHARS(p, &size, "Date: "); p = append_date(p, &size, compat_mono_time(NULL)); p = APPEND_CRLF(p, &size); if ( 0 != http_send(ctx, buf, sizeof buf - size) || 0 != http_send_str(ctx, http_server_header) ) return -1; return 0; }
/** * Sets the default values for the given <major>.<minor> HTTP version. */ void http_set_defaults(http_t *ctx) { RUNTIME_ASSERT(ctx); ctx->keep_alive = http_at_least_ver(ctx, 1, 1) ? true : false; }
void dnslookup_ctx_free(dnslookup_ctx_t *ctx) { RUNTIME_ASSERT(ctx); dnslookup_ctx_reset(ctx); DO_FREE(ctx); }
void RestrictedStratifiedGrid<SamplePointT, SamplePointGrid> ::InitializeRestart( const Mic & RecRegion_, Mic & RestartPoint, Float fMinAcceptThresh, // temporarily not using this - may need to add this back to improve efficiency Float fMinSideLength_, SVector3 oSampleCenter_, Float fSampleRadius_ ) { Initialize( RecRegion_, fMinSideLength_, oSampleCenter_, fSampleRadius_ ); typedef typename Mic::VoxelType_const_iterator const_iter; int nPartialVoxelsAdded = 0; if( RestartPoint.GetVoxels().size() > 0 ) { Utilities::SetMinimumResolution( RestartPoint, fMinSideLength_ ); // make sure that side length is correct for( const_iter pCur = RestartPoint.VoxelListBegin(); pCur != RestartPoint.VoxelListEnd(); ++ pCur ) { SamplePointPtr pCenter = SolutionGrid( *pCur ); RUNTIME_ASSERT( SamplePointGrid::IsValid( pCenter ), "[InitializeRestart:] Unexpected result from outside the solution region\n" ); *pCenter = *pCur; // pCenter->nID = MultiStagedDetails::NOT_VISITED; // should allow option to set this pCenter->nID = MultiStagedDetails::NOT_VISITED; nPartialVoxelsAdded ++; } } std::cout << "PartialVoxel Added: " << nPartialVoxelsAdded << std::endl; }
static int http_handle_transfer_encoding_header(http_t *ctx, const char *name, char *value) { RUNTIME_ASSERT(ctx); RUNTIME_ASSERT(name); RUNTIME_ASSERT(value); if (0 == strcasecmp(value, "chunked")) { ctx->encoding = HTTP_TRANSFER_ENCODING_CHUNKED; return 0; } http_set_status(ctx, 400, "Bad Request (Bad Transfer-Encoding)"); return -1; }
/** * @return * -1 if a header was invalid (see http status for details). * 0 if the end-of-headers was reached. * 1 if a header was successfully parsed. */ int http_parse_header(http_t *ctx, char *buf, size_t buf_size, char **name_ptr, char **value_ptr) { char *value; RUNTIME_ASSERT(ctx); RUNTIME_ASSERT(HTTP_STATE_HEADERS == ctx->state); if (name_ptr) *name_ptr = NULL; if (value_ptr) *value_ptr = NULL; switch (http_read_header(ctx, buf, buf_size)) { case -1: DBUG("http_read_header() failed: %u \"%s\"", ctx->status_code, ctx->status_msg); return -1; case 0: ctx->state = HTTP_STATE_BODY; return 0; case 1: break; default: RUNTIME_ASSERT(0); http_set_status(ctx, 500, "Internal Server Error"); return -1; } value = http_parse_header_name(buf); if (!value) { http_set_status(ctx, 400, "Bad Request (Bad Header)"); return -1; } if (name_ptr) *name_ptr = buf; if (value_ptr) *value_ptr = skip_spaces(value); return 1; }
static int http_handle_connection_header(http_t *ctx, const char *name, char *value) { const char *p; RUNTIME_ASSERT(ctx); RUNTIME_ASSERT(name); RUNTIME_ASSERT(value); for (p = skip_spaces(value); '\0' != *p; p = skip_spaces(p)) { const char *token; size_t len; token = p; while (http_is_tokenchar(*p)) p++; len = p - token; while ('\0' != *p && !http_is_tokenchar(*p)) p++; #define TOKEN_MATCHES(x) \ (len == (sizeof (x) - 1) && NULL != skip_ci_prefix(token, (x))) if (len > 0) { if (TOKEN_MATCHES("close")) { ctx->keep_alive = false; #if 0 DBUG("keep-alive disabled"); #endif } else if (TOKEN_MATCHES("keep-alive")) { ctx->keep_alive = true; #if 0 DBUG("keep-alive enabled"); #endif } else { DBUG("Unknown Connection value: \"%s\"", token); } } #undef TOKEN_MATCHES } return 0; }
/** * Retrieves the payload pointer of the current node. This must not be * used for empty lists unless list_iter points to the previously deleted * only node. * * @param iter a valid list_iter. * @return the payload pointer of the node at the current position. */ void * list_iter_get_ptr(list_iter_t *iter) { LIST_ITER_CHECK(iter); RUNTIME_ASSERT(iter->node != NULL); return iter->node->ptr; }