1 // Copyright (c) 2011-2014 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "rpcconsole.h"
6 #include "ui_rpcconsole.h"
8 #include "clientmodel.h"
10 #include "peertablemodel.h"
13 #include "chainparams.h"
14 #include "rpcserver.h"
15 #include "rpcclient.h"
18 #include "json/json_spirit_value.h"
20 #include <openssl/crypto.h>
31 #if QT_VERSION < 0x050000
35 // TODO: add a scrollback limit, as there is currently none
36 // TODO: make it possible to filter out categories (esp debug messages when implemented)
37 // TODO: receive errors and debug messages through ClientModel
39 const int CONSOLE_HISTORY = 50;
40 const QSize ICON_SIZE(24, 24);
42 const int INITIAL_TRAFFIC_GRAPH_MINS = 30;
48 {"cmd-request", ":/icons/tx_input"},
49 {"cmd-reply", ":/icons/tx_output"},
50 {"cmd-error", ":/icons/tx_output"},
51 {"misc", ":/icons/tx_inout"},
55 /* Object for executing console RPC commands in a separate thread.
57 class RPCExecutor : public QObject
62 void request(const QString &command);
65 void reply(int category, const QString &command);
68 #include "rpcconsole.moc"
71 * Split shell command line into a list of arguments. Aims to emulate \c bash and friends.
73 * - Arguments are delimited with whitespace
74 * - Extra whitespace at the beginning and end and between arguments will be ignored
75 * - Text can be "double" or 'single' quoted
76 * - The backslash \c \ is used as escape character
77 * - Outside quotes, any character can be escaped
78 * - Within double quotes, only escape \c " and backslashes before a \c " or another backslash
79 * - Within single quotes, no escaping is possible and no special interpretation takes place
81 * @param[out] args Parsed arguments will be appended to this list
82 * @param[in] strCommand Command line to split
84 bool parseCommandLine(std::vector<std::string> &args, const std::string &strCommand)
93 STATE_ESCAPE_DOUBLEQUOTED
94 } state = STATE_EATING_SPACES;
96 foreach(char ch, strCommand)
100 case STATE_ARGUMENT: // In or after argument
101 case STATE_EATING_SPACES: // Handle runs of whitespace
104 case '"': state = STATE_DOUBLEQUOTED; break;
105 case '\'': state = STATE_SINGLEQUOTED; break;
106 case '\\': state = STATE_ESCAPE_OUTER; break;
107 case ' ': case '\n': case '\t':
108 if(state == STATE_ARGUMENT) // Space ends argument
110 args.push_back(curarg);
113 state = STATE_EATING_SPACES;
115 default: curarg += ch; state = STATE_ARGUMENT;
118 case STATE_SINGLEQUOTED: // Single-quoted string
121 case '\'': state = STATE_ARGUMENT; break;
122 default: curarg += ch;
125 case STATE_DOUBLEQUOTED: // Double-quoted string
128 case '"': state = STATE_ARGUMENT; break;
129 case '\\': state = STATE_ESCAPE_DOUBLEQUOTED; break;
130 default: curarg += ch;
133 case STATE_ESCAPE_OUTER: // '\' outside quotes
134 curarg += ch; state = STATE_ARGUMENT;
136 case STATE_ESCAPE_DOUBLEQUOTED: // '\' in double-quoted text
137 if(ch != '"' && ch != '\\') curarg += '\\'; // keep '\' for everything but the quote and '\' itself
138 curarg += ch; state = STATE_DOUBLEQUOTED;
142 switch(state) // final state
144 case STATE_EATING_SPACES:
147 args.push_back(curarg);
149 default: // ERROR to end in one of the other states
154 void RPCExecutor::request(const QString &command)
156 std::vector<std::string> args;
157 if(!parseCommandLine(args, command.toStdString()))
159 emit reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
163 return; // Nothing to do
166 std::string strPrint;
167 // Convert argument list to JSON objects in method-dependent way,
168 // and pass it along with the method name to the dispatcher.
169 json_spirit::Value result = tableRPC.execute(
171 RPCConvertValues(args[0], std::vector<std::string>(args.begin() + 1, args.end())));
173 // Format result reply
174 if (result.type() == json_spirit::null_type)
176 else if (result.type() == json_spirit::str_type)
177 strPrint = result.get_str();
179 strPrint = write_string(result, true);
181 emit reply(RPCConsole::CMD_REPLY, QString::fromStdString(strPrint));
183 catch (json_spirit::Object& objError)
185 try // Nice formatting for standard-format error
187 int code = find_value(objError, "code").get_int();
188 std::string message = find_value(objError, "message").get_str();
189 emit reply(RPCConsole::CMD_ERROR, QString::fromStdString(message) + " (code " + QString::number(code) + ")");
191 catch(std::runtime_error &) // raised when converting to invalid type, i.e. missing code or message
192 { // Show raw JSON object
193 emit reply(RPCConsole::CMD_ERROR, QString::fromStdString(write_string(json_spirit::Value(objError), false)));
196 catch (std::exception& e)
198 emit reply(RPCConsole::CMD_ERROR, QString("Error: ") + QString::fromStdString(e.what()));
202 RPCConsole::RPCConsole(QWidget *parent) :
204 ui(new Ui::RPCConsole),
210 GUIUtil::restoreWindowGeometry("nRPCConsoleWindow", this->size(), this);
213 ui->openDebugLogfileButton->setIcon(QIcon(":/icons/export"));
216 // Install event filter for up and down arrow
217 ui->lineEdit->installEventFilter(this);
218 ui->messagesWidget->installEventFilter(this);
220 connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
221 connect(ui->btnClearTrafficGraph, SIGNAL(clicked()), ui->trafficGraph, SLOT(clear()));
223 // set library version labels
224 ui->openSSLVersion->setText(SSLeay_version(SSLEAY_VERSION));
226 ui->berkeleyDBVersion->setText(DbEnv::version(0, 0, 0));
228 ui->label_berkeleyDBVersion->hide();
229 ui->berkeleyDBVersion->hide();
233 setTrafficGraphRange(INITIAL_TRAFFIC_GRAPH_MINS);
235 ui->detailWidget->hide();
236 ui->peerHeading->setText(tr("Select a peer to view detailed information."));
241 RPCConsole::~RPCConsole()
243 GUIUtil::saveWindowGeometry("nRPCConsoleWindow", this);
248 bool RPCConsole::eventFilter(QObject* obj, QEvent *event)
250 if(event->type() == QEvent::KeyPress) // Special key handling
252 QKeyEvent *keyevt = static_cast<QKeyEvent*>(event);
253 int key = keyevt->key();
254 Qt::KeyboardModifiers mod = keyevt->modifiers();
257 case Qt::Key_Up: if(obj == ui->lineEdit) { browseHistory(-1); return true; } break;
258 case Qt::Key_Down: if(obj == ui->lineEdit) { browseHistory(1); return true; } break;
259 case Qt::Key_PageUp: /* pass paging keys to messages widget */
260 case Qt::Key_PageDown:
261 if(obj == ui->lineEdit)
263 QApplication::postEvent(ui->messagesWidget, new QKeyEvent(*keyevt));
268 // Typing in messages widget brings focus to line edit, and redirects key there
269 // Exclude most combinations and keys that emit no text, except paste shortcuts
270 if(obj == ui->messagesWidget && (
271 (!mod && !keyevt->text().isEmpty() && key != Qt::Key_Tab) ||
272 ((mod & Qt::ControlModifier) && key == Qt::Key_V) ||
273 ((mod & Qt::ShiftModifier) && key == Qt::Key_Insert)))
275 ui->lineEdit->setFocus();
276 QApplication::postEvent(ui->lineEdit, new QKeyEvent(*keyevt));
281 return QDialog::eventFilter(obj, event);
284 void RPCConsole::setClientModel(ClientModel *model)
287 ui->trafficGraph->setClientModel(model);
290 // Keep up to date with client
291 setNumConnections(model->getNumConnections());
292 connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
294 setNumBlocks(model->getNumBlocks());
295 connect(model, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
297 updateTrafficStats(model->getTotalBytesRecv(), model->getTotalBytesSent());
298 connect(model, SIGNAL(bytesChanged(quint64,quint64)), this, SLOT(updateTrafficStats(quint64, quint64)));
301 ui->peerWidget->setModel(model->getPeerTableModel());
302 ui->peerWidget->verticalHeader()->hide();
303 ui->peerWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
304 ui->peerWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
305 ui->peerWidget->setSelectionMode(QAbstractItemView::SingleSelection);
306 ui->peerWidget->setColumnWidth(PeerTableModel::Address, ADDRESS_COLUMN_WIDTH);
307 ui->peerWidget->setColumnWidth(PeerTableModel::Subversion, SUBVERSION_COLUMN_WIDTH);
308 ui->peerWidget->setColumnWidth(PeerTableModel::Ping, PING_COLUMN_WIDTH);
310 // connect the peerWidget selection model to our peerSelected() handler
311 connect(ui->peerWidget->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
312 this, SLOT(peerSelected(const QItemSelection &, const QItemSelection &)));
313 connect(model->getPeerTableModel(), SIGNAL(layoutChanged()), this, SLOT(peerLayoutChanged()));
315 // Provide initial values
316 ui->clientVersion->setText(model->formatFullVersion());
317 ui->clientName->setText(model->clientName());
318 ui->buildDate->setText(model->formatBuildDate());
319 ui->startupTime->setText(model->formatClientStartupTime());
321 ui->networkName->setText(QString::fromStdString(Params().NetworkIDString()));
325 static QString categoryClass(int category)
329 case RPCConsole::CMD_REQUEST: return "cmd-request"; break;
330 case RPCConsole::CMD_REPLY: return "cmd-reply"; break;
331 case RPCConsole::CMD_ERROR: return "cmd-error"; break;
332 default: return "misc";
336 void RPCConsole::clear()
338 ui->messagesWidget->clear();
341 ui->lineEdit->clear();
342 ui->lineEdit->setFocus();
344 // Add smoothly scaled icon images.
345 // (when using width/height on an img, Qt uses nearest instead of linear interpolation)
346 for(int i=0; ICON_MAPPING[i].url; ++i)
348 ui->messagesWidget->document()->addResource(
349 QTextDocument::ImageResource,
350 QUrl(ICON_MAPPING[i].url),
351 QImage(ICON_MAPPING[i].source).scaled(ICON_SIZE, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
354 // Set default style sheet
355 ui->messagesWidget->document()->setDefaultStyleSheet(
357 "td.time { color: #808080; padding-top: 3px; } "
358 "td.message { font-family: monospace; font-size: 12px; } " // Todo: Remove fixed font-size
359 "td.cmd-request { color: #006060; } "
360 "td.cmd-error { color: red; } "
361 "b { color: #006060; } "
364 message(CMD_REPLY, (tr("Welcome to the Bitcoin RPC console.") + "<br>" +
365 tr("Use up and down arrows to navigate history, and <b>Ctrl-L</b> to clear screen.") + "<br>" +
366 tr("Type <b>help</b> for an overview of available commands.")), true);
369 void RPCConsole::reject()
371 // Ignore escape keypress if this is not a seperate window
372 if(windowType() != Qt::Widget)
376 void RPCConsole::message(int category, const QString &message, bool html)
378 QTime time = QTime::currentTime();
379 QString timeString = time.toString();
381 out += "<table><tr><td class=\"time\" width=\"65\">" + timeString + "</td>";
382 out += "<td class=\"icon\" width=\"32\"><img src=\"" + categoryClass(category) + "\"></td>";
383 out += "<td class=\"message " + categoryClass(category) + "\" valign=\"middle\">";
387 out += GUIUtil::HtmlEscape(message, true);
388 out += "</td></tr></table>";
389 ui->messagesWidget->append(out);
392 void RPCConsole::setNumConnections(int count)
397 QString connections = QString::number(count) + " (";
398 connections += tr("In:") + " " + QString::number(clientModel->getNumConnections(CONNECTIONS_IN)) + " / ";
399 connections += tr("Out:") + " " + QString::number(clientModel->getNumConnections(CONNECTIONS_OUT)) + ")";
401 ui->numberOfConnections->setText(connections);
404 void RPCConsole::setNumBlocks(int count)
406 ui->numberOfBlocks->setText(QString::number(count));
408 ui->lastBlockTime->setText(clientModel->getLastBlockDate().toString());
411 void RPCConsole::on_lineEdit_returnPressed()
413 QString cmd = ui->lineEdit->text();
414 ui->lineEdit->clear();
418 message(CMD_REQUEST, cmd);
419 emit cmdRequest(cmd);
420 // Remove command, if already in history
421 history.removeOne(cmd);
422 // Append command to history
424 // Enforce maximum history size
425 while(history.size() > CONSOLE_HISTORY)
426 history.removeFirst();
427 // Set pointer to end of history
428 historyPtr = history.size();
429 // Scroll console view to end
434 void RPCConsole::browseHistory(int offset)
436 historyPtr += offset;
439 if(historyPtr > history.size())
440 historyPtr = history.size();
442 if(historyPtr < history.size())
443 cmd = history.at(historyPtr);
444 ui->lineEdit->setText(cmd);
447 void RPCConsole::startExecutor()
449 QThread *thread = new QThread;
450 RPCExecutor *executor = new RPCExecutor();
451 executor->moveToThread(thread);
453 // Replies from executor object must go to this object
454 connect(executor, SIGNAL(reply(int,QString)), this, SLOT(message(int,QString)));
455 // Requests from this object must go to executor
456 connect(this, SIGNAL(cmdRequest(QString)), executor, SLOT(request(QString)));
458 // On stopExecutor signal
459 // - queue executor for deletion (in execution thread)
460 // - quit the Qt event loop in the execution thread
461 connect(this, SIGNAL(stopExecutor()), executor, SLOT(deleteLater()));
462 connect(this, SIGNAL(stopExecutor()), thread, SLOT(quit()));
463 // Queue the thread for deletion (in this thread) when it is finished
464 connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
466 // Default implementation of QThread::run() simply spins up an event loop in the thread,
467 // which is what we want.
471 void RPCConsole::on_tabWidget_currentChanged(int index)
473 if(ui->tabWidget->widget(index) == ui->tab_console)
475 ui->lineEdit->setFocus();
479 void RPCConsole::on_openDebugLogfileButton_clicked()
481 GUIUtil::openDebugLogfile();
484 void RPCConsole::scrollToEnd()
486 QScrollBar *scrollbar = ui->messagesWidget->verticalScrollBar();
487 scrollbar->setValue(scrollbar->maximum());
490 void RPCConsole::on_sldGraphRange_valueChanged(int value)
492 const int multiplier = 5; // each position on the slider represents 5 min
493 int mins = value * multiplier;
494 setTrafficGraphRange(mins);
497 QString RPCConsole::FormatBytes(quint64 bytes)
500 return QString(tr("%1 B")).arg(bytes);
501 if(bytes < 1024 * 1024)
502 return QString(tr("%1 KB")).arg(bytes / 1024);
503 if(bytes < 1024 * 1024 * 1024)
504 return QString(tr("%1 MB")).arg(bytes / 1024 / 1024);
506 return QString(tr("%1 GB")).arg(bytes / 1024 / 1024 / 1024);
509 void RPCConsole::setTrafficGraphRange(int mins)
511 ui->trafficGraph->setGraphRangeMins(mins);
512 ui->lblGraphRange->setText(GUIUtil::formatDurationStr(mins * 60));
515 void RPCConsole::updateTrafficStats(quint64 totalBytesIn, quint64 totalBytesOut)
517 ui->lblBytesIn->setText(FormatBytes(totalBytesIn));
518 ui->lblBytesOut->setText(FormatBytes(totalBytesOut));
521 void RPCConsole::peerSelected(const QItemSelection &selected, const QItemSelection &deselected)
523 Q_UNUSED(deselected);
525 if (!clientModel || selected.indexes().isEmpty())
528 const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(selected.indexes().first().row());
530 updateNodeDetail(stats);
533 void RPCConsole::peerLayoutChanged()
538 const CNodeCombinedStats *stats = NULL;
539 bool fUnselect = false;
540 bool fReselect = false;
542 if (cachedNodeid == -1) // no node selected yet
545 // find the currently selected row
547 QModelIndexList selectedModelIndex = ui->peerWidget->selectionModel()->selectedIndexes();
548 if (selectedModelIndex.isEmpty())
551 selectedRow = selectedModelIndex.first().row();
553 // check if our detail node has a row in the table (it may not necessarily
554 // be at selectedRow since its position can change after a layout change)
555 int detailNodeRow = clientModel->getPeerTableModel()->getRowByNodeId(cachedNodeid);
557 if (detailNodeRow < 0)
559 // detail node dissapeared from table (node disconnected)
562 ui->detailWidget->hide();
563 ui->peerHeading->setText(tr("Select a peer to view detailed information."));
567 if (detailNodeRow != selectedRow)
569 // detail node moved position
574 // get fresh stats on the detail node.
575 stats = clientModel->getPeerTableModel()->getNodeStats(detailNodeRow);
578 if (fUnselect && selectedRow >= 0)
580 ui->peerWidget->selectionModel()->select(QItemSelection(selectedModelIndex.first(), selectedModelIndex.last()),
581 QItemSelectionModel::Deselect);
586 ui->peerWidget->selectRow(detailNodeRow);
590 updateNodeDetail(stats);
593 void RPCConsole::updateNodeDetail(const CNodeCombinedStats *stats)
595 // Update cached nodeid
596 cachedNodeid = stats->nodeStats.nodeid;
598 // update the detail ui with latest node information
599 QString peerAddrDetails(QString::fromStdString(stats->nodeStats.addrName));
600 if (!stats->nodeStats.addrLocal.empty())
601 peerAddrDetails += "<br />" + tr("via %1").arg(QString::fromStdString(stats->nodeStats.addrLocal));
602 ui->peerHeading->setText(peerAddrDetails);
603 ui->peerServices->setText(GUIUtil::formatServicesStr(stats->nodeStats.nServices));
604 ui->peerLastSend->setText(stats->nodeStats.nLastSend ? GUIUtil::formatDurationStr(GetTime() - stats->nodeStats.nLastSend) : tr("never"));
605 ui->peerLastRecv->setText(stats->nodeStats.nLastRecv ? GUIUtil::formatDurationStr(GetTime() - stats->nodeStats.nLastRecv) : tr("never"));
606 ui->peerBytesSent->setText(FormatBytes(stats->nodeStats.nSendBytes));
607 ui->peerBytesRecv->setText(FormatBytes(stats->nodeStats.nRecvBytes));
608 ui->peerConnTime->setText(GUIUtil::formatDurationStr(GetTime() - stats->nodeStats.nTimeConnected));
609 ui->peerPingTime->setText(GUIUtil::formatPingTime(stats->nodeStats.dPingTime));
610 ui->peerVersion->setText(QString("%1").arg(stats->nodeStats.nVersion));
611 ui->peerSubversion->setText(QString::fromStdString(stats->nodeStats.cleanSubVer));
612 ui->peerDirection->setText(stats->nodeStats.fInbound ? tr("Inbound") : tr("Outbound"));
613 ui->peerHeight->setText(QString("%1").arg(stats->nodeStats.nStartingHeight));
615 // This check fails for example if the lock was busy and
616 // nodeStateStats couldn't be fetched.
617 if (stats->fNodeStateStatsAvailable) {
618 // Ban score is init to 0
619 ui->peerBanScore->setText(QString("%1").arg(stats->nodeStateStats.nMisbehavior));
621 // Sync height is init to -1
622 if (stats->nodeStateStats.nSyncHeight > -1)
623 ui->peerSyncHeight->setText(QString("%1").arg(stats->nodeStateStats.nSyncHeight));
625 ui->peerSyncHeight->setText(tr("Unknown"));
627 ui->peerBanScore->setText(tr("Fetching..."));
628 ui->peerSyncHeight->setText(tr("Fetching..."));
631 ui->detailWidget->show();
634 void RPCConsole::resizeEvent(QResizeEvent *event)
636 QWidget::resizeEvent(event);
639 void RPCConsole::showEvent(QShowEvent *event)
641 QWidget::showEvent(event);
646 // start PeerTableModel auto refresh
647 clientModel->getPeerTableModel()->startAutoRefresh();
650 void RPCConsole::hideEvent(QHideEvent *event)
652 QWidget::hideEvent(event);
657 // stop PeerTableModel auto refresh
658 clientModel->getPeerTableModel()->stopAutoRefresh();