| 1 | /* |
| 2 | * SPDX-FileCopyrightText: (C) 2019-2022 swift Project Community / Contributors (https://swift-project.org/) |
| 3 | * SPDX-FileCopyrightText: (C) 2019-2022 Lars Toenning <[email protected]> |
| 4 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #include "plugin.h" |
| 8 | #include "service.h" |
| 9 | #include "traffic.h" |
| 10 | |
| 11 | #include <Main/fg_props.hxx> |
| 12 | #include <Main/globals.hxx> |
| 13 | #include <functional> |
| 14 | #include <iostream> |
| 15 | #include <simgear/structure/commands.hxx> |
| 16 | |
| 17 | namespace flightgear::swift { |
| 18 | |
| 19 | CPlugin::CPlugin() |
| 20 | { |
| 21 | startServer(); |
| 22 | fgSetBool("/sim/swift/serverRunning", true); |
| 23 | } |
| 24 | |
| 25 | CPlugin::~CPlugin() |
| 26 | { |
| 27 | if (m_dbusConnection) { |
| 28 | m_dbusConnection->close(); |
| 29 | } |
| 30 | if (m_dbusThread.joinable()) { m_dbusThread.join(); } |
| 31 | fgSetBool("/sim/swift/serverRunning", false); |
| 32 | } |
| 33 | |
| 34 | void CPlugin::startServer() |
| 35 | { |
| 36 | m_service.reset(new CService()); |
| 37 | m_traffic.reset(new CTraffic()); |
| 38 | m_dbusP2PServer.reset(new CDBusServer()); |
| 39 | |
| 40 | std::string ip = fgGetString("/sim/swift/address", "127.0.0.1"); |
| 41 | std::string port = fgGetString("/sim/swift/port", "45003"); |
| 42 | std::string listenAddress = "tcp:host=" + ip + ",port=" + port; |
| 43 | if (!m_dbusP2PServer->listen(listenAddress)) { |
| 44 | m_service->addTextMessage("FGSwiftBus startup failed!"); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | m_dbusP2PServer->setDispatcher(&m_dbusDispatcher); |
| 49 | m_dbusP2PServer->setNewConnectionFunc([this](const std::shared_ptr<CDBusConnection>& conn) { |
| 50 | m_dbusConnection = conn; |
| 51 | m_dbusConnection->setDispatcher(&m_dbusDispatcher); |
| 52 | m_service->setDBusConnection(m_dbusConnection); |
| 53 | m_service->registerDBusObjectPath(m_service->InterfaceName(), m_service->ObjectPath()); |
| 54 | m_traffic->setDBusConnection(m_dbusConnection); |
| 55 | m_traffic->registerDBusObjectPath(m_traffic->InterfaceName(), m_traffic->ObjectPath()); |
| 56 | }); |
| 57 | |
| 58 | SG_LOG(SG_NETWORK, SG_INFO, "FGSwiftBus started"); |
| 59 | } |
| 60 | |
| 61 | void CPlugin::fastLoop() |
| 62 | { |
| 63 | this->m_dbusDispatcher.runOnce(); |
| 64 | this->m_service->process(); |
| 65 | this->m_traffic->process(); |
| 66 | this->m_traffic->emitSimFrame(); |
| 67 | } |
| 68 | } // namespace flightgear::swift |