blob: 7975514d7e21d314a802eb2f03492cb705b59213 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/p2p/client/socketmonitor.h"
12
13#include "webrtc/base/common.h"
14
15namespace cricket {
16
17enum {
18 MSG_MONITOR_POLL,
19 MSG_MONITOR_START,
20 MSG_MONITOR_STOP,
21 MSG_MONITOR_SIGNAL
22};
23
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000024ConnectionMonitor::ConnectionMonitor(ConnectionStatsGetter* stats_getter,
25 rtc::Thread* worker_thread,
26 rtc::Thread* monitoring_thread) {
27 stats_getter_ = stats_getter;
28 worker_thread_ = worker_thread;
29 monitoring_thread_ = monitoring_thread;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000030 monitoring_ = false;
31}
32
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000033ConnectionMonitor::~ConnectionMonitor() {
34 worker_thread_->Clear(this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000035 monitoring_thread_->Clear(this);
36}
37
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000038void ConnectionMonitor::Start(int milliseconds) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039 rate_ = milliseconds;
40 if (rate_ < 250)
41 rate_ = 250;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070042 worker_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_START);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000043}
44
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000045void ConnectionMonitor::Stop() {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070046 worker_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_STOP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047}
48
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000049void ConnectionMonitor::OnMessage(rtc::Message *message) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050 rtc::CritScope cs(&crit_);
51 switch (message->message_id) {
52 case MSG_MONITOR_START:
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000053 ASSERT(rtc::Thread::Current() == worker_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000054 if (!monitoring_) {
55 monitoring_ = true;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000056 PollConnectionStats_w();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000057 }
58 break;
59
60 case MSG_MONITOR_STOP:
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000061 ASSERT(rtc::Thread::Current() == worker_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000062 if (monitoring_) {
63 monitoring_ = false;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000064 worker_thread_->Clear(this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000065 }
66 break;
67
68 case MSG_MONITOR_POLL:
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000069 ASSERT(rtc::Thread::Current() == worker_thread_);
70 PollConnectionStats_w();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000071 break;
72
73 case MSG_MONITOR_SIGNAL: {
74 ASSERT(rtc::Thread::Current() == monitoring_thread_);
75 std::vector<ConnectionInfo> infos = connection_infos_;
76 crit_.Leave();
77 SignalUpdate(this, infos);
78 crit_.Enter();
79 break;
80 }
81 }
82}
83
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000084void ConnectionMonitor::PollConnectionStats_w() {
85 ASSERT(rtc::Thread::Current() == worker_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000086 rtc::CritScope cs(&crit_);
87
88 // Gather connection infos
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000089 stats_getter_->GetConnectionStats(&connection_infos_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000090
91 // Signal the monitoring thread, start another poll timer
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070092 monitoring_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_SIGNAL);
93 worker_thread_->PostDelayed(RTC_FROM_HERE, rate_, this, MSG_MONITOR_POLL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000094}
95
96} // namespace cricket