Instrument Neutral Distributed Interface INDI 2.2.5
ServerMock.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 <unistd.h>
20#include <stdexcept>
21
22#include "IndiClientMock.h"
23#include "ServerMock.h"
24#include "utils.h"
25
26
28{
29 fd = -1;
30}
31
36
38{
39 if (fd != -1)
40 {
41 ::close(fd);
42 fd = -1;
43 }
44}
45
46
47// Start the listening socket that will receive driver upon their starts
48void ServerMock::listen(int tcpPort)
49{
50 close();
52}
53
54void ServerMock::listen(const std::string &unixPath)
55{
56 close();
57 fd = unixSocketListen(unixPath);
58}
59
61{
62 return tcpSocketPort(fd);
63}
64
66{
67 if (fd == -1)
68 {
69 throw std::runtime_error("Accept called on non listening server");
70 }
71 int child = socketAccept(fd);
72 into.associate(child);
73}
void associate(int fd)
void listen(int tcpPort)
virtual ~ServerMock()
void close()
void accept(IndiClientMock &into)
int tcpPort() const
int socketAccept(int fd)
Definition utils.cpp:144
int unixSocketListen(const std::string &unixAddr)
Definition utils.cpp:71
int tcpSocketPort(int fd)
Definition utils.cpp:340
int tcpSocketListen(int port)
Definition utils.cpp:108