]> git.stg.codes - stg.git/blobdiff - projects/rlm_stg/iface.cpp
Limited waiting for response.
[stg.git] / projects / rlm_stg / iface.cpp
index 485e9ef7439d9b3d1d5854e0a38429b8c6df472c..d99dd393f8037aeadd9482befb4d9d7d5bd5e65c 100644 (file)
@@ -1,6 +1,7 @@
 #include "iface.h"
 
 #include "stg_client.h"
+#include "radlog.h"
 
 #include <cstring>
 
@@ -9,6 +10,27 @@
 namespace
 {
 
+struct Response
+{
+    bool done;
+    pthread_mutex_t mutex;
+    pthread_cond_t cond;
+    RESULT result;
+    bool status;
+
+    static bool callback(void* data, const RESULT& result, bool status)
+    {
+        Response& resp = *static_cast<Response*>(data);
+        pthread_mutex_lock(&resp.mutex);
+        resp.result = result;
+        resp.status = status;
+        resp.done = true;
+        pthread_cond_signal(&resp.cond);
+        pthread_mutex_unlock(&resp.mutex);
+        return true;
+    }
+} response;
+
 STG_PAIR* toSTGPairs(const PAIRS& source)
 {
     STG_PAIR * pairs = new STG_PAIR[source.size() + 1];
@@ -17,7 +39,6 @@ STG_PAIR* toSTGPairs(const PAIRS& source)
         bzero(pairs[pos].value, sizeof(STG_PAIR::value));
         strncpy(pairs[pos].key, source[pos].first.c_str(), sizeof(STG_PAIR::key));
         strncpy(pairs[pos].value, source[pos].second.c_str(), sizeof(STG_PAIR::value));
-        ++pos;
     }
     bzero(pairs[source.size()].key, sizeof(STG_PAIR::key));
     bzero(pairs[source.size()].value, sizeof(STG_PAIR::value));
@@ -64,13 +85,31 @@ STG_RESULT stgRequest(STG_CLIENT::TYPE type, const char* userName, const char* p
 {
     STG_CLIENT* client = STG_CLIENT::get();
     if (client == NULL) {
-        // TODO: log "Not configured"
+        RadLog("Client is not configured.");
         return emptyResult();
     }
     try {
-        return toResult(client->request(type, toString(userName), toString(password), fromSTGPairs(pairs)));
+        if (!client->connected())
+        {
+            if (!STG_CLIENT::reconnect())
+                return emptyResult();
+            client = STG_CLIENT::get();
+        }
+        response.done = false;
+        client->request(type, toString(userName), toString(password), fromSTGPairs(pairs));
+        pthread_mutex_lock(&response.mutex);
+        timespec ts;
+        clock_gettime(CLOCK_REALTIME, &ts);
+        ts.tv_sec += 5;
+        int res = 0;
+        while (!response.done && res == 0)
+            res = pthread_cond_timedwait(&response.cond, &response.mutex, &ts);
+        pthread_mutex_unlock(&response.mutex);
+        if (res != 0 || !response.status)
+            return emptyResult();
+        return toResult(response.result);
     } catch (const STG_CLIENT::Error& ex) {
-        // TODO: log error
+        RadLog("Error: '%s'.", ex.what());
         return emptyResult();
     }
 }
@@ -79,7 +118,11 @@ STG_RESULT stgRequest(STG_CLIENT::TYPE type, const char* userName, const char* p
 
 int stgInstantiateImpl(const char* address)
 {
-    if (STG_CLIENT::configure(toString(address)))
+    pthread_mutex_init(&response.mutex, NULL);
+    pthread_cond_init(&response.cond, NULL);
+    response.done = false;
+
+    if (STG_CLIENT::configure(toString(address), &Response::callback, &response))
         return 1;
 
     return 0;