Commit 47073dedc3d82ac1e624927282d70becc0df0709

Authored by Perry Werneck
1 parent d3a3a087

Implementing D-Bus basic request object.

src/include/lib3270++.h
@@ -212,6 +212,10 @@ @@ -212,6 +212,10 @@
212 return toString(); 212 return toString();
213 } 213 }
214 214
  215 + // Get properties.
  216 + virtual std::string getVersion() const = 0;
  217 + virtual std::string getRevision() const = 0;
  218 +
215 virtual ProgramMessage getProgramMessage() const = 0; 219 virtual ProgramMessage getProgramMessage() const = 0;
216 inline operator ProgramMessage() const { 220 inline operator ProgramMessage() const {
217 return getProgramMessage(); 221 return getProgramMessage();
@@ -326,6 +330,18 @@ @@ -326,6 +330,18 @@
326 session->setCursorPosition(row,col); 330 session->setCursorPosition(row,col);
327 } 331 }
328 332
  333 + // Get properties
  334 +
  335 + /// @brief Get lib3270 version.
  336 + inline std::string getVersion() const {
  337 + return session->getVersion();
  338 + }
  339 +
  340 + /// @brief Get lib3270 revision.
  341 + std::string getRevision() const {
  342 + return session->getRevision();
  343 + }
  344 +
329 // Set contents. 345 // Set contents.
330 346
331 /// @brief Set field at current posicion, jumps to next writable field. 347 /// @brief Set field at current posicion, jumps to next writable field.
src/lib3270++/ipc/session.cc
@@ -120,8 +120,7 @@ @@ -120,8 +120,7 @@
120 } 120 }
121 121
122 void IPC::Session::disconnect() { 122 void IPC::Session::disconnect() {
123 - Request request(*this,"disconnect");  
124 - request.call(); 123 + Request(*this,"disconnect").call();
125 } 124 }
126 125
127 // Wait for session state. 126 // Wait for session state.
@@ -212,6 +211,23 @@ @@ -212,6 +211,23 @@
212 211
213 } 212 }
214 213
  214 + /// @brief Get lib3270 version.
  215 + std::string IPC::Session::getVersion() const {
  216 +
  217 + string rc;
  218 +
  219 + Request request{*this,false,"version"};
  220 + request.call().pop(rc);
  221 +
  222 + return rc;
  223 + }
  224 +
  225 + /// @brief Get lib3270 revision.
  226 + std::string IPC::Session::getRevision() const {
  227 + throw std::system_error(ENOTSUP, std::system_category());
  228 + return "";
  229 + }
  230 +
