15976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org/*
25976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * libjingle
35976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Copyright 2004--2005, Google Inc.
45976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
55976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Redistribution and use in source and binary forms, with or without
65976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * modification, are permitted provided that the following conditions are met:
75976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
85976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
95976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     this list of conditions and the following disclaimer.
105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     this list of conditions and the following disclaimer in the documentation
125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     and/or other materials provided with the distribution.
135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  3. The name of the author may not be used to endorse or promote products
145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     derived from this software without specific prior written permission.
155976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
165976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
175976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
185976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
195976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
205976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
215976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
225976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
235976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
245976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
255976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
265976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org */
275976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
285976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/p2p/client/socketmonitor.h"
295976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
305976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/common.h"
315976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
325976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgnamespace cricket {
335976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
345976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgenum {
355976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  MSG_MONITOR_POLL,
365976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  MSG_MONITOR_START,
375976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  MSG_MONITOR_STOP,
385976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  MSG_MONITOR_SIGNAL
395976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org};
405976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
415976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgSocketMonitor::SocketMonitor(TransportChannel* channel,
425976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                             talk_base::Thread* worker_thread,
435976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                             talk_base::Thread* monitor_thread) {
445976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  channel_ = channel;
455976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  channel_thread_ = worker_thread;
465976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  monitoring_thread_ = monitor_thread;
475976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  monitoring_ = false;
485976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
495976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
505976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgSocketMonitor::~SocketMonitor() {
515976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  channel_thread_->Clear(this);
525976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  monitoring_thread_->Clear(this);
535976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
545976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
555976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgvoid SocketMonitor::Start(int milliseconds) {
565976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  rate_ = milliseconds;
575976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  if (rate_ < 250)
585976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    rate_ = 250;
595976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  channel_thread_->Post(this, MSG_MONITOR_START);
605976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
615976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
625976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgvoid SocketMonitor::Stop() {
635976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  channel_thread_->Post(this, MSG_MONITOR_STOP);
645976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
655976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
665976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgvoid SocketMonitor::OnMessage(talk_base::Message *message) {
675976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  talk_base::CritScope cs(&crit_);
685976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  switch (message->message_id) {
695976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    case MSG_MONITOR_START:
705976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      ASSERT(talk_base::Thread::Current() == channel_thread_);
715976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      if (!monitoring_) {
725976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org        monitoring_ = true;
735976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org        PollSocket(true);
745976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      }
755976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      break;
765976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
775976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    case MSG_MONITOR_STOP:
785976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      ASSERT(talk_base::Thread::Current() == channel_thread_);
795976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      if (monitoring_) {
805976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org        monitoring_ = false;
815976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org        channel_thread_->Clear(this);
825976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      }
835976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      break;
845976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
855976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    case MSG_MONITOR_POLL:
865976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      ASSERT(talk_base::Thread::Current() == channel_thread_);
875976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      PollSocket(true);
885976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      break;
895976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
905976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    case MSG_MONITOR_SIGNAL: {
915976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      ASSERT(talk_base::Thread::Current() == monitoring_thread_);
925976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      std::vector<ConnectionInfo> infos = connection_infos_;
935976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      crit_.Leave();
945976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      SignalUpdate(this, infos);
955976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      crit_.Enter();
965976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      break;
975976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    }
985976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
995976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
1005976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1015976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgvoid SocketMonitor::PollSocket(bool poll) {
1025976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  ASSERT(talk_base::Thread::Current() == channel_thread_);
1035976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  talk_base::CritScope cs(&crit_);
1045976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1055976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Gather connection infos
1065976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  channel_->GetStats(&connection_infos_);
1075976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1085976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Signal the monitoring thread, start another poll timer
1095976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  monitoring_thread_->Post(this, MSG_MONITOR_SIGNAL);
1105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  if (poll)
1115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    channel_thread_->PostDelayed(rate_, this, MSG_MONITOR_POLL);
1125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
1135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}  // namespace cricket
115