diff options
author | deva <deva> | 2005-03-27 13:53:41 +0000 |
---|---|---|
committer | deva <deva> | 2005-03-27 13:53:41 +0000 |
commit | 3392d8657f6f6ebc341709d5f500040ed4f559ef (patch) | |
tree | 8049cb88fe84266189b622b8f1565145905346cc /src/cprquerydialog.cc | |
parent | c61ab7c4232eb80b7cc3c2f37ba2715e16b4ee73 (diff) |
Added more error detection to cprdatabase connection. (Timeout)
Diffstat (limited to 'src/cprquerydialog.cc')
-rw-r--r-- | src/cprquerydialog.cc | 191 |
1 files changed, 132 insertions, 59 deletions
diff --git a/src/cprquerydialog.cc b/src/cprquerydialog.cc index 8ae2c60..e5a722f 100644 --- a/src/cprquerydialog.cc +++ b/src/cprquerydialog.cc @@ -45,6 +45,7 @@ CPRQueryDialog::CPRQueryDialog(QLabel *lcpr, //Read configuration CPR_HOST = cfg.readString("cpr_host"); CPR_PORT = cfg.readInt("cpr_port"); + CPR_TIMEOUT = cfg.readInt("cpr_timeout"); cpr[0] = '\0'; internalCpr[0] = '\0'; @@ -52,9 +53,14 @@ CPRQueryDialog::CPRQueryDialog(QLabel *lcpr, cprSocket = new QSocket(this); connect(cprSocket, SIGNAL(readyRead()), SLOT(cprSocket_readyRead())); connect(cprSocket, SIGNAL(connected()), SLOT(cprSocket_connected())); + connect(cprSocket, SIGNAL(error(int)), SLOT(cprSocket_error(int))); lbl_cpr->setText("Indtast CPR"); + // Setup connection timer + timer = new QTimer(this); + connect(timer, SIGNAL(timeout()), SLOT(cprSocket_timeout())); + // Generate input buttons QGridLayout *gl = new QGridLayout(this, 4, 3, 10, 2); @@ -106,8 +112,11 @@ CPRQueryDialog::~CPRQueryDialog() { } +/** + * createButton: + * A genric button-creater + */ QPushButton *CPRQueryDialog::createButton(QWidget *parent, const char *text, int value) -/* A genric button-creater */ { char buf[32]; sprintf(buf, "%d", value); @@ -120,7 +129,8 @@ QPushButton *CPRQueryDialog::createButton(QWidget *parent, const char *text, int return q; } -/* Event function for handling buttonclicks. +/** + * Event functions for handling buttonclicks. * For button 0-9 the values is sent back. * Backspace and clear are handled via special * signals. @@ -161,31 +171,13 @@ void CPRQueryDialog::b_clicked(int value) } } -void CPRQueryDialog::run(int pos) -/* This is where the mgui thread starts. - * Open a inputwindow and prepare to receive a cpr - */ -{ - - // iw = new CPRQueryDialog(this, "Input", true, pos); - - - // TODO : Reconnect - /* - if (this->exec() == QDialog::Accepted) { -#ifdef WITH_DV - camera->setCpr(cpr); -#endif - verifycpr(cpr); - } - */ -} - -void CPRQueryDialog::remove_digit() -/* Remove one digit from the selected cpr +/** + * remove_digit: + * Remove one digit from the selected cpr * Used when the user pushes backspace in * the inputwindow */ +void CPRQueryDialog::remove_digit() { int temp; temp = strlen(cpr); @@ -196,15 +188,21 @@ void CPRQueryDialog::remove_digit() lbl_cpr->setText(cpr); } +/** + * remove_all: + * Clear the selected cpr + */ void CPRQueryDialog::remove_all() -/* Clear the selected cpr */ { strcpy(cpr, ""); lbl_cpr->setText(cpr); } +/** + * insert_digit: + * Respond to what the user types in the inputWindow + */ void CPRQueryDialog::insert_digit(int value) -/* Respond to what the user types in the inputWindow */ { char temp[2]; switch(strlen(cpr)) { @@ -227,18 +225,20 @@ void CPRQueryDialog::insert_digit(int value) } } -void CPRQueryDialog::verifycpr(char *cpr) -/* Test a cpr via test_cpr(). +/** + * verifycpr: + * Test a cpr via test_cpr(). * If cpr is invalid, then ask user what - * to do via confirmCprWindow + * to do via MessageBox */ +void CPRQueryDialog::verifycpr(char *cpr) { strncpy(internalCpr, cpr, 6); strncpy(internalCpr+6, cpr+7, 4); internalCpr[10] = 0; if (!(test_cpr(internalCpr))) { - MessageBox ccw(this, "Bekræft", "Ugyldigt CPR nummer, brug det alligevel?", TYPE_YES_NO_CANCEL); + MessageBox ccw(this, CONFIRM_INVALID_CPR_TITLE, CONFIRM_INVALID_CPR, TYPE_YES_NO_CANCEL); switch(ccw.exec()) { case MSG_CANCEL: bedit_clicked(); @@ -252,47 +252,48 @@ void CPRQueryDialog::verifycpr(char *cpr) } } else { cprSocket->connectToHost(CPR_HOST->c_str(), CPR_PORT); - accept(); + timer->start(CPR_TIMEOUT); + // accept(); } } -void CPRQueryDialog::bcancel_clicked() /* Clears all data - alerts user if measurements are not stored */ +void CPRQueryDialog::bcancel_clicked() { cpr[0]= '\0'; lbl_cpr->setText("Indtast CPR"); lbl_name->setText(""); - run(0); } -void CPRQueryDialog::bedit_clicked() /* This is used when the user enters a cpr that is not valid and wishes to edit * the cpr. */ +void CPRQueryDialog::bedit_clicked() { cpr[10]= '\0'; lbl_cpr->setText(cpr); lbl_name->setText(""); - run(9); } +/** + * test_cpr: + * Checks that a cpr i valid via a modulo 11 test + */ int CPRQueryDialog::test_cpr(const char *s) -/* Checks that a cpr i valid via a modulo11 test */ { int sum = 0; int ctl; const char *cptr; - if(strlen(s) != 10) - return 0; + // Is the string to short to be a cpr? + if(strlen(s) != 10) return 0; - for(cptr = s; *cptr; cptr++) - { - if(!isdigit(*cptr)) - return 0; - } + // Are all characters digits? + for(cptr = s; *cptr; cptr++) if(!isdigit(*cptr)) return 0; + + // Calculate modulo 11 checksum sum += (s[0] - '0') * 4; sum += (s[1] - '0') * 3; sum += (s[2] - '0') * 2; @@ -303,32 +304,63 @@ int CPRQueryDialog::test_cpr(const char *s) sum += (s[7] - '0') * 3; sum += (s[8] - '0') * 2; ctl = 11 - (sum % 11); - if(ctl == 11) - ctl = 0; + + // Is the checksum correct? + if(ctl == 11) ctl = 0; return s[9] - '0' == ctl; } -void CPRQueryDialog::cprSocket_readyRead() -/* Uses a patients cpr to look up his or hers name +/** + * cprSocket_error + * Called if an error occurres in the corsocket connection. + * Writes out the appropriate error message. + */ +void CPRQueryDialog::cprSocket_error(int errnum) +{ + timer->stop(); + + lbl_name->setText(NAME_NOT_AVAILABLE); + + // Print error message + switch(errnum) { + case QSocket::ErrConnectionRefused: // if the connection was refused + printf("ErrConnectionRefused\n"); + break; + case QSocket::ErrHostNotFound: // if the host was not found + printf("ErrHostNotFound\n"); + break; + case QSocket::ErrSocketRead: // if a read from the socket failed + printf("ErrSocketRead\n"); + break; + } + + accept(); +} + +/** + * cprSocket_readyRead: + * Uses a patients cpr to look up his or hers name * via the departments cpr-server. * This is called by the cprSocket when the socket is ready */ +void CPRQueryDialog::cprSocket_readyRead() { QString name; QString firstname; QString lastname; int timeout = 0; - if (!cprSocket->canReadLine()) - return; + + timer->stop(); + + if (!cprSocket->canReadLine()) return; + while(cprSocket->canReadLine()) { QString s = cprSocket->readLine(); if (s.startsWith("0001")) { name.append(s.right(s.length()-4)); lastname.append(s.right(s.length()-4)); name.setLength(name.length()-1); - if (name.length()) - name += QString(", "); - + if (name.length()) name += QString(", "); } if (s.startsWith("0002")) { name.append(s.right(s.length()-4)); @@ -336,25 +368,66 @@ void CPRQueryDialog::cprSocket_readyRead() name.setLength(name.length()-1); cprSocket->close(); lbl_name->setText(name); + accept(); + return; + } + if (timeout>1000) { + lbl_name->setText(NAME_NOT_AVAILABLE); + MessageBox(this, NAME_NOT_AVAILABLE_TITLE, NAME_NOT_AVAILABLE, TYPE_OK, ICON_ERROR).exec(); + accept(); return; } - if (timeout>1000) - { - lbl_name->setText(NAME_NOT_AVAILABLE); - return; - } timeout++; } + + accept(); } -void CPRQueryDialog::cprSocket_connected() -/* Writes the selected cpr to the cpr-server +/** + * cprSocket_connected: + * Writes the selected cpr to the cpr-server * when the cprSocket is connected. */ +void CPRQueryDialog::cprSocket_connected() { + timer->stop(); cprSocket->writeBlock(internalCpr, 10); cprSocket->writeBlock("\n", 1); } +/** + * cprSocket_timeout: + * Called when the connection has not emitted a signal in CPR_TIMEOUT miliseconds. + */ +void CPRQueryDialog::cprSocket_timeout() +{ + timer->stop(); + + lbl_name->setText(NAME_NOT_AVAILABLE); + + printf("cprSocket timed out tryning to:\n"); + + // Print connection status + switch(cprSocket->state()) { + case QSocket::Idle: // if there is no connection + printf("Idle\n"); + break; + case QSocket::HostLookup: // during a DNS lookup + printf("HostLookup\n"); + break; + case QSocket::Connecting: // during TCP connection establishment + printf("Connecting\n"); + break; + case QSocket::Connected: // when there is an operational connection + printf("Conected\n"); + break; + case QSocket::Closing: // if the socket is closing down, but is not yet closed. + printf("Closing\n"); + break; + } + + accept(); +} + #endif /* USE_GUI */ |