This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
forked from faaxm/spix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnyRpcServer.cpp
126 lines (100 loc) · 6.07 KB
/
AnyRpcServer.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/***
* Copyright (C) Falko Axmann. All rights reserved.
* Licensed under the MIT license.
* See LICENSE.txt file in the project root for full license information.
****/
#include <Spix/AnyRpcServer.h>
#include <Spix/Data/Variant.h>
#include <Utils/AnyRpcFunction.h>
#include <atomic>
namespace spix {
struct AnyRpcServerPimpl {
std::unique_ptr<anyrpc::Server> server;
std::mutex serverAccessMutex;
std::atomic<bool> keepRunning {true};
};
AnyRpcServer::AnyRpcServer(int anyrpcPort)
: m_pimpl(new AnyRpcServerPimpl())
{
m_pimpl->server = std::make_unique<anyrpc::XmlHttpServer>();
anyrpc::MethodManager* methodManager = m_pimpl->server->GetMethodManager();
utils::AddFunctionToAnyRpc<void(int)>(methodManager, "wait",
"Wait given number of milliseconds | wait(int millisecondsToWait)",
[this](int ms) { wait(std::chrono::milliseconds(ms)); });
utils::AddFunctionToAnyRpc<void(std::string)>(methodManager, "mouseClick",
"Click on the object at the given path | mouseClick(string path)",
[this](std::string path) { mouseClick(std::move(path)); });
utils::AddFunctionToAnyRpc<void(std::string, int)>(methodManager, "mouseClickWithButton",
"Click on the object at the given path with the given mouse button | mouseClickWithButton(string path, int "
"mouseButton)",
[this](std::string path, int mouseButton) { mouseClick(std::move(path), mouseButton); });
utils::AddFunctionToAnyRpc<void(std::string)>(methodManager, "mouseBeginDrag",
"Begin a drag with the mouse | mouseBeginDrag(string path)",
[this](std::string path) { mouseBeginDrag(std::move(path)); });
utils::AddFunctionToAnyRpc<void(std::string)>(methodManager, "mouseEndDrag",
"End a drag with the mouse | mouseEndDrag(string path)",
[this](std::string path) { mouseEndDrag(std::move(path)); });
utils::AddFunctionToAnyRpc<void(std::string, std::vector<std::string>)>(methodManager, "mouseDropUrls",
"Drop Urls with mouse | mouseDropUrls(string path, [string url1, ...])",
[this](std::string path, std::vector<std::string> urls) { mouseDropUrls(std::move(path), urls); });
utils::AddFunctionToAnyRpc<void(std::string, std::string)>(methodManager, "inputText",
"Enter text. Events are sent to the item's window at path. | inputText(string path, string text)",
[this](std::string path, std::string text) { inputText(std::move(path), std::move(text)); });
utils::AddFunctionToAnyRpc<void(std::string, int, unsigned)>(methodManager, "enterKey",
"Press and release a key | enterKey(string path, int keyCode, unsigned int keyModifier)",
[this](std::string path, int keyCode, unsigned modifiers) { enterKey(std::move(path), keyCode, modifiers); });
utils::AddFunctionToAnyRpc<std::string(std::string, std::string)>(methodManager, "getStringProperty",
"Return a property as string | getStringProperty(string path, string property) : string property_value",
[this](std::string path, std::string property) {
return getStringProperty(std::move(path), std::move(property));
});
utils::AddFunctionToAnyRpc<void(std::string, std::string, std::string)>(methodManager, "setStringProperty",
"Set the string value of the given property | setStringProperty(string path, string property, string "
"new_value)",
[this](std::string path, std::string property, std::string value) {
setStringProperty(std::move(path), std::move(property), std::move(value));
});
utils::AddFunctionToAnyRpc<Variant(std::string, std::string, std::vector<Variant>)>(methodManager, "invokeMethod",
"Invoke a method on a QML object | invokeMethod(string path, string method, any[] args)",
[this](std::string path, std::string method, std::vector<Variant> args) {
return invokeMethod(std::move(path), std::move(method), std::move(args));
});
utils::AddFunctionToAnyRpc<std::vector<double>(std::string)>(methodManager, "getBoundingBox",
"Return the bounding box of an item in screen coordinates | getBoundingBox(string path) : (doubles) "
"[topLeft.x, topLeft.y , width, height]",
[this](std::string path) {
auto bounds = getBoundingBox(std::move(path));
return std::vector<double> {bounds.topLeft.x, bounds.topLeft.y, bounds.size.width, bounds.size.height};
});
utils::AddFunctionToAnyRpc<bool(std::string)>(methodManager, "existsAndVisible",
"Returns true if the given object exists | existsAndVisible(string path) : bool exists_and_visible",
[this](std::string path) { return existsAndVisible(std::move(path)); });
utils::AddFunctionToAnyRpc<std::vector<std::string>()>(methodManager, "getErrors",
"Returns internal errors that occurred during test execution | getErrors() : (strings) [error1, ...]",
[this]() { return getErrors(); });
utils::AddFunctionToAnyRpc<void(std::string, std::string)>(methodManager, "takeScreenshot",
"Take a screenshot of the object and save it as a file | takeScreenshot(string pathToTargetedItem, string "
"filePath)",
[this](std::string targetItem, std::string filePath) {
return takeScreenshot(std::move(targetItem), std::move(filePath));
});
utils::AddFunctionToAnyRpc<void()>(methodManager, "quit", "Close the app | quit()", [this] { quit(); });
utils::AddFunctionToAnyRpc<void(std::string, std::string)>(methodManager, "command",
"Executes a generic command | command(string command, string payload)",
[this](std::string command, std::string payload) { genericCommand(command, payload); });
m_pimpl->server->BindAndListen(anyrpcPort);
}
AnyRpcServer::~AnyRpcServer()
{
m_pimpl->keepRunning.store(false);
// make sure we exited executeTest at which point we get a lock
std::lock_guard<std::mutex> lock(m_pimpl->serverAccessMutex);
}
void AnyRpcServer::executeTest()
{
std::lock_guard<std::mutex> lock(m_pimpl->serverAccessMutex);
while (m_pimpl->keepRunning.load()) {
m_pimpl->server->Work(1000);
}
}
} // namespace spix