File: protocol_message_formatter.h

package info (click to toggle)
classified-ads 0.13-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,772 kB
  • sloc: cpp: 34,291; tcl: 1,175; xml: 64; makefile: 40
file content (375 lines) | stat: -rw-r--r-- 16,939 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*     -*-C++-*- -*-coding: utf-8-unix;-*-
  Classified Ads is Copyright (c) Antti Järvinen 2013-2017.

  This file is part of Classified Ads.

  Classified Ads is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  Classified Ads is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with Classified Ads; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/



#ifndef PROTOCOL_MESSAGE_FORMATTER_H
#define PROTOCOL_MESSAGE_FORMATTER_H
#include "protocol.h"
#include <QByteArray>
#include <QPair>
#include "../datamodel/searchmodel.h"
#include "voicecallengine.h" // for payload type

class Connection ;
class Node ;
class VoiceCall ; // from datamodel 
class CaDbRecord ; 

/**
 * @brief This class produces items sent to peers over network.
 *
 * Class that contains (static) methods for producing
 * byte-streams of various messages.
 */
class ProtocolMessageFormatter {
public:
    /**
     * method for producing node greeting message
     * @param aNode is greeting about the node
     *        concerned, usually "this" node
     * @return bytearray that is ready to be sent
     *         over socket so that other end will
     *         understand.
     */
    static QByteArray nodeGreeting(const Node& aNode) ;
    /**
     * method for producing random number message
     * @return bytearray that is ready to be sent
     *         over socket so that other end will
     *         understand.
     */
    static QByteArray randomNumbers(void) ;

    /**
     * method for producing nodegreeting request message
     * @return bytearray that is ready to be sent
     *         over socket so that other end will
     *         understand.
     */
    static QByteArray requestForNodesAroundHash (const Hash& aHash) ;

