Commit 1721891b3875173da28a46c38f20a292ee5917e5
1 parent
58a91031
Exists in
master
and in
1 other branch
Refactoring D-Bus methods.
Showing
5 changed files
with
123 additions
and
52 deletions
Show diff stats
client/src/core/linux/request.cc
... | ... | @@ -46,37 +46,38 @@ |
46 | 46 | |
47 | 47 | IPC::Request::Request(DBusConnection * conn) { |
48 | 48 | this->conn = conn; |
49 | - this->msg.in = nullptr; | |
50 | - this->msg.out = nullptr; | |
49 | + this->response.msg = nullptr; | |
50 | + this->request.msg = nullptr; | |
51 | 51 | } |
52 | 52 | |
53 | 53 | IPC::Request::~Request() { |
54 | - if(msg.out) { | |
55 | - dbus_message_unref(msg.out); | |
54 | + if(request.msg) { | |
55 | + dbus_message_unref(request.msg); | |
56 | 56 | } |
57 | - if(msg.in) { | |
58 | - dbus_message_unref(msg.in); | |
57 | + | |
58 | + if(response.msg) { | |
59 | + dbus_message_unref(response.msg); | |
59 | 60 | } |
60 | 61 | } |
61 | 62 | |
62 | 63 | IPC::Request & IPC::Request::call() { |
63 | 64 | |
64 | - if(msg.in) { | |
65 | - dbus_message_unref(msg.in); | |
66 | - msg.in = nullptr; | |
65 | + if(response.msg) { | |
66 | + dbus_message_unref(response.msg); | |
67 | + response.msg = nullptr; | |
67 | 68 | } |
68 | 69 | |
69 | 70 | DBusError error; |
70 | 71 | dbus_error_init(&error); |
71 | - this->msg.in = dbus_connection_send_with_reply_and_block(this->conn,this->msg.out,10000,&error); | |
72 | + response.msg = dbus_connection_send_with_reply_and_block(this->conn,request.msg,10000,&error); | |
72 | 73 | |
73 | - if(!this->msg.in) { | |
74 | + if(!response.msg) { | |
74 | 75 | string message = error.message; |
75 | 76 | dbus_error_free(&error); |
76 | 77 | throw std::runtime_error(message.c_str()); |
77 | 78 | } |
78 | 79 | |
79 | - dbus_message_iter_init(msg.in, &msg.iter); | |
80 | + dbus_message_iter_init(response.msg, &response.iter); | |
80 | 81 | |
81 | 82 | // debug(__FUNCTION__," got a valid response"); |
82 | 83 | |
... | ... | @@ -84,46 +85,53 @@ |
84 | 85 | |
85 | 86 | } |
86 | 87 | |
88 | + IPC::Request & IPC::Request::push(int type, const void *value) { | |
89 | + | |
90 | + DBusMessageIter iter; | |
91 | + dbus_message_iter_init_append(request.msg, &iter); | |
92 | + | |
93 | + if (!dbus_message_iter_append_basic(&iter,type,value)) { | |
94 | + throw std::runtime_error("Can't append value"); | |
95 | + } | |
96 | + | |
97 | + return *this; | |
98 | + | |
99 | + } | |
100 | + | |
87 | 101 | IPC::Request & IPC::Request::push(const char *arg) { |
88 | - dbus_message_append_args(this->msg.out,DBUS_TYPE_STRING,&arg,DBUS_TYPE_INVALID); | |
89 | - return *this; | |
102 | + return push(DBUS_TYPE_STRING,&arg); | |
90 | 103 | } |
91 | 104 | |
92 | 105 | IPC::Request & IPC::Request::push(const bool arg) { |
93 | - dbus_message_append_args(this->msg.out,DBUS_TYPE_BOOLEAN,&arg,DBUS_TYPE_INVALID); | |
94 | - return *this; | |
106 | + return push(DBUS_TYPE_BOOLEAN,&arg); | |
95 | 107 | } |
96 | 108 | |
97 | 109 | IPC::Request & IPC::Request::push(const uint8_t arg) { |
98 | - dbus_message_append_args(this->msg.out,DBUS_TYPE_BYTE,&arg,DBUS_TYPE_INVALID); | |
99 | - return *this; | |
110 | + return push(DBUS_TYPE_BYTE,&arg); | |
100 | 111 | } |
101 | 112 | |
102 | 113 | IPC::Request & IPC::Request::push(const int32_t arg) { |
103 | - dbus_message_append_args(this->msg.out,DBUS_TYPE_INT32,&arg,DBUS_TYPE_INVALID); | |
104 | - return *this; | |
114 | + return push(DBUS_TYPE_INT32,&arg); | |
105 | 115 | } |
106 | 116 | |
107 | 117 | IPC::Request & IPC::Request::push(const uint32_t arg) { |
108 | - dbus_message_append_args(this->msg.out,DBUS_TYPE_UINT32,&arg,DBUS_TYPE_INVALID); | |
109 | - return *this; | |
118 | + return push(DBUS_TYPE_UINT32,&arg); | |
110 | 119 | } |
111 | 120 | |
112 | - | |
113 | 121 | IPC::Request & IPC::Request::pop(std::string &value) { |
114 | 122 | |
115 | 123 | const char * str = ""; |
116 | 124 | |
117 | - if(dbus_message_iter_get_arg_type(&msg.iter) == DBUS_TYPE_STRING) { | |
125 | + if(dbus_message_iter_get_arg_type(&response.iter) == DBUS_TYPE_STRING) { | |
118 | 126 | |
119 | - dbus_message_iter_get_basic(&msg.iter, &str); | |
127 | + dbus_message_iter_get_basic(&response.iter, &str); | |
120 | 128 | |
121 | - } else if(dbus_message_iter_get_arg_type(&msg.iter) == DBUS_TYPE_VARIANT) { | |
129 | + } else if(dbus_message_iter_get_arg_type(&response.iter) == DBUS_TYPE_VARIANT) { | |
122 | 130 | |
123 | 131 | DBusMessageIter sub; |
124 | 132 | int current_type; |
125 | 133 | |
126 | - dbus_message_iter_recurse(&msg.iter, &sub); | |
134 | + dbus_message_iter_recurse(&response.iter, &sub); | |
127 | 135 | |
128 | 136 | while ((current_type = dbus_message_iter_get_arg_type(&sub)) != DBUS_TYPE_INVALID) { |
129 | 137 | |
... | ... | @@ -136,12 +144,12 @@ |
136 | 144 | |
137 | 145 | } else { |
138 | 146 | |
139 | - debug("Argument type is ", ((char) dbus_message_iter_get_arg_type(&msg.iter)) ); | |
147 | + debug("Argument type is ", ((char) dbus_message_iter_get_arg_type(&response.iter)) ); | |
140 | 148 | throw std::runtime_error("Expected an string data type"); |
141 | 149 | |
142 | 150 | } |
143 | 151 | |
144 | - dbus_message_iter_next(&msg.iter); | |
152 | + dbus_message_iter_next(&response.iter); | |
145 | 153 | |
146 | 154 | value.assign(str); |
147 | 155 | |
... | ... | @@ -304,8 +312,8 @@ |
304 | 312 | |
305 | 313 | IPC::Request & IPC::Request::Request::pop(int &value) { |
306 | 314 | |
307 | - value = getIntValue(msg.iter); | |
308 | - dbus_message_iter_next(&msg.iter); | |
315 | + value = getIntValue(response.iter); | |
316 | + dbus_message_iter_next(&response.iter); | |
309 | 317 | // debug(__FUNCTION__,"= \"",value,"\""); |
310 | 318 | |
311 | 319 | return *this; |
... | ... | @@ -314,8 +322,8 @@ |
314 | 322 | |
315 | 323 | IPC::Request & IPC::Request::Request::pop(unsigned int &value) { |
316 | 324 | |
317 | - value = getUIntValue(msg.iter); | |
318 | - dbus_message_iter_next(&msg.iter); | |
325 | + value = getUIntValue(response.iter); | |
326 | + dbus_message_iter_next(&response.iter); | |
319 | 327 | // debug(__FUNCTION__,"= \"",value,"\""); |
320 | 328 | |
321 | 329 | return *this; |
... | ... | @@ -324,8 +332,8 @@ |
324 | 332 | |
325 | 333 | IPC::Request & IPC::Request::Request::pop(bool &value) { |
326 | 334 | |
327 | - value = getBooleanValue(msg.iter); | |
328 | - dbus_message_iter_next(&msg.iter); | |
335 | + value = getBooleanValue(response.iter); | |
336 | + dbus_message_iter_next(&response.iter); | |
329 | 337 | // debug(__FUNCTION__,"= \"",value,"\""); |
330 | 338 | |
331 | 339 | return *this; | ... | ... |
client/src/include/lib3270/ipc/request.h
... | ... | @@ -105,12 +105,17 @@ |
105 | 105 | uint16_t * outvalues; |
106 | 106 | |
107 | 107 | #else |
108 | + /// @brief Message received from server. | |
108 | 109 | struct { |
109 | - DBusMessage * in; | |
110 | - DBusMessage * out; | |
110 | + DBusMessage * msg; | |
111 | 111 | DBusMessageIter iter; |
112 | + } response; | |
113 | + | |
114 | + /// @brief Message who will be sent to server. | |
115 | + struct { | |
116 | + DBusMessage * msg; | |
117 | + } request; | |
112 | 118 | |
113 | - } msg; | |
114 | 119 | DBusConnection * conn; |
115 | 120 | |
116 | 121 | Request(DBusConnection * conn); |
... | ... | @@ -121,6 +126,11 @@ |
121 | 126 | |
122 | 127 | #ifdef _WIN32 |
123 | 128 | Request(HANDLE hPipe, const char *name, uint16_t type); |
129 | + | |
130 | +#else | |
131 | + | |
132 | + IPC::Request & push(int type, const void *value); | |
133 | + | |
124 | 134 | #endif // _WIN32 |
125 | 135 | |
126 | 136 | public: | ... | ... |
client/src/session/remote/linux/request.cc
... | ... | @@ -49,18 +49,14 @@ |
49 | 49 | |
50 | 50 | IPC::Request::Request(const IPC::Session &session, const char *method) : Request(session.conn) { |
51 | 51 | |
52 | -#ifdef DEBUG | |
53 | - clog << "Creating request \"" << method << "\"" << endl; | |
54 | -#endif // DEBUG | |
55 | - | |
56 | - this->msg.out = dbus_message_new_method_call( | |
52 | + request.msg = dbus_message_new_method_call( | |
57 | 53 | session.name.c_str(), // Destination |
58 | 54 | session.path.c_str(), // Path |
59 | 55 | session.interface.c_str(), // Interface |
60 | 56 | method // Method |
61 | 57 | ); |
62 | 58 | |
63 | - if(!msg.out) { | |
59 | + if(!request.msg) { | |
64 | 60 | throw std::runtime_error("Can't create D-Bus Method Call"); |
65 | 61 | } |
66 | 62 | |
... | ... | @@ -68,14 +64,14 @@ |
68 | 64 | |
69 | 65 | IPC::Request::Request(const IPC::Session &session, bool isSet, const char *property) : Request(session.conn) { |
70 | 66 | |
71 | - this->msg.out = dbus_message_new_method_call( | |
67 | + request.msg = dbus_message_new_method_call( | |
72 | 68 | session.name.c_str(), // Destination |
73 | 69 | session.path.c_str(), // Path |
74 | 70 | "org.freedesktop.DBus.Properties", // Interface |
75 | 71 | (isSet ? "Set" : "Get") |
76 | 72 | ); |
77 | 73 | |
78 | - if(!msg.out) { | |
74 | + if(!request.msg) { | |
79 | 75 | throw std::runtime_error("Can't create D-Bus Property Call"); |
80 | 76 | } |
81 | 77 | |
... | ... | @@ -89,12 +85,8 @@ |
89 | 85 | // |
90 | 86 | const char *interface_name = session.interface.c_str(); |
91 | 87 | |
92 | - dbus_message_append_args( | |
93 | - this->msg.out, | |
94 | - DBUS_TYPE_STRING,&interface_name, | |
95 | - DBUS_TYPE_STRING,&property, | |
96 | - DBUS_TYPE_INVALID | |
97 | - ); | |
88 | + push(DBUS_TYPE_STRING,&interface_name); | |
89 | + push(DBUS_TYPE_STRING,&property); | |
98 | 90 | |
99 | 91 | } |
100 | 92 | ... | ... |
... | ... | @@ -0,0 +1,24 @@ |
1 | +#!/bin/bash | |
2 | +# | |
3 | +# https://stackoverflow.com/questions/48648952/set-get-property-using-dbus-send | |
4 | +# | |
5 | + | |
6 | +#dbus-send \ | |
7 | +# --session \ | |
8 | +# --dest=br.com.bb.pw3270.a\ | |
9 | +# --print-reply \ | |
10 | +# "/br/com/bb/tn3270/session" \ | |
11 | +# "org.freedesktop.DBus.Properties.Get" \ | |
12 | +# string:br.com.bb.tn3270.session \ | |
13 | +# string:url | |
14 | + | |
15 | + | |
16 | +gdbus \ | |
17 | + call \ | |
18 | + --session \ | |
19 | + --dest "br.com.bb.pw3270.a" \ | |
20 | + --object-path "/br/com/bb/tn3270/session" \ | |
21 | + --method org.freedesktop.DBus.Properties.Get \ | |
22 | + "br.com.bb.tn3270.session" \ | |
23 | + "url" | |
24 | + | ... | ... |
... | ... | @@ -0,0 +1,37 @@ |
1 | +#!/bin/bash | |
2 | +# | |
3 | +# https://stackoverflow.com/questions/48648952/set-get-property-using-dbus-send | |
4 | +# | |
5 | + | |
6 | +dbus-send \ | |
7 | + --session \ | |
8 | + --dest=br.com.bb.pw3270.a\ | |
9 | + --print-reply \ | |
10 | + "/br/com/bb/tn3270/session" \ | |
11 | + "org.freedesktop.DBus.Properties.Set" \ | |
12 | + string:br.com.bb.tn3270.session \ | |
13 | + string:url \ | |
14 | + variant:string:${1} | |
15 | + | |
16 | + | |
17 | +#dbus-send --system --dest=$BUS_NAME --print-reply $OBJECT_PATH \ | |
18 | + #org.freedesktop.DBus.Properties.Set string:com.pgaur.GDBUS string:Status variant:uint32:10 | |
19 | + | |
20 | +#gdbus \ | |
21 | +# introspect \ | |
22 | +# --session \ | |
23 | +# --dest=br.com.bb.pw3270.a \ | |
24 | +# --object-path=/br/com/bb/tn3270/session | |
25 | + | |
26 | +#gdbus \ | |
27 | +# call \ | |
28 | +# --session \ | |
29 | +# --dest br.com.bb.pw3270.a \ | |
30 | +# --object-path /br/com/bb/tn3270/session \ | |
31 | +# --method org.freedesktop.DBus.Properties.Set \ | |
32 | +# "br.com.bb.tn3270.session" \ | |
33 | +# "url" \ | |
34 | +# ${1} | |
35 | + | |
36 | + | |
37 | + | ... | ... |