OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "build/build_config.h" |
| 6 |
| 7 #if !defined(OS_WIN) |
| 8 extern "C" { |
| 9 #include <unistd.h> |
| 10 } |
| 11 #endif // !defined(OS_WIN) |
| 12 |
| 13 #include <iostream> |
| 14 #include <list> |
| 15 |
| 16 #include "base/at_exit.h" |
| 17 #include "media/base/data_buffer.h" |
| 18 #include "remoting/jingle_glue/jingle_channel.h" |
| 19 #include "remoting/jingle_glue/jingle_client.h" |
| 20 |
| 21 using remoting::JingleClient; |
| 22 using remoting::JingleChannel; |
| 23 |
| 24 void SetConsoleEcho(bool on) { |
| 25 #if defined(OS_WIN) |
| 26 HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); |
| 27 if ((hIn == INVALID_HANDLE_VALUE) || (hIn == NULL)) |
| 28 return; |
| 29 |
| 30 DWORD mode; |
| 31 if (!GetConsoleMode(hIn, &mode)) |
| 32 return; |
| 33 |
| 34 if (on) { |
| 35 mode = mode | ENABLE_ECHO_INPUT; |
| 36 } else { |
| 37 mode = mode & ~ENABLE_ECHO_INPUT; |
| 38 } |
| 39 |
| 40 SetConsoleMode(hIn, mode); |
| 41 #else // defined(OS_WIN) |
| 42 if (on) |
| 43 system("stty echo"); |
| 44 else |
| 45 system("stty -echo"); |
| 46 #endif // !defined(OS_WIN) |
| 47 } |
| 48 |
| 49 class JingleTestClient : public JingleChannel::Callback, |
| 50 public JingleClient::Callback { |
| 51 public: |
| 52 virtual ~JingleTestClient() {} |
| 53 |
| 54 void Run(const std::string& username, const std::string& password, |
| 55 const std::string& host_jid) { |
| 56 client_ = new JingleClient(); |
| 57 client_->Init(username, password, this); |
| 58 |
| 59 if (host_jid != "") { |
| 60 scoped_refptr<JingleChannel> channel = client_->Connect(host_jid, this); |
| 61 channels_.push_back(channel); |
| 62 } |
| 63 |
| 64 while (true) { |
| 65 std::string line; |
| 66 std::getline(std::cin, line); |
| 67 |
| 68 { |
| 69 AutoLock auto_lock(channels_lock_); |
| 70 |
| 71 // Broadcast message to all clients. |
| 72 for (ChannelsList::iterator it = channels_.begin(); |
| 73 it != channels_.end(); ++it) { |
| 74 uint8* buf = new uint8[line.length()]; |
| 75 memcpy(buf, line.c_str(), line.length()); |
| 76 (*it)->Write(new media::DataBuffer(buf, line.length())); |
| 77 } |
| 78 } |
| 79 |
| 80 if (line == "exit") |
| 81 break; |
| 82 } |
| 83 |
| 84 while (!channels_.empty()) { |
| 85 channels_.front()->Close(); |
| 86 channels_.pop_front(); |
| 87 } |
| 88 |
| 89 client_->Close(); |
| 90 } |
| 91 |
| 92 // JingleChannel::Callback interface. |
| 93 void OnStateChange(JingleChannel* channel, JingleChannel::State state) { |
| 94 LOG(INFO) << "State of " << channel->jid() << " changed to " << state; |
| 95 } |
| 96 |
| 97 void OnPacketReceived(JingleChannel* channel, |
| 98 scoped_refptr<media::DataBuffer> buffer) { |
| 99 std::string str(reinterpret_cast<const char*>(buffer->GetData()), |
| 100 buffer->GetDataSize()); |
| 101 std::cout << "(" << channel->jid() << "): " << str << std::endl; |
| 102 } |
| 103 |
| 104 // JingleClient::Callback interface. |
| 105 void OnStateChange(JingleClient* client, JingleClient::State state) { |
| 106 if (state == JingleClient::CONNECTED) { |
| 107 std::cerr << "Connected as " << client->GetFullJid() << std::endl; |
| 108 } else if (state == JingleClient::CLOSED) { |
| 109 std::cerr << "Connection closed" << std::endl; |
| 110 } |
| 111 } |
| 112 |
| 113 bool OnAcceptConnection(JingleClient* client, const std::string& jid, |
| 114 JingleChannel::Callback** callback) { |
| 115 std::cerr << "Accepting new connection from " << jid << std::endl; |
| 116 *callback = this; |
| 117 return true; |
| 118 } |
| 119 |
| 120 void OnNewConnection(JingleClient* client, |
| 121 scoped_refptr<JingleChannel> channel) { |
| 122 std::cerr << "Connected to " << channel->jid() << std::endl; |
| 123 AutoLock auto_lock(channels_lock_); |
| 124 channels_.push_back(channel); |
| 125 } |
| 126 |
| 127 private: |
| 128 typedef std::list<scoped_refptr<JingleChannel> > ChannelsList; |
| 129 |
| 130 scoped_refptr<JingleClient> client_; |
| 131 ChannelsList channels_; |
| 132 Lock channels_lock_; |
| 133 }; |
| 134 |
| 135 int main(int argc, char** argv) { |
| 136 if (argc > 2) |
| 137 std::cerr << "Usage: " << argv[0] << " [<host_jid>]" << std::endl; |
| 138 |
| 139 base::AtExitManager exit_manager; |
| 140 |
| 141 std::string host_jid = argc == 2 ? argv[1] : ""; |
| 142 |
| 143 std::string username; |
| 144 std::cout << "JID: "; |
| 145 std::cin >> username; |
| 146 |
| 147 std::string password; |
| 148 SetConsoleEcho(false); |
| 149 std::cout << "Password: "; |
| 150 std::cin >> password; |
| 151 SetConsoleEcho(true); |
| 152 std::cout << std::endl; |
| 153 |
| 154 JingleTestClient client; |
| 155 |
| 156 client.Run(username, password, host_jid); |
| 157 |
| 158 return 0; |
| 159 } |
OLD | NEW |