    /**
     * method for producing binary blob request message
     * @return bytearray that is ready to be sent
     *         over socket so that other end will
     *         understand.
     */
    static QByteArray requestForBinaryBlob(const Hash& aHash) ;
    /**
     * method for producing operator profile request message
     * @return bytearray that is ready to be sent
     *         over socket so that other end will
     *         understand.
     */
    static QByteArray requestForUserProfile(const Hash& aHash) ;
    /**
     * method for producing message to other node that asks
     * it to send an individual profile comment
     * @return bytearray that is ready to be sent
     *         over socket so that other end will
     *         understand.
     */
    static QByteArray requestForUserProfileComment(const Hash& aIndividualCommentHash) ;
    /**
     * method for producing operator classified ad request message
     * @return bytearray that is ready to be sent
     *         over socket so that other end will
     *         understand.
     */
    static QByteArray requestForClassifiedAd(const Hash& aHash) ;
    /**
     * method for sending request to other nodes regarding private
     * messages having given hash
     * @param aProfileHash hash of message that is to be found
     * @return bytearray that is ready to be sent
     *         over socket so that other end will
     *         understand.
     */
    static QByteArray requestForPrivateMessages(const Hash& aMessageHash) ;
    /**
     * method for sending request to other nodes regarding private
     * messages destined to given profile
     * @param aProfileHash hash of profile whose messages are searched
     * @param aTimeOfOldesMsgToSend since when to send msgs from
     * @return bytearray that is ready to be sent
     *         over socket so that other end will
     *         understand.
     */
    static QByteArray requestForPrivateMessagesForProfile(const Hash& aProfileHash,
            const quint32 aTimeOfOldestMsgToSend) ;
    /**
     * method for sending request to other nodes regarding comments
     * about specified profile. in practice parser (upon receipt) will
     * make calls that check not only for new comments, but also
     * if there has been an update at the profile itself. Call to this
     * method is result of RequestForProfilePoll being put into network
     * request queue.
     *
     * @param aCommentedProfileHash hash of profile whose comments are searched
     * @param aTimeOfOldesMsgToSend since when to send comments from
     * @return bytearray that is ready to be sent
     *         over socket so that other end will
     *         understand.
     */
    static QByteArray requestForProfileComments(const Hash& aCommentedProfileHash,
            const quint32 aTimeOfOldestMsgToSend) ;
    /**
     * Classified ads have classification, the "intent-object-location"
     * triplet shown in UI and it also has a hash. When user chooses
     * this triplet, it might be nice to query surrounding nodes
     * about classified ads regarding that triplet. This method is for
     * sending such a request. Its response will contain hashes of
     * the ads in that classification, possibly followed by the articles
     * too.
     * @param aHashOfClassification hash calculated over
     *                      "intent-object-location" string
     * @param aStartingTimestamp possible starting timestamp for the articles.
     *                           Currently not implemented in protocol, sender
     *                           will limit the number of ads it sends
     * @param aEndingTimestamp possible ending timestamp for the articles
     *                         Currently not implemented in protocol, sender
     *                         will limit the number of ads it sends
     * @return bytearray that is ready to be sent
     *         over socket so that other end will
     *         understand.
     */
    static QByteArray requestForAdsClassified(const Hash& aHashOfClassification,
            const quint32 aStartingTimestamp,
            const quint32 aEndingTimestamp) ;
    /**
     * method for producing byte-array containing (some) published content
     *
     * @param aContentMagicNumber tells what kind of item this is. For
     *        example KProfilePublish (from protocol.h) if we're publishing
     *        a profile here
     * @param aContentHash hash of the content to be published. Reason this
     *                     Hash here is given explicitly is that different
     *                     object types have different hashing methods - for
     *                     binary blob it is the content itself that makes the
     *                     hash but for profile it is the fingerprint of the
     *                     signing key etc.
     * @param aContent actual content
     * @param aSignature signature verifying aContent
     * @param aBangPath list of low-order bits of hosts where this content has
     *                  already been seen, 5 at max
     * @param aSigningKey contains public encryption key used to sign the
     *                    content ; while it over time will be mostly overhead
     *                    it anyway allows reader to reach the poster that
     *                    is great advantage over some overhead..
     * @param aIsContentEncrypted is true of aContent is protected with
     *                            encryption
     * @param aIsContentCompressed is true of aContent is inside qCompress.
     *                             if both compress+encryption, naturally
     *                             the content is first compressed, only
     *                             after that encrypted
     * @param aTimeStamp may be given for content where actual timestamp
     *                   is inside encryption
     *
     * @return bytearray that is ready to be sent
     *         over socket so that other end will
     *         understand.
     */
    static QByteArray contentPublish(const unsigned char aContentMagicNumber,
                                     const Hash& aContentHash,
                                     const QByteArray& aContent,
                                     const QByteArray& aSignature,
                                     const QList<quint32>& aBangPath,
                                     const QByteArray& aSigningKey,
                                     bool aIsContentEncrypted,
                                     bool aIsContentCompressed,
                                     quint32 aTimeStamp ) ;
    /**
     * separate publish-formatter for private messages as they're
     * quite different from rest of the content
     */
    static QByteArray privMsgPublish(const Hash& aContentHash,
                                     const QByteArray& aContent,
                                     const QByteArray& aSignature,
                                     const QList<quint32>& aBangPath,
                                     const Hash& aDestination,
                                     const Hash& aRecipient,
                                     quint32 aTimeStamp ) ;
    /**
     * separate send-formatter for private messages as they're
     * quite different from rest of the content. Similar to publish
     * but no bangpath
     */
    static QByteArray privMsgSend(const Hash& aContentHash,
                                  const QByteArray& aContent,
                                  const QByteArray& aSignature,
                                  const Hash& aDestination,
                                  const Hash& aRecipient,
                                  quint32 aTimeStamp ) ;
    /**
     * separate publish-formatter for profile comments as they're
     * quite different from rest of the content. in case of comments
     * for public profiles they behave a bit like classified ads
     * but comments commenting private profiles need to behave
     * more-or-less like private messages as that's what they are ;
     * private messages with more than 1 recipient .. or it is
     * possible to have private profile with 0 readers and then
     * comment it self. monologue ensues.
     */
    static QByteArray profileCommentPublish(const Hash& aContentHash,
                                            const QByteArray& aContent,
                                            const QByteArray& aSignature,
                                            const QList<quint32>& aBangPath,
                                            const Hash& aProfileCommented,
                                            quint32 aTimeStamp,
                                            quint32 aFlags ) ;

    /**
     * separate send-formatter for profile comment as they're
     * quite different from rest of the content.
     */
    static QByteArray profileCommentSend(const Hash& aContentHash,
                                         const QByteArray& aContent,
                                         const QByteArray& aSignature,
                                         const Hash& aProfileCommented,
                                         quint32 aTimeStamp,
                                         quint32 aFlags ) ;
    /**
     * Method that constructs byte-stream from db record for publish purpose. 
     * @param aRecord is the record 
     * @param aBangPath short list of nodes where the record
     *         came to us, will be included in byte-stream 
     */
    static QByteArray dbRecordPublish(const CaDbRecord& aRecord,
                                      const QList<quint32>& aBangPath) ;
    /**
     * Method that constructs byte-stream from db record for send purpose. 
     * @param aRecord is the record 
     */
    static QByteArray dbRecordSend(const CaDbRecord& aRecord) ;

