static void context_callback(pa_context *context, void *userdata) { struct ausrv *ausrv = (struct ausrv *)userdata; int err = 0; const char *strerr; if (context == NULL) { LOG_ERROR("%s() called with zero context", __FUNCTION__); return; } if (ausrv == NULL || ausrv->context != context) { LOG_ERROR("%s(): Confused with data structures", __FUNCTION__); return; } switch (pa_context_get_state(context)) { case PA_CONTEXT_CONNECTING: TRACE("ausrv: connecting to server"); set_connection_status(ausrv, DISCONNECTED); break; case PA_CONTEXT_AUTHORIZING: TRACE("ausrv: authorizing"); set_connection_status(ausrv, DISCONNECTED); break; case PA_CONTEXT_SETTING_NAME: TRACE("ausrv: setting name"); set_connection_status(ausrv, DISCONNECTED); break; case PA_CONTEXT_READY: TRACE("ausrv: connection established."); set_connection_status(ausrv, CONNECTED); cancel_timer(ausrv); LOG_INFO("Pulse Audio OK"); break; case PA_CONTEXT_TERMINATED: TRACE("ausrv: connection to server terminated"); goto disconnect; case PA_CONTEXT_FAILED: default: if ((err = pa_context_errno(context)) != 0) { if ((strerr = pa_strerror(err)) == NULL) strerr = "<unknown>"; LOG_ERROR("ausrv: server connection failure: %s", strerr); } disconnect: set_connection_status(ausrv, DISCONNECTED); stream_kill_all(ausrv); restart_timer(ausrv, CONNECT_DELAY); } }
/* * Disconnect from database * returns 0 if disconnection was needed */ int oralog_db_disconnect() { pthread_mutex_lock(&oralog_oper_lock); if (!oralog_is_connected()) { debug("[logsoracle] attemping to disconnect a dead connection\n"); pthread_mutex_unlock(&oralog_oper_lock); return 1; } debug("[logsoracle] disconnecting.. "); free_global_handles(); set_connection_status(0); debug("disconnected\n"); pthread_mutex_unlock(&oralog_oper_lock); return 0; }
/* * Connect to database. * db_login - database login * db_password - database password * return : DB_CONNECT_NEW_CONNECTION if a new connection was estabilished * DB_CONNECT_ALREADY_CONNECTED or * DB_CONNECT_ERROR */ int oralog_db_connect(char *db_login, char *db_password, int quiet) { int print_errors = (quiet) ? 0 : 1; int errors = 0; sword retval = 0; OCIError *hp_error = NULL; pthread_mutex_lock(&oralog_oper_lock); if (oralog_is_connected()) { debug("[logsoracle] already connected\n"); pthread_mutex_unlock(&oralog_oper_lock); return DB_CONNECT_ALREADY_CONNECTED; } debug("[logsoracle] connecting.. "); /* initialize the mode to be the threaded and object environment */ OCIEnvCreate(&hp_env, OCI_THREADED|OCI_OBJECT, (dvoid *)0, 0, 0, 0, (size_t) 0, (dvoid **)0); /* allocate a server handle */ OCIHandleAlloc((dvoid *)hp_env, (dvoid **)&hp_server, OCI_HTYPE_SERVER, 0, (dvoid **) 0); /* allocate an error handle */ OCIHandleAlloc((dvoid *)hp_env, (dvoid **)&hp_error, OCI_HTYPE_ERROR, 0, (dvoid **) 0); /* create a server context */ /* TODO: dblink can be set here */ retval = OCIServerAttach(hp_server, hp_error, (text *)0, 0, OCI_DEFAULT); if(oralog_is_error(hp_error, retval, print_errors)) errors++; /* allocate a service handle */ OCIHandleAlloc((dvoid *)hp_env, (dvoid **)&hp_service, OCI_HTYPE_SVCCTX, 0, (dvoid **) 0); /* associate server handle with service handle*/ retval = OCIAttrSet((dvoid *)hp_service, OCI_HTYPE_SVCCTX, (dvoid *)hp_server, (ub4) 0, OCI_ATTR_SERVER, hp_error); if(oralog_is_error(hp_error, retval, print_errors)) errors++; /* allocate a session handle */ OCIHandleAlloc((dvoid *)hp_env, (dvoid **)&hp_session, OCI_HTYPE_SESSION, 0, (dvoid **) 0); /* set username in session handle */ retval = OCIAttrSet((dvoid *)hp_session, OCI_HTYPE_SESSION, (dvoid *)db_login, (ub4)ora_strlen(db_login), OCI_ATTR_USERNAME, hp_error); if(oralog_is_error(hp_error, retval, print_errors)) errors++; /* set password in session handle */ retval = OCIAttrSet((dvoid *)hp_session, OCI_HTYPE_SESSION, (dvoid *)db_password, (ub4)ora_strlen(db_password), OCI_ATTR_PASSWORD, hp_error); if(oralog_is_error(hp_error, retval, print_errors)) errors++; retval = OCISessionBegin ((dvoid *)hp_service, hp_error, hp_session, OCI_CRED_RDBMS, OCI_DEFAULT); if(oralog_is_error(hp_error, retval, print_errors)) errors++; /* associate session with service context */ retval = OCIAttrSet ((dvoid *)hp_service, OCI_HTYPE_SVCCTX, (dvoid *)hp_session, (ub4) 0, OCI_ATTR_SESSION, hp_error); if(oralog_is_error(hp_error, retval, print_errors)) errors++; /* free local handles */ if(hp_error) OCIHandleFree(hp_error, OCI_HTYPE_ERROR); if (errors) { debug("[logsoracle] errors encounterd - cleaning up connection\n"); free_global_handles(); set_connection_status(0); pthread_mutex_unlock(&oralog_oper_lock); return DB_CONNECT_ERROR; } set_connection_status(1); debug("connected\n"); pthread_mutex_unlock(&oralog_oper_lock); return DB_CONNECT_NEW_CONNECTION; }