[623] | 1 | /* |
---|
| 2 | The stellarium telescope library helps building |
---|
| 3 | telescope server programs, that can communicate with stellarium |
---|
| 4 | by means of the stellarium TCP telescope protocol. |
---|
| 5 | It also contains smaple server classes (dummy, Meade LX200). |
---|
| 6 | |
---|
| 7 | Author and Copyright of this file and of the stellarium telescope library: |
---|
| 8 | Johannes Gajdosik, 2006 |
---|
| 9 | |
---|
| 10 | This library is free software; you can redistribute it and/or |
---|
| 11 | modify it under the terms of the GNU Lesser General Public |
---|
| 12 | License as published by the Free Software Foundation; either |
---|
| 13 | version 2.1 of the License, or (at your option) any later version. |
---|
| 14 | |
---|
| 15 | This library is distributed in the hope that it will be useful, |
---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 18 | Lesser General Public License for more details. |
---|
| 19 | |
---|
| 20 | You should have received a copy of the GNU Lesser General Public |
---|
| 21 | License along with this library; if not, write to the Free Software |
---|
| 22 | Foundation, 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 | |
---|
| 29 | void 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 | |
---|
| 38 | Server::Server(int port) |
---|
| 39 | { |
---|
| 40 | Socket *listener = new Listener(*this, port); |
---|
| 41 | socket_list.push_back(listener); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | void 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 | |
---|
| 54 | void 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 | |
---|
| 95 | void 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 | |
---|