Instrument Neutral Distributed Interface INDI  2.0.2
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 
33 {
34  close();
35 }
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
48 void ServerMock::listen(int tcpPort)
49 {
50  close();
51  fd = tcpSocketListen(tcpPort);
52 }
53 
54 void ServerMock::listen(const std::string &unixPath)
55 {
56  close();
57  fd = unixSocketListen(unixPath);
58 }
59 
61 {
62  if (fd == -1)
63  {
64  throw std::runtime_error("Accept called on non listening server");
65  }
66  int child = socketAccept(fd);
67  into.associate(child);
68 }
void associate(int fd)
void listen(int tcpPort)
Definition: ServerMock.cpp:48
virtual ~ServerMock()
Definition: ServerMock.cpp:32
void close()
Definition: ServerMock.cpp:37
void accept(IndiClientMock &into)
Definition: ServerMock.cpp:60
int socketAccept(int fd)
Definition: utils.cpp:144
int unixSocketListen(const std::string &unixAddr)
Definition: utils.cpp:71
int tcpSocketListen(int port)
Definition: utils.cpp:108