File: IndiServerController.cpp

package info (click to toggle)
indi 2.1.9%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 15,888 kB
  • sloc: cpp: 217,447; ansic: 31,363; xml: 1,195; sh: 311; makefile: 13
file content (102 lines) | stat: -rw-r--r-- 3,002 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*******************************************************************************
  Copyright(c) 2022 Ludovic Pollet. All rights reserved.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Library General Public
 License version 2 as published by the Free Software Foundation.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Library General Public License for more details.

 You should have received a copy of the GNU Library General Public License
 along with this library; see the file COPYING.LIB.  If not, write to
 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 Boston, MA 02110-1301, USA.
*******************************************************************************/

#include <system_error>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "utils.h"
#include "IndiServerController.h"


#define TEST_TCP_PORT 17624
#define TEST_UNIX_SOCKET "/tmp/indi-test-server"
#define TEST_INDI_FIFO "/tmp/indi-test-fifo"
#define STRINGIFY_TOK(x) #x
#define TO_STRING(x) STRINGIFY_TOK(x)

IndiServerController::IndiServerController() {
    fifo = false;
}

IndiServerController::~IndiServerController() {
    // Abort any pending indi server
    kill();
    join();
}


void IndiServerController::setFifo(bool fifo) {
    this->fifo = fifo;
}

void IndiServerController::start(const std::vector<std::string> & args) {
    ProcessController::start("../indiserver/indiserver", args);
}

void IndiServerController::startDriver(const std::string & path) {
    std::vector<std::string> args = { "-p", TO_STRING(TEST_TCP_PORT), "-r", "0", "-vvv" };
#ifdef ENABLE_INDI_SHARED_MEMORY
    args.push_back("-u");
    args.push_back(TEST_UNIX_SOCKET);
#endif

    if (fifo) {
        unlink(TEST_INDI_FIFO);
        if (mkfifo(TEST_INDI_FIFO, 0600) == -1) {
            throw std::system_error(errno, std::generic_category(), "mkfifo");
        }
        args.push_back("-f");
        args.push_back(TEST_INDI_FIFO);
    }
    args.push_back(path);

    start(args);
}

void IndiServerController::addDriver(const std::string & driver) {
    if (!fifo) {
        throw new std::runtime_error("Fifo is not enabled - cannot add driver");
    }

    int fifoFd = open(TEST_INDI_FIFO, O_WRONLY);
    if (fifoFd == -1) {
        throw std::system_error(errno, std::generic_category(), "opening fifo");
    }

    std::string cmd = "start " + driver +"\n";
    int wr = write(fifoFd, cmd.data(), cmd.length());
    if (wr == -1) {
        auto e = errno;
        close(fifoFd);
        throw std::system_error(e, std::generic_category(), "write to fifo");
    }

    close(fifoFd);
}

std::string IndiServerController::getUnixSocketPath() const {
    return TEST_UNIX_SOCKET;
}

int IndiServerController::getTcpPort() const {
  return TEST_TCP_PORT;
}