PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/External/Tools/Bridge/bridgeserver.cpp

http://flashdevelop.googlecode.com/
C++ | 110 lines | 87 code | 21 blank | 2 comment | 15 complexity | bc4bb46208dbd24b5cf7b2aa24b5093f MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.0, BSD-2-Clause
  1. #include <QDebug>
  2. #include <QSettings>
  3. #include <QDir>
  4. #include <QProcess>
  5. #include <QDesktopServices>
  6. #include <QUrl>
  7. #include "bridgeserver.h"
  8. #include "bridgethread.h"
  9. BridgeServer::BridgeServer(QObject *parent)
  10. : QTcpServer(parent)
  11. {
  12. runningThreads = 0;
  13. QSettings settings;
  14. settings.beginGroup("server");
  15. qDebug() << "Connecting:" << settings.value("host").toString() << settings.value("port").toInt();
  16. QHostAddress host(settings.value("host").toString());
  17. listen(host, settings.value("port").toInt());
  18. qDebug() << (isListening() ? "Success" : "Failure");
  19. }
  20. void BridgeServer::notifyStatus()
  21. {
  22. emit bridgeStatus(runningThreads, watched.keys().count());
  23. }
  24. void BridgeServer::incomingConnection(int socketDescriptor)
  25. {
  26. BridgeThread *thread = new BridgeThread(socketDescriptor, 0);
  27. connect(thread, SIGNAL(disconnected()), this, SLOT(threadDisconnected()));
  28. connect(thread, SIGNAL(command(QString,QString)), this, SLOT(command(QString,QString)));
  29. runningThreads++;
  30. notifyStatus();
  31. }
  32. void BridgeServer::command(QString cmd, QString value)
  33. {
  34. BridgeThread *thread = (BridgeThread*)sender();
  35. //qDebug() << cmd << value;
  36. if (cmd == "watch")
  37. {
  38. releaseHandler(thread);
  39. BridgeHandler *handler = createWatchHandler(value);
  40. handler->useCount++;
  41. thread->handler = handler;
  42. connect(handler, SIGNAL(notifyChanged(QString)), thread, SLOT(sendMessage(QString)));
  43. }
  44. else if (cmd == "unwatch") releaseHandler(thread);
  45. else if (cmd == "open") openDocument(value);
  46. else qDebug() << cmd << value;
  47. }
  48. void BridgeServer::openDocument(QString path)
  49. {
  50. BridgeHandler handler;
  51. QString localPath = handler.getLocalPath(path.replace('\\', '/'));
  52. qDebug() << "Open:" << localPath;
  53. QFile file(localPath);
  54. if (file.exists())
  55. QDesktopServices::openUrl(QUrl::fromLocalFile(localPath));
  56. else qDebug() << "File not found:" << path;
  57. }
  58. BridgeHandler* BridgeServer::createWatchHandler(QString path)
  59. {
  60. BridgeHandler *handler;
  61. if (watched.contains(path)) handler = watched[path];
  62. else
  63. {
  64. handler = new BridgeHandler(this);
  65. handler->command("watch", path);
  66. watched[path] = handler;
  67. notifyStatus();
  68. }
  69. return handler;
  70. }
  71. void BridgeServer::threadDisconnected()
  72. {
  73. BridgeThread *thread = (BridgeThread*)sender();
  74. //qDebug() << "disconnected" << thread;
  75. releaseHandler(thread);
  76. thread->deleteLater();
  77. runningThreads--;
  78. notifyStatus();
  79. }
  80. void BridgeServer::releaseHandler(BridgeThread *thread)
  81. {
  82. if (thread->handler != 0)
  83. {
  84. disconnect(thread->handler, SIGNAL(notifyChanged(QString)), thread, SLOT(sendMessage(QString)));
  85. if (--thread->handler->useCount <= 0)
  86. watched.remove(thread->handler->watched);
  87. thread->handler = 0;
  88. }
  89. }