Problems using Qt5 and Apache Thrift
PAULUS, Raimund, TI-ABN
Raimund.Paulus@dillinger.biz
Tue Mar 31 05:39:41 GMT 2020
In the meantime i tested using a thread for the Thrift-connection to my service.
If the thread is started, messages are sent in a loop to the service and a sleep() is included so that i have enough time to check the connection via netstat command.
The result is the same as in my first attempt. The network connection is broken after line 27 (create the QPushButton) in main.cpp. The thread prints the error message:
"Thrift: Mon Mar 30 18:45:27 2020 TSocket::write_partial() send() <Host: 192.168.178.200 Port: 9090>Broken pipe"
Here are the sources:
main.cpp:
1 #include <stdio.h>
2
3 #include <QApplication>
4 #include <QWidget>
5 #include <QPushButton>
6 #include <QThread>
7
8 #include "thrift_clnt.hpp"
9
10 int main(int argc, char **argv) {
11
12 printf("Stop 1; Press <Enter> to continue"); getchar();
13
14 QApplication app(argc, argv);
15
16 QThread* thread = new QThread();
17 ThriftClnt* thrift_cl = new ThriftClnt;
18 thrift_cl->moveToThread(thread);
19 QObject::connect(thread, SIGNAL(started()), thrift_cl, SLOT (process()));
20 QObject::connect(thrift_cl, SIGNAL (finished()), thread, SLOT (quit()));
21 QObject::connect(thrift_cl, SIGNAL (finished()), thrift_cl, SLOT (deleteLater()));
22 QObject::connect(thread, SIGNAL (finished()), thread, SLOT (deleteLater()));
23 thread->start();
24
25 printf("Stop 2; Press <Enter> to continue"); getchar();
26
27 QPushButton *b_end = new QPushButton("End");
28
29 printf("Stop 3; Press <Enter> to continue"); getchar();
30
31 QObject::connect(b_end, SIGNAL(clicked()), &app, SLOT(quit()));
32
33 printf("Stop 4; Press <Enter> to continue"); getchar();
34
35 b_end->show();
36
37 printf("Stop 6; Press <Enter> to continue"); getchar();
38
39 app.exec();
40
41 thread->wait(); // do not exit before the thread is completed!
42
43 }
thrift_clnt.hpp:
1 #ifndef __THRIFT_CLNT_HPP
2 #define __THRIFT_CLNT_HPP
3
4 #include <QObject>
5
6 class ThriftClnt : public QObject
7 {
8 Q_OBJECT
9
10 public:
11 ThriftClnt();
12 ~ThriftClnt();
13
14 public slots:
15 void process();
16
17 signals:
18 void finished();
19 void error(QString err);
20
21 private:
22 // add your variables here
23 };
24
25 #endif // __THRIFT_CLNT_HPP
thrift_clnt.cpp:
1 #include "thrift_clnt.hpp"
2
3 #include <thrift/protocol/TBinaryProtocol.h>
4 #include <thrift/transport/TSocket.h>
5 #include <thrift/transport/TTransportUtils.h>
6 #include <thrift/stdcxx.h>
7
8 #include "../gen-cpp/Calculator.h"
9
10 using namespace std;
11 using namespace apache::thrift;
12 using namespace apache::thrift::protocol;
13 using namespace apache::thrift::transport;
14
15 using namespace tutorial;
16 using namespace shared;
17
18 ThriftClnt::ThriftClnt() {
19 }
20
21 ThriftClnt::~ThriftClnt() {
22 }
23
24 void ThriftClnt::process()
25 {
26
27 stdcxx::shared_ptr<TTransport> socket(new TSocket("192.168.178.200", 9090));
28 // stdcxx::shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
29
30 stdcxx::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
31 stdcxx::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
32
33 CalculatorClient client(protocol);
34
35 try {
36 transport->open();
37
38 char txt[128];
39 for(int i=1; 1<20; i++)
40 {
41 sprintf(txt, "%d + %d = %d", i, i, client.add(i, i));
42 sleep(5);
43 }
44
45 } catch (TException& tx) {
46 printf("ERROR: %s\n", tx.what());
47 }
48
49 emit finished();
50 }
51
QT-Thrift.pro:
1 ######################################################################
2 # Automatically generated by qmake (3.0) Mo. März 23 15:29:31 2020
3 ######################################################################
4
5 QT += core gui widgets
6
7 TEMPLATE = app
8 TARGET = QtClient
9 INCLUDEPATH += .
10 INCLUDEPATH += ../gen-cpp
11
12 linux: {
13 TOOLS_DIR = /software/tools
14 }
15
16 cygwin: {
17 TOOLS_DIR = /cygdrive/e/Software/Cygwin/2_5_2-tools
18 }
19
20 # directories for thrift
21 TOOLS_INCL = $${TOOLS_DIR}/include
22 TOOLS_LIB = $${TOOLS_DIR}/lib
23
24 INCLUDEPATH += $${TOOLS_INCL}
25
26 LIBS += -L $${TOOLS_LIB} -lthrift
27
28 # Input
29 HEADERS += thrift_clnt.hpp ../gen-cpp/Calculator.h
30
31 SOURCES += main.cpp thrift_clnt.cpp \
32 ../gen-cpp/Calculator.cpp \
33 ../gen-cpp/SharedService.cpp \
34 ../gen-cpp/shared_constants.cpp \
35 ../gen-cpp/shared_types.cpp \
36 ../gen-cpp/tutorial_constants.cpp \
37 ../gen-cpp/tutorial_types.cpp \
The modules in directory gen-cpp are built with the Thrift compiler in Linux. The Thrift libs are built in Cygwin after unpacking the tar ball thrift-0.11.0.tar.gz
with the commands:
>./bootstrap.sh
>export CXXFLAGS="-D_HAVE_SYS_TIME_H -DPTHREAD_MUTEX_RECURSIVE_NP=PTHREAD_MUTEX_RECURSIVE -O2 -std=gnu++11 -D_GNU_SOURCE"
>./configure --with-qt4=no --with-qt5=no --with-csharp=no --with-java=no \
--with-erlang=no --with-nodejs=no --with-lua=no --with-python=no --with-perl=no \
--with-php=no --with-php_extension=no --with-dart=no --with-ruby=no --with-haskell=no \
--with-go=no --with-rs=no --with-cl=no --with-haxe=no --with-dotnetcore=no --with-d=no \
--disable-tests --prefix=/cygdrive/e/Software/Cygwin/2_5_2-tools
> make
> make install
Greetings
Raimund Paulus
> -----Ursprüngliche Nachricht-----
> Von: Cygwin [mailto:cygwin-bounces@cygwin.com] Im Auftrag von PAULUS,
> Raimund, TI-ABN
> Gesendet: Donnerstag, 26. März 2020 07:59
> An: 'cygwin@cygwin.com'
> Betreff: Re: Problems using Qt5 and Apache Thrift
>
> Hello Andrey Repin
>
> The sources and the documentation are her:
>
> https://thrift.apache.org/tutorial/cpp
>
> You must have libthrift installed.
>
> Maybe using threads is a better programming style, but i don't know, if it solves the
> problem. I use connecting and disconnecting to the service like braces around the
> rest of the application. At some points the application transfers data to the service
> and expects an answer. It is a synchronous communication and i use it like RPC
> (remote procedure call). The client sends a request and the service has to respond.
> Without the response the client cannot continue. Every client has its own service.
>
> Greetings
>
> Ramund Paulus
>
> > -----Ursprüngliche Nachricht-----
> > Von: Andrey Repin [mailto:anrdaemon@yandex.ru]
> > Gesendet: Mittwoch, 25. März 2020 12:13
> > An: PAULUS, Raimund, TI-ABN; cygwin@cygwin.com
> > Betreff: Re: Problems using Qt5 and Apache Thrift
> >
> > Greetings, PAULUS, Raimund, TI-ABN!
> >
> > > Problems using Qt5 and Apache Thrift
> >
> > ...snip...
> >
> > > Now i want to implement the interface parts with Qt 5. Here is the new program
> > sequence:
> >
> > > //------------------------------------------------------------------------------
> > > program starts
> > > step 1: make the connection to the Linux server (Apache Thrift)
> > > step 2: initialize Qt interface (create widgets, buttons, ...)
> > > step 3: user interface (Qt)
> > > step 4: data transfer PC <-> Linux-Host (Apache Thrift)
> > > step 5: user interface (Qt)
> > > step 6: data transfer PC <-> Linux-Host (Apache Thrift)
> > > ...
> > > ...
> > > ...
> > > step n-1: end Qt app
> > > step n: close the connection to the host (Apache Thrift)
> > > program ends
> > > //------------------------------------------------------------------------------
> >
> > > During step 2 the connection to the linux server is broken. You can see it
> > > with the netstat command. First error message arises in step 4:
> >
> > > "TSocket::write_partial() send() <Host: my_host Port: 9090>Broken pipe"
> >
> > I strongly suggest placing communication service in its own thread.
> > Then you could manage connection without having to worry about blocking
> > timeouts caused by GUI operations.
> > They will run asynchronously.
> >
> > > On a Linux box the client program runs perfectly.
> >
> > Only by coincidence, I suppose.
> >
> > > On the windows box the program works, if i initalize Qt before the
> > > connection to the server is made (step 2 before step 1). But that is not
> > > acceptable for me, because afterwards other widgets and buttons are created
> > > and i can not close and create the connection at each point.
> >
> > I suppose, the server dropping connection by timeout. But I'd urge you to
> > investigate this further.
> >
> > > For the tests I used the examples from the Apache Thrift Tutorial.
> >
> > Please include examples as text/plain attachments, if they are longer than a
> > few lines.
> >
> >
> > --
> > With best regards,
> > Andrey Repin
> > Wednesday, March 25, 2020 14:08:25
> >
> > Sorry for my terrible english...
>
> --
> Problem reports: https://cygwin.com/problems.html
> FAQ: https://cygwin.com/faq/
> Documentation: https://cygwin.com/docs.html
> Unsubscribe info: https://cygwin.com/ml/#unsubscribe-simple
>
More information about the Cygwin
mailing list