From 47bda1b593220afcd17df66fe38b6683b8500a4b Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Wed, 21 May 2014 11:41:33 +0200 Subject: Reinstate cprlistener. --- etc/miav.conf | 2 ++ src/Makefile.am | 2 ++ src/cprlisten.cc | 55 ++++++++++++++++++++++++++++++++++++++++++--------- src/cprlisten.h | 15 +++++++++++--- src/cprquerydialog.cc | 37 +++++++++++++++++++++++++++++----- src/cprquerydialog.h | 7 ++++++- 6 files changed, 100 insertions(+), 18 deletions(-) diff --git a/etc/miav.conf b/etc/miav.conf index b551d64..40a8e18 100644 --- a/etc/miav.conf +++ b/etc/miav.conf @@ -11,6 +11,8 @@ cpr_host = "cpr.j.auh.dk" cpr_port = 10301 cpr_timeout = 10000 +cprlisten_port = 13183 + # Video source video_width = 720 video_height = 576 diff --git a/src/Makefile.am b/src/Makefile.am index fa10241..37b0f98 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -19,6 +19,8 @@ miav_LDADD = $(shell ../tools/MocList o) $(QT_LIBS) $(IEC61883_LIBS) \ $(SDL_LIBS) $(DV_LIBS) miav_SOURCES = \ + aa_socket.cc \ + cprlisten.cc \ aboutwindow.cc \ camera.cc \ cprquerydialog.cc \ diff --git a/src/cprlisten.cc b/src/cprlisten.cc index 9a816d2..4cf8670 100644 --- a/src/cprlisten.cc +++ b/src/cprlisten.cc @@ -1,4 +1,4 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: tab; c-basic-offset: 2 -*- */ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /*************************************************************************** * cprlisten.cc * @@ -26,36 +26,71 @@ */ #include "cprlisten.h" -#include "socket.h" +#include "aa_socket.h" #include #include +#include + using namespace std; +#define MAGIC_STOP_STRING "SHUTTHEFUCKUP" + CPRListen::CPRListen(unsigned short port) { this->port = port; cpr = "N/A"; + cprchanged = false; + running = true; } CPRListen::~CPRListen() { } +void CPRListen::stop() +{ + running = false; + try { + AASocket socket; + socket.connect("localhost", port); + socket.send_string(MAGIC_STOP_STRING); + } catch(Network_error &e) { + //info->error("In stop(): %s.", e.error.c_str()); + } +} + void CPRListen::thread_main() { - while(1) { + //printf("Listening for CPRs.\n"); + while(running) { try { - Socket socket; + string newcpr; + AASocket socket; socket.listen(port); - mutex.lock(); - cpr = socket.receive_string(); - mutex.unlock(); - cerr << "Got CPR: " << cpr << endl; + newcpr = socket.receive_string(); + + if(newcpr == MAGIC_STOP_STRING) { + running = false; + } else { + mutex.lock(); + cprchanged = true; + cpr = newcpr; + mutex.unlock(); + //printf("Got CPR: %s.\n", cpr.c_str()); + } + } catch(Network_error &e) { - cerr << e.error << endl; + //printf("In thread_main(): %s.\n", e.error.c_str()); + running = false; } } + //printf("Stopped listening for CPRs.\n"); +} + +bool CPRListen::cprChanged() +{ + return cprchanged; } string CPRListen::getCpr() @@ -66,5 +101,7 @@ string CPRListen::getCpr() cpr_copy = cpr; mutex.unlock(); + cprchanged = false; + return cpr_copy; } diff --git a/src/cprlisten.h b/src/cprlisten.h index 57551f5..d1862f8 100644 --- a/src/cprlisten.h +++ b/src/cprlisten.h @@ -1,4 +1,4 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: tab; c-basic-offset: 2 -*- */ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /*************************************************************************** * cprlisten.h * @@ -31,20 +31,29 @@ #include #include "thread.h" -#include "mutex.h" +#include + +#include "aa_socket.h" class CPRListen: public Thread { public: CPRListen(unsigned short port); ~CPRListen(); + bool cprChanged(); std::string getCpr(); void thread_main(); + void stop(); // Stops the call to listen + private: + volatile bool running; + AASocket *socket; + + bool cprchanged; unsigned short port; std::string cpr; - Mutex mutex; + QMutex mutex; }; #endif/*__AASIMON_FRAMEWORK_CPRLISTEN_H__*/ diff --git a/src/cprquerydialog.cc b/src/cprquerydialog.cc index 8ea0986..c58a8ed 100644 --- a/src/cprquerydialog.cc +++ b/src/cprquerydialog.cc @@ -60,7 +60,8 @@ CPRQueryDialog::CPRQueryDialog(QLabel *lcpr, cprSocket = new QTcpSocket(this); connect(cprSocket, SIGNAL(readyRead()), SLOT(cprSocket_readyRead())); connect(cprSocket, SIGNAL(connected()), SLOT(cprSocket_connected())); - connect(cprSocket, SIGNAL(error(int)), SLOT(cprSocket_error(int))); + connect(cprSocket, SIGNAL(error(QAbstractSocket::SocketError)), + SLOT(cprSocket_error(QAbstractSocket::SocketError))); lbl_cpr->setText("Indtast CPR"); @@ -113,11 +114,35 @@ CPRQueryDialog::CPRQueryDialog(QLabel *lcpr, connect(bca,SIGNAL(clicked()), SLOT(b_c_clicked())); this->move(175,150); + + listen = new CPRListen(config->readInt("cprlisten_port")); + listen_timer = new QTimer(this); + connect(listen_timer, SIGNAL(timeout()), SLOT(listen_timeout())); + listen->run(); + listen_timer->start(500); // Check every 500 ms + show(); } CPRQueryDialog::~CPRQueryDialog() { + // Cleanup after cpr listen + listen->stop(); + // listen->wait_stop(); + delete listen; +} + +void CPRQueryDialog::listen_timeout() +{ + string newcpr; + if(listen->cprChanged()) { + char newcpr_buf[32]; + newcpr = listen->getCpr(); + sprintf(newcpr_buf, "%s-%s", newcpr.substr(0,6).c_str(), newcpr.substr(6,4).c_str()); + //printf("cprbuf[%s]\n", newcpr_buf); + lbl_cpr->setText(newcpr_buf); + verifycpr(newcpr_buf); + } } /** @@ -159,7 +184,7 @@ void CPRQueryDialog::b_c_clicked() { remove_all();} void CPRQueryDialog::b_clicked(int value) { - printf("%d\n", value); + //printf("%d\n", value); switch(value) { case 10: if (digits>0) digits--; @@ -212,9 +237,9 @@ void CPRQueryDialog::remove_all() */ void CPRQueryDialog::insert_digit(int value) { - printf("insert_digit(%d)\n", value); + //printf("insert_digit(%d)\n", value); char temp[3]; - printf("strlen(cpr) = %d cpr: '%s'\n", strlen(cpr), cpr); + //printf("strlen(cpr) = %d cpr: '%s'\n", strlen(cpr), cpr); switch(strlen(cpr)) { case 5: // Automaticaly add a hyphen after the sixth digit sprintf(temp, "%d-", value); @@ -327,8 +352,10 @@ int CPRQueryDialog::test_cpr(const char *s) * Called if an error occurres in the corsocket connection. * Writes out the appropriate error message. */ -void CPRQueryDialog::cprSocket_error(int errnum) +void CPRQueryDialog::cprSocket_error(QAbstractSocket::SocketError err) { + (void)err; + QString msg = QString("cprSocket encountered an error: "); timer->stop(); diff --git a/src/cprquerydialog.h b/src/cprquerydialog.h index 7a84f5e..da42d09 100644 --- a/src/cprquerydialog.h +++ b/src/cprquerydialog.h @@ -82,6 +82,7 @@ using namespace std; #include #include "messagebox.h" +#include "cprlisten.h" class CPRQueryDialog : public QDialog { Q_OBJECT @@ -101,12 +102,16 @@ public slots: void insert_digit(int value); void cprSocket_readyRead(); void cprSocket_connected(); - void cprSocket_error(int errnum); + void cprSocket_error(QAbstractSocket::SocketError err); void cprSocket_timeout(); + void listen_timeout(); private: QStatusBar *statusbar; + CPRListen *listen; + QTimer *listen_timer; + QLabel *lbl_cpr; QLabel *lbl_name; -- cgit v1.2.3