int main(int argc, char** argv) { struct AdminTestFramework* fw = AdminTestFramework_setUp(argc, argv, "UDPInterface_test"); // mock interface controller. struct InterfaceController ifController = { .registerPeer = registerPeer }; UDPInterface_admin_register(fw->eventBase, fw->alloc, fw->logger, fw->admin, &ifController); Dict* dict = Dict_new(fw->alloc); struct AdminClient_Promise* promise = AdminClient_rpcCall(String_CONST("UDPInterface_new"), dict, fw->client, fw->alloc); promise->callback = ifNewCallback; promise->userData = fw; EventBase_beginLoop(fw->eventBase); // bad key dict = Dict_new(fw->alloc); Dict_putString(dict, String_CONST("publicKey"), String_CONST("notAValidKey"), fw->alloc); Dict_putString(dict, String_CONST("address"), String_CONST("127.0.0.1:12345"), fw->alloc); promise = AdminClient_rpcCall( String_CONST("UDPInterface_beginConnection"), dict, fw->client, fw->alloc); promise->callback = badKeyCallback; promise->userData = fw; EventBase_beginLoop(fw->eventBase); dict = Dict_new(fw->alloc); Dict_putString(dict, String_CONST("publicKey"), String_CONST("c86pf0ngj3wlb569juqm10yzv29n9t4w5tmsyhx6xd3fbqjlcu50.k"), fw->alloc); Dict_putString(dict, String_CONST("address"), String_CONST("127.0.0.1:12345"), fw->alloc); promise = AdminClient_rpcCall( String_CONST("UDPInterface_beginConnection"), dict, fw->client, fw->alloc); promise->callback = goodCallback; promise->userData = fw; EventBase_beginLoop(fw->eventBase); AdminTestFramework_tearDown(fw); return 0; }
static void rpcCall0(String* function, Dict* args, struct Context* ctx, struct Allocator* alloc, bool exitIfError) { struct AdminClient_Result* res = AdminClient_rpcCall(function, args, ctx->client, alloc); if (res->err) { Log_critical(ctx->logger, "Failed to make function call [%s], error: [%s]", AdminClient_errorString(res->err), function->bytes); die(res, ctx, alloc); } String* error = Dict_getString(res->responseDict, String_CONST("error")); if (error && !String_equals(error, String_CONST("none"))) { if (exitIfError) { Log_critical(ctx->logger, "Got error [%s] calling [%s]", error->bytes, function->bytes); die(res, ctx, alloc); } Log_warn(ctx->logger, "Got error [%s] calling [%s], ignoring.", error->bytes, function->bytes); } }
static void checkRunningInstance(struct Allocator* allocator, struct EventBase* base, String* addr, String* password, struct Log* logger, struct Except* eh) { struct Allocator* alloc = Allocator_child(allocator); struct Sockaddr_storage pingAddrStorage; if (Sockaddr_parse(addr->bytes, &pingAddrStorage)) { Except_raise(eh, -1, "Unable to parse [%s] as an ip address port, eg: 127.0.0.1:11234", addr->bytes); } struct AdminClient* adminClient = AdminClient_new(&pingAddrStorage.addr, password, base, logger, alloc); // 100 milliseconds is plenty to wait for a process to respond on the same machine. adminClient->millisecondsToWait = 100; Dict* pingArgs = Dict_new(alloc); struct AdminClient_Result* pingResult = AdminClient_rpcCall(String_new("ping", alloc), pingArgs, adminClient, alloc); if (pingResult->err == AdminClient_Error_NONE) { Except_raise(eh, -1, "Startup failed: cjdroute is already running."); } Allocator_free(alloc); }
static void standardClient(struct Context* ctx) { ctx->called = false; struct AdminClient_Promise* promise = AdminClient_rpcCall(String_CONST("adminFunc"), NULL, ctx->framework->client, ctx->framework->alloc); promise->callback = standardClientCallback; promise->userData = ctx; EventBase_beginLoop(ctx->framework->eventBase); }
static int rpcCall0(String* function, Dict* args, struct Context* ctx, struct Allocator* alloc, Dict** resultP, bool exitIfError) { ctx->currentReqAlloc = Allocator_child(alloc); ctx->currentResult = NULL; struct AdminClient_Promise* promise = AdminClient_rpcCall(function, args, ctx->client, alloc); promise->callback = rpcCallback; promise->userData = ctx; EventBase_beginLoop(ctx->base); struct AdminClient_Result* res = ctx->currentResult; Assert_true(res); if (res->err) { Log_critical(ctx->logger, "Failed to make function call [%s], error: [%s]", AdminClient_errorString(res->err), function->bytes); die(res, ctx, alloc); } String* error = Dict_getString(res->responseDict, String_CONST("error")); int ret = 0; if (error && !String_equals(error, String_CONST("none"))) { if (exitIfError) { Log_critical(ctx->logger, "Got error [%s] calling [%s]", error->bytes, function->bytes); die(res, ctx, alloc); } Log_warn(ctx->logger, "Got error [%s] calling [%s], ignoring.", error->bytes, function->bytes); ret = 1; } if (resultP) { *resultP = res->responseDict; } else { Allocator_free(ctx->currentReqAlloc); } ctx->currentReqAlloc = NULL; return ret; }
static void die(struct AdminClient_Result* res, struct Context* ctx, struct Allocator* alloc) { Log_keys(ctx->logger, "message bytes = [%s]", res->messageBytes); #ifndef Log_KEYS Log_critical(ctx->logger, "enable Log_LEVEL=KEYS to see message content."); #endif Dict d = NULL; struct AdminClient_Result* exitRes = AdminClient_rpcCall(String_CONST("Core_exit"), &d, ctx->client, alloc); if (exitRes->err) { Log_critical(ctx->logger, "Failed to stop the core."); } Log_critical(ctx->logger, "Aborting."); exit(1); }
static void checkRunningInstance(struct Allocator* allocator, struct EventBase* base, String* addr, String* password, struct Log* logger, struct Except* eh) { struct Allocator* alloc = Allocator_child(allocator); struct Sockaddr_storage pingAddrStorage; if (Sockaddr_parse(addr->bytes, &pingAddrStorage)) { Except_throw(eh, "Unable to parse [%s] as an ip address port, eg: 127.0.0.1:11234", addr->bytes); } struct AdminClient* adminClient = AdminClient_new(&pingAddrStorage.addr, password, base, logger, alloc); // 100 milliseconds is plenty to wait for a process to respond on the same machine. adminClient->millisecondsToWait = 100; Dict* pingArgs = Dict_new(alloc); struct AdminClient_Promise* pingPromise = AdminClient_rpcCall(String_new("ping", alloc), pingArgs, adminClient, alloc); struct CheckRunningInstanceContext* ctx = Allocator_malloc(alloc, sizeof(struct CheckRunningInstanceContext)); ctx->base = base; ctx->alloc = alloc; ctx->res = NULL; pingPromise->callback = checkRunningInstanceCallback; pingPromise->userData = ctx; EventBase_beginLoop(base); Assert_true(ctx->res); if (ctx->res->err != AdminClient_Error_TIMEOUT) { Except_throw(eh, "Startup failed: cjdroute is already running. [%d]", ctx->res->err); } Allocator_free(alloc); }
static void die(struct AdminClient_Result* res, struct Context* ctx, struct Allocator* alloc) { Log_keys(ctx->logger, "message bytes = [%s]", res->messageBytes); #ifndef Log_KEYS Log_critical(ctx->logger, "enable Log_LEVEL=KEYS to see message content."); #endif Dict d = NULL; struct AdminClient_Promise* exitPromise = AdminClient_rpcCall(String_CONST("Core_exit"), &d, ctx->client, alloc); exitPromise->callback = rpcCallback; exitPromise->userData = ctx; EventBase_beginLoop(ctx->base); if (ctx->currentResult->err) { Log_critical(ctx->logger, "Failed to stop the core."); } Log_critical(ctx->logger, "Aborting."); exit(1); }
static void rpcCall(String* function, Dict* args, struct Context* ctx, struct Allocator* alloc) { struct AdminClient_Result* res = AdminClient_rpcCall(function, args, ctx->client, alloc); if (res->err) { Log_critical2(ctx->logger, "Failed to make function call [%s], error: [%s]", AdminClient_errorString(res->err), function->bytes); showMsg(res, ctx); exit(-1); } String* error = Dict_getString(res->responseDict, String_CONST("error")); if (error && !String_equals(error, String_CONST("none"))) { Log_critical2(ctx->logger, "Router responses with error: [%s]\nCalling function: [%s]", error->bytes, function->bytes); showMsg(res, ctx); exit(-1); } }