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

« back to all changes in this revision

Viewing changes to plugins/TelescopeControl/src/servers/NexStarConnection.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, modified for NexStar telescopes by Michael Heinz.
 
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 "NexStarConnection.hpp"
 
26
#include "NexStarCommand.hpp"
 
27
#include "TelescopeClientDirectNexStar.hpp"
 
28
#include "LogFile.hpp"
 
29
 
 
30
#include <iostream>
 
31
using namespace std;
 
32
 
 
33
NexStarConnection::NexStarConnection(Server &server, const char *serial_device) : SerialPort(server, serial_device)
 
34
{
 
35
}
 
36
 
 
37
void NexStarConnection::resetCommunication(void)
 
38
{
 
39
        while (!command_list.empty())
 
40
        {
 
41
                delete command_list.front();
 
42
                command_list.pop_front();
 
43
        }
 
44
        
 
45
        read_buff_end = read_buff;
 
46
        write_buff_end = write_buff;
 
47
#ifdef DEBUG4
 
48
        *log_file << Now() << "NexStarConnection::resetCommunication" << endl;
 
49
#endif
 
50
}
 
51
 
 
52
void NexStarConnection::sendGoto(unsigned int ra_int, int dec_int)
 
53
{
 
54
  sendCommand(new NexStarCommandGotoPosition(server, ra_int, dec_int));
 
55
}
 
56
 
 
57
void NexStarConnection::dataReceived(const char *&p,const char *read_buff_end)
 
58
{
 
59
        if (isClosed())
 
60
        {
 
61
                *log_file << Now() << "NexStarConnection::dataReceived: strange: fd is closed" << endl;
 
62
        }
 
63
        else if (command_list.empty())
 
64
        {
 
65
                #ifdef DEBUG4
 
66
                *log_file << Now() << "NexStarConnection::dataReceived: "
 
67
                                      "error: command_list is empty" << endl;
 
68
                #endif
 
69
                resetCommunication();
 
70
                static_cast<TelescopeClientDirectNexStar*>(&server)->communicationResetReceived();
 
71
        }
 
72
        else if (command_list.front()->needsNoAnswer())
 
73
        {
 
74
                *log_file << Now() << "NexStarConnection::dataReceived: "
 
75
                                      "strange: command(" << *command_list.front()
 
76
                                   << ") needs no answer" << endl;
 
77
        }
 
78
        else
 
79
        {
 
80
                while(true)
 
81
                {
 
82
                        const int rc=command_list.front()->readAnswerFromBuffer(p, read_buff_end);
 
83
                        //*log_file << Now() << "NexStarConnection::dataReceived: "
 
84
                        //                   << *command_list.front() << "->readAnswerFromBuffer returned "
 
85
                        //                   << rc << endl;
 
86
                        if (rc <= 0)
 
87
                        {
 
88
                                if (rc < 0)
 
89
                                {
 
90
                                        resetCommunication();
 
91
                                        static_cast<TelescopeClientDirectNexStar*>(&server)->communicationResetReceived();
 
92
                                }
 
93
                                break;
 
94
                        }
 
95
                        delete command_list.front();
 
96
                        command_list.pop_front();
 
97
                        if (command_list.empty())
 
98
                                break;
 
99
                        if (!command_list.front()->writeCommandToBuffer(
 
100
                                                           write_buff_end,
 
101
                                                           write_buff+sizeof(write_buff)))
 
102
                                break;
 
103
                }
 
104
        }
 
105
}
 
106
 
 
107
void NexStarConnection::sendCommand(NexStarCommand *command)
 
108
{
 
109
        if (command)
 
110
        {
 
111
                #ifdef DEBUG4
 
112
                *log_file << Now() << "NexStarConnection::sendCommand(" << *command
 
113
                          << ")" << endl;
 
114
                #endif
 
115
                        command_list.push_back(command);
 
116
                while (!command_list.front()->hasBeenWrittenToBuffer())
 
117
                {
 
118
                        if (command_list.front()->writeCommandToBuffer(
 
119
                                                          write_buff_end,
 
120
                                                          write_buff+sizeof(write_buff)))
 
121
                        {
 
122
                                //*log_file << Now() << "NexStarConnection::sendCommand: "
 
123
                                //                   << (*command_list.front())
 
124
                                //                   << "::writeCommandToBuffer ok" << endl;
 
125
                                if (command_list.front()->needsNoAnswer())
 
126
                                {
 
127
                                        delete command_list.front();
 
128
                                        command_list.pop_front();
 
129
                                        if (command_list.empty())
 
130
                                                break;
 
131
                                }
 
132
                                else
 
133
                                {
 
134
                                        break;
 
135
                                }
 
136
                        }
 
137
                        else
 
138
                        {
 
139
                                //*log_file << Now() << "NexStarConnection::sendCommand: "
 
140
                                //                   << (*command_list.front())
 
141
                                //                   << "::writeCommandToBuffer failed" << endl;
 
142
                                break;
 
143
                        }
 
144
                }
 
145
                //*log_file << Now() << "NexStarConnection::sendCommand(" << *command << ") end"
 
146
                //                   << endl;
 
147
        }
 
148
}
 
149