| 1 |
/* |
| 2 |
* Copyright 2022 PixlOne |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 3 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <ipcgull/interface.h> |
| 20 |
#include <ipcgull/node.h> |
| 21 |
#include <ipcgull/server.h> |
| 22 |
#include <iostream> |
| 23 |
#include <thread> |
| 24 |
|
| 25 |
#define SERVER_NAME "pizza.pixl.ipcgull.test" |
| 26 |
#define SERVER_ROOT "/pizza/pixl/ipcgull_test" |
| 27 |
|
| 28 |
constexpr ipcgull::connection_mode default_mode = ipcgull::IPCGULL_USER; |
| 29 |
|
| 30 |
class sample_object : public ipcgull::object { |
| 31 |
int x; |
| 32 |
public: |
| 33 |
explicit sample_object(int i = 0) : x{i} {} |
| 34 |
|
| 35 |
sample_object& operator=(int o) { |
| 36 |
x = o; |
| 37 |
return *this; |
| 38 |
} |
| 39 |
|
| 40 |
operator int&() { |
| 41 |
return x; |
| 42 |
} |
| 43 |
|
| 44 |
operator const int&() const { |
| 45 |
return x; |
| 46 |
} |
| 47 |
}; |
| 48 |
|
| 49 |
class sample_interface : public ipcgull::interface { |
| 50 |
private: |
| 51 |
std::weak_ptr<ipcgull::server> _server; |
| 52 |
std::weak_ptr<ipcgull::node> owner; |
| 53 |
|
| 54 |
static std::string echo(const std::string& input) { |
| 55 |
return input; |
| 56 |
} |
| 57 |
|
| 58 |
static void print(const std::string& input) { |
| 59 |
std::cout << input << std::endl; |
| 60 |
} |
| 61 |
|
| 62 |
void stop() { |
| 63 |
if (auto server = _server.lock()) { |
| 64 |
server->stop(); |
| 65 |
} else { |
| 66 |
throw std::runtime_error("null server"); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
void drop() { |
| 71 |
if (auto owner_sp = owner.lock()) |
| 72 |
owner_sp->drop_interface("pizza.pixl.ipcgull.test.sample"); |
| 73 |
} |
| 74 |
|
| 75 |
static void set_sample_object(const std::shared_ptr<sample_object>& o, |
| 76 |
const int& x) { |
| 77 |
*o = x; |
| 78 |
} |
| 79 |
|
| 80 |
static int get_sample_object(const std::shared_ptr<sample_object>& o) { |
| 81 |
return *o; |
| 82 |
} |
| 83 |
|
| 84 |
static std::tuple<std::string, int> cut_string(const std::string& in) { |
| 85 |
return {in.substr(0, 5), in.length()}; |
| 86 |
} |
| 87 |
|
| 88 |
public: |
| 89 |
sample_interface(const std::shared_ptr<ipcgull::server>& server, |
| 90 |
const std::shared_ptr<ipcgull::node>& o, |
| 91 |
const ipcgull::property<int>& ret) : |
| 92 |
ipcgull::interface("pizza.pixl.ipcgull.test.sample", { |
| 93 |
{"Echo", {echo, {"input"}, {"output"}}}, |
| 94 |
{"Print", {print, {"input"}}}, |
| 95 |
{"Stop", {this, &sample_interface::stop}}, |
| 96 |
{"CutString", {cut_string, {"input"}, {"cut", "original_length"}}}, |
| 97 |
{"Drop", {this, &sample_interface::drop}}, |
| 98 |
{"SetObject", {set_sample_object, {"object", "value"}}}, |
| 99 |
{"GetObject", {get_sample_object, {"object"}, {"value"}}} |
| 100 |
}, { |
| 101 |
{"ReturnCode", ret} |
| 102 |
}, { |
| 103 |
{"InputReceived", |
| 104 |
ipcgull::make_signal<std::string>({"line"})} |
| 105 |
}), _server(server), owner(o) {} |
| 106 |
|
| 107 |
void input_received(const std::string& line) { |
| 108 |
emit_signal("InputReceived", line); |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
int main() { |
| 113 |
auto server = ipcgull::make_server(SERVER_NAME, |
| 114 |
SERVER_ROOT, |
| 115 |
default_mode); |
| 116 |
|
| 117 |
auto ret = ipcgull::property<int>(ipcgull::property_full_permissions); |
| 118 |
|
| 119 |
auto root = ipcgull::node::make_root("sample"); |
| 120 |
auto ptr = std::make_shared<sample_object>(10); |
| 121 |
root->manage(ptr); |
| 122 |
root->add_server(server); |
| 123 |
auto iface = root->make_interface<sample_interface>( |
| 124 |
server, root, ret); |
| 125 |
|
| 126 |
std::thread signal_thread([iface, server, &ret]() { |
| 127 |
std::string line; |
| 128 |
|
| 129 |
while (std::getline(std::cin, line)) |
| 130 |
iface->input_received(line); |
| 131 |
}); |
| 132 |
|
| 133 |
server->start(); |
| 134 |
signal_thread.join(); |
| 135 |
|
| 136 |
return ret; |
| 137 |
} |