Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
rpcconsole.cpp
Go to the documentation of this file.
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.
4 
5 #include "rpcconsole.h"
6 #include "ui_rpcconsole.h"
7 
8 #include "clientmodel.h"
9 #include "guiutil.h"
10 
11 #include "rpcserver.h"
12 #include "rpcclient.h"
13 
14 #include "json/json_spirit_value.h"
15 #include <openssl/crypto.h>
16 #include <QKeyEvent>
17 #include <QScrollBar>
18 #include <QThread>
19 #include <QTime>
20 
21 #if QT_VERSION < 0x050000
22 #include <QUrl>
23 #endif
24 
25 // TODO: add a scrollback limit, as there is currently none
26 // TODO: make it possible to filter out categories (esp debug messages when implemented)
27 // TODO: receive errors and debug messages through ClientModel
28 
29 const int CONSOLE_HISTORY = 50;
30 const QSize ICON_SIZE(24, 24);
31 
33 
34 const struct {
35  const char *url;
36  const char *source;
37 } ICON_MAPPING[] = {
38  {"cmd-request", ":/icons/tx_input"},
39  {"cmd-reply", ":/icons/tx_output"},
40  {"cmd-error", ":/icons/tx_output"},
41  {"misc", ":/icons/tx_inout"},
42  {NULL, NULL}
43 };
44 
45 /* Object for executing console RPC commands in a separate thread.
46 */
47 class RPCExecutor : public QObject
48 {
49  Q_OBJECT
50 
51 public slots:
52  void request(const QString &command);
53 
54 signals:
55  void reply(int category, const QString &command);
56 };
57 
58 #include "rpcconsole.moc"
59 
74 bool parseCommandLine(std::vector<std::string> &args, const std::string &strCommand)
75 {
76  enum CmdParseState
77  {
78  STATE_EATING_SPACES,
79  STATE_ARGUMENT,
80  STATE_SINGLEQUOTED,
81  STATE_DOUBLEQUOTED,
82  STATE_ESCAPE_OUTER,
83  STATE_ESCAPE_DOUBLEQUOTED
84  } state = STATE_EATING_SPACES;
85  std::string curarg;
86  foreach(char ch, strCommand)
87  {
88  switch(state)
89  {
90  case STATE_ARGUMENT: // In or after argument
91  case STATE_EATING_SPACES: // Handle runs of whitespace
92  switch(ch)
93  {
94  case '"': state = STATE_DOUBLEQUOTED; break;
95  case '\'': state = STATE_SINGLEQUOTED; break;
96  case '\\': state = STATE_ESCAPE_OUTER; break;
97  case ' ': case '\n': case '\t':
98  if(state == STATE_ARGUMENT) // Space ends argument
99  {
100  args.push_back(curarg);
101  curarg.clear();
102  }
103  state = STATE_EATING_SPACES;
104  break;
105  default: curarg += ch; state = STATE_ARGUMENT;
106  }
107  break;
108  case STATE_SINGLEQUOTED: // Single-quoted string
109  switch(ch)
110  {
111  case '\'': state = STATE_ARGUMENT; break;
112  default: curarg += ch;
113  }
114  break;
115  case STATE_DOUBLEQUOTED: // Double-quoted string
116  switch(ch)
117  {
118  case '"': state = STATE_ARGUMENT; break;
119  case '\\': state = STATE_ESCAPE_DOUBLEQUOTED; break;
120  default: curarg += ch;
121  }
122  break;
123  case STATE_ESCAPE_OUTER: // '\' outside quotes
124  curarg += ch; state = STATE_ARGUMENT;
125  break;
126  case STATE_ESCAPE_DOUBLEQUOTED: // '\' in double-quoted text
127  if(ch != '"' && ch != '\\') curarg += '\\'; // keep '\' for everything but the quote and '\' itself
128  curarg += ch; state = STATE_DOUBLEQUOTED;
129  break;
130  }
131  }
132  switch(state) // final state
133  {
134  case STATE_EATING_SPACES:
135  return true;
136  case STATE_ARGUMENT:
137  args.push_back(curarg);
138  return true;
139  default: // ERROR to end in one of the other states
140  return false;
141  }
142 }
143 
144 void RPCExecutor::request(const QString &command)
145 {
146  std::vector<std::string> args;
147  if(!parseCommandLine(args, command.toStdString()))
148  {
149  emit reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
150  return;
151  }
152  if(args.empty())
153  return; // Nothing to do
154  try
155  {
156  std::string strPrint;
157  // Convert argument list to JSON objects in method-dependent way,
158  // and pass it along with the method name to the dispatcher.
159  json_spirit::Value result = tableRPC.execute(
160  args[0],
161  RPCConvertValues(args[0], std::vector<std::string>(args.begin() + 1, args.end())));
162 
163  // Format result reply
164  if (result.type() == json_spirit::null_type)
165  strPrint = "";
166  else if (result.type() == json_spirit::str_type)
167  strPrint = result.get_str();
168  else
169  strPrint = write_string(result, true);
170 
171  emit reply(RPCConsole::CMD_REPLY, QString::fromStdString(strPrint));
172  }
173  catch (json_spirit::Object& objError)
174  {
175  try // Nice formatting for standard-format error
176  {
177  int code = find_value(objError, "code").get_int();
178  std::string message = find_value(objError, "message").get_str();
179  emit reply(RPCConsole::CMD_ERROR, QString::fromStdString(message) + " (code " + QString::number(code) + ")");
180  }
181  catch(std::runtime_error &) // raised when converting to invalid type, i.e. missing code or message
182  { // Show raw JSON object
183  emit reply(RPCConsole::CMD_ERROR, QString::fromStdString(write_string(json_spirit::Value(objError), false)));
184  }
185  }
186  catch (std::exception& e)
187  {
188  emit reply(RPCConsole::CMD_ERROR, QString("Error: ") + QString::fromStdString(e.what()));
189  }
190 }
191 
193  QDialog(parent),
194  ui(new Ui::RPCConsole),
195  clientModel(0),
196  historyPtr(0)
197 {
198  ui->setupUi(this);
199  GUIUtil::restoreWindowGeometry("nRPCConsoleWindow", this->size(), this);
200 
201 #ifndef Q_OS_MAC
202  ui->openDebugLogfileButton->setIcon(QIcon(":/icons/export"));
203 #endif
204 
205  // Install event filter for up and down arrow
206  ui->lineEdit->installEventFilter(this);
207  ui->messagesWidget->installEventFilter(this);
208 
209  connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
210  connect(ui->btnClearTrafficGraph, SIGNAL(clicked()), ui->trafficGraph, SLOT(clear()));
211 
212  // set OpenSSL version label
213  ui->openSSLVersion->setText(SSLeay_version(SSLEAY_VERSION));
214 
215  startExecutor();
216  setTrafficGraphRange(INITIAL_TRAFFIC_GRAPH_MINS);
217 
218  clear();
219 }
220 
222 {
223  GUIUtil::saveWindowGeometry("nRPCConsoleWindow", this);
224  emit stopExecutor();
225  delete ui;
226 }
227 
228 bool RPCConsole::eventFilter(QObject* obj, QEvent *event)
229 {
230  if(event->type() == QEvent::KeyPress) // Special key handling
231  {
232  QKeyEvent *keyevt = static_cast<QKeyEvent*>(event);
233  int key = keyevt->key();
234  Qt::KeyboardModifiers mod = keyevt->modifiers();
235  switch(key)
236  {
237  case Qt::Key_Up: if(obj == ui->lineEdit) { browseHistory(-1); return true; } break;
238  case Qt::Key_Down: if(obj == ui->lineEdit) { browseHistory(1); return true; } break;
239  case Qt::Key_PageUp: /* pass paging keys to messages widget */
240  case Qt::Key_PageDown:
241  if(obj == ui->lineEdit)
242  {
243  QApplication::postEvent(ui->messagesWidget, new QKeyEvent(*keyevt));
244  return true;
245  }
246  break;
247  default:
248  // Typing in messages widget brings focus to line edit, and redirects key there
249  // Exclude most combinations and keys that emit no text, except paste shortcuts
250  if(obj == ui->messagesWidget && (
251  (!mod && !keyevt->text().isEmpty() && key != Qt::Key_Tab) ||
252  ((mod & Qt::ControlModifier) && key == Qt::Key_V) ||
253  ((mod & Qt::ShiftModifier) && key == Qt::Key_Insert)))
254  {
255  ui->lineEdit->setFocus();
256  QApplication::postEvent(ui->lineEdit, new QKeyEvent(*keyevt));
257  return true;
258  }
259  }
260  }
261  return QDialog::eventFilter(obj, event);
262 }
263 
265 {
266  clientModel = model;
267  ui->trafficGraph->setClientModel(model);
268  if(model)
269  {
270  // Keep up to date with client
272  connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
273 
274  setNumBlocks(model->getNumBlocks());
275  connect(model, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
276 
278  connect(model, SIGNAL(bytesChanged(quint64,quint64)), this, SLOT(updateTrafficStats(quint64, quint64)));
279 
280  // Provide initial values
281  ui->clientVersion->setText(model->formatFullVersion());
282  ui->clientName->setText(model->clientName());
283  ui->buildDate->setText(model->formatBuildDate());
284  ui->startupTime->setText(model->formatClientStartupTime());
285 
286  ui->networkName->setText(model->getNetworkName());
287  }
288 }
289 
290 static QString categoryClass(int category)
291 {
292  switch(category)
293  {
294  case RPCConsole::CMD_REQUEST: return "cmd-request"; break;
295  case RPCConsole::CMD_REPLY: return "cmd-reply"; break;
296  case RPCConsole::CMD_ERROR: return "cmd-error"; break;
297  default: return "misc";
298  }
299 }
300 
302 {
303  ui->messagesWidget->clear();
304  history.clear();
305  historyPtr = 0;
306  ui->lineEdit->clear();
307  ui->lineEdit->setFocus();
308 
309  // Add smoothly scaled icon images.
310  // (when using width/height on an img, Qt uses nearest instead of linear interpolation)
311  for(int i=0; ICON_MAPPING[i].url; ++i)
312  {
313  ui->messagesWidget->document()->addResource(
314  QTextDocument::ImageResource,
315  QUrl(ICON_MAPPING[i].url),
316  QImage(ICON_MAPPING[i].source).scaled(ICON_SIZE, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
317  }
318 
319  // Set default style sheet
320  ui->messagesWidget->document()->setDefaultStyleSheet(
321  "table { }"
322  "td.time { color: #808080; padding-top: 3px; } "
323  "td.message { font-family: monospace; font-size: 12px; } " // Todo: Remove fixed font-size
324  "td.cmd-request { color: #006060; } "
325  "td.cmd-error { color: red; } "
326  "b { color: #006060; } "
327  );
328 
329  message(CMD_REPLY, (tr("Welcome to the Bitcoin RPC console.") + "<br>" +
330  tr("Use up and down arrows to navigate history, and <b>Ctrl-L</b> to clear screen.") + "<br>" +
331  tr("Type <b>help</b> for an overview of available commands.")), true);
332 }
333 
335 {
336  // Ignore escape keypress if this is not a seperate window
337  if(windowType() != Qt::Widget)
338  QDialog::reject();
339 }
340 
341 void RPCConsole::message(int category, const QString &message, bool html)
342 {
343  QTime time = QTime::currentTime();
344  QString timeString = time.toString();
345  QString out;
346  out += "<table><tr><td class=\"time\" width=\"65\">" + timeString + "</td>";
347  out += "<td class=\"icon\" width=\"32\"><img src=\"" + categoryClass(category) + "\"></td>";
348  out += "<td class=\"message " + categoryClass(category) + "\" valign=\"middle\">";
349  if(html)
350  out += message;
351  else
352  out += GUIUtil::HtmlEscape(message, true);
353  out += "</td></tr></table>";
354  ui->messagesWidget->append(out);
355 }
356 
358 {
359  if (!clientModel)
360  return;
361 
362  QString connections = QString::number(count) + " (";
363  connections += tr("In:") + " " + QString::number(clientModel->getNumConnections(CONNECTIONS_IN)) + " / ";
364  connections += tr("Out:") + " " + QString::number(clientModel->getNumConnections(CONNECTIONS_OUT)) + ")";
365 
366  ui->numberOfConnections->setText(connections);
367 }
368 
370 {
371  ui->numberOfBlocks->setText(QString::number(count));
372  if(clientModel)
373  ui->lastBlockTime->setText(clientModel->getLastBlockDate().toString());
374 }
375 
377 {
378  QString cmd = ui->lineEdit->text();
379  ui->lineEdit->clear();
380 
381  if(!cmd.isEmpty())
382  {
383  message(CMD_REQUEST, cmd);
384  emit cmdRequest(cmd);
385  // Truncate history from current position
386  history.erase(history.begin() + historyPtr, history.end());
387  // Append command to history
388  history.append(cmd);
389  // Enforce maximum history size
390  while(history.size() > CONSOLE_HISTORY)
391  history.removeFirst();
392  // Set pointer to end of history
393  historyPtr = history.size();
394  // Scroll console view to end
395  scrollToEnd();
396  }
397 }
398 
400 {
401  historyPtr += offset;
402  if(historyPtr < 0)
403  historyPtr = 0;
404  if(historyPtr > history.size())
405  historyPtr = history.size();
406  QString cmd;
407  if(historyPtr < history.size())
408  cmd = history.at(historyPtr);
409  ui->lineEdit->setText(cmd);
410 }
411 
413 {
414  QThread *thread = new QThread;
415  RPCExecutor *executor = new RPCExecutor();
416  executor->moveToThread(thread);
417 
418  // Replies from executor object must go to this object
419  connect(executor, SIGNAL(reply(int,QString)), this, SLOT(message(int,QString)));
420  // Requests from this object must go to executor
421  connect(this, SIGNAL(cmdRequest(QString)), executor, SLOT(request(QString)));
422 
423  // On stopExecutor signal
424  // - queue executor for deletion (in execution thread)
425  // - quit the Qt event loop in the execution thread
426  connect(this, SIGNAL(stopExecutor()), executor, SLOT(deleteLater()));
427  connect(this, SIGNAL(stopExecutor()), thread, SLOT(quit()));
428  // Queue the thread for deletion (in this thread) when it is finished
429  connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
430 
431  // Default implementation of QThread::run() simply spins up an event loop in the thread,
432  // which is what we want.
433  thread->start();
434 }
435 
437 {
438  if(ui->tabWidget->widget(index) == ui->tab_console)
439  {
440  ui->lineEdit->setFocus();
441  }
442 }
443 
445 {
447 }
448 
450 {
451  QScrollBar *scrollbar = ui->messagesWidget->verticalScrollBar();
452  scrollbar->setValue(scrollbar->maximum());
453 }
454 
456 {
457  const int multiplier = 5; // each position on the slider represents 5 min
458  int mins = value * multiplier;
459  setTrafficGraphRange(mins);
460 }
461 
462 QString RPCConsole::FormatBytes(quint64 bytes)
463 {
464  if(bytes < 1024)
465  return QString(tr("%1 B")).arg(bytes);
466  if(bytes < 1024 * 1024)
467  return QString(tr("%1 KB")).arg(bytes / 1024);
468  if(bytes < 1024 * 1024 * 1024)
469  return QString(tr("%1 MB")).arg(bytes / 1024 / 1024);
470 
471  return QString(tr("%1 GB")).arg(bytes / 1024 / 1024 / 1024);
472 }
473 
475 {
477  if(mins < 60) {
478  ui->lblGraphRange->setText(QString(tr("%1 m")).arg(mins));
479  } else {
480  int hours = mins / 60;
481  int minsLeft = mins % 60;
482  if(minsLeft == 0) {
483  ui->lblGraphRange->setText(QString(tr("%1 h")).arg(hours));
484  } else {
485  ui->lblGraphRange->setText(QString(tr("%1 h %2 m")).arg(hours).arg(minsLeft));
486  }
487  }
488 }
489 
490 void RPCConsole::updateTrafficStats(quint64 totalBytesIn, quint64 totalBytesOut)
491 {
492  ui->lblBytesIn->setText(FormatBytes(totalBytesIn));
493  ui->lblBytesOut->setText(FormatBytes(totalBytesOut));
494 }
void openDebugLogfile()
Definition: guiutil.cpp:367
void reject()
Definition: rpcconsole.cpp:334
Local Bitcoin RPC console.
Definition: rpcconsole.h:17
QLabel * clientVersion
Definition: ui_rpcconsole.h:45
QLabel * lblGraphRange
Definition: ui_rpcconsole.h:79
static QString FormatBytes(quint64 bytes)
Definition: rpcconsole.cpp:462
QPushButton * openDebugLogfileButton
Definition: ui_rpcconsole.h:64
void on_lineEdit_returnPressed()
Definition: rpcconsole.cpp:376
QLabel * numberOfConnections
Definition: ui_rpcconsole.h:56
QLabel * networkName
Definition: ui_rpcconsole.h:54
void message(int category, const QString &message, bool html=false)
Definition: rpcconsole.cpp:341
QStringList history
Definition: rpcconsole.h:72
quint64 getTotalBytesRecv() const
Definition: clientmodel.cpp:68
bool parseCommandLine(std::vector< std::string > &args, const std::string &strCommand)
Split shell command line into a list of arguments.
Definition: rpcconsole.cpp:74
QTextEdit * messagesWidget
Definition: ui_rpcconsole.h:68
int getNumConnections(unsigned int flags=CONNECTIONS_ALL) const
Return number of connections, default is in- and outbound (total)
Definition: clientmodel.cpp:42
void scrollToEnd()
Scroll console view to end.
Definition: rpcconsole.cpp:449
json_spirit::Value execute(const std::string &method, const json_spirit::Array &params) const
Execute a method.
Definition: rpcserver.cpp:872
static QString categoryClass(int category)
Definition: rpcconsole.cpp:290
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:222
const struct @5 ICON_MAPPING[]
QLabel * startupTime
Definition: ui_rpcconsole.h:51
QString formatClientStartupTime() const
void setupUi(QDialog *RPCConsole)
Definition: ui_rpcconsole.h:94
void on_tabWidget_currentChanged(int index)
Definition: rpcconsole.cpp:436
void saveWindowGeometry(const QString &strSetting, QWidget *parent)
Save window size and position.
Definition: guiutil.cpp:732
QPushButton * btnClearTrafficGraph
Definition: ui_rpcconsole.h:80
QString getNetworkName() const
Return network (main, testnet3, regtest)
void setClientModel(ClientModel *model)
Definition: rpcconsole.cpp:264
const char * url
Definition: rpcconsole.cpp:35
void reply(int category, const QString &command)
QLabel * numberOfBlocks
Definition: ui_rpcconsole.h:59
const char * source
Definition: rpcconsole.cpp:36
void browseHistory(int offset)
Go forward or back in history.
Definition: rpcconsole.cpp:399
const QSize ICON_SIZE(24, 24)
const int CONSOLE_HISTORY
Definition: rpcconsole.cpp:29
QDateTime getLastBlockDate() const
Definition: clientmodel.cpp:78
QPushButton * clearButton
Definition: ui_rpcconsole.h:72
QLabel * lblBytesOut
Definition: ui_rpcconsole.h:91
QLineEdit * lineEdit
Definition: ui_rpcconsole.h:71
QLabel * lastBlockTime
Definition: ui_rpcconsole.h:61
void request(const QString &command)
Definition: rpcconsole.cpp:144
QString clientName() const
int historyPtr
Definition: rpcconsole.h:73
QLabel * openSSLVersion
Definition: ui_rpcconsole.h:47
QString formatBuildDate() const
void on_openDebugLogfileButton_clicked()
open the debug.log from the current datadir
Definition: rpcconsole.cpp:444
const CRPCTable tableRPC
Definition: rpcserver.cpp:928
void restoreWindowGeometry(const QString &strSetting, const QSize &defaultSize, QWidget *parent)
Restore window size and position.
Definition: guiutil.cpp:739
Model for Bitcoin network client.
Definition: clientmodel.h:36
ClientModel * clientModel
Definition: rpcconsole.h:71
TrafficGraphWidget * trafficGraph
Definition: ui_rpcconsole.h:76
virtual bool eventFilter(QObject *obj, QEvent *event)
Definition: rpcconsole.cpp:228
QLabel * buildDate
Definition: ui_rpcconsole.h:49
void setTrafficGraphRange(int mins)
Definition: rpcconsole.cpp:474
int getNumBlocks() const
Definition: clientmodel.cpp:56
QLabel * lblBytesIn
Definition: ui_rpcconsole.h:87
quint64 getTotalBytesSent() const
Definition: clientmodel.cpp:73
RPCConsole(QWidget *parent)
Definition: rpcconsole.cpp:192
void updateTrafficStats(quint64 totalBytesIn, quint64 totalBytesOut)
update traffic statistics
Definition: rpcconsole.cpp:490
void setNumConnections(int count)
Set number of connections shown in the UI.
Definition: rpcconsole.cpp:357
void startExecutor()
Definition: rpcconsole.cpp:412
void on_sldGraphRange_valueChanged(int value)
change the time range of the network traffic graph
Definition: rpcconsole.cpp:455
void clear()
Definition: rpcconsole.cpp:301
QWidget * tab_console
Definition: ui_rpcconsole.h:66
const int INITIAL_TRAFFIC_GRAPH_MINS
Definition: rpcconsole.cpp:32
void setGraphRangeMins(int mins)
QLabel * clientName
Definition: ui_rpcconsole.h:43
QTabWidget * tabWidget
Definition: ui_rpcconsole.h:38
Ui::RPCConsole * ui
Definition: rpcconsole.h:70
void stopExecutor()
void setNumBlocks(int count)
Set number of blocks shown in the UI.
Definition: rpcconsole.cpp:369
QString formatFullVersion() const
void cmdRequest(const QString &command)
Array RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Definition: rpcclient.cpp:117
void setClientModel(ClientModel *model)