Skip to content

Commit 871da42

Browse files
Detect running server on startup (#2828)
Signed-off-by: GauravKumar9920 <[email protected]> Signed-off-by: Gaurav Kumar <[email protected]> Co-authored-by: Arjo Chakravarty <[email protected]>
1 parent 7eb7532 commit 871da42

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

src/SimulationRunner.cc

+8
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ SimulationRunner::SimulationRunner(const sdf::World &_world,
192192

193193
this->node = std::make_unique<transport::Node>(opts);
194194

195+
std::vector<transport::MessagePublisher> pubs;
196+
std::vector<transport::MessagePublisher> subs;
197+
this->node->TopicInfo("/world/" + this->worldName + "/stats", pubs, subs);
198+
if (!pubs.empty())
199+
{
200+
gzerr << "Another world of the same name is running" << std::endl;
201+
}
202+
195203
// Create the system manager
196204
this->systemMgr = std::make_unique<SystemManager>(
197205
_systemLoader, &this->entityCompMgr, &this->eventMgr, validNs,

src/gui/Gui_TEST.cc

+2
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ TEST_F(GuiTest, GZ_UTILS_TEST_ENABLED_ONLY_ON_LINUX(QuickStart))
252252
// Close the quick start window
253253
gzdbg << "Closing the quickstart window" << std::endl;
254254
ASSERT_EQ(1, gui::App()->allWindows().count());
255+
while (!gui::App()->allWindows()[0]->isExposed())
256+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
255257
gui::App()->allWindows()[0]->close();
256258

257259
gzdbg << "Waiting for main window" << std::endl;

src/gui/QuickStartHandler.cc

+13
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,23 @@
1818
#include "QuickStartHandler.hh"
1919

2020
#include "gz/sim/InstallationDirectories.hh"
21+
#include <gz/transport/Node.hh>
22+
#include <gz/common/Console.hh>
2123

2224
using namespace gz;
2325
using namespace sim::gui;
2426

27+
QuickStartHandler::QuickStartHandler()
28+
{
29+
std::vector<transport::MessagePublisher> publishers;
30+
std::vector<transport::MessagePublisher> subscribers;
31+
this->node.TopicInfo("/stats", publishers, subscribers);
32+
if (!publishers.empty())
33+
{
34+
gzerr << "Detected an already running world on start" << std::endl;
35+
}
36+
}
37+
2538
/////////////////////////////////////////////////
2639
QString QuickStartHandler::WorldsPath() const
2740
{

src/gui/QuickStartHandler.hh

+12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
#include "gz/sim/config.hh"
2424
#include "gz/sim/gui/Export.hh"
2525

26+
#include <gz/transport/Node.hh>
27+
#include <gz/common/Console.hh>
28+
29+
#include <memory>
30+
#include <vector>
31+
2632
namespace gz
2733
{
2834
namespace sim
@@ -36,6 +42,9 @@ class GZ_SIM_GUI_VISIBLE QuickStartHandler : public QObject
3642
{
3743
Q_OBJECT
3844

45+
// Explicitly declare the constructor
46+
public: QuickStartHandler();
47+
3948
/// \brief Get worlds path
4049
/// \return worlds directory path
4150
public: Q_INVOKABLE QString WorldsPath() const;
@@ -69,6 +78,9 @@ class GZ_SIM_GUI_VISIBLE QuickStartHandler : public QObject
6978

7079
/// \brief Get starting world url.
7180
private: std::string startingWorld{""};
81+
82+
// Add a member for the transport node
83+
private: gz::transport::Node node;
7284
};
7385
}
7486
}

0 commit comments

Comments
 (0)