1/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/p2p/client/socketmonitor.h"
29#include "talk/base/common.h"
30
31namespace cricket {
32
33const uint32 MSG_MONITOR_POLL = 1;
34const uint32 MSG_MONITOR_START = 2;
35const uint32 MSG_MONITOR_STOP = 3;
36const uint32 MSG_MONITOR_SIGNAL = 4;
37
38SocketMonitor::SocketMonitor(TransportChannel* channel,
39                             talk_base::Thread* worker_thread,
40                             talk_base::Thread* monitor_thread) {
41  channel_ = channel;
42  channel_thread_ = worker_thread;
43  monitoring_thread_ = monitor_thread;
44  monitoring_ = false;
45}
46
47SocketMonitor::~SocketMonitor() {
48  channel_thread_->Clear(this);
49  monitoring_thread_->Clear(this);
50}
51
52void SocketMonitor::Start(int milliseconds) {
53  rate_ = milliseconds;
54  if (rate_ < 250)
55    rate_ = 250;
56  channel_thread_->Post(this, MSG_MONITOR_START);
57}
58
59void SocketMonitor::Stop() {
60  channel_thread_->Post(this, MSG_MONITOR_STOP);
61}
62
63void SocketMonitor::OnMessage(talk_base::Message *message) {
64  talk_base::CritScope cs(&crit_);
65
66  switch (message->message_id) {
67  case MSG_MONITOR_START:
68    ASSERT(talk_base::Thread::Current() == channel_thread_);
69    if (!monitoring_) {
70      monitoring_ = true;
71      if (GetP2PChannel() != NULL) {
72        GetP2PChannel()->SignalConnectionMonitor.connect(
73            this, &SocketMonitor::OnConnectionMonitor);
74      }
75      PollSocket(true);
76    }
77    break;
78
79  case MSG_MONITOR_STOP:
80    ASSERT(talk_base::Thread::Current() == channel_thread_);
81    if (monitoring_) {
82      monitoring_ = false;
83      if (GetP2PChannel() != NULL)
84        GetP2PChannel()->SignalConnectionMonitor.disconnect(this);
85      channel_thread_->Clear(this);
86    }
87    break;
88
89  case MSG_MONITOR_POLL:
90    ASSERT(talk_base::Thread::Current() == channel_thread_);
91    PollSocket(true);
92    break;
93
94  case MSG_MONITOR_SIGNAL:
95    {
96      ASSERT(talk_base::Thread::Current() == monitoring_thread_);
97      std::vector<ConnectionInfo> infos = connection_infos_;
98      crit_.Leave();
99      SignalUpdate(this, infos);
100      crit_.Enter();
101    }
102    break;
103  }
104}
105
106void SocketMonitor::OnConnectionMonitor(P2PTransportChannel* channel) {
107  talk_base::CritScope cs(&crit_);
108  if (monitoring_)
109    PollSocket(false);
110}
111
112void SocketMonitor::PollSocket(bool poll) {
113  ASSERT(talk_base::Thread::Current() == channel_thread_);
114  talk_base::CritScope cs(&crit_);
115
116  // Gather connection infos
117  P2PTransportChannel* p2p_channel = GetP2PChannel();
118  if (p2p_channel != NULL) {
119    connection_infos_.clear();
120    const std::vector<Connection *> &connections = p2p_channel->connections();
121    std::vector<Connection *>::const_iterator it;
122    for (it = connections.begin(); it != connections.end(); it++) {
123      Connection *connection = *it;
124      ConnectionInfo info;
125      info.best_connection = p2p_channel->best_connection() == connection;
126      info.readable =
127          (connection->read_state() == Connection::STATE_READABLE);
128      info.writable =
129          (connection->write_state() == Connection::STATE_WRITABLE);
130      info.timeout =
131          (connection->write_state() == Connection::STATE_WRITE_TIMEOUT);
132      info.new_connection = !connection->reported();
133      connection->set_reported(true);
134      info.rtt = connection->rtt();
135      info.sent_total_bytes = connection->sent_total_bytes();
136      info.sent_bytes_second = connection->sent_bytes_second();
137      info.recv_total_bytes = connection->recv_total_bytes();
138      info.recv_bytes_second = connection->recv_bytes_second();
139      info.local_candidate = connection->local_candidate();
140      info.remote_candidate = connection->remote_candidate();
141      info.est_quality = connection->port()->network()->quality();
142      info.key = connection;
143      connection_infos_.push_back(info);
144    }
145  }
146
147  // Signal the monitoring thread, start another poll timer
148
149  monitoring_thread_->Post(this, MSG_MONITOR_SIGNAL);
150  if (poll)
151    channel_thread_->PostDelayed(rate_, this, MSG_MONITOR_POLL);
152}
153
154}
155