Instrument Neutral Distributed Interface INDI  2.0.2
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"
26 #include "IndiServerController.h"
27 
28 
29 #define TEST_TCP_PORT 17624
30 #define TEST_UNIX_SOCKET "/tmp/indi-test-server"
31 #define TEST_INDI_FIFO "/tmp/indi-test-fifo"
32 #define STRINGIFY_TOK(x) #x
33 #define TO_STRING(x) STRINGIFY_TOK(x)
34 
36  fifo = false;
37 }
38 
40  // Abort any pending indi server
41  kill();
42  join();
43 }
44 
45 
47  this->fifo = fifo;
48 }
49 
50 void IndiServerController::start(const std::vector<std::string> & args) {
51  ProcessController::start("../indiserver/indiserver", args);
52 }
53 
54 void IndiServerController::startDriver(const std::string & path) {
55  std::vector<std::string> args = { "-p", TO_STRING(TEST_TCP_PORT), "-r", "0", "-vvv" };
56 #ifdef ENABLE_INDI_SHARED_MEMORY
57  args.push_back("-u");
58  args.push_back(TEST_UNIX_SOCKET);
59 #endif
60 
61  if (fifo) {
62  unlink(TEST_INDI_FIFO);
63  if (mkfifo(TEST_INDI_FIFO, 0600) == -1) {
64  throw std::system_error(errno, std::generic_category(), "mkfifo");
65  }
66  args.push_back("-f");
67  args.push_back(TEST_INDI_FIFO);
68  }
69  args.push_back(path);
70 
71  start(args);
72 }
73 
74 void IndiServerController::addDriver(const std::string & driver) {
75  if (!fifo) {
76  throw new std::runtime_error("Fifo is not enabled - cannot add driver");
77  }
78 
79  int fifoFd = open(TEST_INDI_FIFO, O_WRONLY);
80  if (fifoFd == -1) {
81  throw std::system_error(errno, std::generic_category(), "opening fifo");
82  }
83 
84  std::string cmd = "start " + driver +"\n";
85  int wr = write(fifoFd, cmd.data(), cmd.length());
86  if (wr == -1) {
87  auto e = errno;
88  close(fifoFd);
89  throw std::system_error(e, std::generic_category(), "write to fifo");
90  }
91 
92  close(fifoFd);
93 }
94 
96  return TEST_UNIX_SOCKET;
97 }
98 
100  return TEST_TCP_PORT;
101 }
102 
#define TO_STRING(x)
#define TEST_INDI_FIFO
#define TEST_TCP_PORT
#define TEST_UNIX_SOCKET
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
__u8 cmd[4]
Definition: pwc-ioctl.h:2