bool Client::initContext( int argc, char**argv){ try{ boost::thread_group threads; socket_ptr sock( new tcp::socket(service)); string_ptr m_prompt( buildPrompt()); strPrompt = m_prompt; sock->connect(ep); cout << "init\n"; threads.create_thread( boost::bind(&Client::displayLoop, this, sock) ); threads.create_thread( boost::bind(&Client::inboundLoop, this, sock, m_prompt)); threads.create_thread( boost::bind(&Client::writeLoop, this, sock, m_prompt)); threads.join_all(); }catch( std::exception &e){ cout << e.what() << endl; return false; } cout << "bye\n"; getchar(); return true; }
bool MainWindow::eventFilter(QObject *target, QEvent *event) { QString userRequest; QString pmuResponse; QString prompt; if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); switch (keyEvent->key()) { case Qt::Key_Return: case Qt::Key_Enter: // scrub the input userRequest = ui->commandLine->text().simplified(); // ignore requests until a connection is established if (userRequest.isEmpty() || (m_interface->isConnected() == false)) { ui->commandLine->clear(); break; } // process the command m_cmdHistory->append(userRequest); prompt = buildPrompt(); pmuResponse = processUserRequest(&userRequest); ui->commandLine->clear(); ui->outputFeed->insertHtml(prompt + userRequest + "<br>"); ui->outputFeed->insertHtml(pmuResponse + "<br>"); // scroll to bottom QCoreApplication::processEvents(); ui->outputFeed->verticalScrollBar()->setValue(ui->outputFeed->verticalScrollBar()->maximum()); break; case Qt::Key_Tab: if (ui->commandLine->cursorPosition() != m_cmdHelper->getCurrentCompletionLength()) { // accept current completion ui->commandLine->end(false); } else { ui->commandLine->setText(m_cmdHelper->getNextCompletion()); } break; case Qt::Key_Up: ui->commandLine->setText(m_cmdHistory->scrollBack()); break; case Qt::Key_Down: ui->commandLine->setText(m_cmdHistory->scrollForward()); break; case Qt::Key_Left: // SHIFT + LEFT clears the command line if (keyEvent->modifiers() & Qt::ShiftModifier) { ui->commandLine->clear(); } break; case Qt::Key_Home: ui->commandLine->home(false); break; case Qt::Key_End: ui->commandLine->end(false); break; default: break; } } return QObject::eventFilter(target, event); }