Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #ifndef RTKBASE_H
  2. #define RTKBASE_H
  3.  
  4. #include <sys/socket.h>
  5. #include <signal.h>
  6.  
  7. #include <QCoreApplication>
  8. #include <QList>
  9. #include <QTimer>
  10. #include <QStringList>
  11.  
  12.  
  13. #include "server.h"
  14. #include "gpsdevice.h"
  15.  
  16. class RtkBase : public QCoreApplication
  17. {
  18. Q_OBJECT
  19.  
  20. public:
  21. RtkBase(int argc, char **argv);
  22. ~RtkBase(void);
  23.  
  24. // Unix signal handlers.
  25. static void hupSignalHandler(int unused);
  26. static void termSignalHandler(int unused);
  27.  
  28. public slots:
  29. // Qt signal handlers.
  30. void slotHandleSigHup();
  31. void slotHandleSigTerm();
  32.  
  33. private:
  34. static int sighupFd[2];
  35. static int sigtermFd[2];
  36. Server *mServer;
  37. GpsDevice *mGpsDevice;
  38.  
  39. QSocketNotifier *snHup;
  40. QSocketNotifier *snTerm;
  41. };
  42.  
  43. #endif
  44.  
  45.  
  46. ...and the .cpp:
  47.  
  48.  
  49.  
  50. #include "rtkbase.h"
  51.  
  52. int RtkBase::sighupFd[] = {0,0};
  53. int RtkBase::sigtermFd[] = {0,0};
  54.  
  55.  
  56. static int setup_unix_signal_handlers()
  57. {
  58. qDebug() << "setup_unix_signal_handlers()";
  59. struct sigaction hup, term;
  60.  
  61. hup.sa_handler = RtkBase::hupSignalHandler;
  62. sigemptyset(&hup.sa_mask);
  63. hup.sa_flags = 0;
  64. hup.sa_flags |= SA_RESTART;
  65.  
  66. if (sigaction(SIGHUP, &hup, 0) > 0)
  67. return 1;
  68.  
  69. term.sa_handler = RtkBase::termSignalHandler;
  70. sigemptyset(&term.sa_mask);
  71. term.sa_flags |= SA_RESTART;
  72.  
  73. if (sigaction(SIGTERM, &term, 0) > 0)
  74. return 2;
  75.  
  76. return 0;
  77. }
  78.  
  79. RtkBase::RtkBase(int argc, char **argv) : QCoreApplication(argc, argv)
  80. {
  81. // set up signal handling
  82. if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sighupFd)) qFatal("Couldn't create HUP socketpair");
  83.  
  84. if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sigtermFd)) qFatal("Couldn't create TERM socketpair");
  85.  
  86.  
  87. snHup = new QSocketNotifier(sighupFd[1], QSocketNotifier::Read, this);
  88. connect(snHup, SIGNAL(activated(int)), SLOT(slotHandleSigHup()));
  89. snTerm = new QSocketNotifier(sigtermFd[1], QSocketNotifier::Read, this);
  90. connect(snTerm, SIGNAL(activated(int)), SLOT(slotHandleSigTerm()));
  91.  
  92.  
  93. ...
  94. }
  95.  
  96. RtkBase::~RtkBase()
  97. {
  98. ...
  99. }
  100.  
  101. void RtkBase::hupSignalHandler(int)
  102. {
  103. char a = 1;
  104. qDebug() << "RtkBase::hupSignalHandler(int)";
  105. ::write(sighupFd[0], &a, sizeof(a));
  106. }
  107.  
  108. void RtkBase::termSignalHandler(int)
  109. {
  110. char a = 1;
  111. qDebug() << "RtkBase::termSignalHandler(int)";
  112. ::write(sigtermFd[0], &a, sizeof(a));
  113. }
  114.  
  115. void RtkBase::slotHandleSigTerm()
  116. {
  117. snTerm->setEnabled(false);
  118. char tmp;
  119. ::read(sigtermFd[1], &tmp, sizeof(tmp));
  120.  
  121. // do Qt stuff
  122. qFatal("Wheee, SIGTERM pressed!");
  123.  
  124. snTerm->setEnabled(true);
  125. }
  126.  
  127. void RtkBase::slotHandleSigHup()
  128. {
  129. snHup->setEnabled(false);
  130. char tmp;
  131. ::read(sighupFd[1], &tmp, sizeof(tmp));
  132.  
  133. // do Qt stuff
  134. qFatal("Wheee, SIGHUP pressed!");
  135.  
  136. snHup->setEnabled(true);
  137. }
  138.  
  139.  
  140. int main(int argc, char **argv)
  141. {
  142. RtkBase rtkBase(argc, argv);
  143. setup_unix_signal_handlers();
  144.  
  145. return rtkBase.exec();
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement