1 /*
2  * Stellarium Remote Sync plugin
3  * Copyright (C) 2015 Florian Schaukowitsch
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
18  */
19 
20 #include "SyncMessages.hpp"
21 
22 using namespace SyncProtocol;
23 
ErrorMessage()24 ErrorMessage::ErrorMessage()
25 {
26 }
27 
ErrorMessage(const QString & msg)28 ErrorMessage::ErrorMessage(const QString &msg)
29 	: message(msg)
30 {
31 }
32 
serialize(QDataStream & stream) const33 void ErrorMessage::serialize(QDataStream &stream) const
34 {
35 	writeString(stream,message);
36 }
37 
deserialize(QDataStream & stream,tPayloadSize dataSize)38 bool ErrorMessage::deserialize(QDataStream &stream, tPayloadSize dataSize)
39 {
40 	Q_UNUSED(dataSize);
41 	message = readString(stream);
42 	return !stream.status();
43 }
44 
ServerChallenge()45 ServerChallenge::ServerChallenge()
46 	: protocolVersion(SYNC_PROTOCOL_VERSION),
47 	  remoteSyncVersion((REMOTESYNC_MAJOR<<16) | (REMOTESYNC_MINOR<<8) | REMOTESYNC_PATCH),
48 	  stellariumVersion((STELLARIUM_MAJOR<<16) | (STELLARIUM_MINOR<<8) | STELLARIUM_PATCH)
49 {
50 }
51 
serialize(QDataStream & stream) const52 void ServerChallenge::serialize(QDataStream &stream) const
53 {
54 	//write the MAGIC value directly without encoding
55 	stream.writeRawData(SYNC_MAGIC_VALUE.constData(),SYNC_MAGIC_VALUE.size());
56 	//write protocol version, plugin + stellarium version
57 	stream<<protocolVersion;
58 	stream<<remoteSyncVersion;
59 	stream<<stellariumVersion;
60 
61 	//write the client ID
62 	stream<<clientId;
63 }
64 
deserialize(QDataStream & stream,tPayloadSize dataSize)65 bool ServerChallenge::deserialize(QDataStream &stream, tPayloadSize dataSize)
66 {
67 	//check if the size is what we expect
68 	if(dataSize != (SYNC_MAGIC_VALUE.size() + 9 + 16))
69 		return false;
70 
71 	QByteArray data(SYNC_MAGIC_VALUE.size(),'\0');
72 	stream.readRawData(data.data(),data.size());
73 
74 	//check if magic value matches
75 	if(data!=SYNC_MAGIC_VALUE)
76 	{
77 		qWarning()<<"[ServerHello] invalid magic value";
78 		return false;
79 	}
80 
81 	stream>>protocolVersion;
82 	stream>>remoteSyncVersion;
83 	stream>>stellariumVersion;
84 
85 	stream>>clientId;
86 
87 	return !stream.status();
88 }
89 
ClientChallengeResponse()90 ClientChallengeResponse::ClientChallengeResponse()
91 	: remoteSyncVersion((REMOTESYNC_MAJOR<<16) | (REMOTESYNC_MINOR<<8) | REMOTESYNC_PATCH),
92 	  stellariumVersion((STELLARIUM_MAJOR<<16) | (STELLARIUM_MINOR<<8) | STELLARIUM_PATCH)
93 {
94 }
95 
serialize(QDataStream & stream) const96 void ClientChallengeResponse::serialize(QDataStream &stream) const
97 {
98 	stream<<remoteSyncVersion;
99 	stream<<stellariumVersion;
100 	stream<<clientId;
101 }
102 
deserialize(QDataStream & stream,tPayloadSize dataSize)103 bool ClientChallengeResponse::deserialize(QDataStream &stream, tPayloadSize dataSize)
104 {
105 	//check if the size is what we expect
106 	if(dataSize != (8 + 16))
107 		return false;
108 
109 	stream>>remoteSyncVersion;
110 	stream>>stellariumVersion;
111 	stream>>clientId;
112 
113 	return !stream.status();
114 }
115 
serialize(QDataStream & stream) const116 void Time::serialize(QDataStream &stream) const
117 {
118 	stream<<lastTimeSyncTime;
119 	stream<<jDay;
120 	stream<<timeRate;
121 }
122 
deserialize(QDataStream & stream,tPayloadSize dataSize)123 bool Time::deserialize(QDataStream &stream, tPayloadSize dataSize)
124 {
125 	if(dataSize != 24)
126 		return false;
127 
128 	stream>>lastTimeSyncTime;
129 	stream>>jDay;
130 	stream>>timeRate;
131 
132 	return !stream.status();
133 }
134 
Location()135 Location::Location()
136 	: totalDuration(0.0),timeToGo(0.0)
137 {
138 }
139 
serialize(QDataStream & stream) const140 void Location::serialize(QDataStream &stream) const
141 {
142 	//! Serialize StelLocation directly
143 	//! Note: this introduces dependency on Stellarium version!
144 	stream<<stelLocation;
145 	stream<<totalDuration;
146 	stream<<timeToGo;
147 }
148 
deserialize(QDataStream & stream,tPayloadSize dataSize)149 bool Location::deserialize(QDataStream &stream, tPayloadSize dataSize)
150 {
151 	Q_UNUSED(dataSize);
152 	stream>>stelLocation;
153 	stream>>totalDuration;
154 	stream>>timeToGo;
155 	return !stream.status();
156 }
157 
serialize(QDataStream & stream) const158 void Selection::serialize(QDataStream &stream) const
159 {
160 	stream<<selectedObjects;
161 }
162 
deserialize(QDataStream & stream,tPayloadSize dataSize)163 bool Selection::deserialize(QDataStream &stream, tPayloadSize dataSize)
164 {
165 	Q_UNUSED(dataSize);
166 	stream>>selectedObjects;
167 	return !stream.status();
168 }
169 
serialize(QDataStream & stream) const170 void StelPropertyUpdate::serialize(QDataStream &stream) const
171 {
172 	writeString(stream,propId);
173 	stream<<value;
174 }
175 
deserialize(QDataStream & stream,tPayloadSize dataSize)176 bool StelPropertyUpdate::deserialize(QDataStream &stream, tPayloadSize dataSize)
177 {
178 	Q_UNUSED(dataSize);
179 	propId = readString(stream);
180 	stream>>value;
181 	return !stream.status();
182 }
183 
serialize(QDataStream & stream) const184 void View::serialize(QDataStream &stream) const
185 {
186 	stream<<viewAltAz;
187 }
188 
deserialize(QDataStream & stream,tPayloadSize dataSize)189 bool View::deserialize(QDataStream &stream, tPayloadSize dataSize)
190 {
191 	if(dataSize != 3 * sizeof(double))
192 		return false;
193 
194 	stream>>viewAltAz;
195 
196 	return !stream.status();
197 }
198 
serialize(QDataStream & stream) const199 void Fov::serialize(QDataStream &stream) const
200 {
201 	stream<<fov;
202 }
203 
deserialize(QDataStream & stream,tPayloadSize dataSize)204 bool Fov::deserialize(QDataStream &stream, tPayloadSize dataSize)
205 {
206 	if(dataSize != sizeof(double))
207 		return false;
208 
209 	stream>>fov;
210 
211 	return !stream.status();
212 }
213