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 // A simple client implements a minimalize Chromoting client and shows |
| 6 // network traffic for debugging. |
| 7 |
| 8 #include <iostream> |
| 9 #include <list> |
| 10 |
| 11 #include "base/at_exit.h" |
| 12 #include "base/message_loop.h" |
| 13 #include "base/stl_util-inl.h" |
| 14 #include "media/base/data_buffer.h" |
| 15 #include "remoting/base/protocol_decoder.h" |
| 16 #include "remoting/client/host_connection.h" |
| 17 #include "remoting/jingle_glue/jingle_channel.h" |
| 18 #include "remoting/jingle_glue/jingle_client.h" |
| 19 |
| 20 using chromotocol_pb::HostMessage; |
| 21 using chromotocol_pb::InitClientMessage; |
| 22 using chromotocol_pb::BeginUpdateStreamMessage; |
| 23 using chromotocol_pb::EndUpdateStreamMessage; |
| 24 using chromotocol_pb::UpdateStreamPacketMessage; |
| 25 using remoting::HostConnection; |
| 26 using remoting::HostMessageList; |
| 27 using remoting::JingleClient; |
| 28 using remoting::JingleChannel; |
| 29 using remoting::ProtocolDecoder; |
| 30 |
| 31 void SetConsoleEcho(bool on) { |
| 32 #ifdef WIN32 |
| 33 HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); |
| 34 if ((hIn == INVALID_HANDLE_VALUE) || (hIn == NULL)) |
| 35 return; |
| 36 |
| 37 DWORD mode; |
| 38 if (!GetConsoleMode(hIn, &mode)) |
| 39 return; |
| 40 |
| 41 if (on) { |
| 42 mode = mode | ENABLE_ECHO_INPUT; |
| 43 } else { |
| 44 mode = mode & ~ENABLE_ECHO_INPUT; |
| 45 } |
| 46 |
| 47 SetConsoleMode(hIn, mode); |
| 48 #else |
| 49 if (on) |
| 50 system("stty echo"); |
| 51 else |
| 52 system("stty -echo"); |
| 53 #endif |
| 54 } |
| 55 |
| 56 class SimpleHostEventHandler : public HostConnection::EventHandler { |
| 57 public: |
| 58 SimpleHostEventHandler(MessageLoop* loop) |
| 59 : main_loop_(loop) { |
| 60 } |
| 61 |
| 62 virtual void HandleMessages(HostConnection* conn, |
| 63 HostMessageList* messages) { |
| 64 HostMessageList list; |
| 65 messages->swap(list); |
| 66 for (size_t i = 0; i < list.size(); ++i) { |
| 67 HostMessage* msg = list[i]; |
| 68 if (msg->has_init_client()) { |
| 69 HandleInitClientMessage(msg); |
| 70 } else if (msg->has_begin_update_stream()) { |
| 71 HandleBeginUpdateStreamMessage(msg); |
| 72 } else if (msg->has_update_stream_packet()) { |
| 73 HandleUpdateStreamPacketMessage(msg); |
| 74 } else if (msg->has_end_update_stream()) { |
| 75 HandleEndUpdateStreamMessage(msg); |
| 76 } |
| 77 } |
| 78 STLDeleteElements<HostMessageList>(&list); |
| 79 } |
| 80 |
| 81 virtual void OnConnectionOpened(HostConnection* conn) { |
| 82 std::cout << "Connection establised." << std::endl; |
| 83 } |
| 84 |
| 85 virtual void OnConnectionClosed(HostConnection* conn) { |
| 86 std::cout << "Connection closed." << std::endl; |
| 87 |
| 88 // Quit the message if the connection has closed. |
| 89 DCHECK(main_loop_); |
| 90 main_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 91 } |
| 92 |
| 93 virtual void OnConnectionFailed(HostConnection* conn) { |
| 94 std::cout << "Conection failed." << std::endl; |
| 95 |
| 96 // Quit the message if the connection has failed. |
| 97 DCHECK(main_loop_); |
| 98 main_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 99 } |
| 100 |
| 101 private: |
| 102 void HandleInitClientMessage(HostMessage* host_msg) { |
| 103 const InitClientMessage& msg = host_msg->init_client(); |
| 104 std::cout << "InitClient (" << msg.width() |
| 105 << ", " << msg.height() << ")" << std::endl; |
| 106 } |
| 107 |
| 108 void HandleBeginUpdateStreamMessage(HostMessage* host_msg) { |
| 109 const BeginUpdateStreamMessage& msg = host_msg->begin_update_stream(); |
| 110 } |
| 111 |
| 112 void HandleUpdateStreamPacketMessage(HostMessage* host_msg) { |
| 113 const UpdateStreamPacketMessage& msg = host_msg->update_stream_packet(); |
| 114 std::cout << "UpdateStreamPacket (" << msg.header().x() |
| 115 << ", " << msg.header().y() << ") [" |
| 116 << msg.header().width() << " x " << msg.header().height() << "]" |
| 117 << std::endl; |
| 118 } |
| 119 |
| 120 void HandleEndUpdateStreamMessage(HostMessage* host_msg) { |
| 121 const EndUpdateStreamMessage& msg = host_msg->end_update_stream(); |
| 122 } |
| 123 |
| 124 // JingleClient::Callback interface. |
| 125 void OnStateChange(JingleClient* client, JingleClient::State state) { |
| 126 if (state == JingleClient::CONNECTED) { |
| 127 LOG(INFO) << "Connected as: " << client->GetFullJid(); |
| 128 } else if (state == JingleClient::CLOSED) { |
| 129 LOG(INFO) << "Connection closed."; |
| 130 } |
| 131 } |
| 132 |
| 133 MessageLoop* main_loop_; |
| 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(SimpleHostEventHandler); |
| 136 }; |
| 137 |
| 138 int main(int argc, char** argv) { |
| 139 base::AtExitManager exit_manager; |
| 140 |
| 141 if (argc > 2) { |
| 142 std::cerr << "Usage: " << argv[0] << " [<host_jid>]" << std::endl; |
| 143 return 1; |
| 144 } |
| 145 |
| 146 // Get host JID from command line arguments, or stdin if not specified. |
| 147 std::string host_jid; |
| 148 if (argc == 2) { |
| 149 host_jid = argv[1]; |
| 150 } else { |
| 151 std::cout << "Host JID: "; |
| 152 std::cin >> host_jid; |
| 153 std::cin.ignore(); // Consume the leftover '\n' |
| 154 } |
| 155 if (host_jid.find("/chromoting") == std::string::npos) { |
| 156 std::cerr << "Error: Expected Host JID in format: <jid>/chromoting<id>" |
| 157 << std::endl; |
| 158 return 1; |
| 159 } |
| 160 |
| 161 // Get username (JID). |
| 162 // Extract default JID from host_jid. |
| 163 std::string username; |
| 164 std::string default_username; |
| 165 size_t jid_end = host_jid.find('/'); |
| 166 if (jid_end != std::string::npos) { |
| 167 default_username = host_jid.substr(0, jid_end); |
| 168 } |
| 169 std::cout << "JID [" << default_username << "]: "; |
| 170 getline(std::cin, username); |
| 171 if (username.length() == 0) { |
| 172 username = default_username; |
| 173 } |
| 174 if (username.length() == 0) { |
| 175 std::cerr << "Error: Expected valid JID username" << std::endl; |
| 176 return 1; |
| 177 } |
| 178 |
| 179 // Get password (with console echo turned off). |
| 180 std::string password; |
| 181 SetConsoleEcho(false); |
| 182 std::cout << "Password: "; |
| 183 getline(std::cin, password); |
| 184 SetConsoleEcho(true); |
| 185 std::cout << std::endl; |
| 186 |
| 187 // The message loop that everything runs on. |
| 188 MessageLoop main_loop; |
| 189 SimpleHostEventHandler handler(&main_loop); |
| 190 HostConnection connection(new ProtocolDecoder(), &handler); |
| 191 connection.Connect(username, password, host_jid); |
| 192 |
| 193 // Run the message. |
| 194 main_loop.Run(); |
| 195 return 0; |
| 196 } |
OLD | NEW |