3 #include "stg_client.h"
16 pthread_mutex_t mutex;
21 static bool callback(void* data, const RESULT& result, bool status)
23 Response& resp = *static_cast<Response*>(data);
24 pthread_mutex_lock(&resp.mutex);
28 pthread_cond_signal(&resp.cond);
29 pthread_mutex_unlock(&resp.mutex);
34 STG_PAIR* toSTGPairs(const PAIRS& source)
36 STG_PAIR * pairs = new STG_PAIR[source.size() + 1];
37 for (size_t pos = 0; pos < source.size(); ++pos) {
38 bzero(pairs[pos].key, sizeof(STG_PAIR::key));
39 bzero(pairs[pos].value, sizeof(STG_PAIR::value));
40 strncpy(pairs[pos].key, source[pos].first.c_str(), sizeof(STG_PAIR::key));
41 strncpy(pairs[pos].value, source[pos].second.c_str(), sizeof(STG_PAIR::value));
43 bzero(pairs[source.size()].key, sizeof(STG_PAIR::key));
44 bzero(pairs[source.size()].value, sizeof(STG_PAIR::value));
49 PAIRS fromSTGPairs(const STG_PAIR* pairs)
51 const STG_PAIR* pair = pairs;
54 while (!emptyPair(pair)) {
55 res.push_back(std::pair<std::string, std::string>(pair->key, pair->value));
62 STG_RESULT toResult(const RESULT& source)
65 result.modify = toSTGPairs(source.modify);
66 result.reply = toSTGPairs(source.reply);
70 STG_RESULT emptyResult()
72 STG_RESULT result = {NULL, NULL};
76 std::string toString(const char* value)
84 STG_RESULT stgRequest(STG_CLIENT::TYPE type, const char* userName, const char* password, const STG_PAIR* pairs)
86 STG_CLIENT* client = STG_CLIENT::get();
88 RadLog("Client is not configured.");
92 if (!client->connected())
94 if (!STG_CLIENT::reconnect())
96 client = STG_CLIENT::get();
98 response.done = false;
99 client->request(type, toString(userName), toString(password), fromSTGPairs(pairs));
100 pthread_mutex_lock(&response.mutex);
102 clock_gettime(CLOCK_REALTIME, &ts);
105 while (!response.done && res == 0)
106 res = pthread_cond_timedwait(&response.cond, &response.mutex, &ts);
107 pthread_mutex_unlock(&response.mutex);
108 if (res != 0 || !response.status)
109 return emptyResult();
110 return toResult(response.result);
111 } catch (const STG_CLIENT::Error& ex) {
112 RadLog("Error: '%s'.", ex.what());
113 return emptyResult();
119 int stgInstantiateImpl(const char* address)
121 pthread_mutex_init(&response.mutex, NULL);
122 pthread_cond_init(&response.cond, NULL);
123 response.done = false;
125 if (STG_CLIENT::configure(toString(address), &Response::callback, &response))
131 STG_RESULT stgAuthorizeImpl(const char* userName, const char* password, const STG_PAIR* pairs)
133 return stgRequest(STG_CLIENT::AUTHORIZE, userName, password, pairs);
136 STG_RESULT stgAuthenticateImpl(const char* userName, const char* password, const STG_PAIR* pairs)
138 return stgRequest(STG_CLIENT::AUTHENTICATE, userName, password, pairs);
141 STG_RESULT stgPostAuthImpl(const char* userName, const char* password, const STG_PAIR* pairs)
143 return stgRequest(STG_CLIENT::POST_AUTH, userName, password, pairs);
146 STG_RESULT stgPreAcctImpl(const char* userName, const char* password, const STG_PAIR* pairs)
148 return stgRequest(STG_CLIENT::PRE_ACCT, userName, password, pairs);
151 STG_RESULT stgAccountingImpl(const char* userName, const char* password, const STG_PAIR* pairs)
153 return stgRequest(STG_CLIENT::ACCOUNT, userName, password, pairs);