3 #include "stg_client.h"
15 pthread_mutex_t mutex;
19 static bool callback(void* data, const RESULT& res)
21 Response& resp = *static_cast<Response*>(data);
22 pthread_mutex_lock(&resp.mutex);
25 pthread_cond_signal(&resp.cond);
26 pthread_mutex_unlock(&resp.mutex);
31 STG_PAIR* toSTGPairs(const PAIRS& source)
33 STG_PAIR * pairs = new STG_PAIR[source.size() + 1];
34 for (size_t pos = 0; pos < source.size(); ++pos) {
35 bzero(pairs[pos].key, sizeof(STG_PAIR::key));
36 bzero(pairs[pos].value, sizeof(STG_PAIR::value));
37 strncpy(pairs[pos].key, source[pos].first.c_str(), sizeof(STG_PAIR::key));
38 strncpy(pairs[pos].value, source[pos].second.c_str(), sizeof(STG_PAIR::value));
41 bzero(pairs[source.size()].key, sizeof(STG_PAIR::key));
42 bzero(pairs[source.size()].value, sizeof(STG_PAIR::value));
47 PAIRS fromSTGPairs(const STG_PAIR* pairs)
49 const STG_PAIR* pair = pairs;
52 while (!emptyPair(pair)) {
53 res.push_back(std::pair<std::string, std::string>(pair->key, pair->value));
60 STG_RESULT toResult(const RESULT& source)
63 result.modify = toSTGPairs(source.modify);
64 result.reply = toSTGPairs(source.reply);
68 STG_RESULT emptyResult()
70 STG_RESULT result = {NULL, NULL};
74 std::string toString(const char* value)
82 STG_RESULT stgRequest(STG_CLIENT::TYPE type, const char* userName, const char* password, const STG_PAIR* pairs)
84 STG_CLIENT* client = STG_CLIENT::get();
86 // TODO: log "Not configured"
90 client->request(type, toString(userName), toString(password), fromSTGPairs(pairs));
91 pthread_mutex_lock(&response.mutex);
92 while (!response.done)
93 pthread_cond_wait(&response.cond, &response.mutex);
94 pthread_mutex_unlock(&response.mutex);
95 return toResult(response.result);
96 } catch (const STG_CLIENT::Error& ex) {
104 int stgInstantiateImpl(const char* address)
106 pthread_mutex_init(&response.mutex, NULL);
107 pthread_cond_init(&response.cond, NULL);
108 response.done = false;
110 if (STG_CLIENT::configure(toString(address), &Response::callback, &response))
116 STG_RESULT stgAuthorizeImpl(const char* userName, const char* password, const STG_PAIR* pairs)
118 return stgRequest(STG_CLIENT::AUTHORIZE, userName, password, pairs);
121 STG_RESULT stgAuthenticateImpl(const char* userName, const char* password, const STG_PAIR* pairs)
123 return stgRequest(STG_CLIENT::AUTHENTICATE, userName, password, pairs);
126 STG_RESULT stgPostAuthImpl(const char* userName, const char* password, const STG_PAIR* pairs)
128 return stgRequest(STG_CLIENT::POST_AUTH, userName, password, pairs);
131 STG_RESULT stgPreAcctImpl(const char* userName, const char* password, const STG_PAIR* pairs)
133 return stgRequest(STG_CLIENT::PRE_ACCT, userName, password, pairs);
136 STG_RESULT stgAccountingImpl(const char* userName, const char* password, const STG_PAIR* pairs)
138 return stgRequest(STG_CLIENT::ACCOUNT, userName, password, pairs);