source: BAORadio/libindi/libindi/Indi_Stellarium/src/Server.cpp @ 623

Last change on this file since 623 was 623, checked in by frichard, 13 years ago
File size: 2.6 KB
Line 
1/*
2The stellarium telescope library helps building
3telescope server programs, that can communicate with stellarium
4by means of the stellarium TCP telescope protocol.
5It also contains smaple server classes (dummy, Meade LX200).
6
7Author and Copyright of this file and of the stellarium telescope library:
8Johannes Gajdosik, 2006
9
10This library is free software; you can redistribute it and/or
11modify it under the terms of the GNU Lesser General Public
12License as published by the Free Software Foundation; either
13version 2.1 of the License, or (at your option) any later version.
14
15This library is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18Lesser General Public License for more details.
19
20You should have received a copy of the GNU Lesser General Public
21License along with this library; if not, write to the Free Software
22Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23*/
24
25#include "Server.hpp"
26#include "Socket.hpp"
27#include "Listener.hpp"
28
29void Server::SocketList::clear(void)
30{
31        for (const_iterator it(begin()); it != end(); it++)
32        {
33                delete (*it);
34        }
35        list<Socket*>::clear();
36}
37
38Server::Server(int port)
39{
40        Socket *listener = new Listener(*this, port);
41        socket_list.push_back(listener);
42}
43
44void Server::sendPosition(unsigned int ra_int, int dec_int, int status)
45{
46        for (SocketList::const_iterator it(socket_list.begin());
47             it != socket_list.end();
48             it++)
49        {
50                (*it)->sendPosition(ra_int, dec_int, status);
51        }
52}
53
54void Server::step(long long int timeout_micros)
55{
56        fd_set read_fds, write_fds;
57        FD_ZERO(&read_fds);
58        FD_ZERO(&write_fds);
59        int fd_max = -1;
60       
61        for (SocketList::const_iterator it(socket_list.begin());
62             it != socket_list.end();
63             it++)
64        {
65                (*it)->prepareSelectFds(read_fds, write_fds, fd_max);
66        }
67       
68        struct timeval tv;
69        if (timeout_micros < 0)
70                timeout_micros = 0;
71        tv.tv_sec = timeout_micros / 1000000;
72        tv.tv_usec = timeout_micros % 1000000;
73        const int select_rc = select(fd_max+1, &read_fds, &write_fds, 0, &tv);
74        if (select_rc > 0)
75        {
76                SocketList::iterator it(socket_list.begin());
77                while (it != socket_list.end())
78                {
79                        (*it)->handleSelectFds(read_fds, write_fds);
80                        if ((*it)->isClosed())
81                        {
82                                SocketList::iterator tmp(it);
83                                it++;
84                                delete (*tmp);
85                                socket_list.erase(tmp);
86                        }
87                        else
88                        {
89                                it++;
90                        }
91                }
92        }
93}
94
95void Server::closeAcceptedConnections(void)
96{
97        for (SocketList::iterator it(socket_list.begin());
98             it != socket_list.end();
99             it++)
100        {
101                if ((*it)->isTcpConnection())
102                {
103                        (*it)->hangup();
104                }
105        }
106}
107
108
Note: See TracBrowser for help on using the repository browser.