Debian Codegraph @

demo/client/client.cpp

raw
1 /*
2 * Copyright (C) 2024 KylinSoft Co., Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License as published by the Free Software
6 * Foundation, either version 3 of the License, or (at your option) any later
7 * version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include <gio/gio.h>
18
19 #include <iostream>
20
21 #include "exampleglue.h"
22 #include "log.h"
23 #include "response.h"
24
25 using namespace std;
26
27 GDBusConnection* connection;
28 AiBusinessExample* proxy;
29 GMainLoop* loop;
30 const char* objectPath = "/com/kylin/AiBusiness/example";
31 const char* interfaceName = "com.kylinos.aisdk.business.example";
32
33 // 同步接口
34 void callMethod1() {
35 gchar* output;
36 string param = R"({"input1":"hello","input2":"world","input3":10})";
37 GError* error = nullptr;
38
39 gboolean success = ai_business_example_call_sync_method_sync(proxy, param.c_str(), &output, nullptr, &error);
40 if (!success) {
41 if (error) {
42 PRINT_ERROR("Call method1 Error: %s\n", error->message);
43 g_error_free(error);
44 return;
45 }
46 }
47 PRINT_INFO("Call method1 result: %s\n", output);
48 }
49
50 void callBack2(GObject* source_object, GAsyncResult* res, gpointer user_data) {
51 PRINT_INFO("callBack2 回调成功\n");
52
53 GError* error = nullptr;
54 gchar* out_response;
55 ai_business_example_call_async_method_finish((AiBusinessExample*)source_object, &out_response, res, &error);
56 if (error) {
57 PRINT_ERROR("Call method2 Error: %s\n", error->message);
58 g_error_free(error);
59 return;
60 }
61 PRINT_INFO("Call method2 response: %s\n", out_response);
62 }
63
64 // 异步接口
65 void callMethod2() {
66 string param = R"({"input1":"method2","input2":"world","input3":10})";
67
68 ai_business_example_call_async_method(proxy, param.c_str(), nullptr, callBack2, nullptr);
69
70 PRINT_INFO("Call method2 success.\n");
71 }
72
73 void callBack3Continue(GDBusConnection* conn, const gchar* sender_name, const gchar* object_path,
74 const gchar* interface_name, const gchar* signal_name, GVariant* parameters,
75 gpointer user_data) {
76 PRINT_INFO("callBack3Continue 流式返回\n");
77 // cout << sender_name << endl; 打印有问题
78 // cout << object_path << endl;
79 // cout << interface_name << endl;
80 // cout << signal_name << endl;
81
82 Response response = Response::parseFromGVariant(parameters);
83 PRINT_INFO("callBack3Continue response: %s\n", response.toString().c_str());
84
85 if (!response.isSuccess()) {
86 g_main_loop_quit(loop);
87 }
88 }
89
90 void callBack3(GObject* source_object, GAsyncResult* res, gpointer user_data) {
91 PRINT_INFO("callBack3 回调成功\n");
92
93 GError* error = nullptr;
94 char* out_response;
95 ai_business_example_call_async_flow_method_finish(reinterpret_cast<AiBusinessExample*>(source_object),
96 &out_response, res, &error);
97 if (error) {
98 PRINT_ERROR("Call method3 Error: %s\n", error->message);
99 g_error_free(error);
100 return;
101 }
102 PRINT_INFO("Call method3 response: %s\n", out_response);
103 }
104
105 // 异步流式返回接口
106 void callMethod3() {
107 string param = R"({"request":"method3"})";
108 ai_business_example_call_async_flow_method(proxy, param.c_str(), nullptr, nullptr, nullptr);
109 PRINT_INFO("call method3 success.\n");
110 }
111
112 void startServer() {
113 GError* error = nullptr;
114
115 PRINT_INFO("start connect\n");
116 std::string serverUnixPath =
117 "unix:abstract=/tmp/.kylin-ai-business-unix/" + std::to_string(getuid()) + "/example.sock";
118 connection = g_dbus_connection_new_for_address_sync(
119 serverUnixPath.c_str(), G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT, nullptr, nullptr, &error);
120 if (connection == nullptr) {
121 PRINT_ERROR("Error connecting to D-Bus address %s: %s\n", serverUnixPath.c_str(), error->message);
122 g_error_free(error);
123 return;
124 }
125
126 PRINT_INFO("Connected. Negotiated capabilities: unix-fd-passing=%d\n",
127 g_dbus_connection_get_capabilities(connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
128
129 proxy =
130 ai_business_example_proxy_new_sync(connection, G_DBUS_PROXY_FLAGS_NONE, nullptr, objectPath, nullptr, &error);
131 if (proxy == nullptr) {
132 PRINT_ERROR("Error creating speech processor proxy %s: %s\n", objectPath, error->message);
133 g_error_free(error);
134 return;
135 }
136 PRINT_INFO("connected server.\n");
137 }
138
139 int main() {
140 startServer();
141
142 // 同步请求
143 callMethod1();
144 callMethod1();
145 // 异步请求
146 callMethod2();
147 callMethod2();
148 // 异步流式返回
149 unsigned int ret =
150 g_dbus_connection_signal_subscribe(connection, nullptr, interfaceName, "callBack3Continue", objectPath, nullptr,
151 G_DBUS_SIGNAL_FLAGS_NONE, callBack3Continue, nullptr, nullptr);
152 std::cout << "g_dbus_connection_signal_subscribe ret = " << ret << std::endl;
153 callMethod3();
154 callMethod3();
155
156 loop = g_main_loop_new(nullptr, false);
157 g_main_loop_run(loop);
158
159 return 0;
160 }