~ubuntu-branches/ubuntu/precise/stellarium/precise

« back to all changes in this revision

Viewing changes to plugins/TelescopeControl/src/servers/Server.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2010-02-15 20:48:39 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20100215204839-u3qgbv60rho997yk
Tags: 0.10.3-0ubuntu1
* New upstream release.
  - fixes intel rendering bug (LP: #480553)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include "LogFile.hpp"
 
29
 
 
30
void Server::SocketList::clear(void)
 
31
{
 
32
        for (const_iterator it(begin()); it != end(); it++)
 
33
        {
 
34
                delete (*it);
 
35
        }
 
36
        list<Socket*>::clear();
 
37
}
 
38
 
 
39
Server::Server(int port)
 
40
{
 
41
        //Socket *listener = new Listener(*this, port);
 
42
        //socket_list.push_back(listener);
 
43
}
 
44
 
 
45
void Server::sendPosition(unsigned int ra_int, int dec_int, int status)
 
46
{
 
47
        for (SocketList::const_iterator it(socket_list.begin());
 
48
             it != socket_list.end();
 
49
             it++)
 
50
        {
 
51
                (*it)->sendPosition(ra_int, dec_int, status);
 
52
        }
 
53
}
 
54
 
 
55
void Server::step(long long int timeout_micros)
 
56
{
 
57
        fd_set read_fds, write_fds;
 
58
        FD_ZERO(&read_fds);
 
59
        FD_ZERO(&write_fds);
 
60
        int fd_max = -1;
 
61
        
 
62
        for (SocketList::const_iterator it(socket_list.begin());
 
63
             it != socket_list.end();
 
64
             it++)
 
65
        {
 
66
                (*it)->prepareSelectFds(read_fds, write_fds, fd_max);
 
67
        }
 
68
        
 
69
        struct timeval tv;
 
70
        if (timeout_micros < 0)
 
71
                timeout_micros = 0;
 
72
        tv.tv_sec = timeout_micros / 1000000;
 
73
        tv.tv_usec = timeout_micros % 1000000;
 
74
        const int select_rc = select(fd_max+1, &read_fds, &write_fds, 0, &tv);
 
75
        if (select_rc > 0)
 
76
        {
 
77
                SocketList::iterator it(socket_list.begin());
 
78
                while (it != socket_list.end())
 
79
                {
 
80
                        (*it)->handleSelectFds(read_fds, write_fds);
 
81
                        if ((*it)->isClosed())
 
82
                        {
 
83
                                SocketList::iterator tmp(it);
 
84
                                it++;
 
85
                                delete (*tmp);
 
86
                                socket_list.erase(tmp);
 
87
                        }
 
88
                        else
 
89
                        {
 
90
                                it++;
 
91
                        }
 
92
                }
 
93
        }
 
94
}
 
95
 
 
96
void Server::closeAcceptedConnections(void)
 
97
{
 
98
        for (SocketList::iterator it(socket_list.begin());
 
99
             it != socket_list.end();
 
100
             it++)
 
101
        {
 
102
                if ((*it)->isTcpConnection())
 
103
                {
 
104
                        (*it)->hangup();
 
105
                }
 
106
        }
 
107
}
 
108
 
 
109