Instrument Neutral Distributed Interface INDI 2.2.5
IndiServerController.cpp
Go to the documentation of this file.
1/*******************************************************************************
2 Copyright(c) 2022 Ludovic Pollet. All rights reserved.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*******************************************************************************/
18
19#include <system_error>
20#include <sys/wait.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24
25#include "utils.h"
27
28
30 fifo = false;
31 // Use resources unique to this process so that tests can run in parallel
32 unixSocketPath = uniqueTestName("/tmp/indi-test-server");
33 fifoPath = uniqueTestName("/tmp/indi-test-fifo");
34 tcpPort = findFreeTcpPort();
35}
36
38 // Abort any pending indi server
39 kill();
40 join();
41 if (fifo) {
42 unlink(fifoPath.c_str());
43 }
44}
45
46
48 this->fifo = fifo;
49}
50
51void IndiServerController::start(const std::vector<std::string> & args) {
52 ProcessController::start(INDISERVER_PATH, args);
53}
54
55void IndiServerController::startDriver(const std::string & path) {
56 std::vector<std::string> args = { "-p", std::to_string(tcpPort), "-r", "0", "-vvv" };
57#ifdef ENABLE_INDI_SHARED_MEMORY
58 args.push_back("-u");
59 args.push_back(unixSocketPath);
60#endif
61
62 if (fifo) {
63 unlink(fifoPath.c_str());
64 if (mkfifo(fifoPath.c_str(), 0600) == -1) {
65 throw std::system_error(errno, std::generic_category(), "mkfifo");
66 }
67 args.push_back("-f");
68 args.push_back(fifoPath);
69 }
70 args.push_back(path);
71
72 start(args);
73}
74
75void IndiServerController::addDriver(const std::string & driver) {
76 if (!fifo) {
77 throw new std::runtime_error("Fifo is not enabled - cannot add driver");
78 }
79
80 int fifoFd = open(fifoPath.c_str(), O_WRONLY);
81 if (fifoFd == -1) {
82 throw std::system_error(errno, std::generic_category(), "opening fifo");
83 }
84
85 std::string cmd = "start " + driver +"\n";
86 int wr = write(fifoFd, cmd.data(), cmd.length());
87 if (wr == -1) {
88 auto e = errno;
89 close(fifoFd);
90 throw std::system_error(e, std::generic_category(), "write to fifo");
91 }
92
93 close(fifoFd);
94}
95
97 return unixSocketPath;
98}
99
101 return tcpPort;
102}
103
void addDriver(const std::string &path)
void start(const std::vector< std::string > &args)
std::string getUnixSocketPath() const
void startDriver(const std::string &driver)
void start(const std::string &path, const std::vector< std::string > &args)
int errno
uint8_t cmd[4]
Definition pwc-ioctl.h:2
std::string uniqueTestName(const std::string &prefix)
Definition utils.cpp:360
int findFreeTcpPort()
Definition utils.cpp:351