-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.cpp
76 lines (62 loc) · 2.65 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <valhalla/worker.h>
#include "main.h"
#include "valhalla_actor.h"
#ifdef __ANDROID__
// The Android JNI interface uses a different function signature.
#include <jni.h>
extern "C"
JNIEXPORT jstring
JNICALL
Java_com_valhalla_valhalla_ValhallaKotlin_route(JNIEnv *env,
jobject thiz,
jstring jRequest,
jstring jConfigPath) {
const char *request = env->GetStringUTFChars(jRequest, 0);
const char *config_path = env->GetStringUTFChars(jConfigPath, 0);
std::string result;
try {
// TODO: Android currently creates a new actor every time. Optimize to be like iOS later.
ValhallaActor valhallaActor(config_path);
result = valhallaActor.route(request);
} catch (const valhalla::valhalla_exception_t &err) {
printf("[ValhallaActor] route valhalla_exception: %s\n", err.what());
std::string code = std::to_string(err.code);
std::string message = err.message.c_str();
result = "{\"code\":" + code + ",\"message\":\"" + message + "\"}";
} catch (const std::exception &err) {
printf("[ValhallaActor] route std::exception: %s\n", err.what());
result = "{\"code\":-1,\"message\":\"" + std::string(err.what()) + "\"}";
} catch (...) {
printf("[ValhallaActor] route unknown exception");
result = "{\"code\":-1,\"message\":\"unknown exception\"}";
}
env->ReleaseStringUTFChars(jRequest, request);
env->ReleaseStringUTFChars(jConfigPath, config_path);
return env->NewStringUTF(result.c_str());
}
#elif __APPLE__
void* create_valhalla_actor(const char *config_path) {
return new ValhallaActor(config_path);
}
void delete_valhalla_actor(void* actor) {
delete ((ValhallaActor*) actor);
}
std::string route(const char *request, void* actor) {
std::string result;
try {
result = ((ValhallaActor*) actor)->route(request);
} catch (const valhalla::valhalla_exception_t &err) {
printf("[ValhallaActor] route valhalla_exception: %s\n", err.what());
std::string code = std::to_string(err.code);
std::string message = err.message.c_str();
result = "{\"code\":" + code + ",\"message\":\"" + message + "\"}";
} catch (const std::exception &err) {
printf("[ValhallaActor] route std::exception: %s\n", err.what());
result = "{\"code\":-1,\"message\":\"" + std::string(err.what()) + "\"}";
} catch (...) {
printf("[ValhallaActor] route unknown exception");
result = "{\"code\":-1,\"message\":\"unknown exception\"}";
}
return result;
}
#endif