    /**
     * this is almost same as @ProtocolMessageFormatter.contentPublish
     * but is reply to another node regarding request of content,
     * not users own publishing so this has no bangpath but otherwise
     * this behaves in about same way.
     */
    static QByteArray contentSend(const unsigned char aContentMagicNumber,
                                  const Hash& aContentHash,
                                  const QByteArray& aContent,
                                  const QByteArray& aSignature,
                                  const QByteArray& aSigningKey,
                                  bool aIsContentEncrypted,
                                  bool aIsContentCompressed,
                                  quint32 aTimeStamp ) ;

    static QByteArray replyToAdsClassified(const Hash& aClassificationHash,
                                           const QList<QPair<Hash,quint32> >& aListOfAds	) ;

    /**
     * method for formatting a network search query
     */
    static QByteArray searchSend(const QString& aSearch,
                                 bool aSearchAds,
                                 bool aSearchProfiles,
                                 bool aSearchComments,
                                 const Hash& aSearchIdentifier) ;

    /**
     * method for formatting network search results
     */
    static QByteArray searchResultsSend(const QList<SearchModel::SearchResultItem>& aResults,
                                        quint32 aSearchId) ;

    /**
     * method for serializing a database search so that
     * it can be sent to other nodes
     */
    static QByteArray dbSearchTerms(const CaDbRecord::SearchTerms aSearchTerms) ;

    /**
     * Method for formatting voice call status object 
     * @param aCall is the call status object
     * @param aController is application controller instance
     * @param aSelectedProfile is hash of the profile who will sign
     *        the serialized object
     * @param aDoSign if set to true, normal signature will be added.
     *        If false, zero-len signature will be used. 
     * @return serialized bytes of call status
     */
    static QByteArray voiceCall(const VoiceCall& aCall,
                                MController& aController,
                                const Hash& aSelectedProfile,
                                bool aDoSign = true ) ;
    /**
     * Method for formatting voice call real-time data like audio
     *
     * @param aCallId call (stream) identifier
     * @param aSeqNo sequence number of payload in stream
     * @param aPayloadType type payload
     * @param aPayload actual payload bytes to send, like call audio
     * @return serialized bytes of call status
     */
    static QByteArray voiceCallRtData(quint32 aCallId,
                                      quint32 aSeqNo,
                                      VoiceCallEngine::PayloadType aPayloadType,
                                      const QByteArray& aPayload ) ;

private:
    /**
     * workhorse of @ProtocolMessageFormatter.contentPublish and
     * @ProtocolMessageFormatter.contentSend has parameter
     * telling which action to take
     */
    static QByteArray doContentSendOrPublish(QByteArray& retval,
            const unsigned char aContentMagicNumber,
            const Hash& aContentHash,
            const QByteArray& aContent,
            const QByteArray& aSignature,
            const QList<quint32>& aBangPath,
            const QByteArray& aSigningKey,
            bool aIsContentEncrypted,
            bool aIsContentCompressed,
            quint32 aTimeStamp,
            bool aIsPublish) ;
    /**
     * workhorse of @ProtocolMessageFormatter.profileCommentPublish and
     * @ProtocolMessageFormatter.profileCommentSend has parameter
     * telling which action to take
     */
    static QByteArray doCommentSendOrPublish(const Hash& aContentHash,
            const QByteArray& aContent,
            const QByteArray& aSignature,
            const QList<quint32>& aBangPath,
            const Hash& aProfileCommented,
            quint32 aTimeStamp,
            quint32 aFlags,
            bool aIsPublish) ;

    /**
     * Workhorse-method that constructs byte-stream based on db record
     * publish- or send-operation. 
     * @param aRecord is the record 
     * @param aBangPath short list of nodes where the record
     *         came to us, will be included in byte-stream 
     * @param aIsPublish is set to false if record is merely sent.
     *        If false, aBangPath is ignored and may be empty.
     */
    static QByteArray doDbRecordPublishOrSend(const CaDbRecord& aRecord,
                                              const QList<quint32>& aBangPath,
                                              bool aIsPublish ) ;
} ;

#endif