~ubuntu-branches/ubuntu/saucy/goldencheetah/saucy

« back to all changes in this revision

Viewing changes to src/deprecated/ProtocolHandler.cpp

  • Committer: Package Import Robot
  • Author(s): KURASHIKI Satoru
  • Date: 2013-08-18 07:02:45 UTC
  • mfrom: (4.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130818070245-zgdvb47e1k3mtgil
Tags: 3.0-3
debian/control: remove needless dependency. (Closes: #719571)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2009 Steve Gribble (gribble [at] cs.washington.edu) and
 
3
 *                    Mark Liversedge (liversedge@gmail.com)
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify it
 
6
 * under the terms of the GNU General Public License as published by the Free
 
7
 * Software Foundation; either version 2 of the License, or (at your option)
 
8
 * any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful, but WITHOUT
 
11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 
13
 * more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License along
 
16
 * with this program; if not, write to the Free Software Foundation, Inc., 51
 
17
 * Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
// C, C++ includes
 
21
#include <stdio.h>
 
22
#include <stdexcept>
 
23
 
 
24
// QT includes
 
25
#include <QRegExp>
 
26
 
 
27
// GoldenCheetah includes
 
28
#include "ProtocolHandler.h"
 
29
 
 
30
//////// Main parser; does bulk of parsing in constructor of subclasses.
 
31
QSharedPointer<ProtocolMessage> ProtocolHandler::parseLine(QString line) {
 
32
  try {
 
33
    if (line.startsWith("hello ") == true) {
 
34
      QSharedPointer<ProtocolMessage> hm(new HelloMessage(line));
 
35
      return hm;
 
36
    } else if (line.startsWith("hellofail ") == true) {
 
37
      QSharedPointer<ProtocolMessage> hfm(new HelloFailMessage(line));
 
38
      return hfm;
 
39
    } else if (line.startsWith("hellosucceed ") == true) {
 
40
      QSharedPointer<ProtocolMessage> hsm(new HelloSucceedMessage(line));
 
41
      return hsm;
 
42
    } else if (line.startsWith("clientlist ") == true) {
 
43
      QSharedPointer<ProtocolMessage> clm(new ClientListMessage(line));
 
44
      return clm;
 
45
    } else if (line.startsWith("client ") == true) {
 
46
      QSharedPointer<ProtocolMessage> cm(new ClientMessage(line));
 
47
      return cm;
 
48
    } else if (line.startsWith("telemetry ") == true) {
 
49
      QSharedPointer<ProtocolMessage> tm(new TelemetryMessage(line));
 
50
      return tm;
 
51
    } else if (line.startsWith("standings ") == true) {
 
52
      QSharedPointer<ProtocolMessage> sm(new StandingsMessage(line));
 
53
      return sm;
 
54
    } else if (line.startsWith("racer ") == true) {
 
55
      QSharedPointer<ProtocolMessage> rm(new RacerMessage(line));
 
56
      return rm;
 
57
    } else if (line.startsWith("raceconcluded ") == true) {
 
58
      QSharedPointer<ProtocolMessage> rcm(new RaceConcludedMessage(line));
 
59
      return rcm;
 
60
    } else if (line.startsWith("result ") == true) {
 
61
      QSharedPointer<ProtocolMessage> resm(new ResultMessage(line));
 
62
      return resm;
 
63
    } else if (line.startsWith("goodbye ") == true) {
 
64
      QSharedPointer<ProtocolMessage> gm(new GoodbyeMessage(line));
 
65
      return gm;
 
66
    }
 
67
  } catch (std::invalid_argument& ia) {
 
68
    // couldn't parse, so return an unknownmessage.  pass along
 
69
    // the error message embedded in the exception.
 
70
    QSharedPointer<ProtocolMessage> um(new UnknownMessage(ia.what()));
 
71
    return um;
 
72
  }
 
73
 
 
74
  // couldn't figure out what kind of message this is, so return
 
75
  // an UnknownMessage.
 
76
  QSharedPointer<ProtocolMessage> um(new UnknownMessage("unknown protocol message"));
 
77
  return um;
 
78
}
 
79
 
 
80
 
 
81
/////// HelloMessage methods
 
82
HelloMessage::HelloMessage(QString line) {
 
83
  static QRegExp regexp("hello\\s+(\\d+\\.\\d+)\\s+raceid='([0-9a-fA-F]+)'\\s+ridername='([a-zA-Z0-9 ]+)'\\s+ftp='([0-9]+)'\\s+weight='([0-9.]+)'");
 
84
  bool ok;
 
85
 
 
86
  if (regexp.indexIn(line) != 0) {
 
87
    throw std::invalid_argument("couldn't parse hello message");
 
88
  }
 
89
  this->message_type = ProtocolMessage::HELLO;
 
90
  this->protoversion = regexp.cap(1);
 
91
  this->raceid = regexp.cap(2).toLower();
 
92
  this->ridername = regexp.cap(3);
 
93
  this->ftp_watts = regexp.cap(4).toInt(&ok);
 
94
  if (!ok) {
 
95
    throw std::invalid_argument("couldn't parse hello message -- bogus ftp_watts");
 
96
  }
 
97
  this->weight_kg = regexp.cap(5).toFloat(&ok);
 
98
  if (!ok) {
 
99
    throw std::invalid_argument("couldn't parse hello message -- bogus weight_kg");
 
100
  }
 
101
}
 
102
 
 
103
HelloMessage::HelloMessage(QString protoversion, QString raceid, QString ridername,
 
104
                           int ftp_watts, float weight_kg) {
 
105
  this->message_type = ProtocolMessage::HELLO;
 
106
  this->protoversion = protoversion;
 
107
  this->raceid = raceid;
 
108
  this->ridername = ridername;
 
109
  this->ftp_watts = ftp_watts;
 
110
  this->weight_kg = weight_kg;
 
111
}
 
112
 
 
113
QString HelloMessage::toString() {
 
114
  QString retstr = QString("hello %1 raceid='%2' ridername='%3' ftp='%4' weight='%5'\n")
 
115
    .arg(this->protoversion).arg(this->raceid).arg(this->ridername)
 
116
    .arg(this->ftp_watts).arg(this->weight_kg);
 
117
  return retstr;
 
118
 
 
119
}
 
120
 
 
121
//////// HelloFail methods
 
122
HelloFailMessage::HelloFailMessage(QString line) {
 
123
  static QRegExp regexp("hellofail\\s+(\\d+\\.\\d+)\\s+(\\S+)\\s+raceid='([0-9a-fA-F]+)'");
 
124
 
 
125
  if (regexp.indexIn(line) != 0) {
 
126
    throw std::invalid_argument("couldn't parse hellofail message");
 
127
  }
 
128
  this->message_type = ProtocolMessage::HELLOFAIL;
 
129
  this->protoversion = regexp.cap(1);
 
130
  this->errmessage = regexp.cap(2);
 
131
  this->raceid = regexp.cap(3).toLower();
 
132
}
 
133
 
 
134
HelloFailMessage::HelloFailMessage(QString protoversion, QString errmessage,
 
135
                                   QString raceid) {
 
136
  this->message_type = ProtocolMessage::HELLOFAIL;
 
137
  this->protoversion = protoversion;
 
138
  this->errmessage = errmessage;
 
139
  this->raceid = raceid;
 
140
}
 
141
 
 
142
QString HelloFailMessage::toString() {
 
143
  QString retstr = QString("hellofail %1 %2 raceid='%3'\n")
 
144
    .arg(this->protoversion).arg(this->errmessage).arg(this->raceid);
 
145
  return retstr;
 
146
}
 
147
 
 
148
/////// HelloSucceedMessage methods
 
149
HelloSucceedMessage::HelloSucceedMessage(QString line) {
 
150
  static QRegExp regexp("hellosucceed\\s+(\\d+\\.\\d+)\\s+raceid='([0-9a-fA-F]+)'\\s+riderid='([0-9a-fA-F]+)'\\s+racedistance='([0-9.]+)'");
 
151
  bool ok;
 
152
 
 
153
  if (regexp.indexIn(line) != 0) {
 
154
    throw std::invalid_argument("couldn't parse hellosucceed message");
 
155
  }
 
156
  this->message_type = ProtocolMessage::HELLOSUCCEED;
 
157
  this->protoversion = regexp.cap(1);
 
158
  this->raceid = regexp.cap(2).toLower();
 
159
  this->riderid = regexp.cap(3).toLower();
 
160
  this->racedistance_km = regexp.cap(4).toFloat(&ok);
 
161
  if (!ok) {
 
162
    throw std::invalid_argument("couldn't parse hellosucceed message -- bogus racedistance_km");
 
163
  }
 
164
}
 
165
 
 
166
HelloSucceedMessage::HelloSucceedMessage(QString protoversion, QString raceid,
 
167
                                         QString riderid, float racedistance_km) {
 
168
  this->message_type = ProtocolMessage::HELLOSUCCEED;
 
169
  this->protoversion = protoversion;
 
170
  this->raceid = raceid;
 
171
  this->riderid = riderid;
 
172
  this->racedistance_km = racedistance_km;
 
173
}
 
174
 
 
175
QString HelloSucceedMessage::toString() {
 
176
  QString retstr = QString("hellosucceed %1 raceid='%2' riderid='%3' racedistance='%4'\n")
 
177
    .arg(this->protoversion).arg(this->raceid).arg(this->riderid)
 
178
    .arg(this->racedistance_km);
 
179
  return retstr;
 
180
}
 
181
 
 
182
/////// ClientListMessage methods
 
183
ClientListMessage::ClientListMessage(QString line) {
 
184
  static QRegExp regexp("clientlist\\s+raceid='([0-9a-fA-F]+)'\\s+numclients='([0-9]+)'");
 
185
  bool ok;
 
186
 
 
187
  if (regexp.indexIn(line) != 0) {
 
188
    throw std::invalid_argument("couldn't parse clientlist message");
 
189
  }
 
190
  this->message_type = ProtocolMessage::CLIENTLIST;
 
191
  this->raceid = regexp.cap(1).toLower();
 
192
  this->numclients = regexp.cap(2).toInt(&ok);
 
193
  if (!ok) {
 
194
    throw std::invalid_argument("couldn't parse clientlist message -- bogus numclients");
 
195
  }
 
196
}
 
197
 
 
198
ClientListMessage::ClientListMessage(QString raceid, int numclients) {
 
199
  this->message_type = ProtocolMessage::CLIENTLIST;
 
200
  this->raceid = raceid;
 
201
  this->numclients = numclients;
 
202
}
 
203
 
 
204
QString ClientListMessage::toString() {
 
205
  QString retstr = QString("clientlist raceid='%1' numclients='%2'\n")
 
206
    .arg(this->raceid).arg(this->numclients);
 
207
  return retstr;
 
208
}
 
209
 
 
210
/////// ClientMessage methods
 
211
ClientMessage::ClientMessage(QString line) {
 
212
  static QRegExp regexp("client\\s+ridername='([a-zA-Z0-9 ]+)'\\s+riderid='([0-9a-fA-F]+)'\\s+ftp='([0-9]+)'\\s+weight='([0-9.]+)'");
 
213
  bool ok;
 
214
 
 
215
  if (regexp.indexIn(line) != 0) {
 
216
    throw std::invalid_argument("couldn't parse client message");
 
217
  }
 
218
  this->message_type = ProtocolMessage::CLIENT;
 
219
  this->ridername = regexp.cap(1);
 
220
  this->riderid = regexp.cap(2).toLower();
 
221
  this->ftp_watts = regexp.cap(3).toInt(&ok);
 
222
  if (!ok) {
 
223
    throw std::invalid_argument("couldn't parse client message -- bogus ftp_watts");
 
224
  }
 
225
  this->weight_kg = regexp.cap(4).toFloat(&ok);
 
226
  if (!ok) {
 
227
    throw std::invalid_argument("couldn't parse client message -- bogus weight_kg");
 
228
  }
 
229
}
 
230
 
 
231
ClientMessage::ClientMessage(QString ridername, QString riderid,
 
232
                             int ftp_watts, float weight_kg) {
 
233
  this->message_type = ProtocolMessage::CLIENT;
 
234
  this->ridername = ridername;
 
235
  this->riderid = riderid;
 
236
  this->ftp_watts = ftp_watts;
 
237
  this->weight_kg = weight_kg;
 
238
}
 
239
 
 
240
QString ClientMessage::toString() {
 
241
  QString retstr = QString("client ridername='%1' riderid='%2' ftp='%3' weight='%4'\n")
 
242
    .arg(this->ridername).arg(this->riderid)
 
243
    .arg(this->ftp_watts).arg(this->weight_kg);
 
244
  return retstr;
 
245
}
 
246
 
 
247
/////// TelemetryMessage methods
 
248
TelemetryMessage::TelemetryMessage(QString line) {
 
249
  static QRegExp regexp("telemetry\\s+raceid='([0-9a-fA-F]+)'\\s+riderid='([0-9a-fA-F]+)'\\s+power='([0-9]+)'\\s+cadence='([0-9]+)'\\s+distance='([0-9.]+)'\\s+heartrate='([0-9]+)'\\s+speed='([0-9.]+)'");
 
250
  bool ok;
 
251
 
 
252
  if (regexp.indexIn(line) != 0) {
 
253
    throw std::invalid_argument("couldn't parse telemetry message");
 
254
  }
 
255
  this->message_type = ProtocolMessage::TELEMETRY;
 
256
  this->raceid = regexp.cap(1).toLower();
 
257
  this->riderid = regexp.cap(2).toLower();
 
258
  this->power_watts = regexp.cap(3).toInt(&ok);
 
259
  if (!ok) {
 
260
    throw std::invalid_argument("couldn't parse telemetry message -- bogus power_watts");
 
261
  }
 
262
  this->cadence_rpm = regexp.cap(4).toInt(&ok);
 
263
  if (!ok) {
 
264
    throw std::invalid_argument("couldn't parse telemetry message -- bogus cadence_rpm");
 
265
  }
 
266
  this->distance_km = regexp.cap(5).toFloat(&ok);
 
267
  if (!ok) {
 
268
    throw std::invalid_argument("couldn't parse telemetry message -- bogus distance_km");
 
269
  }
 
270
  this->heartrate_bpm = regexp.cap(6).toInt(&ok);
 
271
  if (!ok) {
 
272
    throw std::invalid_argument("couldn't parse telemetry message -- bogus heartrate_bpm");
 
273
  }
 
274
  this->speed_kph = regexp.cap(7).toFloat(&ok);
 
275
  if (!ok) {
 
276
    throw std::invalid_argument("couldn't parse telemetry message -- bogus speed_kph");
 
277
  }
 
278
}
 
279
 
 
280
TelemetryMessage::TelemetryMessage(QString raceid, QString riderid, int power_watts,
 
281
                                   int cadence_rpm, float distance_km, int heartrate_bpm,
 
282
                                   float speed_kph) {
 
283
  this->message_type = ProtocolMessage::TELEMETRY;
 
284
  this->raceid = raceid;
 
285
  this->riderid = riderid;
 
286
  this->power_watts = power_watts;
 
287
  this->cadence_rpm = cadence_rpm;
 
288
  this->distance_km = distance_km;
 
289
  this->heartrate_bpm = heartrate_bpm;
 
290
  this->speed_kph = speed_kph;
 
291
}
 
292
 
 
293
QString TelemetryMessage::toString() {
 
294
  QString retstr =
 
295
    QString("telemetry raceid='%1' riderid='%2' power='%3' cadence='%4' distance='%5' heartrate='%6' speed='%7'\n")
 
296
    .arg(this->raceid).arg(this->riderid).arg(this->power_watts)
 
297
    .arg(this->cadence_rpm).arg(this->distance_km).arg(this->heartrate_bpm)
 
298
    .arg(this->speed_kph);
 
299
  return retstr;
 
300
}
 
301
 
 
302
/////// StandingsMessage methods
 
303
StandingsMessage::StandingsMessage(QString line) {
 
304
  static QRegExp regexp("standings\\s+raceid='([0-9a-fA-F]+)'\\s+numclients='([0-9]+)'");
 
305
  bool ok;
 
306
 
 
307
  if (regexp.indexIn(line) != 0) {
 
308
    throw std::invalid_argument("couldn't parse standings message");
 
309
  }
 
310
  this->message_type = ProtocolMessage::STANDINGS;
 
311
  this->raceid = regexp.cap(1).toLower();
 
312
  this->numclients = regexp.cap(2).toInt(&ok);
 
313
  if (!ok) {
 
314
    throw std::invalid_argument("couldn't parse standings message -- bogus numclients");
 
315
  }
 
316
}
 
317
 
 
318
StandingsMessage::StandingsMessage(QString raceid, int numclients) {
 
319
  this->message_type = ProtocolMessage::STANDINGS;
 
320
  this->raceid = raceid;
 
321
  this->numclients = numclients;
 
322
}
 
323
 
 
324
QString StandingsMessage::toString() {
 
325
  QString retstr = QString("standings raceid='%1' numclients='%2'\n")
 
326
    .arg(this->raceid).arg(this->numclients);
 
327
  return retstr;
 
328
}
 
329
 
 
330
/////// TelemetryMessage methods
 
331
RacerMessage::RacerMessage(QString line) {
 
332
  static QRegExp regexp("racer\\s+riderid='([0-9a-fA-F]+)'\\s+power='([0-9]+)'\\s+cadence='([0-9]+)'\\s+distance='([0-9.]+)'\\s+heartrate='([0-9]+)'\\s+speed='([0-9.]+)'\\s+place='([0-9]+)'");
 
333
  bool ok;
 
334
 
 
335
  if (regexp.indexIn(line) != 0) {
 
336
    throw std::invalid_argument("couldn't parse racer message");
 
337
  }
 
338
  this->message_type = ProtocolMessage::RACER;
 
339
  this->riderid = regexp.cap(1).toLower();
 
340
  this->power_watts = regexp.cap(2).toInt(&ok);
 
341
  if (!ok) {
 
342
    throw std::invalid_argument("couldn't parse racer message -- bogus power_watts");
 
343
  }
 
344
  this->cadence_rpm = regexp.cap(3).toInt(&ok);
 
345
  if (!ok) {
 
346
    throw std::invalid_argument("couldn't parse racer message -- bogus cadence_rpm");
 
347
  }
 
348
  this->distance_km = regexp.cap(4).toFloat(&ok);
 
349
  if (!ok) {
 
350
    throw std::invalid_argument("couldn't parse racer message -- bogus distance_km");
 
351
  }
 
352
  this->heartrate_bpm = regexp.cap(5).toInt(&ok);
 
353
  if (!ok) {
 
354
    throw std::invalid_argument("couldn't parse racer message -- bogus heartrate_bpm");
 
355
  }
 
356
  this->speed_kph = regexp.cap(6).toFloat(&ok);
 
357
  if (!ok) {
 
358
    throw std::invalid_argument("couldn't parse racer message -- bogus speed_kph");
 
359
  }
 
360
  this->place = regexp.cap(7).toInt(&ok);
 
361
  if (!ok) {
 
362
    throw std::invalid_argument("couldn't parse racer message -- bogus place");
 
363
  }
 
364
}
 
365
 
 
366
RacerMessage::RacerMessage(QString riderid, int power_watts,
 
367
                           int cadence_rpm, float distance_km, int heartrate_bpm,
 
368
                           float speed_kph, int place) {
 
369
  this->message_type = ProtocolMessage::RACER;
 
370
  this->riderid = riderid;
 
371
  this->power_watts = power_watts;
 
372
  this->cadence_rpm = cadence_rpm;
 
373
  this->distance_km = distance_km;
 
374
  this->heartrate_bpm = heartrate_bpm;
 
375
  this->speed_kph = speed_kph;
 
376
  this->place = place;
 
377
}
 
378
 
 
379
QString RacerMessage::toString() {
 
380
  QString retstr =
 
381
    QString("racer riderid='%1' power='%2' cadence='%3' distance='%4' heartrate='%5' speed='%6' place='%7'\n")
 
382
    .arg(this->riderid).arg(this->power_watts)
 
383
    .arg(this->cadence_rpm).arg(this->distance_km).arg(this->heartrate_bpm)
 
384
    .arg(this->speed_kph).arg(this->place);
 
385
  return retstr;
 
386
}
 
387
 
 
388
/////// RaceConcludedMessage methods
 
389
RaceConcludedMessage::RaceConcludedMessage(QString line) {
 
390
  static QRegExp regexp("raceconcluded\\s+raceid='([0-9a-fA-F]+)'\\s+numclients='([0-9]+)'");
 
391
  bool ok;
 
392
 
 
393
  if (regexp.indexIn(line) != 0) {
 
394
    throw std::invalid_argument("couldn't parse raceconcluded message");
 
395
  }
 
396
  this->message_type = ProtocolMessage::RACECONCLUDED;
 
397
  this->raceid = regexp.cap(1).toLower();
 
398
  this->numclients = regexp.cap(2).toInt(&ok);
 
399
  if (!ok) {
 
400
    throw std::invalid_argument("couldn't parse raceconcluded message -- bogus numclients");
 
401
  }
 
402
}
 
403
 
 
404
RaceConcludedMessage::RaceConcludedMessage(QString raceid, int numclients) {
 
405
  this->message_type = ProtocolMessage::RACECONCLUDED;
 
406
  this->raceid = raceid;
 
407
  this->numclients = numclients;
 
408
}
 
409
 
 
410
QString RaceConcludedMessage::toString() {
 
411
  QString retstr = QString("raceconcluded raceid='%1' numclients='%2'\n")
 
412
    .arg(this->raceid).arg(this->numclients);
 
413
  return retstr;
 
414
}
 
415
 
 
416
/////// ResultMessage methods
 
417
ResultMessage::ResultMessage(QString line) {
 
418
  static QRegExp regexp("result\\s+riderid='([0-9a-fA-F]+)'\\s+distance='([0-9.]+)'\\s+place='([0-9]+)'");
 
419
  bool ok;
 
420
 
 
421
  if (regexp.indexIn(line) != 0) {
 
422
    throw std::invalid_argument("couldn't parse result message");
 
423
  }
 
424
  this->message_type = ProtocolMessage::RESULT;
 
425
  this->riderid = regexp.cap(1).toLower();
 
426
  this->distance_km = regexp.cap(2).toFloat(&ok);
 
427
  if (!ok) {
 
428
    throw std::invalid_argument("couldn't parse result message -- bogus distance");
 
429
  }
 
430
  this->place = regexp.cap(3).toInt(&ok);
 
431
  if (!ok) {
 
432
    throw std::invalid_argument("couldn't parse result message -- bogus place");
 
433
  }
 
434
}
 
435
 
 
436
ResultMessage::ResultMessage(QString riderid, float distance_km, int place) {
 
437
  this->message_type = ProtocolMessage::RESULT;
 
438
  this->riderid = riderid;
 
439
  this->distance_km = distance_km;
 
440
  this->place = place;
 
441
}
 
442
 
 
443
QString ResultMessage::toString() {
 
444
  QString retstr = QString("result riderid='%1' distance='%2' place='%3'\n")
 
445
    .arg(this->riderid).arg(this->distance_km).arg(this->place);
 
446
  return retstr;
 
447
}
 
448
 
 
449
//////// Goodbye methods
 
450
GoodbyeMessage::GoodbyeMessage(QString line) {
 
451
  static QRegExp regexp("goodbye\\s+raceid='([0-9a-fA-F]+)'\\s+riderid='([0-9a-fA-F]+)'");
 
452
 
 
453
  if (regexp.indexIn(line) != 0) {
 
454
    throw std::invalid_argument("couldn't parse goodbye message");
 
455
  }
 
456
  this->message_type = ProtocolMessage::GOODBYE;
 
457
  this->raceid = regexp.cap(1).toLower();
 
458
  this->riderid = regexp.cap(2).toLower();
 
459
}
 
460
 
 
461
GoodbyeMessage::GoodbyeMessage(QString raceid, QString riderid) {
 
462
  this->message_type = ProtocolMessage::GOODBYE;
 
463
  this->raceid = raceid;
 
464
  this->riderid = riderid;
 
465
}
 
466
 
 
467
QString GoodbyeMessage::toString() {
 
468
  QString retstr = QString("goodbye raceid='%1' riderid='%2'\n")
 
469
    .arg(this->raceid).arg(this->riderid);
 
470
  return retstr;
 
471
}
 
472
 
 
473
 
 
474
////////////// Test code; assumes you've compiled so stdout appears at a terminal.
 
475
void ProtocolHandler::test() {
 
476
  // Test "Unknown" message type
 
477
  QSharedPointer<ProtocolMessage> msg = ProtocolHandler::parseLine("blah");
 
478
  if (msg->message_type == ProtocolMessage::UNKNOWN) {
 
479
    QSharedPointer<UnknownMessage> um =
 
480
      qSharedPointerDynamicCast<UnknownMessage>(msg);
 
481
    printf("UNKNOWN -- %s\n", um->toString().toAscii().constData());
 
482
  } else {
 
483
    printf("!! expected UNKNOWN, but didn't get it...\n");
 
484
  }
 
485
 
 
486
  // Test broken "Hello" message
 
487
  QSharedPointer<ProtocolMessage> msg2 = ProtocolHandler::parseLine("hello there\n");
 
488
  if (msg2->message_type == ProtocolMessage::UNKNOWN) {
 
489
    QSharedPointer<UnknownMessage> um =
 
490
      qSharedPointerDynamicCast<UnknownMessage>(msg2);
 
491
    printf("broken hello --> UNKNOWN -- %s", um->toString().toAscii().constData());
 
492
  } else {
 
493
    printf("!! expected broken hello --> UNKNOWN, but didn't get it...\n");
 
494
  }
 
495
 
 
496
  // Test valid "Hello" message
 
497
  QSharedPointer<ProtocolMessage> msg3 = ProtocolHandler::parseLine(
 
498
     "hello 0.1 raceid='18d1a1bcd104ee116a772310bbc61211' ridername='Steve G' ftp='213' weight='74.8'\n"
 
499
                                                                       );
 
500
  if (msg3->message_type == ProtocolMessage::HELLO) {
 
501
    QSharedPointer<HelloMessage> hm =
 
502
      qSharedPointerDynamicCast<HelloMessage>(msg3);
 
503
    printf("HELLO:  %s", hm->toString().toAscii().constData());
 
504
  } else {
 
505
    printf("!! expected hello, but didn't get it...\n");
 
506
  }
 
507
  HelloMessage hm2("0.1", "deadbeef01234567cafebabe76543210", "Steve G",
 
508
                   213, 74.8);
 
509
  printf("cons'ed up hellomessage: %s", hm2.toString().toAscii().constData());
 
510
 
 
511
  // Test valid HelloFail message
 
512
  QSharedPointer<ProtocolMessage> msg4 = ProtocolHandler::parseLine(
 
513
     "hellofail 0.1 nosuchrace raceid='18d1a1bcd104ee116a772310bbc61211'\n"
 
514
                                                                       );
 
515
  if (msg4->message_type == ProtocolMessage::HELLOFAIL) {
 
516
    QSharedPointer<HelloFailMessage> hfm =
 
517
      qSharedPointerDynamicCast<HelloFailMessage>(msg4);
 
518
    printf("HELLOFAIL:  %s", hfm->toString().toAscii().constData());
 
519
  } else {
 
520
    printf("!! expected hellofail, but didn't get it...\n");
 
521
  }
 
522
 
 
523
  // Test valid HelloSucceed message
 
524
  QSharedPointer<ProtocolMessage> msg5 = ProtocolHandler::parseLine(
 
525
     "hellosucceed 0.1 raceid='18d1a1bcd104ee116a772310bbc61211' riderid='123212321232123a' racedistance='180.0'\n"
 
526
                                                                       );
 
527
  if (msg5->message_type == ProtocolMessage::HELLOSUCCEED) {
 
528
    QSharedPointer<HelloSucceedMessage> hsm =
 
529
      qSharedPointerDynamicCast<HelloSucceedMessage>(msg5);
 
530
    printf("HELLOSUCCEED:  %s", hsm->toString().toAscii().constData());
 
531
  } else {
 
532
    printf("!! expected hellosucceed, but didn't get it...\n");
 
533
  }
 
534
 
 
535
  // Test valid ClientList message
 
536
  QSharedPointer<ProtocolMessage> msg6 = ProtocolHandler::parseLine(
 
537
     "clientlist raceid='18d1a1bcd104ee116a772310bbc61211' numclients='5'\n"
 
538
                                                                       );
 
539
  if (msg6->message_type == ProtocolMessage::CLIENTLIST) {
 
540
    QSharedPointer<ClientListMessage> clm =
 
541
      qSharedPointerDynamicCast<ClientListMessage>(msg6);
 
542
    printf("CLIENTLIST:  %s", clm->toString().toAscii().constData());
 
543
  } else {
 
544
    printf("!! expected clientlist, but didn't get it...\n");
 
545
  }
 
546
 
 
547
  // Test valid Client message
 
548
  QSharedPointer<ProtocolMessage> msg7 = ProtocolHandler::parseLine(
 
549
     "client ridername='Steve G' riderid='123212321232123a' ftp='213' weight='75.8'"
 
550
                                                                       );
 
551
  if (msg7->message_type == ProtocolMessage::CLIENT) {
 
552
    QSharedPointer<ClientMessage> cm =
 
553
      qSharedPointerDynamicCast<ClientMessage>(msg7);
 
554
    printf("CLIENT:  %s", cm->toString().toAscii().constData());
 
555
  } else {
 
556
    printf("!! expected client, but didn't get it...\n");
 
557
  }
 
558
 
 
559
  // Test valid Telemetry message
 
560
  QSharedPointer<ProtocolMessage> msg8 = ProtocolHandler::parseLine(
 
561
     "telemetry raceid='18d1a1bcd104ee116a772310bbc61211' riderid='123212321232123a' power='250' cadence='85' distance='5.41' heartrate='155' speed='31.5'\n"
 
562
                                                                       );
 
563
  if (msg8->message_type == ProtocolMessage::TELEMETRY) {
 
564
    QSharedPointer<TelemetryMessage> tm =
 
565
      qSharedPointerDynamicCast<TelemetryMessage>(msg8);
 
566
    printf("TELEMETRY:  %s", tm->toString().toAscii().constData());
 
567
  } else {
 
568
    printf("!! expected telemetry, but didn't get it...\n");
 
569
  }
 
570
 
 
571
  // Test valid Standings message
 
572
  QSharedPointer<ProtocolMessage> msg9 = ProtocolHandler::parseLine(
 
573
     "standings raceid='18d1a1bcd104ee116a772310bbc61211' numclients='5'\n"
 
574
                                                                       );
 
575
  if (msg9->message_type == ProtocolMessage::STANDINGS) {
 
576
    QSharedPointer<StandingsMessage> sm =
 
577
      qSharedPointerDynamicCast<StandingsMessage>(msg9);
 
578
    printf("STANDINGS:  %s", sm->toString().toAscii().constData());
 
579
  } else {
 
580
    printf("!! expected standings, but didn't get it...\n");
 
581
  }
 
582
 
 
583
  // Test valid Racer message
 
584
  QSharedPointer<ProtocolMessage> msg10 = ProtocolHandler::parseLine(
 
585
     "racer riderid='123212321232123a' power='250' cadence='85' distance='5.41' heartrate='155' speed='31.5' place='1'\n"
 
586
                                                                       );
 
587
  if (msg10->message_type == ProtocolMessage::RACER) {
 
588
    QSharedPointer<RacerMessage> rm =
 
589
      qSharedPointerDynamicCast<RacerMessage>(msg10);
 
590
    printf("RACER:  %s", rm->toString().toAscii().constData());
 
591
  } else {
 
592
    printf("!! expected racer, but didn't get it...\n");
 
593
  }
 
594
 
 
595
  // Test valid RaceConcluded message
 
596
  QSharedPointer<ProtocolMessage> msg11 = ProtocolHandler::parseLine(
 
597
     "raceconcluded raceid='18d1a1bcd104ee116a772310bbc61211' numclients='5'\n"
 
598
                                                                       );
 
599
  if (msg11->message_type == ProtocolMessage::RACECONCLUDED) {
 
600
    QSharedPointer<RaceConcludedMessage> rcm =
 
601
      qSharedPointerDynamicCast<RaceConcludedMessage>(msg11);
 
602
    printf("RACECONCLUDED:  %s", rcm->toString().toAscii().constData());
 
603
  } else {
 
604
    printf("!! expected raceconclded, but didn't get it...\n");
 
605
  }
 
606
 
 
607
  // Test valid Result message
 
608
  QSharedPointer<ProtocolMessage> msg12 = ProtocolHandler::parseLine(
 
609
     "result riderid='123212321232123a' distance='5.41' place='1'\n"
 
610
                                                                       );
 
611
  if (msg12->message_type == ProtocolMessage::RESULT) {
 
612
    QSharedPointer<ResultMessage> resm =
 
613
      qSharedPointerDynamicCast<ResultMessage>(msg12);
 
614
    printf("RESULT:  %s", resm->toString().toAscii().constData());
 
615
  } else {
 
616
    printf("!! expected result, but didn't get it...\n");
 
617
  }
 
618
 
 
619
  // Test valid Goodbye message
 
620
  QSharedPointer<ProtocolMessage> msg13 = ProtocolHandler::parseLine(
 
621
     "goodbye raceid='18d1a1bcd104ee116a772310bbc61211' riderid='123212321232123a'\n"
 
622
                                                                       );
 
623
  if (msg13->message_type == ProtocolMessage::GOODBYE) {
 
624
    QSharedPointer<GoodbyeMessage> gbm =
 
625
      qSharedPointerDynamicCast<GoodbyeMessage>(msg13);
 
626
    printf("GOODBYE:  %s", gbm->toString().toAscii().constData());
 
627
  } else {
 
628
    printf("!! expected goodbye, but didn't get it...\n");
 
629
  }
 
630
 
 
631
}