Debian Codegraph @

protolib/examples/ting.cpp

raw
1
2 #include <protoApp.h>
3 #include <protoSocket.h>
4 #include <protoPipe.h>
5 #include <protoPkt.h>
6 #include <protoQueue.h>
7 #include <protoNet.h>
8
9 #include <stdio.h> // for stdout/stderr printouts
10 #include <string.h>
11 #include <stdlib.h> // for rand(), etc
12
13
14 // IMPORTANT NOTE: Although this works, it is still a work in progress. It currently does
15 // a basic ping/time check (ting) over a UDP sockett to a multicast and/or unicast destinations.
16 // The results it reports are currently simple. One measurement is based on a handshake and
17 // observation of RTT (similar to NTP) while for multicast it also can use a form of
18 // Reference Broadcast Synchronization (RBS) through shared observations of other hosts'
19 // broadcasts. RTT is also opportunistically measure during the multicast rounds.
20 // There is more work to do here including:
21 //
22 // 1) Optional use of PCAP socket (via ProtoCap class) to access the more accurate timestamp
23 // for received packets. This helps RBS and the RTT measurement by removing uncertainty
24 // due to the application space UDP socket getting serviced. RBS benefits more here since the
25 // UDP tx socket queuing/sending doesn't matter for it.
26 //
27 // 2) Allowing for multiple rounds in multicast sessions, or even ongoing signaling with different
28 // hosts initiating the "ting" chatter" This would also mean maintaining some observation state
29 // from one round to the next, pruning old observations, etc. Also need to do more sophisticated
30 // processing of multicast feedback to compute offsets for hosts that share common observations/
31 // with _other_ receivers but not ourself, but where a "chain" of observations is available to
32 // deduce the offsets to those hosts. This can be done iteratively
33 //
34 // 3) Observe outbound PCAP timestamps and send "corrections" to sent_time for previous feedback
35 // reports. This would tighten round trip measurements a little more.
36 //
37 // 4) More sophisticated filtering of observations. For example, lower RTT is a good indicator of
38 // a better measurement.
39 //
40 // 5) Related to #4, account for clock skew among hosts
41 //
42 // 6) Add option to actually perform iterative time synchronization, making this into something
43 // more useful than a time check.
44 //
45 // 7) Add sequence number to packets to measure packet loss.
46 //
47 // 8) Use ProtoPipe coordination from new ting initiator(s) to already running ting instance
48 // instead of the "reuse port" hack that we now use. Any initiated commands would be
49 // relayed to running instance and relevant feedback would be collected before initiator exit
50 //
51 // 9) Add (optional?) timestamp to log output lines so we can track time offset overs time
52 //
53
54
55
56 // ting ("time ping") is an application that can either idle, listening for ting requests
57 // or send ting requests to initiate a time check to another ting listener
58 //
59 // 0 1 2 3
60 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
61 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62 // |version| type | flags | backoff | round_id |
63 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 // | sent_time_sec |
65 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66 // | sent_time_usec |
67 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 // | |
69 // + +
70 // | Feedback Reports |
71 // + +
72 // | ... |
73
74
75 // Feedback report format
76 // 0 1 2 3
77 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
78 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
79 // | length | round_id | src_port |
80 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
81 // | |
82 // + src_addr +
83 // | ... |
84 //
85 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86 // | reference_time_sec |
87 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
88 // | reference_time_usec |
89 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 // | observe_time_sec |
91 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92 // | observe_time_usec |
93 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 // | rtt_usec |
95 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96
97
98 // Pick a random number from 0..max
99 inline double UniformRand(double max)
100 {return (max * ((double)rand() / (double)RAND_MAX));}
101
102 // Pick a random number from 0..max
103 // (truncated exponential dist. lambda = log(groupSize) + 1)
104 inline double ExponentialRand(double max, double groupSize)
105 {
106 double lambda = log(groupSize) + 1;
107 double x = UniformRand(lambda/max)+lambda/(max*(exp(lambda)-1));
108 return ((max/lambda)*log(x*(exp(lambda)-1)*(max/lambda)));
109 }
110
111 enum {TING_MSG_MAX = 1400};
112
113 class TingMessage : public ProtoPkt
114 {
115 public:
116 enum {VERSION = 1};
117
118 TingMessage(UINT32* bufferPtr = 0,
119 unsigned int numBytes = 0,
120 bool initFromBuffer = false,
121 bool freeOnDestruct = false);
122 ~TingMessage();
123
124 // TBD - should we define different formats for each message type?
125 enum Type
126 {
127 INVALID = 0,
128 HAIL = 1, // a "hail" is sent first to make sure ARP, etc is all good
129 REQUEST = 2, // requests have valid timestamps
130 REPLY = 3
131 };
132
133 enum Flag
134 {
135 UNICAST = 0x01
136 };
137
138 class Report;
139
140 bool IsValid() const
141 {
142 return ((buffer_bytes >= GetMinimumLength()) &&
143 (VERSION == GetVersion()) &&
144 (INVALID != GetType()));
145 }
146
147 // Message parsing methods
148 UINT8 GetVersion() const
149 {return (GetUINT8(OFFSET_VERSION) >> 4);}
150 Type GetType() const
151 {return (Type)(GetUINT8(OFFSET_TYPE) & 0x0f);}
152 bool FlagIsSet(Flag flag) const
153 {return (0 != (flag & GetUINT8(OFFSET_FLAGS)));}
154 bool IsUnicast() const
155 {return FlagIsSet(UNICAST);}
156 UINT8 GetBackoffQuantized() const
157 {return GetUINT8(OFFSET_BACKOFF);}
158 double GetBackoff() const
159 {return UnquantizeBackoff(GetBackoffQuantized());}
160 UINT8 GetRoundId() const
161 {return GetUINT8(OFFSET_ROUND_ID);}
162 void GetSentTime(struct timeval& theTime) const
163 {
164 theTime.tv_sec = GetUINT32(buffer_ptr+OFFSET_SENT_SEC);
165 theTime.tv_usec = GetUINT32(buffer_ptr+OFFSET_SENT_USEC);
166 }
167
168 // Start with a null buffer report to reset iteration
169 bool GetNextReport(Report& report) const
170 {
171 UINT32* nextBuffer = (UINT32*)report.AccessBuffer();
172 nextBuffer = (NULL != nextBuffer) ? (nextBuffer + report.GetReportWords()) :
173 (buffer_ptr + OFFSET_REPORT);
174 unsigned int nextOffset = (nextBuffer - buffer_ptr) << 2;
175 unsigned int space = (nextOffset < GetLength()) ? (GetLength() - nextOffset) : 0;
176 report.AttachBuffer(nextBuffer, space);
177 return report.InitFromBuffer();
178 }
179
180 // Message creation methods
181 // (These assume the buffer size is sufficient!)
182 void Init()
183 {
184 memset(buffer_ptr, 0, GetMinimumLength());
185 SetVersion(VERSION);
186 }
187 void SetVersion(UINT8 version)
188 {
189 SetUINT8(OFFSET_VERSION, (GetUINT8(OFFSET_VERSION) & 0x0f) | (version << 4));
190 SetLength(GetMinimumLength());
191 }
192 void SetType(Type type)
193 {SetUINT8(OFFSET_TYPE, (GetUINT8(OFFSET_TYPE) & 0xf0) | type);}
194 void SetFlag(Flag flag)
195 {SetUINT8(OFFSET_FLAGS, flag | GetUINT8(OFFSET_FLAGS));}
196 void ClearFlag(Flag flag)
197 {SetUINT8(OFFSET_FLAGS, ~flag & GetUINT8(OFFSET_FLAGS));}
198 void SetBackoff(double seconds)
199 {SetBackoffQuantized(QuantizeBackoff(seconds));}
200 void SetBackoffQuantized(UINT8 quant)
201 {SetUINT8(OFFSET_BACKOFF, quant);}
202 void SetRoundId(UINT8 roundId)
203 {SetUINT8(OFFSET_ROUND_ID, roundId);}
204 void SetSentTime(struct timeval theTime)
205 {
206 SetUINT32(buffer_ptr+OFFSET_SENT_SEC, theTime.tv_sec);
207 SetUINT32(buffer_ptr+OFFSET_SENT_USEC, theTime.tv_usec);
208 }
209 bool AppendReport(const ProtoAddress& srcAddr,
210 UINT8 roundId,
211 const struct timeval& refTime,
212 const struct timeval& obsTime,
213 double rtt);
214
215
216 class Report : public ProtoPkt
217 {
218 public:
219 Report(UINT32* bufferPtr = NULL,
220 unsigned int numBytes = 0,
221 bool initFromBuffer = false,
222 bool freeOnDestruct = false);
223 ~Report();
224
225
226 // Report parsing
227 bool InitFromBuffer(UINT32* bufferPtr = NULL, unsigned int numBytes = 0, bool freeOnDestruct = false);
228 UINT8 GetReportWords() const
229 {return GetUINT8(OFFSET_LENGTH);}
230 unsigned int GetReportLength() const
231 {return ((unsigned int)GetReportWords() << 2);}
232 UINT8 GetRoundId() const
233 {return GetUINT8(OFFSET_ROUND_ID);}
234 bool GetSrcAddress(ProtoAddress& srcAddr) const;
235
236 void GetReferenceTime(struct timeval& theTime) const
237 {
238 theTime.tv_sec = GetUINT32(buffer_ptr + OffsetReferenceSec());
239 theTime.tv_usec = GetUINT32(buffer_ptr + OffsetReferenceUsec());
240 }
241 void GetObservedTime(struct timeval& theTime) const
242 {
243 theTime.tv_sec = GetUINT32(buffer_ptr + OffsetObservedSec());
244 theTime.tv_usec = GetUINT32(buffer_ptr + OffsetObservedUsec());
245 }
246 double GetRtt() const
247 {
248 UINT32 value = GetUINT32(buffer_ptr + OffsetRttUsec());
249 return ((0xffffffff != value) ? (double)value : -1.0);
250 }
251
252 // Report building (call in order, not valid until address is set)
253 void InitIntoBuffer(UINT32* bufferPtr = NULL, unsigned int numBytes = 0, bool freeOnDestruct = false);
254 void SetRoundId(UINT8 roundId)
255 {SetUINT8(OFFSET_ROUND_ID, roundId);}
256 bool SetSrcAddress(const ProtoAddress& srcAddr);
257 void SetReferenceTime(const struct timeval& theTime)
258 {
259 SetUINT32(buffer_ptr + OffsetReferenceSec(), theTime.tv_sec);
260 SetUINT32(buffer_ptr + OffsetReferenceUsec(), theTime.tv_usec);
261 }
262 void SetObservedTime(const struct timeval& theTime)
263 {
264 SetUINT32(buffer_ptr + OffsetObservedSec(), theTime.tv_sec);
265 SetUINT32(buffer_ptr + OffsetObservedUsec(), theTime.tv_usec);
266 }
267 void SetRtt(double rtt)
268 {
269 UINT32 value = (rtt >= 0.0) ? (UINT32)(1.0e+06*rtt + 0.5) : 0xffffffff;
270 SetUINT32(buffer_ptr + OffsetRttUsec(), value);
271 }
272
273
274 private:
275 enum
276 {
277 OFFSET_LENGTH = 0, // UINT8 offset (1 byte)
278 OFFSET_ROUND_ID = OFFSET_LENGTH+1, // UINT8 offset (1 byte)
279 OFFSET_SRC_PORT = (OFFSET_ROUND_ID+1)/2, // UINT16 offset 2 bytes
280 OFFSET_SRC_ADDR = (2*(OFFSET_SRC_PORT+1))/4 // UINT32 offset
281 };
282
283 void SetReportWords(UINT8 words)
284 {SetUINT8(OFFSET_LENGTH, words);}
285
286 unsigned int GetAddressWords() const
287 {
288 unsigned int reportWords = GetUINT8(OFFSET_LENGTH);
289 return ((reportWords > 6) ? (reportWords - 6) : 0);
290 }
291
292 unsigned int GetAddressLength() const
293 {return (GetAddressWords() << 2);}
294
295 unsigned int OffsetReferenceSec() const // UINT32 offset
296 {return (OFFSET_SRC_ADDR + GetAddressWords());}
297 unsigned int OffsetReferenceUsec() const // UINT32 offset
298 {return (OffsetReferenceSec() + 1);}
299 unsigned int OffsetObservedSec() const // UINT32 offset
300 {return (OffsetReferenceUsec() + 1);}
301 unsigned int OffsetObservedUsec() const // UINT32 offset
302 {return (OffsetObservedSec() + 1);}
303 unsigned int OffsetRttUsec() const // UINT32 offset
304 {return (OffsetObservedUsec() + 1);}
305
306 }; // end class TingMessage::Report
307
308
309 static const double BACKOFF_MIN;
310 static const double BACKOFF_MAX;
311 static UINT8 QuantizeBackoff(double backoff);
312 static double UnquantizeBackoff(UINT8 quant)
313 {return BACKOFF[quant];}
314
315 private:
316 static const double BACKOFF[];
317
318 enum
319 {
320 OFFSET_VERSION = 0, // UINT8 offset (upper 4 bits)
321 OFFSET_TYPE = OFFSET_VERSION, // UINT8 offset (lower 4 bits)
322 OFFSET_FLAGS = OFFSET_TYPE + 1, // UINT8 offset (1 byte)
323 OFFSET_BACKOFF = OFFSET_FLAGS + 1, // UINT8 offset (1 byte)
324 OFFSET_ROUND_ID = OFFSET_BACKOFF + 1, // UINT8 offset (1 byte)
325 OFFSET_SENT_SEC = (OFFSET_ROUND_ID+1)/4, // UINT32 offset (4 bytes)
326 OFFSET_SENT_USEC = OFFSET_SENT_SEC + 1, // UINT32 offset (4 bytes)
327 OFFSET_REPORT = OFFSET_SENT_USEC + 1 // UINT32 offset (4 bytes)
328 };
329
330 static unsigned int GetMinimumLength()
331 {return (OFFSET_REPORT << 2);}
332
333 }; // end class TingMessage
334
335 // An "AddressableItem" is indexed by is address and port number
336
337 class AddressableItem : public ProtoQueue::Item
338 {
339 public:
340 AddressableItem(const ProtoAddress& theAddr);
341 virtual ~AddressableItem();
342
343 const ProtoAddress& GetAddress() const
344 {return item_addr;}
345 const char* GetAddressKey() const
346 {return item_key;}
347 unsigned GetAddressKeysize() const
348 {return ((item_addr.GetLength()+2) << 3);}
349
350 private:
351 ProtoAddress item_addr;
352 char item_key[16+2]; // room for IPv6 addr and port number
353
354 }; // end class AddressableItem
355
356 // This is currently implemented as a ProtoSortedQueue but it may be
357 // useful to allow for ProtoIndexedQueue support or even ProtoTree
358 class AddressableList : public ProtoSortedQueueTemplate<AddressableItem>
359 {
360 public:
361
362 AddressableItem* Find(const ProtoAddress& addr) const;
363 class Iterator : public ProtoSortedQueueTemplate<AddressableItem>::Iterator
364 {
365 public:
366 Iterator(AddressableList& theList, const ProtoAddress* matchAddr = NULL);
367 void Reset(const ProtoAddress* matchAddr = NULL);
368 AddressableItem* GetNextItem();
369
370 private:
371 ProtoAddress match_addr;
372
373 }; // end class AddressableList::Iterator
374
375 private:
376 const char* GetKey(const Item& item) const
377 {return static_cast<const AddressableItem&>(item).GetAddressKey();}
378 unsigned int GetKeysize(const Item& item) const
379 {return static_cast<const AddressableItem&>(item).GetAddressKeysize();}
380
381 }; // end class AddressableList
382
383 template<class ITEM_TYPE>
384 class AddressableListTemplate : public AddressableList
385 {
386 public:
387 // Insert the "item" into the tree
388 bool Insert(ITEM_TYPE& item)
389 {return AddressableList::Insert(item);}
390
391 // Remove the "item" from the tree
392 void Remove(ITEM_TYPE& item)
393 {AddressableList::Remove(item);}
394
395 // Find item with exact match to "key" and "keysize" (keysize is in bits)
396 ITEM_TYPE* Find(const ProtoAddress& theAddr) const
397 {return static_cast<ITEM_TYPE*>(AddressableList::Find(theAddr));}
398
399 class Iterator : public AddressableList::Iterator
400 {
401 public:
402 Iterator(AddressableListTemplate& theList, const ProtoAddress* matchAddr = NULL)
403 : AddressableList::Iterator(theList, matchAddr) {}
404 ~Iterator() {}
405
406 void Reset(const ProtoAddress* matchAddr = NULL)
407 {AddressableList::Iterator::Reset(matchAddr);}
408
409 ITEM_TYPE* GetNextItem()
410 {return static_cast<ITEM_TYPE*>(AddressableList::Iterator::GetNextItem());}
411 }; // end class AddressableListTemplate::Iterator
412 }; // end class AddressableListTemplate
413
414
415 class TingObservation : public AddressableItem
416 {
417 public:
418 TingObservation(const ProtoAddress& srcAddr);
419 ~TingObservation();
420
421 const ProtoAddress& GetAddress() const
422 {return AddressableItem::GetAddress();}
423 UINT8 GetRoundId() const
424 {return round_id;}
425 const struct timeval& GetReferenceTime() const
426 {return ref_time;}
427 const struct timeval& GetObservedTime() const
428 {return obs_time;}
429 double GetRtt() const
430 {return rtt_measured;}
431
432 void SetRoundId(UINT8 roundId)
433 {round_id = roundId;}
434 void SetReferenceTime(const struct timeval refTime)
435 {ref_time = refTime;}
436 void SetObservedTime(const struct timeval obsTime)
437 {obs_time = obsTime;}
438 void SetRtt(double rtt)
439 {rtt_measured = rtt;}
440
441 private:
442 UINT8 round_id;
443 struct timeval ref_time;
444 struct timeval obs_time;
445 double rtt_measured;
446 }; // end class TingObservation
447
448 TingObservation::TingObservation(const ProtoAddress& srcAddr)
449 : AddressableItem(srcAddr), rtt_measured(-1.0)
450 {
451 ref_time.tv_sec = ref_time.tv_usec = 0;
452 obs_time.tv_sec = obs_time.tv_usec = 0;
453 }
454
455 TingObservation::~TingObservation()
456 {
457 }
458
459 // We organize observations in two ways. The first way is indexed by the source
460 // address/port of the observed message. The second way is a simple linked list
461 // where the first item in list is the first observation.
462 class TingObservationTree : public AddressableListTemplate<TingObservation> {};
463 class TingObservationList : public ProtoSimpleQueueTemplate<TingObservation> {};
464
465 class TingObserver : public AddressableItem
466 {
467 public:
468 TingObserver(const ProtoAddress& addr);
469 ~TingObserver();
470
471 bool Insert(const ProtoAddress& reportAddr,
472 UINT8 roundId,
473 const struct timeval refTime,
474 const struct timeval obsTime,
475 double rtt);
476
477 class Iterator : public TingObservationTree::Iterator
478 {
479 public:
480 Iterator(TingObserver& observer, const ProtoAddress* matchAddr = NULL)
481 : TingObservationTree::Iterator(observer.observation_tree, matchAddr) {}
482 TingObservation* GetNextObservation()
483 {return TingObservationTree::Iterator::GetNextItem();}
484 }; // end class TingObserver::Iterator
485
486 private:
487 TingObservationTree observation_tree;
488 TingObservationList observation_list;
489 }; // end class TingObserver
490
491 class TingObserverList : public AddressableListTemplate<TingObserver> {};
492
493 typedef void (TingCallback)(const void* userData);
494
495 class TingSession : public AddressableItem
496 {
497 public:
498
499 enum State
500 {
501 IDLE = 0, // doing nothing, waiting for request
502 HAILING = 1, // requestor, hail initiated with initial request transmission
503 CONNECTED = 2 // reply to hail received (or request received)
504 };
505
506 TingSession(const ProtoAddress& addr,
507 UINT8 roundId,
508 ProtoSocket& tingSocket,
509 ProtoTimerMgr& timerMgr,
510 const ProtoAddressList& localAddrList);
511 ~TingSession();
512
513 void SetLogFile(FILE* logFile)
514 {log_file = logFile;}
515
516 void SetTerminateCallback(TingCallback* funcPtr, const void* userData);
517
518 const ProtoAddress& GetSessionAddress() const
519 {return GetAddress();}
520
521 UINT8 GetRoundId() const
522 {return round_id;}
523
524 bool IsUnicast() const
525 {return !GetSessionAddress().IsMulticast();}
526 bool IsMulticast() const
527 {return GetSessionAddress().IsMulticast();}
528
529 bool IsActive()
530 {return (IDLE != session_state);}
531
532 bool Initiate(TingCallback* termCallback, const void* termData);
533 void Terminate()
534 {
535 if (session_timer.IsActive()) session_timer.Deactivate();
536 Reset(round_id);
537 if (NULL != term_callback) term_callback(term_data);
538 }
539
540 void HandleMessage(const TingMessage& msg, const ProtoAddress& srcAddr, const struct timeval& recvTime);
541 void HandleHail(const TingMessage& msg, const ProtoAddress& srcAddr, const struct timeval& recvTime);
542 void HandleReply(const TingMessage& msg, const ProtoAddress& srcAddr, const struct timeval& recvTime);
543 void HandleUnicastRequest(const TingMessage& msg, const ProtoAddress& srcAddr, const struct timeval& recvTime);
544 void HandleMulticastRequest(const TingMessage& msg, const ProtoAddress& srcAddr, const struct timeval& recvTime);
545 bool RecordObservation(const TingMessage& msg, const ProtoAddress& srcAddr, const struct timeval& recvTime);
546
547
548 bool SendMessage(TingMessage::Type msgType, double backoff);
549 bool OnSessionTimeout(ProtoTimer& theTimer);
550 void Summarize();
551 void Reset(UINT8 roundId);
552
553 static const double REPLY_TIME_MAX;
554 enum {REQUEST_COUNT_MAX = 5};
555 enum {REQUEST_ROUND_MAX = 3};
556 enum {REPORT_COUNT_MAX = 10};
557
558 private:
559 ProtoSocket& ting_socket;
560 ProtoTimerMgr& timer_mgr;
561 const ProtoAddressList& local_address_list;
562 ProtoTimer session_timer;
563 State session_state;
564 UINT8 round_id;
565 UINT8 sent_round_id;
566 struct timeval sent_msg_time;
567 unsigned int sent_request_count; // send attempts with reply
568 unsigned int request_round_count; // number of request rounds for unicast handshake
569 TingObservationTree observation_tree; // indexed by observation srcAddr
570 TingObservationList observation_list; // most recent observations at tail
571 TingObserverList observer_list; // indexed by observer srcAddr
572 FILE* log_file;
573 TingCallback* term_callback;
574 const void* term_data;
575 }; // end class TingSession
576
577 class TingSessionList : public AddressableListTemplate<TingSession> {};
578
579 class TingApp : public ProtoApp
580 {
581 public:
582 TingApp();
583 ~TingApp();
584
585 enum {MSG_LENGTH_MAX = 1400};
586
587 static const char* DEFAULT_GROUP;
588 static const UINT16 DEFAULT_PORT;
589
590 enum {REQ_COUNT = 1}; // number of handshakes to perform
591
592 bool OnStartup(int argc, const char*const* argv);
593
594 bool ProcessCommands(int argc, const char*const* argv)
595 {return true;}
596
597 void OnShutdown();
598
599
600 private:
601
602 void Usage();
603
604 void HandleMessage(const TingMessage& msg, const ProtoAddress& srcAddr, const struct timeval& currentTime);
605
606 void OnTingSocketEvent(ProtoSocket& theSocket,
607 ProtoSocket::Event theEvent);
608
609 static void DoTerminate(const void* termData);
610
611 ProtoSocket ting_socket;
612 UINT16 ting_port;
613 ProtoAddress group_address;
614 TingSession* group_session; // our group session
615 TingSessionList session_list; // list of unicast sessions, initiated or received
616 ProtoAddressList local_address_list;
617 UINT8 next_round_id;
618 FILE* log_file;
619 char mcast_iface[256];
620
621
622 }; // end class TingApp
623
624 ////// IMPLEMENTATION /////////////////////
625
626 // Our application instance
627 PROTO_INSTANTIATE_APP(TingApp)
628
629
630 AddressableItem::AddressableItem(const ProtoAddress& theAddr)
631 : item_addr(theAddr)
632 {
633 unsigned int addrLen = theAddr.GetLength();
634 memcpy(item_key, theAddr.GetRawHostAddress(), addrLen);
635 UINT16 port = htons(theAddr.GetPort());
636 memcpy(item_key + addrLen, &port, 2);
637 }
638
639 AddressableItem::~AddressableItem()
640 {
641 }
642
643 AddressableItem* AddressableList::Find(const ProtoAddress& addr) const
644 {
645 char key[16+2];
646 unsigned int addrLen = addr.GetLength();
647 memcpy(key, addr.GetRawHostAddress(), addrLen);
648 UINT16 port = htons(addr.GetPort());
649 memcpy(key+addrLen, &port, 2);
650 return static_cast<AddressableItem*>(ProtoSortedQueue::Find(key, (addrLen+2) << 3));
651 } // end AddressableList::Find()
652
653 AddressableList::Iterator::Iterator(AddressableList& theList, const ProtoAddress* matchAddr)
654 : ProtoSortedQueueTemplate<AddressableItem>::Iterator(theList)
655 {
656 Reset(matchAddr);
657 }
658
659 void AddressableList::Iterator::Reset(const ProtoAddress* matchAddr)
660 {
661 if (NULL != matchAddr)
662 {
663 match_addr = *matchAddr;
664 char matchKey[16+2];
665 unsigned int addrLen = matchAddr->GetLength();
666 memcpy(matchKey, matchAddr->GetRawHostAddress(), addrLen);
667 UINT16 port = htons(matchAddr->GetPort());
668 memcpy(matchKey + addrLen, &port, 2);
669 ProtoSortedQueue::Iterator::Reset(false, matchKey, (addrLen+2)<<3);
670 }
671 else
672 {
673 match_addr.Invalidate();
674 }
675 } // end AddressableList::Iterator::Reset()
676
677 AddressableItem* AddressableList::Iterator::GetNextItem()
678 {
679 AddressableItem* item = ProtoSortedQueueTemplate<AddressableItem>::Iterator::GetNextItem();
680 if (NULL == item) return NULL;
681 if (match_addr.IsValid())
682 if (item->GetAddress().IsEqual(match_addr))
683 return item;
684 else
685 return NULL; // doesn't match
686 else
687 return item;
688 } // end AddressableList::Iterator::GetNextItem()
689
690
691 TingObserver::TingObserver(const ProtoAddress& addr)
692 : AddressableItem(addr)
693 {
694 }
695
696 TingObserver::~TingObserver()
697 {
698 observation_tree.Empty();
699 observation_list.Destroy();
700 }
701
702 bool TingObserver::Insert(const ProtoAddress& reportAddr,
703 UINT8 roundId,
704 const struct timeval refTime,
705 const struct timeval obsTime,
706 double rtt)
707 {
708 // Is this a duplicative or adjusted former observation
709 TingObservationTree::Iterator iterator(observation_tree, &reportAddr);
710 TingObservation* obs;
711 while (NULL != (obs = iterator.GetNextItem()))
712 {
713 const struct timeval& t = obs->GetReferenceTime();
714 if ((t.tv_sec == refTime.tv_sec) && (t.tv_usec == refTime.tv_usec))
715 {
716 char observerName[256];
717 observerName[255] = '\0';
718 GetAddress().GetHostString(observerName, 255);
719 PLOG(PL_WARN, "TingObserver::Insert() warning: duplicate observation of %s/%hu from %s/%hu\n",
720 reportAddr.GetHostString(), reportAddr.GetPort(), observerName, GetAddress().GetPort());
721 return true;
722 }
723 }
724 if (NULL == (obs = new TingObservation(reportAddr)))
725 {
726 PLOG(PL_ERROR, "TingObserver::Insert() new TingObservation error: %s\n", GetErrorString());
727 return false;
728 }
729 obs->SetRoundId(roundId);
730 obs->SetReferenceTime(refTime);
731 obs->SetObservedTime(obsTime);
732 obs->SetRtt(rtt);
733 if (!observation_tree.Insert(*obs))
734 {
735 PLOG(PL_ERROR, "TingObserver::Insert() error: observation_tree insertion failed!\n");
736 delete obs;
737 return false;
738 }
739 if (!observation_list.Append(*obs))
740 {
741 PLOG(PL_ERROR, "TingObserver::Insert() error: observation_tree insertion failed!\n");
742 observation_tree.Remove(*obs);
743 delete obs;
744 return false;
745 }
746 return true;
747 } // end TingObserver::Insert()
748
749 const double TingMessage::BACKOFF_MIN = 1.0e-06;
750 const double TingMessage::BACKOFF_MAX = 1000.0;
751
752 TingMessage::TingMessage(UINT32* bufferPtr,
753 unsigned int numBytes,
754 bool initFromBuffer,
755 bool freeOnDestruct)
756 : ProtoPkt(bufferPtr, numBytes, freeOnDestruct)
757 {
758 if (initFromBuffer)
759 {
760 ProtoPkt::InitFromBuffer(numBytes);
761 }
762 else if (NULL != bufferPtr)
763 {
764 if (numBytes >= GetMinimumLength())
765 Init();
766 else
767 memset(buffer_ptr, 0, numBytes); // invalid version zero
768 }
769 }
770
771 TingMessage::~TingMessage()
772 {
773 }
774
775 bool TingMessage::AppendReport(const ProtoAddress& srcAddr,
776 UINT8 roundId,
777 const struct timeval& refTime,
778 const struct timeval& obsTime,
779 double rtt)
780 {
781 // Is there room for this added report
782 unsigned int reportLength = srcAddr.GetLength() + 6*4;
783 unsigned int bufferSpace = buffer_bytes - GetLength();
784 if (reportLength > bufferSpace) return false;
785 Report report(buffer_ptr + (GetLength()/4), bufferSpace);
786 report.SetRoundId(roundId);
787 report.SetSrcAddress(srcAddr);
788 report.SetReferenceTime(refTime);
789 report.SetObservedTime(obsTime);
790 report.SetRtt(rtt);
791 SetLength(GetLength() + reportLength);
792 return true;
793
794 } // end TingMessage::AppendReport()
795
796 TingMessage::Report::Report(UINT32* bufferPtr,
797 unsigned int numBytes,
798 bool initFromBuffer,
799 bool freeOnDestruct)
800 : ProtoPkt(bufferPtr, numBytes, freeOnDestruct)
801 {
802 if (initFromBuffer)
803 InitFromBuffer();
804 else
805 InitIntoBuffer();
806 }
807
808 TingMessage::Report::~Report()
809 {
810 }
811
812 void TingMessage::Report::InitIntoBuffer(UINT32* bufferPtr,
813 unsigned int numBytes,
814 bool freeOnDestruct)
815 {
816 if (NULL != bufferPtr)
817 AttachBuffer(bufferPtr, numBytes, freeOnDestruct);
818 if (0 != buffer_bytes) SetReportWords(0);
819 } // end TingMessage::Report::InitIntoBuffer()
820
821 bool TingMessage::Report::InitFromBuffer(UINT32* bufferPtr,
822 unsigned int numBytes,
823 bool freeOnDestruct)
824 {
825 if (NULL != bufferPtr)
826 AttachBuffer(bufferPtr, numBytes, freeOnDestruct);
827 if (0 != buffer_bytes)
828 {
829 unsigned int reportLength = GetReportLength();
830 if (reportLength <= buffer_bytes)
831 {
832 SetLength(reportLength);
833 return true;
834 }
835 }
836 SetLength(0);
837 return false;
838 } // end TingMessage::Report::InitFromBuffer()
839
840 bool TingMessage::Report::SetSrcAddress(const ProtoAddress& srcAddr)
841 {
842 SetUINT16(((UINT16*)buffer_ptr)+OFFSET_SRC_PORT, srcAddr.GetPort());
843 switch (srcAddr.GetType())
844 {
845 case ProtoAddress::IPv4:
846 SetReportWords(1 + 6);
847 break;
848 case ProtoAddress::IPv6:
849 SetReportWords(4 + 6);
850 break;
851 default:
852 PLOG(PL_ERROR, "TingMessage::Report::SetSrcAddress() error: unsupported address type\n");
853 return false;
854 }
855 unsigned int addrLen = srcAddr.GetLength();
856 unsigned int addrSpace = buffer_bytes - 6*4;
857 if (addrLen <= addrSpace)
858 {
859 memcpy((char*)(buffer_ptr+OFFSET_SRC_ADDR), srcAddr.GetRawHostAddress(), addrLen);
860 return true;
861 }
862 else
863 {
864 PLOG(PL_ERROR, "TingMessage::Report::SetSrcAddress() error: insufficient buffer space\n");
865 SetReportWords(0);
866 return false;
867 }
868 } // end TingMessage::Report::SetSrcAddress()
869
870 bool TingMessage::Report::GetSrcAddress(ProtoAddress& srcAddr) const
871 {
872 // infer address type (IPv4 or IPv6) from address length
873 switch (GetAddressLength())
874 {
875 case 4:
876 srcAddr.SetRawHostAddress(ProtoAddress::IPv4, (char*)(buffer_ptr + OFFSET_SRC_ADDR), 4);
877 break;
878 case 16:
879 srcAddr.SetRawHostAddress(ProtoAddress::IPv6, (char*)(buffer_ptr + OFFSET_SRC_ADDR), 16);
880 default:
881 PLOG(PL_ERROR, "TingMessage::Report::GetSrcAddress() error: invalid address length: %d\n", GetAddressLength());
882 return false;
883 }
884 srcAddr.SetPort(GetUINT16(((UINT16*)buffer_ptr) + OFFSET_SRC_PORT));
885 return true;
886 } // end TingMessage::Report::GetSrcAddress()
887
888
889 const double TingSession::REPLY_TIME_MAX = 2.0;
890
891 TingSession::TingSession(const ProtoAddress& addr,
892 UINT8 roundId,
893 ProtoSocket& tingSocket,
894 ProtoTimerMgr& timerMgr,
895 const ProtoAddressList& localAddrList)
896 : AddressableItem(addr), ting_socket(tingSocket), timer_mgr(timerMgr), local_address_list(localAddrList),
897 session_state(IDLE), round_id(roundId), sent_round_id(0), sent_request_count(0), request_round_count(0),
898 log_file(stdout), term_callback(NULL), term_data(NULL)
899 {
900 sent_msg_time.tv_sec = sent_msg_time.tv_usec= 0;
901 session_timer.SetListener(this, &TingSession::OnSessionTimeout);
902 }
903
904 TingSession::~TingSession()
905 {
906 if (session_timer.IsActive()) session_timer.Deactivate();
907 }
908
909
910 void TingSession::HandleHail(const TingMessage& msg, const ProtoAddress& srcAddr, const struct timeval& recvTime)
911 {
912 // It's a unicast HAIL message, so send a REPLY
913 ASSERT(IsUnicast() && msg.IsUnicast());
914 if (IDLE == session_state)
915 {
916 // Transition to CONNECTED state and activate session_timer
917 Reset(round_id);
918 session_state = CONNECTED;
919 session_timer.SetInterval(REPLY_TIME_MAX);
920 timer_mgr.ActivateTimer(session_timer);
921 sent_request_count = REQUEST_COUNT_MAX;
922 }
923 else
924 {
925 ASSERT(session_timer.IsActive());
926 session_timer.Reset();
927 }
928 // Note that "backoff" here doesn't really matter
929 if (!SendMessage(TingMessage::REPLY, REPLY_TIME_MAX))
930 {
931 PLOG(PL_ERROR, "TingSession::HandleHail() error: unable to send REPLY message to host %s\n", srcAddr.GetHostString());
932 session_timer.Deactivate();
933 Reset(round_id);
934 }
935 } // end TingSession::HandleHail()
936
937 void TingSession::HandleReply(const TingMessage& msg, const ProtoAddress& srcAddr, const struct timeval& recvTime)
938 {
939 ASSERT(IsUnicast() && msg.IsUnicast());
940 if (IDLE == session_state)
941 {
942 PLOG(PL_WARN, "TingSession::HandleReply() warning: session received unexpected REPLY from host %s/%hu\n",
943 srcAddr.GetHostString(), srcAddr.GetPort());
944 return;
945 }
946 if (msg.GetRoundId() != round_id)
947 {
948 PLOG(PL_WARN, "TingSession::HandleReply() warning: session received REPLY with non matching round_id from host %s/%hu\n",
949 srcAddr.GetHostString(), srcAddr.GetPort());
950 return;
951 }
952
953 RecordObservation(msg, srcAddr, recvTime);
954
955 // Send followup REQUEST if rounds remain
956 if (++request_round_count <= REQUEST_ROUND_MAX)
957 {
958 sent_request_count = 0;
959 session_timer.Reset();
960 if (!SendMessage(TingMessage::REQUEST, REPLY_TIME_MAX))
961 {
962 PLOG(PL_ERROR, "TingSession::HandleReply() warning: unable to send REPLY to host %s/%hu\n",
963 srcAddr.GetHostString(), srcAddr.GetPort());
964 // we let the session retry / timeout on its own
965 }
966 }
967 else
968 {
969 // TBD - We received the final reply, so we could finish up here instead of waiting for timeout
970 sent_request_count = REQUEST_COUNT_MAX;
971
972 }
973 } // end TingSession::HandleReply()
974
975 void TingSession::HandleUnicastRequest(const TingMessage& msg, const ProtoAddress& srcAddr, const struct timeval& recvTime)
976 {
977 ASSERT(IsUnicast() && msg.IsUnicast());
978 if (CONNECTED != session_state)
979 {
980 PLOG(PL_WARN, "TingSession::HandleUnicastRequest() warning: session received unexpected REQUEST from host %s/%hu\n",
981 srcAddr.GetHostString(), srcAddr.GetPort());
982 return;
983 }
984 if (msg.GetRoundId() != round_id)
985 {
986 PLOG(PL_WARN, "TingSession::HandleUnicastRequest() warning: session received REQUEST with non matching round_id from host %s/%hu\n",
987 srcAddr.GetHostString(), srcAddr.GetPort());
988 return;
989 }
990
991 bool validFeedback = RecordObservation(msg, srcAddr, recvTime);
992
993 // Respond accordingly (note REQUEST should always have "valid feedback")
994 if (validFeedback)
995 {
996 if (SendMessage(TingMessage::REPLY, REPLY_TIME_MAX))
997 session_timer.Reset();
998 else
999 PLOG(PL_WARN, "TingSession::HandleUnicastRequest() warning: unable to send REPLY to host %s/%hu\n",
1000 srcAddr.GetHostString(), srcAddr.GetPort());
1001 }
1002 else
1003 {
1004 PLOG(PL_WARN, "TingSession::HandleUnicastRequest() warning: received REQUEST with no valid feedback from host %s/%hu\n",
1005 srcAddr.GetHostString(), srcAddr.GetPort());
1006 }
1007 } // end TingSession::HandleUnicastRequest()
1008
1009 void TingSession::HandleMulticastRequest(const TingMessage& msg, const ProtoAddress& srcAddr, const struct timeval& recvTime)
1010 {
1011 if (IDLE == session_state)
1012 {
1013 Reset(msg.GetRoundId());
1014 }
1015 else if (msg.GetRoundId() != round_id)
1016 {
1017 // Warn / ignore REQUEST with non-matching round id
1018 PLOG(PL_WARN, "TingSession::HandleMulticastRequest() warning: received REQUEST with non matching round_id from host %s/%hu\n",
1019 srcAddr.GetHostString(), srcAddr.GetPort());
1020 return;
1021 }
1022
1023 RecordObservation(msg, srcAddr, recvTime);
1024
1025 if (0 == sent_round_id)
1026 {
1027 // We haven't transmitted yet, so do a random backoff
1028 double backoff = ExponentialRand(REPLY_TIME_MAX, 10);
1029 session_timer.SetInterval(backoff);
1030 if (IDLE == session_state)
1031 {
1032 timer_mgr.ActivateTimer(session_timer);
1033 session_state = CONNECTED;
1034 }
1035 else
1036 {
1037 session_timer.Reset();
1038 }
1039 }
1040 else
1041 {
1042 session_timer.Reset();
1043 }
1044 } // end TingSession::HandleMulticastRequest()
1045
1046 bool TingSession::RecordObservation(const TingMessage& msg, const ProtoAddress& srcAddr, const struct timeval& recvTime)
1047 {
1048 TingMessage::Type msgType = msg.GetType();
1049 // Any (or none) feedback is valid for multicast signaling.
1050 // For unicast, feedback MUST reflect prior REQUEST / REPLY round_id and sent_time
1051 bool validFeedback = IsMulticast();
1052 // At this point, the message is valid for our given session state, etc so we can record an observation
1053 // (Note we validate the feedback reports that our for ourself)
1054 // Record an observation for incoming message
1055 TingObservation* myObservation = new TingObservation(srcAddr);
1056 if (NULL == myObservation)
1057 {
1058 PLOG(PL_ERROR, "TingSession::RecordObservation() new TingObservation error: %s\n", GetErrorString());
1059 return validFeedback;
1060 }
1061 myObservation->SetRoundId(msg.GetRoundId());
1062 struct timeval sentTime;
1063 msg.GetSentTime(sentTime);
1064 myObservation->SetReferenceTime(sentTime); // use message sent time as reference time
1065 myObservation->SetObservedTime(recvTime);
1066 // rtt will be set below if this message has an observation of a message I previously sent.
1067 if (!observation_list.Append(*myObservation))
1068 {
1069 PLOG(PL_ERROR, "TingSession::RecordObservation() error: observation_list insertion failure!\n");
1070 delete myObservation;
1071 return validFeedback;
1072 }
1073 if (!observation_tree.Insert(*myObservation))
1074 {
1075 PLOG(PL_ERROR, "TingSession::RecordObservation() error: observation_tree insertion failure!\n");
1076 observation_list.Remove(*myObservation);
1077 delete myObservation;
1078 return validFeedback;
1079 }
1080 // If the REQUEST or REPLY contains any reports, record them
1081 TingObserver* observer = observer_list.Find(srcAddr);
1082 TingMessage::Report report;
1083 while (msg.GetNextReport(report))
1084 {
1085 ProtoAddress reportAddr;
1086 report.GetSrcAddress(reportAddr);
1087 if (!reportAddr.IsValid())
1088 {
1089 PLOG(PL_ERROR, "TingSession::RecordObservation() error: received message with invalid report address!\n");
1090 // TBD - remove this observation?
1091 return validFeedback;
1092 }
1093 struct timeval refTime;
1094 report.GetReferenceTime(refTime);
1095 struct timeval obsTime;
1096 report.GetObservedTime(obsTime);
1097 double reportRtt = report.GetRtt();
1098 // Is the report about me?
1099 // If so, compute RTT for ourselves and to report
1100 if (local_address_list.Contains(reportAddr))
1101 {
1102 // Calculate RTT and update "myObservation"
1103 // rtt = recvTime - report.GetReferenceTime() - (msg.GetSentTime() - report.GetObservedTime())
1104 double rtt = ProtoTime(recvTime) - ProtoTime(refTime);
1105 double holdTime = ProtoTime(sentTime) - ProtoTime(obsTime);
1106 rtt -= holdTime;
1107 if (IsUnicast())
1108 { if (report.GetRoundId() != round_id)
1109 {
1110 PLOG(PL_WARN, "TingSession::RecordObservation() warning: received unicast %s message from %s with invalid round id: %d\n",
1111 (TingMessage::REQUEST == msgType) ? "REQUEST" : "REPLY", srcAddr.GetHostString(), report.GetRoundId());
1112 continue;
1113 }
1114 if ((refTime.tv_sec != sent_msg_time.tv_sec) || (refTime.tv_usec != sent_msg_time.tv_usec))
1115 {
1116 PLOG(PL_DEBUG, "TingSession::RecordObservation() warning: received unicast %s message from %s with invalid reference time\n",
1117 (TingMessage::REQUEST == msgType) ? "REQUEST" : "REPLY", srcAddr.GetHostString());
1118 continue; // in case, for some reason there is a valid feedback report included, too
1119 }
1120 }
1121 // TBD - if reportRtt < 0.0, should we use this as the "reportRtt", too???
1122 myObservation->SetRtt(rtt);
1123 validFeedback = true;
1124 }
1125 else
1126 {
1127 if (IsUnicast())
1128 {
1129 PLOG(PL_WARN, "TingSession::RecordObservation() warning: received unicast report with non-matching report address\n");
1130 continue;
1131 }
1132 }
1133 if (report.GetRoundId() != round_id)
1134 PLOG(PL_WARN, "TingSession::RecordObservation() warning: received report with non-matching round_id\n");
1135
1136 if (NULL == observer)
1137 {
1138 if (NULL == (observer = new TingObserver(srcAddr)))
1139 {
1140 PLOG(PL_ERROR, "TingSession::RecordObservation() new TingObserver error: %s\n", GetErrorString());
1141 break;
1142 }
1143 if (!observer_list.Insert(*observer))
1144 {
1145 PLOG(PL_ERROR, "TingSession::RecordObservation() error: observer_list insertion error!\n");
1146 delete observer;
1147 break;
1148 }
1149 }
1150 // TBD - make sure observer handles duplicate observations OK?
1151 if (!observer->Insert(reportAddr, report.GetRoundId(), refTime, obsTime, reportRtt))
1152 {
1153 PLOG(PL_ERROR, "TingSession::RecordObservation() error:observer observation insertion failure!\n");
1154 break;
1155 }
1156 }
1157 return validFeedback;
1158
1159 } // end TingSession::RecordObservation()
1160
1161
1162 bool TingSession::SendMessage(TingMessage::Type msgType, double backoff)
1163 {
1164 const ProtoAddress& destAddr = GetSessionAddress();
1165 UINT32 buffer[TING_MSG_MAX/4 + 1];
1166 TingMessage msg(buffer, TING_MSG_MAX);
1167 msg.SetType(msgType);
1168 if (IsUnicast()) msg.SetFlag(TingMessage::UNICAST);
1169 msg.SetBackoff(backoff);
1170 msg.SetRoundId(GetRoundId());
1171 if (TingMessage::HAIL != msgType)
1172 {
1173 // Append a finite number of our observations as reports
1174 TingObservationList::Iterator observerator(observation_list);
1175 TingObservation* obs;
1176 unsigned int reportCount = 0;
1177 while (NULL != (obs = observerator.GetNextItem()))
1178 {
1179 // For unicast, we only send observations relevant to that destAddr
1180 // TBD - should we send only our most recent observation? Or most recent first?
1181 if (IsUnicast() && !obs->GetAddress().IsEqual(destAddr)) continue;
1182 if (!msg.AppendReport(obs->GetAddress(), obs->GetRoundId(), obs->GetReferenceTime(), obs->GetObservedTime(), obs->GetRtt()))
1183 {
1184 PLOG(PL_WARN, "TingApp::SendMessage() warning: full message!\n");
1185 break;
1186 }
1187 if (++reportCount >= REPORT_COUNT_MAX)
1188 break;
1189 }
1190 }
1191
1192 // Last, but not least, get the current time, timestamp the message and send it
1193 struct timeval currentTime;
1194 ProtoSystemTime(currentTime);
1195 msg.SetSentTime(currentTime);
1196 unsigned int numBytes = msg.GetLength();
1197 if (!ting_socket.SendTo((const char*)msg.GetBuffer(), numBytes, destAddr))
1198 {
1199 PLOG(PL_ERROR, "TingApp::SendMessage() error: unable to send message!\n");
1200 return false;
1201 }
1202 sent_msg_time = currentTime;
1203 sent_round_id = msg.GetRoundId();
1204 return true;
1205 } // end TingSession::SendMessage()
1206
1207
1208 bool TingSession::OnSessionTimeout(ProtoTimer& theTimer)
1209 {
1210 // Send a ting message depending on session type and state
1211 ASSERT(IDLE != session_state);
1212
1213 TingMessage::Type msgType = TingMessage::INVALID;
1214 if (sent_request_count < REQUEST_COUNT_MAX)
1215 {
1216 if (IsUnicast())
1217 msgType = (HAILING == session_state) ? TingMessage::HAIL : TingMessage::REQUEST;
1218 else if (0 == sent_round_id)
1219 msgType = TingMessage::REQUEST;
1220 }
1221
1222 if (TingMessage::INVALID != msgType)
1223 {
1224 if (SendMessage(msgType, REPLY_TIME_MAX))
1225 sent_round_id = round_id;
1226 else
1227 PLOG(PL_ERROR, "TingSession::OnSessionTimeout() error: unable to send unicast message to host %s\n", GetSessionAddress().GetHostString());
1228 sent_request_count++;
1229 theTimer.SetInterval(REPLY_TIME_MAX);
1230 theTimer.Reset();
1231 }
1232 else
1233 {
1234 Summarize();
1235 theTimer.Deactivate();
1236 Reset(round_id);
1237 }
1238
1239 if (NULL != term_callback)
1240 {
1241 // We're an "initiator" so shutdown after summarization
1242 term_callback(term_data);
1243 }
1244
1245
1246 return false; // we return false because the timer is either reset or deactivated above
1247 } // end TingSession::OnSessionTimeout()
1248
1249
1250 void TingSession::Summarize()
1251 {
1252 // Summarize my observations
1253 TingObservationTree::Iterator iterator(observation_tree);
1254 TingObservation* obs;
1255
1256 ProtoAddress currentAddr;
1257 double offsetMin = 0.0;
1258 double offsetMax = 0.0;
1259 double offsetSum = 0.0;
1260 unsigned int offsetCount = 0;
1261 double rttMin = 0.0;
1262 double rttMax = 0.0;
1263 double rttSum = 0.0;
1264 unsigned int rttCount = 0;
1265 while (NULL != (obs = iterator.GetNextItem()))
1266 {
1267 // Skip local addresses
1268 if (local_address_list.Contains(obs->GetAddress())) continue;
1269 if (!currentAddr.IsValid()) currentAddr = obs->GetAddress();
1270 if (!currentAddr.IsEqual(obs->GetAddress()))
1271 {
1272 fprintf(log_file, "host %s/%hu ", currentAddr.GetHostString(), currentAddr.GetPort());
1273 if (offsetCount > 0)
1274 {
1275 double offsetAve = offsetSum / (double)offsetCount;
1276 fprintf(log_file, "offset: min>%f ave>%f max>%f ", offsetMin, offsetAve, offsetMax);
1277 }
1278 else
1279 {
1280 fprintf(log_file, "offset: undetermined ");
1281 }
1282 if (rttCount > 0)
1283 {
1284 double rttAve = rttSum / (double)rttCount;
1285 fprintf(log_file, "rtt: min>%f ave>%f max>%f\n", rttMin, rttAve, rttMax);
1286 }
1287 else
1288 {
1289 fprintf(log_file, "rtt: undetermined\n");
1290 }
1291 rttCount = offsetCount = 0;
1292 currentAddr = obs->GetAddress();
1293 }
1294
1295 if (currentAddr.IsEqual(obs->GetAddress()))
1296 {
1297 double rtt = obs->GetRtt();
1298 if (rtt >= 0.0)
1299 {
1300 // update rtt tracking
1301 if (rttCount > 0)
1302 {
1303 if (rtt < rttMin)
1304 rttMin = rtt;
1305 else if (rtt > rttMax)
1306 rttMax = rtt;
1307 rttSum += rtt;
1308 }
1309 else
1310 {
1311 rttMin = rttMax = rttSum = obs->GetRtt();
1312 }
1313 rttCount++;
1314 // update offset tracking
1315 double offset = ProtoTime(obs->GetObservedTime()) - ProtoTime(obs->GetReferenceTime()) - (rtt / 2.0);
1316 if (offsetCount > 0)
1317 {
1318 if (offset < offsetMin)
1319 offsetMin = offset;
1320 else if (offset > offsetMax)
1321 offsetMax = offset;
1322 offsetSum += offset;
1323 }
1324 else
1325 {
1326 offsetMin = offsetMax = offsetSum = offset;
1327 }
1328 offsetCount++;
1329 }
1330 }
1331 }
1332 if (currentAddr.IsValid())
1333 {
1334 fprintf(log_file, "host %s/%hu ", currentAddr.GetHostString(), currentAddr.GetPort());
1335 if (offsetCount > 0)
1336 {
1337 double offsetAve = offsetSum / (double)offsetCount;
1338 fprintf(log_file, "offset: min>%f ave>%f max>%f ", offsetMin, offsetAve, offsetMax);
1339 }
1340 else
1341 {
1342 fprintf(log_file, "offset: undetermined ");
1343 }
1344 if (rttCount > 0)
1345 {
1346 double rttAve = rttSum / (double)rttCount;
1347 fprintf(log_file, "rtt: min>%f ave>%f max>%f\n", rttMin, rttAve, rttMax);
1348 }
1349 else
1350 {
1351 fprintf(log_file, "rtt: undetermined\n");
1352 }
1353 }
1354
1355 // Summarize based on mutual observations (i.e. multicast ting)
1356 TingObserverList::Iterator observerator(observer_list);
1357 TingObserver* observer;
1358 while (NULL != (observer = observerator.GetNextItem()))
1359 {
1360 const ProtoAddress& observerAddr = observer->GetAddress();
1361 // Skip local addresses
1362 if (local_address_list.Contains(observerAddr)) continue;
1363
1364 //fprintf(log_file, "Summarizing observer %s/%hu ...\n", observerAddr.GetHostString(), observerAddr.GetPort());
1365 offsetCount = 0;
1366 // Find observations of common reference(s) and compute offset
1367 TingObserver::Iterator it1(*observer);
1368 TingObservation* obs1;
1369 while (NULL != (obs1 = it1.GetNextObservation()))
1370 {
1371 // Do we have a common observation? (by addr and refTime)
1372 const struct timeval& refTime1 = obs1->GetReferenceTime();
1373 TingObservationTree::Iterator it2(observation_tree, &obs1->GetAddress());
1374 TingObservation* obs2;
1375 while (NULL != (obs2 = it2.GetNextItem()))
1376 {
1377 ASSERT(obs2->GetAddress().IsEqual(obs1->GetAddress()));
1378 const struct timeval& refTime2 = obs2->GetReferenceTime();
1379 if ((refTime1.tv_sec != refTime2.tv_sec) || (refTime1.tv_usec != refTime2.tv_usec))
1380 continue;
1381 // Common observation, so compute offset time
1382 // (offset = my observed time minus their observed time)
1383 double offset = ProtoTime(obs2->GetObservedTime()) - ProtoTime(obs1->GetObservedTime());
1384 if (offsetCount > 0)
1385 {
1386 if (offset < offsetMin)
1387 offsetMin = offset;
1388 else if (offset > offsetMax)
1389 offsetMax = offset;
1390 offsetSum += offset;
1391 }
1392 else
1393 {
1394 offsetMin = offsetMax = offsetSum = offset;
1395 }
1396 offsetCount++;
1397 break;
1398 }
1399 }
1400 fprintf(log_file, "host %s/%hu ", observerAddr.GetHostString(), observerAddr.GetPort());
1401 if (offsetCount > 0)
1402 {
1403 double offsetAve = offsetSum / (double)offsetCount;
1404 fprintf(log_file, "RBS offset: min>%f ave>%f max>%f ", offsetMin, offsetAve, offsetMax);
1405 }
1406 else
1407 {
1408 fprintf(log_file, "RBS offset: undetermined ");
1409 }
1410 // Have I been able to measure RTT to this observer?
1411 rttCount = 0;
1412 iterator.Reset(&observerAddr);
1413 while (NULL != (obs = iterator.GetNextItem()))
1414 {
1415 double rtt = obs->GetRtt();
1416 if (rtt >= 0.0)
1417 {
1418 // update rtt tracking
1419 if (rttCount > 0)
1420 {
1421 if (rtt < rttMin)
1422 rttMin = rtt;
1423 else if (rtt > rttMax)
1424 rttMax = rtt;
1425 rttSum += rtt;
1426 }
1427 else
1428 {
1429 rttMin = rttMax = rttSum = obs->GetRtt();
1430 }
1431 rttCount++;
1432 }
1433 }
1434 if (rttCount > 0)
1435 {
1436 double rttAve = rttSum / (double)rttCount;
1437 fprintf(log_file, "rtt: min>%f ave>%f max>%f\n", rttMin, rttAve, rttMax);
1438 }
1439 else
1440 {
1441 fprintf(log_file, "rtt: undetermined\n");
1442 }
1443 }
1444 } // end TingSession::Summarize()
1445
1446 void TingSession::Reset(UINT8 roundId)
1447 {
1448 session_state = IDLE;
1449 round_id = roundId;
1450 sent_round_id = 0;
1451 sent_msg_time.tv_sec = sent_msg_time.tv_usec = 0;
1452 sent_request_count = 0;
1453 request_round_count = 0;
1454 // TBD - carry some historical state into future rounds?
1455 observation_list.Destroy();
1456 observer_list.Destroy();
1457 }
1458
1459
1460 bool TingSession::Initiate(TingCallback* termCallback, const void* termData)
1461 {
1462 if (IDLE != session_state)
1463 {
1464 PLOG(PL_ERROR, "TingSession::Initiate() error: session %s/%hu already active\n",
1465 GetSessionAddress().GetHostString(), GetSessionAddress().GetPort());
1466 return false;
1467 }
1468 Reset(round_id);
1469 bool result;
1470 if (IsUnicast())
1471 {
1472 session_state = HAILING;
1473 result = SendMessage(TingMessage::HAIL, REPLY_TIME_MAX);
1474 }
1475 else
1476 {
1477 session_state = CONNECTED;
1478 result = SendMessage(TingMessage::REQUEST, REPLY_TIME_MAX);
1479 }
1480 if (result)
1481 {
1482 session_timer.SetInterval(REPLY_TIME_MAX);
1483 timer_mgr.ActivateTimer(session_timer);
1484 }
1485 if (!result)
1486 PLOG(PL_ERROR, "TingSession::Initiate() error: unable to send message to %s/%hu\n",
1487 GetSessionAddress().GetHostString(), GetSessionAddress().GetPort());
1488
1489 term_callback = termCallback;
1490 term_data = termData;
1491
1492 return result;
1493 } // end TingSession::Initiate()
1494
1495
1496 const char* TingApp::DEFAULT_GROUP = "225.5.2.2";
1497 const UINT16 TingApp::DEFAULT_PORT = 5522;
1498
1499 TingApp::TingApp()
1500 : ting_socket(ProtoSocket::UDP), ting_port(DEFAULT_PORT), next_round_id(1), log_file(stdout)
1501 {
1502 ting_socket.SetNotifier(&GetSocketNotifier());
1503 ting_socket.SetListener(this, &TingApp::OnTingSocketEvent);
1504 mcast_iface[0] = '\0';
1505 }
1506
1507 TingApp::~TingApp()
1508 {
1509 if (ting_socket.IsOpen()) ting_socket.Close();
1510 if (NULL != group_session)
1511 {
1512 delete group_session;
1513 group_session = NULL;
1514 }
1515 session_list.Destroy();
1516 }
1517
1518 void TingApp::Usage()
1519 {
1520 fprintf(stderr, "ting [{<host>[/<port>] | group}][interface <ifaceName>]\n"
1521 " [listen [<groupAddr>/]<port>][log <fileName>\n");
1522 } // end TingApp::Usage()
1523
1524 bool TingApp::OnStartup(int argc, const char*const* argv)
1525 {
1526
1527 SetDebugLevel(8);
1528
1529 // By default, listen on the default address/port
1530 group_address.ResolveFromString(DEFAULT_GROUP);
1531 group_address.SetPort(ting_port);
1532
1533 // Note this apply to initiated sessions only
1534 bool mcastOnly = true;
1535 bool unicastOnly = true;
1536
1537 ProtoAddressList destList;
1538
1539 int i = 1;
1540 while (i < argc)
1541 {
1542 const char* cmd = argv[i++];
1543 size_t len = strlen(cmd);
1544 if (!strncmp("log", cmd, len))
1545 {
1546 const char* val = argv[i++];
1547 if (NULL == val)
1548 {
1549 fprintf(stderr, "ting error: missing 'log' argument !\n");
1550 Usage();
1551 return false;
1552 }
1553 if (stdout != log_file) fclose(log_file);
1554 if (0 == strcmp(val, "STDOUT"))
1555 {
1556 log_file = stdout;
1557 }
1558 else
1559 {
1560 if (NULL == (log_file = fopen(val, "w+")))
1561 {
1562 fprintf(stderr, "ting log fopen(%s) error: %s\n", val, GetErrorString());
1563 return false;
1564 }
1565 }
1566 }
1567 if (!strncmp("interface", cmd, len))
1568 {
1569 const char* val = argv[i++];
1570 if (NULL == val)
1571 {
1572 fprintf(stderr, "ting error: missing 'log' argument !\n");
1573 Usage();
1574 return false;
1575 }
1576 strncpy(mcast_iface, val, 255);
1577 mcast_iface[255] = '\0';
1578 }
1579 else if (!strncmp("listen", cmd, len))
1580 {
1581 // It's a listen command, so look for port number and open socket
1582 // (an optional group address may be specified as prefix to port)
1583 const char* val = argv[i++];
1584 if (NULL == val)
1585 {
1586 fprintf(stderr, "ting error: missing 'listen' argument !\n");
1587 Usage();
1588 return false;
1589 }
1590 char addrText[256];
1591 strncpy(addrText, val, 256);
1592 addrText[255] = '\0';
1593 char* addrPtr = addrText;
1594 char* portPtr = strchr(addrText, '/');
1595 if (NULL != portPtr)
1596 {
1597 *portPtr++ = '\0';
1598 }
1599 else
1600 {
1601 addrPtr = NULL; // no group addr was provided.
1602 portPtr = addrText;
1603 }
1604
1605 UINT16 port;
1606 if (1 != sscanf(portPtr, "%hu", &port))
1607 {
1608 fprintf(stderr, "ting listen error: invalid port number \"%s\"\n", portPtr);
1609 Usage();
1610 return false;
1611 }
1612 ting_port = port;
1613 if (NULL != addrPtr)
1614 {
1615 ProtoAddress addr;
1616 if (!addr.ResolveFromString(addrPtr))
1617 {
1618 fprintf(stderr, "ting error: invalid 'listen' address \"%s\"\n", addrPtr);
1619 Usage();
1620 return false;
1621 }
1622 if (!addr.IsMulticast())
1623 {
1624 fprintf(stderr, "ting error: 'listen' address is not IP multicast!\n");
1625 Usage();
1626 return false;
1627 }
1628 group_address = addr;
1629 group_address.SetPort(port);
1630 }
1631 else
1632 {
1633 group_address.Invalidate();
1634 }
1635 }
1636 else if (!strncmp("group", cmd, len))
1637 {
1638 // Was a prior group address already specified?
1639 // (this will replace it, so remove it)
1640 ProtoAddressList::Iterator iterator(destList);
1641 ProtoAddress nextAddr;
1642 while (iterator.GetNextAddress(nextAddr))
1643 {
1644 if (!nextAddr.IsValid() || nextAddr.IsMulticast())
1645 {
1646 destList.Remove(nextAddr);
1647 break;
1648 }
1649 }
1650 if (!destList.Insert(group_address))
1651 {
1652 fprintf(stderr, "ting error: unable to add group destination!\n");
1653 Usage();
1654 return false;
1655 }
1656 unicastOnly = false;
1657 }
1658 else
1659 {
1660 // Assume its a <host>[/<port>] to time check
1661 char addr[256];
1662 strncpy(addr, cmd, 256);
1663 addr[255] ='\0';
1664 char* portPtr = strchr(addr, '/');
1665 if (NULL != portPtr) *portPtr++ = '\0';
1666 ProtoAddress destAddr;
1667 if (!destAddr.ResolveFromString(addr))
1668 {
1669 PLOG(PL_ERROR, "ting error: unable to resolve destination host address \"%s\"\n", addr);
1670 Usage();
1671 return false;
1672 }
1673 destAddr.SetPort(DEFAULT_PORT);
1674 if (NULL != portPtr)
1675 {
1676 UINT16 port;
1677 if (1 != sscanf(portPtr, "%hu", &port))
1678 {
1679 PLOG(PL_ERROR, "ting error: invalid destination port \"%s\"\n", portPtr);
1680 Usage();
1681 return false;
1682 }
1683 destAddr.SetPort(port);
1684 }
1685 if (destAddr.IsMulticast())
1686 {
1687 group_address = destAddr;
1688 // Was a prior group address already specified?
1689 // (this will replace it, so remove it)
1690 ProtoAddressList::Iterator iterator(destList);
1691 ProtoAddress nextAddr;
1692 while (iterator.GetNextAddress(nextAddr))
1693 {
1694 if (!nextAddr.IsValid() || nextAddr.IsMulticast())
1695 {
1696 destList.Remove(nextAddr);
1697 break;
1698 }
1699 }
1700 unicastOnly = false;
1701 }
1702 else
1703 {
1704 mcastOnly = false;
1705 }
1706 if (!destList.Insert(destAddr))
1707 {
1708 fprintf(stderr, "ting error: unable to add destination!\n");
1709 Usage();
1710 return false;
1711 }
1712 }
1713 }
1714
1715 const char* ifaceName = ('\0' != mcast_iface[0]) ? mcast_iface : NULL;
1716
1717 // Put all local unicast addrs in list
1718 if (NULL != ifaceName)
1719 {
1720 if (!ProtoNet::GetInterfaceAddressList(ifaceName, ProtoAddress::IPv4, local_address_list))
1721 PLOG(PL_WARN, "NormSession::Open() warning: incomplete IPv4 interface address list\n");
1722 if (!ProtoNet::GetInterfaceAddressList(ifaceName, ProtoAddress::IPv6, local_address_list))
1723 PLOG(PL_WARN, "NormSession::Open() warning: incomplete IPv6 interface address list\n");
1724 }
1725 else
1726 {
1727 // Get all interface addresses
1728 if (!ProtoNet::GetHostAddressList(ProtoAddress::IPv4, local_address_list))
1729 PLOG(PL_WARN, "NormSession::Open() warning: incomplete IPv4 host address list\n");
1730 if (!ProtoNet::GetHostAddressList(ProtoAddress::IPv6, local_address_list))
1731 PLOG(PL_WARN, "NormSession::Open() warning: incomplete IPv6 host address list\n");
1732 }
1733
1734 ProtoAddressList::Iterator it(local_address_list);
1735 ProtoAddress localAddr;
1736 while (it.GetNextAddress(localAddr))
1737 TRACE("ting: found local address %s\n", localAddr.GetHostString());
1738
1739 // Open socket and bind to "ting_port"
1740 // By default, "listen" on the default port if no command given
1741 if (!(ting_socket.Open(0, ProtoAddress::IPv4, false)))
1742 {
1743 PLOG(PL_ERROR, "ting error: unable to open ting_socket!\n");
1744 return false;
1745 }
1746 if (mcastOnly)
1747 {
1748 ting_socket.SetReuse(true); // let's use separate ting to "initiate" checks
1749 ting_socket.SetLoopback(true);
1750 }
1751 if (!(ting_socket.Bind(ting_port)))
1752 {
1753 // Bind to random socket since as this may be a unicast "initiator"
1754 if (!unicastOnly || !ting_socket.Bind(0))
1755 {
1756 PLOG(PL_ERROR, "ting error: unable to bind ting_socket!\n");
1757 return false;
1758 }
1759 }
1760
1761 if (group_address.IsValid())
1762 {
1763 group_address.SetPort(ting_port);
1764 if (NULL == (group_session = new TingSession(group_address, next_round_id, ting_socket, GetTimerMgr(), local_address_list)))
1765 {
1766 PLOG(PL_ERROR, "TingApp::OnStartup() error: new group TingSession error: %s\n", GetErrorString());
1767 return false;
1768 }
1769 group_session->SetLogFile(log_file);
1770
1771 if (!ting_socket.JoinGroup(group_address, ifaceName))
1772 {
1773 PLOG(PL_ERROR, "TingApp::OnStartup() error: unable to join group %s\n", group_address.GetHostString());
1774 delete group_session;
1775 group_session = NULL;
1776 return false;
1777 }
1778 if (NULL != ifaceName) ting_socket.SetMulticastInterface(ifaceName);
1779 if (0 == ++next_round_id) next_round_id = 1;
1780 if (NULL != ifaceName)
1781 TRACE("ting: listening on group/port %s/%hu interface %s\n", group_address.GetHostString(), ting_port, ifaceName);
1782 else
1783 TRACE("ting: listening on group/port %s/%hu\n", group_address.GetHostString(), ting_port);
1784 }
1785 else
1786 {
1787 TRACE("ting: listening on port %hu\n", ting_port);
1788 }
1789
1790 // Iterate through the "destList" and initiate "ting" to each
1791 ProtoAddress destAddr;
1792 ProtoAddressList::Iterator iterator(destList);
1793 while (iterator.GetNextAddress(destAddr))
1794 {
1795 TingSession* session;
1796 if (destAddr.IsMulticast())
1797 {
1798 ASSERT(NULL != group_session);
1799 ASSERT(destAddr.IsEqual(group_session->GetAddress()));
1800 session = group_session;
1801 }
1802 else
1803 {
1804 session = session_list.Find(destAddr);
1805 if (NULL != session)
1806 {
1807 fprintf(stderr, "ting warning: duplicate destination %s/%hu\n", destAddr.GetHostString(), destAddr.GetPort());
1808 continue;
1809 }
1810 else if (NULL == (session = new TingSession(destAddr, next_round_id, ting_socket, GetTimerMgr(), local_address_list)))
1811 {
1812 fprintf(stderr, "ting new unicast TingSession error: %s\n", GetErrorString());
1813 return false;
1814 }
1815 else
1816 {
1817 session->SetLogFile(log_file);
1818 if (!session_list.Insert(*session))
1819 {
1820 fprintf(stderr, "ting error: session_list insertion failure!\n");
1821 delete session;
1822 return false;
1823 }
1824 if (0 == ++next_round_id) next_round_id = 1;
1825 }
1826 }
1827 if (!iterator.PeekNextAddress(destAddr))
1828 session->Initiate(DoTerminate, this);
1829 }
1830 return true;
1831 } // end TingApp::OnStartup()
1832
1833 void TingApp::OnShutdown()
1834 {
1835 session_list.Destroy();
1836 if (NULL != group_session)
1837 {
1838 delete group_session;
1839 group_session = NULL;
1840 }
1841 if (ting_socket.IsOpen()) ting_socket.Close();
1842 TRACE("ting: Done.\n");
1843 } // end TingApp::OnShutdown()
1844
1845 void TingApp::DoTerminate(const void* termData)
1846 {
1847 TingApp* theApp = (TingApp*)(termData);
1848 theApp->Stop();
1849 } // end TingApp::DoTerminate()
1850
1851 void TingApp::OnTingSocketEvent(ProtoSocket& theSocket,
1852 ProtoSocket::Event theEvent)
1853 {
1854 switch (theEvent)
1855 {
1856 case ProtoSocket::RECV:
1857 {
1858 // Receive ting message and process according to type / state
1859 for (;;)
1860 {
1861 struct timeval currentTime;
1862 ProtoSystemTime(currentTime);
1863 ProtoAddress srcAddr;
1864 UINT32 buffer[MSG_LENGTH_MAX];
1865 unsigned int len = MSG_LENGTH_MAX;
1866 if (!theSocket.RecvFrom((char*)buffer, len, srcAddr))
1867 {
1868 PLOG(PL_ERROR, "TingApp::OnUdpSocketEvent() error receiving!\n");
1869 break;
1870 }
1871 if (0 == len) break; // nothing more to read
1872 //TRACE("recvd %d byte message from %s/%hu\n", len, srcAddr.GetHostString(), srcAddr.GetPort());
1873 TingMessage msg(buffer, len, true);
1874 switch (msg.GetType())
1875 {
1876 case TingMessage::HAIL:
1877 case TingMessage::REQUEST:
1878 case TingMessage::REPLY:
1879 HandleMessage(msg, srcAddr, currentTime);
1880 break;
1881 default:
1882 PLOG(PL_WARN, "TingApp::OnUdpSocketEvent() warning: received invalid message type!\n");
1883 break;
1884 }
1885 }
1886 break;
1887 }
1888 case ProtoSocket::EXCEPTION:
1889 case ProtoSocket::ERROR_:
1890 default:
1891 TRACE("Unhandled event\n");
1892 break;
1893 }
1894 } // end TingApp::OnTingSocketEvent()
1895
1896 void TingApp::HandleMessage(const TingMessage& msg, const ProtoAddress& srcAddr, const struct timeval& recvTime)
1897 {
1898 TRACE("TingApp::HandleMessage() from %s/%hu ...\n", srcAddr.GetHostString(), srcAddr.GetPort());
1899 // Make sure the message round id is valid
1900 if (0 == msg.GetRoundId())
1901 {
1902 PLOG(PL_WARN, "TingApp::HandleMessage() warning: received messsage with null round id from host %s\n",
1903 srcAddr.GetHostString());
1904 return;
1905 }
1906 if (msg.IsUnicast())
1907 {
1908 // Do we have an existing session for this unicast addr/port?
1909 TingSession* session = session_list.Find(srcAddr);
1910 if (NULL == session)
1911 {
1912 // It's possibly a new session, MUST be a HAIL message
1913 if (TingMessage::HAIL == msg.GetType())
1914 {
1915 if (NULL == (session = new TingSession(srcAddr, msg.GetRoundId(), ting_socket, GetTimerMgr(), local_address_list)))
1916 {
1917 PLOG(PL_ERROR, "TingApp::HandleMessage() new TingSession error: %s\n", GetErrorString());
1918 return;
1919 }
1920 session->SetLogFile(log_file);
1921 if (!session_list.Insert(*session))
1922 {
1923 PLOG(PL_ERROR, "TingApp::HandleMessage() error: session_list insertion failure!\n");
1924 delete session;
1925 return;
1926 }
1927 }
1928 else
1929 {
1930 PLOG(PL_WARN, "TingApp::HandleMessage() warning: received unexpected unicast %s message from host %s\n",
1931 (TingMessage::REQUEST == msg.GetType()) ? "REQUEST" : "REPLY", srcAddr.GetHostString());
1932 return;
1933 }
1934 }
1935
1936 switch (msg.GetType())
1937 {
1938 case TingMessage::HAIL:
1939 session->HandleHail(msg, srcAddr, recvTime);
1940 break;
1941 case TingMessage::REQUEST:
1942 session->HandleUnicastRequest(msg, srcAddr, recvTime);
1943 break;
1944 case TingMessage::REPLY:
1945 session->HandleReply(msg, srcAddr, recvTime);
1946 break;
1947 default:
1948 break;
1949 }
1950 }
1951 else
1952 {
1953 if (NULL == group_session)
1954 {
1955 PLOG(PL_WARN, "TingApp::HandleMessage() warning: received unexpected multicast message from host %s/%hu\n",
1956 srcAddr.GetHostString(), srcAddr.GetPort());
1957 return;
1958 }
1959 if (TingMessage::REQUEST == msg.GetType())
1960 {
1961 group_session->HandleMulticastRequest(msg, srcAddr, recvTime);
1962 }
1963 else
1964 {
1965 PLOG(PL_WARN, "TingApp::HandleMessage() warning: received unexpected multicast %s message from host %s/%hu\n",
1966 (TingMessage::HAIL == msg.GetType()) ? "HAIL" : "REPLY", srcAddr.GetHostString(), srcAddr.GetPort());
1967 return;
1968 }
1969 }
1970 } // end TingApp::HandleMessage()
1971
1972
1973 // valid for backoff = 1.0e-06 to 1.0e+03 seconds
1974 UINT8 TingMessage::QuantizeBackoff(double backoff)
1975 {
1976 if (backoff > BACKOFF_MAX)
1977 backoff = BACKOFF_MAX;
1978 else if (backoff < BACKOFF_MIN)
1979 backoff = BACKOFF_MIN;
1980 if (backoff < 3.3e-05)
1981 return ((UINT8)((backoff/BACKOFF_MIN)) - 1);
1982 else
1983 return ((UINT8)(ceil(255.0 - (13.0*log(BACKOFF_MAX/backoff)))));
1984 } // end NormQuantizebackoff()
1985
1986 ////////////////////////////////////////////////
1987 // inline double TingMessage::UnquantizeBackoff(UINT8 qrtt)
1988 // {
1989 // return ((qrtt < 31) ?
1990 // (((double)(qrtt+1))*(double)BACKOFF_MIN) :
1991 // (BACKOFF_MAX/exp(((double)(255-qrtt))/(double)13.0)));
1992 // } // end TingMessage::UnquantizeBackoff(
1993
1994 // The lookup table here was generated from the QuantizeBackoff() method
1995 const double TingMessage::BACKOFF[256] =
1996 {
1997 1.000e-06, 2.000e-06, 3.000e-06, 4.000e-06,
1998 5.000e-06, 6.000e-06, 7.000e-06, 8.000e-06,
1999 9.000e-06, 1.000e-05, 1.100e-05, 1.200e-05,
2000 1.300e-05, 1.400e-05, 1.500e-05, 1.600e-05,
2001 1.700e-05, 1.800e-05, 1.900e-05, 2.000e-05,
2002 2.100e-05, 2.200e-05, 2.300e-05, 2.400e-05,
2003 2.500e-05, 2.600e-05, 2.700e-05, 2.800e-05,
2004 2.900e-05, 3.000e-05, 3.100e-05, 3.287e-05,
2005 3.550e-05, 3.833e-05, 4.140e-05, 4.471e-05,
2006 4.828e-05, 5.215e-05, 5.631e-05, 6.082e-05,
2007 6.568e-05, 7.093e-05, 7.660e-05, 8.273e-05,
2008 8.934e-05, 9.649e-05, 1.042e-04, 1.125e-04,
2009 1.215e-04, 1.313e-04, 1.417e-04, 1.531e-04,
2010 1.653e-04, 1.785e-04, 1.928e-04, 2.082e-04,
2011 2.249e-04, 2.429e-04, 2.623e-04, 2.833e-04,
2012 3.059e-04, 3.304e-04, 3.568e-04, 3.853e-04,
2013 4.161e-04, 4.494e-04, 4.853e-04, 5.241e-04,
2014 5.660e-04, 6.113e-04, 6.602e-04, 7.130e-04,
2015 7.700e-04, 8.315e-04, 8.980e-04, 9.698e-04,
2016 1.047e-03, 1.131e-03, 1.222e-03, 1.319e-03,
2017 1.425e-03, 1.539e-03, 1.662e-03, 1.795e-03,
2018 1.938e-03, 2.093e-03, 2.260e-03, 2.441e-03,
2019 2.636e-03, 2.847e-03, 3.075e-03, 3.321e-03,
2020 3.586e-03, 3.873e-03, 4.182e-03, 4.517e-03,
2021 4.878e-03, 5.268e-03, 5.689e-03, 6.144e-03,
2022 6.635e-03, 7.166e-03, 7.739e-03, 8.358e-03,
2023 9.026e-03, 9.748e-03, 1.053e-02, 1.137e-02,
2024 1.228e-02, 1.326e-02, 1.432e-02, 1.547e-02,
2025 1.670e-02, 1.804e-02, 1.948e-02, 2.104e-02,
2026 2.272e-02, 2.454e-02, 2.650e-02, 2.862e-02,
2027 3.090e-02, 3.338e-02, 3.604e-02, 3.893e-02,
2028 4.204e-02, 4.540e-02, 4.903e-02, 5.295e-02,
2029 5.718e-02, 6.176e-02, 6.669e-02, 7.203e-02,
2030 7.779e-02, 8.401e-02, 9.072e-02, 9.798e-02,
2031 1.058e-01, 1.143e-01, 1.234e-01, 1.333e-01,
2032 1.439e-01, 1.554e-01, 1.679e-01, 1.813e-01,
2033 1.958e-01, 2.114e-01, 2.284e-01, 2.466e-01,
2034 2.663e-01, 2.876e-01, 3.106e-01, 3.355e-01,
2035 3.623e-01, 3.913e-01, 4.225e-01, 4.563e-01,
2036 4.928e-01, 5.322e-01, 5.748e-01, 6.207e-01,
2037 6.704e-01, 7.240e-01, 7.819e-01, 8.444e-01,
2038 9.119e-01, 9.848e-01, 1.064e+00, 1.149e+00,
2039 1.240e+00, 1.340e+00, 1.447e+00, 1.562e+00,
2040 1.687e+00, 1.822e+00, 1.968e+00, 2.125e+00,
2041 2.295e+00, 2.479e+00, 2.677e+00, 2.891e+00,
2042 3.122e+00, 3.372e+00, 3.641e+00, 3.933e+00,
2043 4.247e+00, 4.587e+00, 4.953e+00, 5.349e+00,
2044 5.777e+00, 6.239e+00, 6.738e+00, 7.277e+00,
2045 7.859e+00, 8.487e+00, 9.166e+00, 9.898e+00,
2046 1.069e+01, 1.154e+01, 1.247e+01, 1.346e+01,
2047 1.454e+01, 1.570e+01, 1.696e+01, 1.832e+01,
2048 1.978e+01, 2.136e+01, 2.307e+01, 2.491e+01,
2049 2.691e+01, 2.906e+01, 3.138e+01, 3.389e+01,
2050 3.660e+01, 3.953e+01, 4.269e+01, 4.610e+01,
2051 4.979e+01, 5.377e+01, 5.807e+01, 6.271e+01,
2052 6.772e+01, 7.314e+01, 7.899e+01, 8.530e+01,
2053 9.212e+01, 9.949e+01, 1.074e+02, 1.160e+02,
2054 1.253e+02, 1.353e+02, 1.462e+02, 1.578e+02,
2055 1.705e+02, 1.841e+02, 1.988e+02, 2.147e+02,
2056 2.319e+02, 2.504e+02, 2.704e+02, 2.921e+02,
2057 3.154e+02, 3.406e+02, 3.679e+02, 3.973e+02,
2058 4.291e+02, 4.634e+02, 5.004e+02, 5.404e+02,
2059 5.836e+02, 6.303e+02, 6.807e+02, 7.351e+02,
2060 7.939e+02, 8.574e+02, 9.260e+02, 1.000e+03
2061 }; // TingMessage::BACKOFF[]
2062