215 } 231 }
216 232
217 233
src/lib3270++/linux/request.cc
@@ -44,13 +44,13 @@ @@ -44,13 +44,13 @@
44 44
45 namespace TN3270 { 45 namespace TN3270 {
46 46
47 - IPC::Request::Request(Session &session) { 47 + IPC::Request::Request(const Session &session) {
48 this->conn = session.conn; 48 this->conn = session.conn;
49 this->msg.in = nullptr; 49 this->msg.in = nullptr;
50 this->msg.out = nullptr; 50 this->msg.out = nullptr;
51 } 51 }
52 52
53 - IPC::Request::Request(Session &session, const char *method) : Request(session) { 53 + IPC::Request::Request(const Session &session, const char *method) : Request(session) {
54 54
55 this->msg.out = dbus_message_new_method_call( 55 this->msg.out = dbus_message_new_method_call(
56 session.name.c_str(), // Destination 56 session.name.c_str(), // Destination
@@ -65,13 +65,23 @@ @@ -65,13 +65,23 @@
65 65
66 } 66 }
67 67
68 - IPC::Request::Request(Session &session, const char *method, const char *property) : Request(session) { 68 + IPC::Request::Request(const Session &session, bool isSet, const char *property) : Request(session) {
69 69
  70 +/*
  71 + dbus-send \
  72 + --session \
  73 + --dest=br.com.bb.pw3270.a\
  74 + --print-reply \
  75 + "/br/com/bb/tn3270/session" \
  76 + "org.freedesktop.DBus.Properties.Get" \
  77 + string:br.com.bb.tn3270.session \
  78 + string:${1}
  79 +*/
70 this->msg.out = dbus_message_new_method_call( 80 this->msg.out = dbus_message_new_method_call(
71 session.name.c_str(), // Destination 81 session.name.c_str(), // Destination
72 session.path.c_str(), // Path 82 session.path.c_str(), // Path
73 "org.freedesktop.DBus.Properties", // Interface 83 "org.freedesktop.DBus.Properties", // Interface
74 - method // Method 84 + (isSet ? "Set" : "Get")
75 ); 85 );
76 86
77 if(!msg.out) { 87 if(!msg.out) {
@@ -91,7 +101,7 @@ @@ -91,7 +101,7 @@
91 dbus_message_append_args( 101 dbus_message_append_args(
92 this->msg.out, 102 this->msg.out,
93 DBUS_TYPE_STRING,&interface_name, 103 DBUS_TYPE_STRING,&interface_name,
94 - DBUS_TYPE_STRING,&method, 104 + DBUS_TYPE_STRING,&property,
95 DBUS_TYPE_INVALID 105 DBUS_TYPE_INVALID
96 ); 106 );
97 107
@@ -123,6 +133,10 @@ @@ -123,6 +133,10 @@
123 throw std::runtime_error(message.c_str()); 133 throw std::runtime_error(message.c_str());
124 } 134 }
125 135
  136 + dbus_message_iter_init(msg.in, &msg.iter);
  137 +
  138 + debug(__FUNCTION__," got a valid response");
  139 +
126 return *this; 140 return *this;
127 141
128 } 142 }
@@ -132,6 +146,45 @@ @@ -132,6 +146,45 @@
132 return *this; 146 return *this;
133 } 147 }
134 148
  149 + IPC::Request & IPC::Request::pop(std::string &value) {
  150 +
  151 + const char * str = "";
  152 +
  153 + if(dbus_message_iter_get_arg_type(&msg.iter) == DBUS_TYPE_STRING) {
  154 +
  155 + dbus_message_iter_get_basic(&msg.iter, &str);
  156 +
  157 + } else if(dbus_message_iter_get_arg_type(&msg.iter) == DBUS_TYPE_VARIANT) {
  158 +
  159 + DBusMessageIter sub;
  160 + int current_type;
  161 +
  162 + dbus_message_iter_recurse(&msg.iter, &sub);
  163 +
  164 + while ((current_type = dbus_message_iter_get_arg_type(&sub)) != DBUS_TYPE_INVALID) {
  165 +
  166 + if (current_type == DBUS_TYPE_STRING) {
  167 + dbus_message_iter_get_basic(&sub, &str);
  168 + break;
  169 + }
  170 + dbus_message_iter_next(&sub);
  171 + }
  172 +
  173 + } else {
  174 +
  175 + debug("Argument type is ", ((char) dbus_message_iter_get_arg_type(&msg.iter)) );
  176 + throw std::runtime_error("Expected an string data type");
  177 +
  178 + }
  179 +
  180 + value.assign(str);
  181 +
  182 + debug(__FUNCTION__,"= \"",str,"\"");
  183 +
  184 + return *this;
  185 + }
  186 +
  187 +
135 } 188 }
136 189
137 190
src/lib3270++/local/session.cc
@@ -298,6 +298,15 @@ @@ -298,6 +298,15 @@
298 298
299 } 299 }
300 300
  301 + // Get properties.
  302 + std::string Local::Session::getVersion() const {
  303 + return lib3270_get_version();
  304 + }
  305 +
  306 + std::string Local::Session::getRevision() const {
  307 + return lib3270_get_revision();
  308 + }
  309 +
