/*! \fn printStatus * * \param [in] [solverData] * \param [in] [nfunc_evals] * \param [in] [xerror] * \param [in] [xerror_scaled] * \param [in] [logLevel] * * \author wbraun */ static void printStatus(DATA_HYBRD *solverData, const int *nfunc_evals, const double *xerror, const double *xerror_scaled, const int logLevel) { int i; INFO(logLevel, "nls status"); INDENT(logLevel); INFO(logLevel, "variables"); INDENT(logLevel); for(i=0; i<solverData->n; i++) INFO3(logLevel, "x[%d] = %.20e [scaling factor = %16e]", i, solverData->x[i], solverData->diag[i]); RELEASE(logLevel); INFO(logLevel, "functions"); INDENT(logLevel); for(i=0; i<solverData->n; i++) INFO3(logLevel, "res[%d] = %.20e [scaling factor = %16e]", i, solverData->fvec[i], solverData->resScaling[i]); RELEASE(logLevel); INFO(logLevel, "statistics"); INDENT(logLevel); INFO3(logLevel, "nfunc = %d\nerror = %.20e\nerror_scaled = %.20e", *nfunc_evals, *xerror, *xerror_scaled); RELEASE(logLevel); RELEASE(logLevel); }
int alert_push(int event, const char *msg) { /* 日志以及调试功能回调入口 */ switch(event){ case ERR_BASE: case ERR_VERIFY: case ERR_BRIDGE: case ERR_PORT: case ERR_CTRL_BRIDGE: case ERR_CTRL_PORT: ERROR2(GLOBAL_OUT_GROUP,"%s\n", msg); break; case ERR_MSTI: ERROR2(MSTI_GROUP,"%s\n", msg); break; case ALERT_BASE: case ALERT_BRIDGE: case ALERT_PORT: break; case INFO_BRIDGE_TXBPDU: case INFO_PORT_TXBPDU: INFO2(BPDU_TX_GROUP,"%s\n", msg); break; case DEBUG_TXBPDU: INFO3(BPDU_TX_GROUP,"%s\n", msg); break; case INFO_BRIDGE_RXBPDU: case INFO_PORT_RXBPDU: INFO2(BPDU_RX_GROUP,"%s\n", msg); break; case DEBUG_RXBPDU: INFO3(BPDU_RX_GROUP,"%s\n", msg); break; case INFO_MSTI: INFO2(MSTI_GROUP,"%s\n", msg); break; case INFO_BASE: case INFO_BRIDGE: case INFO_PORT: case INFO_CTRL_BRIDGE: case INFO_CTRL_PORT: case INFO_CTRL_BRIDGE_MAC: INFO2(GLOBAL_OUT_GROUP,"%s\n", msg); break; case DEBUG_INFO: break; case NOTIFY_MSG: send_notify(event, msg); break; default: break; }; return 0; }
/* This does the actual connection for a relay. A thread is * started off if a connection can be acquired */ int open_relay (relay_server *relay) { source_t *src = relay->source; relay_server_master *master = relay->masters; client_t *client = src->client; do { int ret; if (master->skip) { INFO3 ("skipping %s:%d for %s", master->ip, master->port, relay->localmount); continue; } thread_mutex_unlock (&src->lock); ret = open_relay_connection (client, relay, master); thread_mutex_lock (&src->lock); if (ret < 0) continue; if (connection_complete_source (src) < 0) { WARN1 ("Failed to complete initialisation on %s", relay->localmount); continue; } return 1; } while ((master = master->next) && global.running == ICE_RUNNING); return -1; }
void yp_recheck_config (ice_config_t *config) { int i; struct yp_server *server; DEBUG0("Updating YP configuration"); thread_rwlock_rlock (&yp_lock); server = (struct yp_server *)active_yps; while (server) { server->remove = 1; server = server->next; } /* for each yp url in config, check to see if one exists if not, then add it. */ for (i=0 ; i < config->num_yp_directories; i++) { server = find_yp_server (config->yp_url[i]); if (server == NULL) { server = calloc (1, sizeof (struct yp_server)); if (server == NULL) { destroy_yp_server (server); break; } server->url = strdup (config->yp_url[i]); server->url_timeout = config->yp_url_timeout[i]; server->touch_interval = config->yp_touch_interval[i]; server->curl = curl_easy_init(); if (server->curl == NULL) { destroy_yp_server (server); break; } if (server->touch_interval < 30) server->touch_interval = 30; curl_easy_setopt (server->curl, CURLOPT_URL, server->url); curl_easy_setopt (server->curl, CURLOPT_HEADERFUNCTION, handle_returned_header); curl_easy_setopt (server->curl, CURLOPT_WRITEFUNCTION, handle_returned_data); curl_easy_setopt (server->curl, CURLOPT_WRITEDATA, server->curl); curl_easy_setopt (server->curl, CURLOPT_TIMEOUT, server->url_timeout); curl_easy_setopt (server->curl, CURLOPT_NOSIGNAL, 1L); curl_easy_setopt (server->curl, CURLOPT_ERRORBUFFER, &(server->curl_error[0])); server->next = (struct yp_server *)pending_yps; pending_yps = server; INFO3 ("Adding new YP server \"%s\" (timeout %ds, default interval %ds)", server->url, server->url_timeout, server->touch_interval); } else { server->remove = 0; } } thread_rwlock_unlock (&yp_lock); yp_update = 1; }
int dump_rdata(rdata_t *data) { if(data == NULL){ INFO3(output_group, "data is empty"); }else{ rdata_t *t = data; char str[500]; char *p = str; int c = 0; INFO3(output_group, "--------- dunmp RDATA ----------"); while(1){ c = sprintf(p,"%15s ", t->feild); p = p + c; t = t->next; if(t== NULL) break; } INFO3(output_group, "%s", str); t = data; p = str; c = 0; while(1){ switch(t->dtype){ case RDB_DATA_INT: c = sprintf(p,"%15d\t", *(int *)(t->data)); break; case RDB_DATA_STRING: c = sprintf(p,"%15s ", (char *)t->data); break; default: c = sprintf(p,"\0 "); c = 0; break; } p = p + c; t = t->next; if(t== NULL) break; } INFO3(output_group, "%s", str); INFO3(output_group, "-------- dunmp RDATA end --------"); } return LR_OK; }
FILE* sOpen(const char *name, const char *mode, int type) { file f = try_find_file(name, mode, type); if (f.path) { INFO3("opened file `%s' (mode %s)", f.path, mode); free(f.path); } return f.handle; }
/* handler for curl, checks if successful handling occurred * return 0 for ok, -1 for this entry failed, -2 for server fail. * On failure case, update and process are modified */ static int send_to_yp (const char *cmd, ypdata_t *yp, char *post) { int curlcode; struct yp_server *server = yp->server; /* DEBUG2 ("send YP (%s):%s", cmd, post); */ yp->cmd_ok = 0; curl_easy_setopt (server->curl, CURLOPT_POSTFIELDS, post); curl_easy_setopt (server->curl, CURLOPT_WRITEHEADER, yp); curlcode = curl_easy_perform (server->curl); if (curlcode) { yp->process = do_yp_add; yp_schedule (yp, 1200); ERROR2 ("connection to %s failed with \"%s\"", server->url, server->curl_error); return -2; } if (yp->cmd_ok == 0) { if (yp->error_msg == NULL) yp->error_msg = strdup ("no response from server"); if (yp->process == do_yp_add) { ERROR3 ("YP %s on %s failed: %s", cmd, server->url, yp->error_msg); yp_schedule (yp, 7200); } if (yp->process == do_yp_touch) { /* At this point the touch request failed, either because they rejected our session * or the server isn't accessible. This means we have to wait before doing another * add request. We have a minimum delay but we could allow the directory server to * give us a wait time using the TouchFreq header. This time could be given in such * cases as a firewall block or incorrect listenurl. */ if (yp->touch_interval < 1200) yp_schedule (yp, 1200); else yp_schedule (yp, yp->touch_interval); INFO3 ("YP %s on %s failed: %s", cmd, server->url, yp->error_msg); } yp->process = do_yp_add; free (yp->sid); yp->sid = NULL; return -1; } DEBUG2 ("YP %s at %s succeeded", cmd, server->url); return 0; }
/* This does the actual connection for a relay. A thread is * started off if a connection can be acquired */ int open_relay (relay_server *relay) { source_t *src = relay->source; relay_server_master *master = relay->masters; client_t *client = src->client; do { int ret; if (master->skip) { INFO3 ("skipping %s:%d for %s", master->ip, master->port, relay->localmount); continue; } thread_rwlock_unlock (&src->lock); ret = open_relay_connection (client, relay, master); thread_rwlock_wlock (&src->lock); if (ret < 0) continue; return 1; } while ((master = master->next) && global.running == ICE_RUNNING); return -1; }
template <bool F> matrix_type BetheSalpeter<matrix_type, F>::solve_inversion() { if (verbosity_ > 0) INFO_NONEWLINE("Running" << ((!fwd)?" inverse ":" ") << "matrix BS equation..."); size_t size = vertex_.rows(); matrix_type V4Chi = fwd ? matrix_type(matrix_type::Identity(size,size) - vertex_*bubble_) : matrix_type(matrix_type::Identity(size,size) + bubble_*vertex_); Eigen::PartialPivLU<matrix_type> Solver(V4Chi); det_ = Solver.determinant(); assert(is_float_equal(det_, V4Chi.determinant(), 1e-6)); if (std::abs(std::imag(det_))>1e-2 * std::abs(std::real(det_))) { ERROR("Determinant : " << det_); throw (std::logic_error("Complex determinant in BS. Exiting.")); }; if ((std::real(det_))<1e-2) INFO3("Determinant : " << det_); if (std::real(det_) < std::numeric_limits<real_type>::epsilon()) { ERROR("Can't solve Bethe-Salpeter equation by inversion"); return std::move(V4Chi); } V4Chi = fwd ? matrix_type(Solver.solve(vertex_)) : matrix_type(vertex_*Solver.inverse()); //V4Chi=vertex_*Solver.inverse(); if (verbosity_ > 0) INFO("done."); return std::move(V4Chi); }
/*! \fn solve non-linear system with hybrd method * * \param [in] [data] * [sysNumber] index of the corresponing non-linear system * * \author wbraun */ int solveHybrd(DATA *data, int sysNumber) { NONLINEAR_SYSTEM_DATA* systemData = &(data->simulationInfo.nonlinearSystemData[sysNumber]); DATA_HYBRD* solverData = (DATA_HYBRD*)systemData->solverData; /* * We are given the number of the non-linear system. * We want to look it up among all equations. */ int eqSystemNumber = systemData->equationIndex; int i, j; integer iflag = 1; double xerror, xerror_scaled; int success = 0; double local_tol = 1e-12; double initial_factor = solverData->factor; int nfunc_evals = 0; int continuous = 1; int nonContinuousCase = 0; int giveUp = 0; int retries = 0; int retries2 = 0; int retries3 = 0; int assertCalled = 0; int assertRetries = 0; int assertMessage = 0; static state mem_state; modelica_boolean* relationsPreBackup; relationsPreBackup = (modelica_boolean*) malloc(data->modelData.nRelations*sizeof(modelica_boolean)); /* debug output */ if(ACTIVE_STREAM(LOG_NLS)) { INFO2(LOG_NLS, "start solving non-linear system >>%s<< at time %g", modelInfoXmlGetEquation(&data->modelData.modelDataXml, eqSystemNumber).name, data->localData[0]->timeValue); INDENT(LOG_NLS); for(i=0; i<solverData->n; i++) { INFO2(LOG_NLS, "x[%d] = %f", i, systemData->nlsx[i]); INDENT(LOG_NLS); INFO3(LOG_NLS, "scaling = %f\nold = %f\nextrapolated = %f", systemData->nominal[i], systemData->nlsxOld[i], systemData->nlsxExtrapolation[i]); RELEASE(LOG_NLS); } RELEASE(LOG_NLS); } /* set x vector */ if(data->simulationInfo.discreteCall) memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double))); else memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double))); for(i=0; i<solverData->n; i++) solverData->xScalefactors[i] = fmax(fabs(solverData->x[i]), systemData->nominal[i]); /* evaluate with discontinuities */ { int scaling = solverData->useXScaling; if(scaling) solverData->useXScaling = 0; mem_state = get_memory_state(); /* try */ if(!setjmp(nonlinearJmpbuf)) { wrapper_fvec_hybrj(&solverData->n, solverData->x, solverData->fvec, solverData->fjac, &solverData->ldfjac, &iflag, data, sysNumber); restore_memory_state(mem_state); } else { /* catch */ restore_memory_state(mem_state); WARNING(LOG_STDOUT, "Non-Linear Solver try to handle a problem with a called assert."); } if(scaling) { solverData->useXScaling = 1; } } /* start solving loop */ while(!giveUp && !success) { for(i=0; i<solverData->n; i++) solverData->xScalefactors[i] = fmax(fabs(solverData->x[i]), systemData->nominal[i]); /* debug output */ if(ACTIVE_STREAM(LOG_NLS_V)) { printVector(solverData->xScalefactors, &(solverData->n), LOG_NLS_V, "scaling factors x vector"); printVector(solverData->x, &(solverData->n), LOG_NLS_V, "Iteration variable values"); } /* Scaling x vector */ if(solverData->useXScaling) { for(i=0; i<solverData->n; i++) { solverData->x[i] = (1.0/solverData->xScalefactors[i]) * solverData->x[i]; } } /* debug output */ if(ACTIVE_STREAM(LOG_NLS_V)) { printVector(solverData->x, &solverData->n, LOG_NLS_V, "Iteration variable values (scaled)"); } /* set residual function continuous */ if(continuous) { ((DATA*)data)->simulationInfo.solveContinuous = 1; } else { ((DATA*)data)->simulationInfo.solveContinuous = 0; } giveUp = 1; mem_state = get_memory_state(); /* try */ if(!setjmp(nonlinearJmpbuf)) { _omc_hybrj_(wrapper_fvec_hybrj, &solverData->n, solverData->x, solverData->fvec, solverData->fjac, &solverData->ldfjac, &solverData->xtol, &solverData->maxfev, solverData->diag, &solverData->mode, &solverData->factor, &solverData->nprint, &solverData->info, &solverData->nfev, &solverData->njev, solverData->r__, &solverData->lr, solverData->qtf, solverData->wa1, solverData->wa2, solverData->wa3, solverData->wa4, data, sysNumber); restore_memory_state(mem_state); if(assertCalled) { INFO(LOG_NLS, "After asserts was called, values reached which avoided assert call."); memcpy(systemData->nlsxOld, solverData->x, solverData->n*(sizeof(double))); } assertRetries = 0; assertCalled = 0; } else { /* catch */ restore_memory_state(mem_state); if(!assertMessage) { INDENT(LOG_STDOUT); WARNING(LOG_STDOUT, "While solving non-linear system an assert was called."); WARNING(LOG_STDOUT, "The non-linear solver tries to solve the problem that could take some time."); WARNING(LOG_STDOUT, "It could help to provide better start-values for the iteration variables."); WARNING(LOG_STDOUT, "For more information simulate with -lv LOG_NLS"); RELEASE(LOG_STDOUT); assertMessage = 1; } solverData->info = -1; xerror_scaled = 1; xerror = 1; assertCalled = 1; } /* set residual function continuous */ if(continuous) { ((DATA*)data)->simulationInfo.solveContinuous = 0; } else { ((DATA*)data)->simulationInfo.solveContinuous = 1; } /* re-scaling x vector */ if(solverData->useXScaling) for(i=0; i<solverData->n; i++) solverData->x[i] = solverData->x[i]*solverData->xScalefactors[i]; /* check for proper inputs */ if(solverData->info == 0) { printErrorEqSyst(IMPROPER_INPUT, modelInfoXmlGetEquation(&data->modelData.modelDataXml, eqSystemNumber), data->localData[0]->timeValue); } if(solverData->info != -1) { /* evaluate with discontinuities */ if(data->simulationInfo.discreteCall){ int scaling = solverData->useXScaling; if(scaling) solverData->useXScaling = 0; ((DATA*)data)->simulationInfo.solveContinuous = 0; mem_state = get_memory_state(); /* try */ if(!setjmp(nonlinearJmpbuf)) { wrapper_fvec_hybrj(&solverData->n, solverData->x, solverData->fvec, solverData->fjac, &solverData->ldfjac, &iflag, data, sysNumber); restore_memory_state(mem_state); } else { /* catch */ restore_memory_state(mem_state); WARNING(LOG_STDOUT, "Non-Linear Solver try to handle a problem with a called assert."); solverData->info = -1; xerror_scaled = 1; xerror = 1; assertCalled = 1; } if(scaling) solverData->useXScaling = 1; storeRelations(data); } } if(solverData->info != -1) { /* scaling residual vector */ { int l=0; for(i=0; i<solverData->n; i++){ solverData->resScaling[i] = 1e-16; for(j=0; j<solverData->n; j++){ solverData->resScaling[i] = (fabs(solverData->fjacobian[l]) > solverData->resScaling[i]) ? fabs(solverData->fjacobian[l]) : solverData->resScaling[i]; l++; } solverData->fvecScaled[i] = solverData->fvec[i] * (1 / solverData->resScaling[i]); } /* debug output */ if(ACTIVE_STREAM(LOG_NLS_V)) { INFO(LOG_NLS_V, "scaling factors for residual vector"); INDENT(LOG_NLS_V); for(i=0; i<solverData->n; i++) { INFO2(LOG_NLS_V, "scaled residual [%d] : %.20e", i, solverData->fvecScaled[i]); INDENT(LOG_NLS_V); INFO2(LOG_NLS_V, "scaling factor [%d] : %.20e", i, solverData->resScaling[i]); RELEASE(LOG_NLS_V); } RELEASE(LOG_NLS_V); } /* debug output */ if(ACTIVE_STREAM(LOG_NLS_JAC)) { char buffer[4096]; INFO2(LOG_NLS_JAC, "jacobian matrix [%dx%d]", (int)solverData->n, (int)solverData->n); INDENT(LOG_NLS_JAC); for(i=0; i<solverData->n; i++) { buffer[0] = 0; for(j=0; j<solverData->n; j++) sprintf(buffer, "%s%10g ", buffer, solverData->fjacobian[i*solverData->n+j]); INFO1(LOG_NLS_JAC, "%s", buffer); } RELEASE(LOG_NLS_JAC); } /* check for error */ xerror_scaled = enorm_(&solverData->n, solverData->fvecScaled); xerror = enorm_(&solverData->n, solverData->fvec); } } /* reset non-contunuousCase */ if(nonContinuousCase && xerror > local_tol && xerror_scaled > local_tol) { memcpy(data->simulationInfo.relationsPre, relationsPreBackup, sizeof(modelica_boolean)*data->modelData.nRelations); nonContinuousCase = 0; } if(solverData->info < 4 && xerror > local_tol && xerror_scaled > local_tol) solverData->info = 4; /* solution found */ if(solverData->info == 1 || xerror <= local_tol || xerror_scaled <= local_tol) { int scaling; success = 1; nfunc_evals += solverData->nfev; if(ACTIVE_STREAM(LOG_NLS)) { INFO(LOG_NLS, "system solved"); INDENT(LOG_NLS); INFO2(LOG_NLS, "%d retries\n%d restarts", retries, retries2+retries3); RELEASE(LOG_NLS); printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS); } scaling = solverData->useXScaling; if(scaling) solverData->useXScaling = 0; /* take the solution */ memcpy(systemData->nlsx, solverData->x, solverData->n*(sizeof(double))); mem_state = get_memory_state(); /* try */ if(!setjmp(nonlinearJmpbuf)) { wrapper_fvec_hybrj(&solverData->n, solverData->x, solverData->fvec, solverData->fjac, &solverData->ldfjac, &iflag, data, sysNumber); restore_memory_state(mem_state); } else { /* catch */ restore_memory_state(mem_state); WARNING(LOG_STDOUT, "Non-Linear Solver try to handle a problem with a called assert."); solverData->info = 4; xerror_scaled = 1; xerror = 1; assertCalled = 1; success = 0; giveUp = 0; } if(scaling) solverData->useXScaling = 1; } else if((solverData->info == 4 || solverData->info == 5) && assertRetries < 1+solverData->n && assertCalled) { /* case only used, when the Modelica code called an assert * then, we try to modify start values to avoid the assert call.*/ int i; memcpy(solverData->x, systemData->nlsxOld, solverData->n*(sizeof(double))); /* set all zero values to nominal values */ if(assertRetries < 1) { for(i=0; i<solverData->n; i++) { if(systemData->nlsx[i] == 0) { systemData->nlsx[i] = systemData->nominal[i]; solverData->x[i] = systemData->nominal[i]; } } } /* change initial guess values one by one */ else if(assertRetries < solverData->n+1) { i = assertRetries-1; solverData->x[i] += 0.01*systemData->nominal[i]; } giveUp = 0; nfunc_evals += solverData->nfev; assertRetries++; if(ACTIVE_STREAM(LOG_NLS)) { INFO1(LOG_NLS, " - try to handle a problem with a called assert vary initial value a bit. (Retry: %d)",assertRetries); printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V); } } else if((solverData->info == 4 || solverData->info == 5) && retries < 3) { /* first try to decrease factor */ /* set x vector */ if(data->simulationInfo.discreteCall) memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double))); else memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double))); solverData->factor = solverData->factor / 10.0; retries++; giveUp = 0; nfunc_evals += solverData->nfev; if(ACTIVE_STREAM(LOG_NLS)) { INFO1(LOG_NLS, " - iteration making no progress:\t decreasing initial step bound to %f.", solverData->factor); printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V); } } else if((solverData->info == 4 || solverData->info == 5) && retries < 4) { /* try to vary the initial values */ for(i = 0; i < solverData->n; i++) solverData->x[i] += systemData->nominal[i] * 0.1; solverData->factor = initial_factor; retries++; giveUp = 0; nfunc_evals += solverData->nfev; if(ACTIVE_STREAM(LOG_NLS)) { INFO(LOG_NLS, "iteration making no progress:\t vary solution point by 1%%."); printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V); } } else if((solverData->info == 4 || solverData->info == 5) && retries < 5) { /* try old values as x-Scaling factors */ /* set x vector */ if(data->simulationInfo.discreteCall) memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double))); else memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double))); for(i=0; i<solverData->n; i++) solverData->xScalefactors[i] = fmax(fabs(systemData->nlsxOld[i]), systemData->nominal[i]); retries++; giveUp = 0; nfunc_evals += solverData->nfev; if(ACTIVE_STREAM(LOG_NLS)) { INFO(LOG_NLS, "iteration making no progress:\t try old values as scaling factors."); printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V); } } else if((solverData->info == 4 || solverData->info == 5) && retries < 6) { int scaling = 0; /* try to disable x-Scaling */ /* set x vector */ if(data->simulationInfo.discreteCall) memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double))); else memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double))); scaling = solverData->useXScaling; if(scaling) solverData->useXScaling = 0; /* reset x-scalling factors */ for(i=0; i<solverData->n; i++) solverData->xScalefactors[i] = fmax(fabs(solverData->x[i]), systemData->nominal[i]); retries++; giveUp = 0; nfunc_evals += solverData->nfev; if(ACTIVE_STREAM(LOG_NLS)) { INFO(LOG_NLS, "iteration making no progress:\t try without scaling at all."); printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V); } } else if((solverData->info == 4 || solverData->info == 5) && retries < 7 && data->simulationInfo.discreteCall) { /* try to solve non-continuous * work-a-round: since other wise some model does * stuck in event iteration. e.g.: Modelica.Mechanics.Rotational.Examples.HeatLosses */ memcpy(solverData->x, systemData->nlsxOld, solverData->n*(sizeof(double))); retries++; /* try to solve a discontinuous system */ continuous = 0; nonContinuousCase = 1; memcpy(relationsPreBackup, data->simulationInfo.relationsPre, sizeof(modelica_boolean)*data->modelData.nRelations); giveUp = 0; nfunc_evals += solverData->nfev; if(ACTIVE_STREAM(LOG_NLS)) { INFO(LOG_NLS, " - iteration making no progress:\t try to solve a discontinuous system."); printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V); } /* Then try with old values (instead of extrapolating )*/ } else if((solverData->info == 4 || solverData->info == 5) && retries2 < 1) { int scaling = 0; /* set x vector */ memcpy(solverData->x, systemData->nlsxOld, solverData->n*(sizeof(double))); scaling = solverData->useXScaling; if(!scaling) solverData->useXScaling = 1; continuous = 1; solverData->factor = initial_factor; retries = 0; retries2++; giveUp = 0; nfunc_evals += solverData->nfev; if(ACTIVE_STREAM(LOG_NLS)) { INFO(LOG_NLS, " - iteration making no progress:\t use old values instead extrapolated."); printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V); } /* try to vary the initial values */ } else if((solverData->info == 4 || solverData->info == 5) && retries2 < 2) { /* set x vector */ if(data->simulationInfo.discreteCall) memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double))); else memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double))); for(i = 0; i < solverData->n; i++) { solverData->x[i] *= 1.01; }; retries = 0; retries2++; giveUp = 0; nfunc_evals += solverData->nfev; if(ACTIVE_STREAM(LOG_NLS)) { INFO(LOG_NLS, " - iteration making no progress:\t vary initial point by adding 1%%."); printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V); } /* try to vary the initial values */ } else if((solverData->info == 4 || solverData->info == 5) && retries2 < 3) { /* set x vector */ if(data->simulationInfo.discreteCall) memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double))); else memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double))); for(i = 0; i < solverData->n; i++) { solverData->x[i] *= 0.99; }; retries = 0; retries2++; giveUp = 0; nfunc_evals += solverData->nfev; if(ACTIVE_STREAM(LOG_NLS)) { INFO(LOG_NLS, " - iteration making no progress:\t vary initial point by -1%%."); printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V); } /* try to vary the initial values */ } else if((solverData->info == 4 || solverData->info == 5) && retries2 < 4) { /* set x vector */ memcpy(solverData->x, systemData->nominal, solverData->n*(sizeof(double))); retries = 0; retries2++; giveUp = 0; nfunc_evals += solverData->nfev; if(ACTIVE_STREAM(LOG_NLS)) { INFO(LOG_NLS, " - iteration making no progress:\t try scaling factor as initial point."); printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V); } /* try own scaling factors */ } else if((solverData->info == 4 || solverData->info == 5) && retries2 < 5 && !assertCalled) { /* set x vector */ if(data->simulationInfo.discreteCall) memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double))); else memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double))); for(i = 0; i < solverData->n; i++) { solverData->diag[i] = fabs(solverData->resScaling[i]); if(solverData->diag[i] <= 1e-16) solverData->diag[i] = 1e-16; } retries = 0; retries2++; giveUp = 0; solverData->mode = 2; nfunc_evals += solverData->nfev; if(ACTIVE_STREAM(LOG_NLS)) { INFO(LOG_NLS, " - iteration making no progress:\t try with own scaling factors."); printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V); } /* try without internal scaling */ } else if((solverData->info == 4 || solverData->info == 5) && retries3 < 1) { /* set x vector */ if(data->simulationInfo.discreteCall) memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double))); else memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double))); for(i = 0; i < solverData->n; i++) solverData->diag[i] = 1.0; solverData->useXScaling = 1; retries = 0; retries2 = 0; retries3++; solverData->mode = 2; giveUp = 0; nfunc_evals += solverData->nfev; if(ACTIVE_STREAM(LOG_NLS)) { INFO(LOG_NLS, " - iteration making no progress:\t disable solver internal scaling."); printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V); } /* try to reduce the tolerance a bit */ } else if((solverData->info == 4 || solverData->info == 5) && retries3 < 6) { /* set x vector */ if(data->simulationInfo.discreteCall) memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double))); else memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double))); /* reduce tolarance */ local_tol = local_tol*10; solverData->factor = initial_factor; solverData->mode = 1; retries = 0; retries2 = 0; retries3++; giveUp = 0; nfunc_evals += solverData->nfev; if(ACTIVE_STREAM(LOG_NLS)) { INFO1(LOG_NLS, " - iteration making no progress:\t reduce the tolerance slightly to %e.", local_tol); printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V); } } else if(solverData->info >= 2 && solverData->info <= 5) { /* while the initialization it's ok to every time a solution */ if(!data->simulationInfo.initial){ printErrorEqSyst(ERROR_AT_TIME, modelInfoXmlGetEquation(&data->modelData.modelDataXml, eqSystemNumber), data->localData[0]->timeValue); } if(ACTIVE_STREAM(LOG_NLS)) { RELEASE(LOG_NLS); INFO1(LOG_NLS, "### No Solution! ###\n after %d restarts", retries*retries2*retries3); printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS); } /* take the best approximation */ memcpy(systemData->nlsx, solverData->x, solverData->n*(sizeof(double))); } } /* reset some solving data */ solverData->factor = initial_factor; solverData->mode = 1; free(relationsPreBackup); return success; }
static void command_metadata(client_t *client, source_t *source, int response) { const char *action; const char *song, *title, *artist, *charset; format_plugin_t *plugin; xmlDocPtr doc; xmlNodePtr node; doc = xmlNewDoc (XMLSTR("1.0")); node = xmlNewDocNode (doc, NULL, XMLSTR("iceresponse"), NULL); xmlDocSetRootElement(doc, node); DEBUG0("Got metadata update request"); COMMAND_REQUIRE(client, "mode", action); COMMAND_OPTIONAL(client, "song", song); COMMAND_OPTIONAL(client, "title", title); COMMAND_OPTIONAL(client, "artist", artist); COMMAND_OPTIONAL(client, "charset", charset); if (strcmp (action, "updinfo") != 0) { xmlNewChild(node, NULL, XMLSTR("message"), XMLSTR("No such action")); xmlNewChild(node, NULL, XMLSTR("return"), XMLSTR("0")); admin_send_response(doc, client, response, ADMIN_XSL_RESPONSE); xmlFreeDoc(doc); return; } plugin = source->format; if (plugin && plugin->set_tag) { if (song) { plugin->set_tag (plugin, "song", song, charset); INFO2 ("Metadata on mountpoint %s changed to \"%s\"", source->mount, song); } else { if (artist && title) { plugin->set_tag (plugin, "title", title, charset); plugin->set_tag (plugin, "artist", artist, charset); INFO3("Metadata on mountpoint %s changed to \"%s - %s\"", source->mount, artist, title); } } } else { xmlNewChild(node, NULL, XMLSTR("message"), XMLSTR("Mountpoint will not accept URL updates")); xmlNewChild(node, NULL, XMLSTR("return"), XMLSTR("1")); admin_send_response(doc, client, response, ADMIN_XSL_RESPONSE); xmlFreeDoc(doc); return; } xmlNewChild(node, NULL, XMLSTR("message"), XMLSTR("Metadata update successful")); xmlNewChild(node, NULL, XMLSTR("return"), XMLSTR("1")); admin_send_response(doc, client, response, ADMIN_XSL_RESPONSE); xmlFreeDoc(doc); }
/* Actually open the connection and do some http parsing, handle any 302 * responses within here. */ static int open_relay_connection (client_t *client, relay_server *relay, relay_server_master *master) { int redirects = 0; http_parser_t *parser = NULL; connection_t *con = &client->connection; char *server = strdup (master->ip); char *mount = strdup (master->mount); int port = master->port, timeout = master->timeout, ask_for_metadata = relay->mp3metadata; char *auth_header = NULL; if (relay->username && relay->password) { char *esc_authorisation; unsigned len = strlen(relay->username) + strlen(relay->password) + 2; DEBUG2 ("using username %s for %s", relay->username, relay->localmount); auth_header = malloc (len); snprintf (auth_header, len, "%s:%s", relay->username, relay->password); esc_authorisation = util_base64_encode(auth_header); free(auth_header); len = strlen (esc_authorisation) + 24; auth_header = malloc (len); snprintf (auth_header, len, "Authorization: Basic %s\r\n", esc_authorisation); free(esc_authorisation); } while (redirects < 10) { sock_t streamsock; char *bind = NULL; /* policy decision, we assume a source bind even after redirect, possible option */ if (master->bind) bind = strdup (master->bind); if (bind) INFO4 ("connecting to %s:%d for %s, bound to %s", server, port, relay->localmount, bind); else INFO3 ("connecting to %s:%d for %s", server, port, relay->localmount); con->con_time = time (NULL); relay->in_use = master; streamsock = sock_connect_wto_bind (server, port, bind, timeout); free (bind); if (connection_init (con, streamsock, server) < 0) { WARN2 ("Failed to connect to %s:%d", server, port); break; } parser = get_relay_response (con, mount, server, ask_for_metadata, auth_header); if (parser == NULL) { ERROR4 ("Problem trying to start relay on %s (%s:%d%s)", relay->localmount, server, port, mount); break; } if (strcmp (httpp_getvar (parser, HTTPP_VAR_ERROR_CODE), "302") == 0) { /* better retry the connection again but with different details */ const char *uri, *mountpoint; int len; uri = httpp_getvar (parser, "location"); INFO1 ("redirect received %s", uri); if (strncmp (uri, "http://", 7) != 0) break; uri += 7; mountpoint = strchr (uri, '/'); free (mount); if (mountpoint) mount = strdup (mountpoint); else mount = strdup ("/"); len = strcspn (uri, ":/"); port = 80; if (uri [len] == ':') port = atoi (uri+len+1); free (server); server = calloc (1, len+1); strncpy (server, uri, len); connection_close (con); httpp_destroy (parser); parser = NULL; } else { if (httpp_getvar (parser, HTTPP_VAR_ERROR_MESSAGE)) { ERROR3 ("Error from relay request on %s (%s %s)", relay->localmount, master->mount, httpp_getvar(parser, HTTPP_VAR_ERROR_MESSAGE)); client->parser = NULL; break; } sock_set_blocking (streamsock, 0); thread_rwlock_wlock (&relay->source->lock); client->parser = parser; // old parser will be free in the format clear thread_rwlock_unlock (&relay->source->lock); client->connection.discon_time = 0; client->connection.con_time = time (NULL); client_set_queue (client, NULL); free (server); free (mount); free (auth_header); return 0; } redirects++; } /* failed, better clean up */ free (server); free (mount); free (auth_header); if (parser) httpp_destroy (parser); connection_close (con); con->con_time = time (NULL); // sources count needs to drop in such cases if (relay->in_use) relay->in_use->skip = 1; return -1; }
/* rval < 0: error, > 0: have audio or video */ int mm_open_fp(mm_file *mf, FILE *file) { int retval = -1; int res = 0; int have_vorbis = 0; int have_theora = 0; ogg_page pg; assert(mf); memset(mf, 0, sizeof(*mf)); mf->file = file; // This is important. If there is no file // then we need to reset the audio and video // pointers so that the other functions // ignore the file. if (!mf->file) { mf->audio = NULL; mf->video = NULL; return retval; } ogg_sync_init(&mf->sync); /* get first page to start things up */ if (get_page(mf, &pg) <= 0) { goto err; } DEBUG1("trying theora decoder..."); res = init_theora(mf, &pg); if (res < 0) { goto err; } else { have_theora = !!res * MEDIA_VIDEO; } DEBUG1("trying vorbis decoder..."); res = init_vorbis(mf, &pg); if (res < 0) { goto err; } else { have_vorbis = !!res * MEDIA_AUDIO; } if (have_vorbis) { unsigned c = 0, r = 0; mm_audio_info(mf, &c, &r); INFO3("audio %u channel(s) at %u Hz", c, r); } if (have_theora) { unsigned w, h; float fps; mm_video_info(mf, &w, &h, &fps); INFO4("video %ux%u pixels at %g fps", w, h, fps); } return have_vorbis | have_theora; err: WARNING1("unable to decode stream"); mm_close(mf); return retval; }
static auth_result url_add_listener (auth_client *auth_user) { client_t *client = auth_user->client; auth_t *auth = auth_user->auth; auth_url *url = auth->state; auth_thread_data *atd = auth_user->thread_data; int res = 0, ret = AUTH_FAILED, poffset = 0; struct build_intro_contents *x; char *userpwd = NULL, post [8192]; if (url->addurl == NULL || client == NULL) return AUTH_OK; if (url->stop_req_until) { time_t now = time(NULL); if (url->stop_req_until <= now) { INFO1 ("restarting url after timeout on %s", auth_user->mount); url->stop_req_until = 0; } else { if (auth->flags & AUTH_SKIP_IF_SLOW) { client->flags |= CLIENT_AUTHENTICATED; return AUTH_OK; } return AUTH_FAILED; } } do { ice_config_t *config = config_get_config (); char *user_agent, *username, *password, *mount, *ipaddr, *referer, *current_listeners, *server = util_url_escape (config->hostname); int port = config->port; config_release_config (); const char *tmp = httpp_getvar (client->parser, "user-agent"); if (tmp == NULL) tmp = "-"; user_agent = util_url_escape (tmp); if (client->username) username = util_url_escape (client->username); else username = strdup (""); if (client->password) password = util_url_escape (client->password); else password = strdup (""); /* get the full uri (with query params if available) */ tmp = httpp_getvar (client->parser, HTTPP_VAR_QUERYARGS); snprintf (post, sizeof post, "%s%s", auth_user->mount, tmp ? tmp : ""); mount = util_url_escape (post); ipaddr = util_url_escape (client->connection.ip); tmp = httpp_getvar (client->parser, "referer"); referer = tmp ? util_url_escape (tmp) : strdup (""); current_listeners = stats_get_value(auth->mount, "listeners"); if (current_listeners == NULL) current_listeners = strdup(""); poffset = snprintf (post, sizeof (post), "action=listener_add&server=%s&port=%d&client=%" PRIu64 "&mount=%s" "&user=%s&pass=%s&ip=%s&agent=%s&referer=%s&listeners=%s", server, port, client->connection.id, mount, username, password, ipaddr, user_agent, referer, current_listeners); free (current_listeners); free (server); free (mount); free (referer); free (user_agent); free (username); free (password); free (ipaddr); if (poffset < 0 || poffset >= sizeof (post)) { WARN2 ("client from %s (on %s), rejected with headers problem", &client->connection.ip[0], auth_user->mount); return AUTH_FAILED; } } while (0); if (url->header_chk_list) { int c = url->header_chk_count, remaining = sizeof(post) - poffset; char *cur_header = url->header_chk_list; const char *prefix = (url->header_chk_prefix && isalnum (url->header_chk_prefix[0])) ? url->header_chk_prefix : "ClientHeader-"; for (; c ; c--) { int len = strlen (cur_header); const char *val = httpp_getvar (client->parser, cur_header); if (val) { char *valesc = util_url_escape (val); int r = remaining > 0 ? snprintf (post+poffset, remaining, "&%s%s=%s", prefix, cur_header, valesc) : -1; free (valesc); if (r < 0 || r > remaining) { WARN2 ("client from %s (on %s), rejected with too much in headers", &client->connection.ip[0], auth_user->mount); return AUTH_FAILED; } poffset += r; remaining -= r; } cur_header += (len + 1); // get past next nul } } if (strchr (url->addurl, '@') == NULL) { if (url->userpwd) curl_easy_setopt (atd->curl, CURLOPT_USERPWD, url->userpwd); else { /* auth'd requests may not have a user/pass, but may use query args */ if (client->username && client->password) { int len = strlen (client->username) + strlen (client->password) + 2; userpwd = malloc (len); snprintf (userpwd, len, "%s:%s", client->username, client->password); curl_easy_setopt (atd->curl, CURLOPT_USERPWD, userpwd); } else curl_easy_setopt (atd->curl, CURLOPT_USERPWD, ""); } } else { /* url has user/pass but libcurl may need to clear any existing settings */ curl_easy_setopt (atd->curl, CURLOPT_USERPWD, ""); } curl_easy_setopt (atd->curl, CURLOPT_URL, url->addurl); curl_easy_setopt (atd->curl, CURLOPT_POSTFIELDS, post); curl_easy_setopt (atd->curl, CURLOPT_WRITEHEADER, auth_user); curl_easy_setopt (atd->curl, CURLOPT_WRITEDATA, auth_user); atd->errormsg[0] = '\0'; free (atd->location); atd->location = NULL; /* setup in case intro data is returned */ x = (void *)client->refbuf->data; x->type = 0; x->head = NULL; x->intro_len = 0; x->tailp = &x->head; DEBUG2 ("handler %d (%s) sending request", auth_user->handler, auth_user->mount); res = curl_easy_perform (atd->curl); DEBUG2 ("handler %d (%s) request finished", auth_user->handler, auth_user->mount); free (userpwd); if (client->flags & CLIENT_AUTHENTICATED) { if (client->flags & CLIENT_HAS_INTRO_CONTENT) { client->refbuf->next = x->head; DEBUG3 ("intro (%d) received %lu for %s", x->type, (unsigned long)x->intro_len, client->connection.ip); } if (x->head == NULL) client->flags &= ~CLIENT_HAS_INTRO_CONTENT; x->head = NULL; ret = AUTH_OK; } if (res) { url->stop_req_until = time (NULL) + url->stop_req_duration; /* prevent further attempts for a while */ WARN3 ("auth to server %s (%s) failed with %s", url->addurl, auth_user->mount, atd->errormsg); INFO1 ("will not auth new listeners for %d seconds", url->stop_req_duration); if (auth->flags & AUTH_SKIP_IF_SLOW) { client->flags |= CLIENT_AUTHENTICATED; ret = AUTH_OK; } } /* better cleanup memory */ while (x->head) { refbuf_t *n = x->head; x->head = n->next; n->next = NULL; refbuf_release (n); } if (x->type) mpeg_cleanup (&x->sync); if (atd->location) { client_send_302 (client, atd->location); auth_user->client = NULL; free (atd->location); atd->location = NULL; } else if (atd->errormsg[0]) { INFO3 ("listener %s (%s) returned \"%s\"", client->connection.ip, url->addurl, atd->errormsg); if (atoi (atd->errormsg) == 403) { auth_user->client = NULL; client_send_403 (client, atd->errormsg+4); } } return ret; }