Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #ifndef _HOST_H_ |
| 31 | #define _HOST_H_ |
| 32 | |
Vadim Iosevich | 20d50d8 | 2017-09-19 11:38:15 +0300 | [diff] [blame] | 33 | #include <memory> |
| 34 | #include <future> |
| 35 | #include "EventsDefinitions.h" |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 36 | #include "CommandsTcpServer.h" |
| 37 | #include "EventsTcpServer.h" |
| 38 | #include "CommandsHandler.h" |
| 39 | #include "UdpServer.h" |
| 40 | #include "HostInfo.h" |
| 41 | #include "DeviceManager.h" |
| 42 | |
| 43 | /* |
| 44 | * Host is a class that holds the TCP and UDP server. |
| 45 | * It also holds the Device Manager. |
| 46 | */ |
| 47 | class Host |
| 48 | { |
| 49 | public: |
| 50 | |
| 51 | /* |
| 52 | * Host is s singletone due to HandleOsSignals - signal is a global function that can't get a pointer to the host as an argument |
| 53 | */ |
| 54 | static Host& GetHost() |
| 55 | { |
| 56 | static Host host; |
| 57 | return host; |
| 58 | } |
| 59 | |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 60 | /* |
| 61 | * StratHost starts each one of the servers it holds |
| 62 | */ |
Vadim Iosevich | e76832c | 2017-11-05 09:09:14 +0200 | [diff] [blame] | 63 | void StartHost(unsigned int commandsTcpPort, unsigned int eventsTcpPort, unsigned int udpPortIn, unsigned int udpPortOut, unsigned int httpPort); |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * StopHost stops each one of the servers it holds |
| 67 | */ |
| 68 | void StopHost(); |
| 69 | |
| 70 | HostInfo& GetHostInfo() { return m_hostInfo; } |
Vadim Iosevich | e76832c | 2017-11-05 09:09:14 +0200 | [diff] [blame] | 71 | bool GetHostUpdate(HostData& data); |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 72 | |
| 73 | DeviceManager& GetDeviceManager() { return m_deviceManager; } |
| 74 | |
Vadim Iosevich | 20d50d8 | 2017-09-19 11:38:15 +0300 | [diff] [blame] | 75 | // Push the given event through Events TCP Server |
| 76 | void PushEvent(const HostManagerEventBase& event) const; |
| 77 | |
Vadim Iosevich | e76832c | 2017-11-05 09:09:14 +0200 | [diff] [blame] | 78 | // Decide whether to show host_manager menu |
| 79 | void SetMenuDisplay(bool menuDisplayOn); |
| 80 | |
Vadim Iosevich | 20d50d8 | 2017-09-19 11:38:15 +0300 | [diff] [blame] | 81 | // delete copy Cnstr and assignment operator |
| 82 | // keep public for better error message |
| 83 | // no move members will be declared implicitly |
| 84 | Host(const Host&) = delete; |
| 85 | Host& operator=(const Host&) = delete; |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 86 | |
| 87 | private: |
Vadim Iosevich | 20d50d8 | 2017-09-19 11:38:15 +0300 | [diff] [blame] | 88 | std::shared_ptr<CommandsTcpServer> m_pCommandsTcpServer; |
| 89 | std::shared_ptr<EventsTcpServer> m_pEventsTcpServer; |
| 90 | std::shared_ptr<UdpServer> m_pUdpServer; |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 91 | HostInfo m_hostInfo; |
Vadim Iosevich | 20d50d8 | 2017-09-19 11:38:15 +0300 | [diff] [blame] | 92 | std::promise<void> m_eventsTCPServerReadyPromise; // sync. of device manager with events TCP server creation |
| 93 | DeviceManager m_deviceManager; // note: it should be defined after the promise passsed to its Cnstr |
Vadim Iosevich | e76832c | 2017-11-05 09:09:14 +0200 | [diff] [blame] | 94 | bool m_MenuDisplayOn; |
Vadim Iosevich | 20d50d8 | 2017-09-19 11:38:15 +0300 | [diff] [blame] | 95 | |
| 96 | // define Cnstr to be private, part of Singleton pattern |
| 97 | Host(); |
Vadim Iosevich | e76832c | 2017-11-05 09:09:14 +0200 | [diff] [blame] | 98 | |
| 99 | // Menu display function |
| 100 | void DisplayMenu(); |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | |
| 104 | #endif // ! _HOST_H_ |