Skip to content

Commit d60165b

Browse files
committed
Adding more codes to rpc loader
1 parent 63b3ff1 commit d60165b

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

source/loaders/rpc_loader/source/rpc_loader_impl.cpp

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <reflect/reflect_function.h>
2727
#include <reflect/reflect_scope.h>
2828
#include <reflect/reflect_context.h>
29+
#include <memory/memory_allocator_std.h>
2930
//#include <serial/serial.h>
3031

3132
#include <log/log.h>
@@ -66,7 +67,8 @@ typedef struct loader_impl_rpc_type
6667

6768
typedef struct JsonData
6869
{
69-
std::string url();
70+
std::string url;
71+
std::string response;
7072
} JsonData;
7173

7274
void jsondata_constructor(JsonData *thiz, std::string url)
@@ -262,11 +264,19 @@ static loader_impl_rpc_function rpc_function_create(loader_impl_rpc_handle handl
262264

263265
static size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
264266
{
265-
267+
size_t realsize = size * nmemb;
268+
JsonData *jd = (JsonData*)userp;
269+
char *buf = static_cast<char *>(buffer);
270+
for (size_t i = 0; i < realsize; i++)
271+
{
272+
jd->response.append(&buf[i]);
273+
}
274+
266275
}
267276

268277
int rpc_loader_impl_discover(loader_impl impl, loader_handle handle, context ctx)
269278
{
279+
loader_impl_rpc rpc_impl = (loader_impl_rpc)loader_impl_get(impl);
270280
FILE *fp = fopen(METACALL, "r");
271281
if (fp == NULL)
272282
{
@@ -285,9 +295,9 @@ int rpc_loader_impl_discover(loader_impl impl, loader_handle handle, context ctx
285295
{
286296
if(endpoint->length() == 0){
287297
continue;
288-
} else if(endpoint->length > 0){
298+
} else if(endpoint->length() > 0){
289299
vctrSrings.push_back(*endpoint);
290-
endpoint = new std::string();
300+
endpoint->clear();
291301
}
292302
}
293303
else
@@ -296,8 +306,28 @@ int rpc_loader_impl_discover(loader_impl impl, loader_handle handle, context ctx
296306
}
297307
}
298308

309+
memory_allocator allocator = memory_allocator_std(&malloc, &realloc, &free);
310+
JsonData jd;
311+
312+
for (auto &&i : vctrSrings)
313+
{
314+
jsondata_constructor(&jd, i);
315+
i.append("/inspect");
316+
curl_easy_setopt(rpc_impl->curl, CURLOPT_WRITEFUNCTION, write_data);
317+
curl_easy_setopt(rpc_impl->curl, CURLOPT_WRITEDATA, &jd);
318+
curl_easy_setopt(rpc_impl->curl, CURLOPT_URL, i);
319+
CURLcode res = curl_easy_perform(rpc_impl->curl);
320+
if(res != CURLE_OK && jd.response.length() == 0) {
321+
// Do some logging here
322+
continue;
323+
}
324+
command_inspect(jd.response.c_str(), jd.response.length(), allocator, [](const std::string, size_t, void *){
325+
// TODO get params and create objects
326+
});
327+
328+
}
299329
// TODO handle inpect funtion output
300-
// Free all Prisoners(pointers)
330+
// Free all Prisoners(pointers)0.0
301331
}
302332

303333
int rpc_loader_impl_destroy(loader_impl impl)
@@ -330,7 +360,7 @@ void value_map_for_each(void *v, const std::function<void(const char *, void *)>
330360
});
331361
}
332362

333-
static void command_inspect(const char *str, size_t size, void *allocator, std::function<void(const std::string, size_t, void *)> &functionLambda)
363+
static void command_inspect(const char *str, size_t size, memory_allocator allocator, const std::function<void(const std::string, size_t, void *)> &functionLambda)
334364
{
335365
void *v = metacall_deserialize(metacall_serial(), str, size, allocator);
336366

0 commit comments

Comments
 (0)