301 310
302 } 311 }
303 312
src/lib3270++/private.h
@@ -176,6 +176,10 @@ @@ -176,6 +176,10 @@
176 // Wait for session state. 176 // Wait for session state.
177 void waitForReady(time_t timeout = 5) throw() override; 177 void waitForReady(time_t timeout = 5) throw() override;
178 178
  179 + // Get properties.
  180 + std::string getVersion() const override;
  181 + std::string getRevision() const override;
  182 +
179 // Gets 183 // Gets
180 std::string toString(int baddr, size_t len, char lf) const override; 184 std::string toString(int baddr, size_t len, char lf) const override;
181 std::string toString(int row, int col, size_t sz, char lf) const override; 185 std::string toString(int row, int col, size_t sz, char lf) const override;
@@ -233,28 +237,42 @@ @@ -233,28 +237,42 @@
233 static DWORD pack(std::vector<DataBlock *> &args, uint8_t * outBuffer, size_t szBuffer); 237 static DWORD pack(std::vector<DataBlock *> &args, uint8_t * outBuffer, size_t szBuffer);
234 #else 238 #else
235 struct { 239 struct {
236 - DBusMessage * in;  
237 - DBusMessage * out; 240 + DBusMessage * in;
  241 + DBusMessage * out;
  242 + DBusMessageIter iter;
  243 +
238 } msg; 244 } msg;
239 DBusConnection * conn; 245 DBusConnection * conn;
240 246
241 #endif // _WIN32 247 #endif // _WIN32
242 248
243 - Request(Session &session); 249 + Request(const Session &session);
244 250
245 public: 251 public:
246 252
247 /// @brief Create a method call. 253 /// @brief Create a method call.
248 - Request(Session &session, const char *method); 254 + Request(const Session &session, const char *method);
249 255
250 /// @brief Create a get/set property call. 256 /// @brief Create a get/set property call.
251 - Request(Session &session, const char *method, const char *property); 257 + ///
  258 + /// @param session Session object.
  259 + /// @param isSet true if this is a setProperty call.
  260 + /// @param property Property name.
  261 + //
  262 + Request(const Session &session, bool isSet, const char *property);
252 263
253 ~Request(); 264 ~Request();
254 265
255 Request & call(); 266 Request & call();
  267 +
  268 + // Push values
  269 +
256 Request & push(const char *arg); 270 Request & push(const char *arg);
257 271
  272 + // Pop values
  273 +
  274 + Request & pop(std::string &value);
  275 +
258 }; 276 };
259 277
260 class TN3270_PRIVATE Session : public TN3270::Abstract::Session { 278 class TN3270_PRIVATE Session : public TN3270::Abstract::Session {
@@ -288,6 +306,10 @@ @@ -288,6 +306,10 @@
288 // Wait for session state. 306 // Wait for session state.
289 void waitForReady(time_t timeout = 5) throw() override; 307 void waitForReady(time_t timeout = 5) throw() override;
290 308
  309 + // Get properties.
  310 + std::string getVersion() const override;
  311 + std::string getRevision() const override;
  312 +
291 // Gets 313 // Gets
292 std::string toString(int baddr, size_t len, char lf) const override; 314 std::string toString(int baddr, size_t len, char lf) const override;
293 std::string toString(int row, int col, size_t sz, char lf) const override; 315 std::string toString(int row, int col, size_t sz, char lf) const override;
src/lib3270++/testprogram/testprogram.cc
@@ -45,9 +45,14 @@ @@ -45,9 +45,14 @@
45 45
46 int main(int argc, const char *argv[]) { 46 int main(int argc, const char *argv[]) {
47 47
48 - TN3270::Host host("pw3270:a"); 48 + TN3270::Host host{"pw3270:a"};
49 49
50 - host.connect(getenv("LIB3270_DEFAULT_HOST")); 50 + cout
  51 + << "Version: " << host.getVersion()
  52 +// << " Revision: " << host.getRevision()
  53 + << std::endl;
  54 +
  55 + // host.connect(getenv("LIB3270_DEFAULT_HOST"));
51 56
52 /* 57 /*
53 cout << host << endl; 58 cout << host << endl;