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. --- src/cprlisten.cc | 55 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 9 deletions(-) (limited to 'src/cprlisten.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; } -- cgit v1.2.3