Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: remoting/jingle_glue/jingle_test_client.cc

Issue 3167047: Jingle_glue bugfixes. (Closed)
Patch Set: - Created 10 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/jingle_glue/jingle_client_unittest.cc ('k') | remoting/jingle_glue/jingle_thread.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #if !defined(OS_WIN) 7 #if !defined(OS_WIN)
8 extern "C" { 8 extern "C" {
9 #include <unistd.h> 9 #include <unistd.h>
10 } 10 }
11 #endif // !defined(OS_WIN) 11 #endif // !defined(OS_WIN)
12 12
13 #include <iostream> 13 #include <iostream>
14 #include <list> 14 #include <list>
15 15
16 #include "base/at_exit.h" 16 #include "base/at_exit.h"
17 #include "base/nss_util.h"
18 #include "base/time.h"
17 #include "media/base/data_buffer.h" 19 #include "media/base/data_buffer.h"
18 #include "remoting/base/constants.h" 20 #include "remoting/base/constants.h"
19 #include "remoting/jingle_glue/jingle_channel.h" 21 #include "remoting/jingle_glue/jingle_channel.h"
20 #include "remoting/jingle_glue/jingle_client.h" 22 #include "remoting/jingle_glue/jingle_client.h"
21 #include "remoting/jingle_glue/jingle_thread.h" 23 #include "remoting/jingle_glue/jingle_thread.h"
22 24
23 using remoting::JingleClient; 25 using remoting::JingleClient;
24 using remoting::JingleChannel; 26 using remoting::JingleChannel;
25 using remoting::kChromotingTokenServiceName; 27 using remoting::kChromotingTokenServiceName;
26 28
27 class JingleTestClient : public JingleChannel::Callback, 29 class JingleTestClient : public JingleChannel::Callback,
28 public JingleClient::Callback { 30 public JingleClient::Callback,
31 public base::RefCountedThreadSafe<JingleTestClient> {
29 public: 32 public:
33 JingleTestClient()
34 : closed_event_(true, false) {
35 }
36
30 virtual ~JingleTestClient() {} 37 virtual ~JingleTestClient() {}
31 38
32 void Run(const std::string& username, const std::string& auth_token, 39 void Run(const std::string& username, const std::string& auth_token,
33 const std::string& host_jid) { 40 const std::string& host_jid) {
34 // TODO(hclam): Fix the threading problem.
35 remoting::JingleThread jingle_thread; 41 remoting::JingleThread jingle_thread;
36 jingle_thread.Start(); 42 jingle_thread.Start();
37 client_ = new JingleClient(&jingle_thread); 43 client_ = new JingleClient(&jingle_thread);
38 client_->Init(username, auth_token, kChromotingTokenServiceName, this); 44 client_->Init(username, auth_token, kChromotingTokenServiceName, this);
39 45
40 if (host_jid != "") { 46 if (host_jid != "") {
41 scoped_refptr<JingleChannel> channel = client_->Connect(host_jid, this); 47 scoped_refptr<JingleChannel> channel = client_->Connect(host_jid, this);
42 channels_.push_back(channel); 48 channels_.push_back(channel);
43 } 49 }
44 50
(...skipping 11 matching lines...) Expand all
56 memcpy(buf, line.c_str(), line.length()); 62 memcpy(buf, line.c_str(), line.length());
57 (*it)->Write(new media::DataBuffer(buf, line.length())); 63 (*it)->Write(new media::DataBuffer(buf, line.length()));
58 } 64 }
59 } 65 }
60 66
61 if (line == "exit") 67 if (line == "exit")
62 break; 68 break;
63 } 69 }
64 70
65 while (!channels_.empty()) { 71 while (!channels_.empty()) {
66 channels_.front()->Close(); 72 closed_event_.Reset();
73 channels_.front()->Close(
74 NewRunnableMethod(this, &JingleTestClient::OnClosed));
75 // Wait until channel is closed. If it is not closed within 0.1 seconds
76 // continue closing everything else.
77 closed_event_.TimedWait(base::TimeDelta::FromMilliseconds(100));
67 channels_.pop_front(); 78 channels_.pop_front();
68 } 79 }
69 80
70 client_->Close(); 81 client_->Close();
71 jingle_thread.Stop(); 82 jingle_thread.Stop();
72 } 83 }
73 84
74 // JingleChannel::Callback interface. 85 // JingleChannel::Callback interface.
75 void OnStateChange(JingleChannel* channel, JingleChannel::State state) { 86 void OnStateChange(JingleChannel* channel, JingleChannel::State state) {
76 LOG(INFO) << "State of " << channel->jid() << " changed to " << state; 87 LOG(INFO) << "State of " << channel->jid() << " changed to " << state;
(...skipping 22 matching lines...) Expand all
99 return true; 110 return true;
100 } 111 }
101 112
102 void OnNewConnection(JingleClient* client, 113 void OnNewConnection(JingleClient* client,
103 scoped_refptr<JingleChannel> channel) { 114 scoped_refptr<JingleChannel> channel) {
104 std::cerr << "Connected to " << channel->jid() << std::endl; 115 std::cerr << "Connected to " << channel->jid() << std::endl;
105 AutoLock auto_lock(channels_lock_); 116 AutoLock auto_lock(channels_lock_);
106 channels_.push_back(channel); 117 channels_.push_back(channel);
107 } 118 }
108 119
120 void OnClosed() {
121 closed_event_.Signal();
122 }
123
109 private: 124 private:
110 typedef std::list<scoped_refptr<JingleChannel> > ChannelsList; 125 typedef std::list<scoped_refptr<JingleChannel> > ChannelsList;
111 126
112 scoped_refptr<JingleClient> client_; 127 scoped_refptr<JingleClient> client_;
113 ChannelsList channels_; 128 ChannelsList channels_;
114 Lock channels_lock_; 129 Lock channels_lock_;
130 base::WaitableEvent closed_event_;
115 }; 131 };
116 132
117 int main(int argc, char** argv) { 133 int main(int argc, char** argv) {
118 if (argc > 2) 134 if (argc > 2)
119 std::cerr << "Usage: " << argv[0] << " [<host_jid>]" << std::endl; 135 std::cerr << "Usage: " << argv[0] << " [<host_jid>]" << std::endl;
120 136
121 base::AtExitManager exit_manager; 137 base::AtExitManager exit_manager;
122 138
139 base::EnsureNSPRInit();
140 base::EnsureNSSInit();
141
123 std::string host_jid = argc == 2 ? argv[1] : ""; 142 std::string host_jid = argc == 2 ? argv[1] : "";
124 143
125 std::string username; 144 std::string username;
126 std::cout << "JID: "; 145 std::cout << "JID: ";
127 std::cin >> username; 146 std::cin >> username;
128 147
129 std::string auth_token; 148 std::string auth_token;
130 std::cout << "Auth token: "; 149 std::cout << "Auth token: ";
131 std::cin >> auth_token; 150 std::cin >> auth_token;
132 151
133 JingleTestClient client; 152 scoped_refptr<JingleTestClient> client = new JingleTestClient();
134 153
135 client.Run(username, auth_token, host_jid); 154 client->Run(username, auth_token, host_jid);
136 155
137 return 0; 156 return 0;
138 } 157 }
OLDNEW
« no previous file with comments | « remoting/jingle_glue/jingle_client_unittest.cc ('k') | remoting/jingle_glue/jingle_thread.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698