| 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 minimal 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 | |
| 15 #include "base/waitable_event.h" | |
| 16 #include "media/base/data_buffer.h" | |
| 17 #include "remoting/base/protocol_decoder.h" | |
| 18 #include "remoting/client/client_util.h" | |
| 19 #include "remoting/client/jingle_host_connection.h" | |
| 20 #include "remoting/jingle_glue/jingle_client.h" | |
| 21 #include "remoting/jingle_glue/jingle_thread.h" | |
| 22 | |
| 23 using remoting::BeginUpdateStreamMessage; | |
| 24 using remoting::EndUpdateStreamMessage; | |
| 25 using remoting::HostConnection; | |
| 26 using remoting::HostMessage; | |
| 27 using remoting::HostMessageList; | |
| 28 using remoting::InitClientMessage; | |
| 29 using remoting::JingleClient; | |
| 30 using remoting::JingleHostConnection; | |
| 31 using remoting::JingleThread; | |
| 32 using remoting::ProtocolDecoder; | |
| 33 using remoting::UpdateStreamPacketMessage; | |
| 34 | |
| 35 class SimpleHostEventCallback : public HostConnection::HostEventCallback { | |
| 36 public: | |
| 37 explicit SimpleHostEventCallback(MessageLoop* loop, | |
| 38 base::WaitableEvent* client_done) | |
| 39 : main_loop_(loop), client_done_(client_done) { | |
| 40 } | |
| 41 | |
| 42 virtual void HandleMessages(HostConnection* conn, | |
| 43 HostMessageList* messages) { | |
| 44 HostMessageList list; | |
| 45 messages->swap(list); | |
| 46 for (size_t i = 0; i < list.size(); ++i) { | |
| 47 HostMessage* msg = list[i]; | |
| 48 if (msg->has_init_client()) { | |
| 49 HandleInitClientMessage(msg); | |
| 50 } else if (msg->has_begin_update_stream()) { | |
| 51 HandleBeginUpdateStreamMessage(msg); | |
| 52 } else if (msg->has_update_stream_packet()) { | |
| 53 HandleUpdateStreamPacketMessage(msg); | |
| 54 } else if (msg->has_end_update_stream()) { | |
| 55 HandleEndUpdateStreamMessage(msg); | |
| 56 } | |
| 57 } | |
| 58 STLDeleteElements<HostMessageList>(&list); | |
| 59 } | |
| 60 | |
| 61 virtual void OnConnectionOpened(HostConnection* conn) { | |
| 62 std::cout << "Connection establised." << std::endl; | |
| 63 } | |
| 64 | |
| 65 virtual void OnConnectionClosed(HostConnection* conn) { | |
| 66 std::cout << "Connection closed." << std::endl; | |
| 67 client_done_->Signal(); | |
| 68 } | |
| 69 | |
| 70 virtual void OnConnectionFailed(HostConnection* conn) { | |
| 71 std::cout << "Conection failed." << std::endl; | |
| 72 client_done_->Signal(); | |
| 73 } | |
| 74 | |
| 75 private: | |
| 76 void HandleInitClientMessage(HostMessage* host_msg) { | |
| 77 const InitClientMessage& msg = host_msg->init_client(); | |
| 78 std::cout << "InitClient (" << msg.width() | |
| 79 << ", " << msg.height() << ")" << std::endl; | |
| 80 } | |
| 81 | |
| 82 void HandleBeginUpdateStreamMessage(HostMessage* host_msg) { | |
| 83 const BeginUpdateStreamMessage& msg = host_msg->begin_update_stream(); | |
| 84 std::cout << msg.GetTypeName() << std::endl; | |
| 85 } | |
| 86 | |
| 87 void HandleUpdateStreamPacketMessage(HostMessage* host_msg) { | |
| 88 const UpdateStreamPacketMessage& msg = host_msg->update_stream_packet(); | |
| 89 if (!msg.has_begin_rect()) | |
| 90 return; | |
| 91 | |
| 92 std::cout << "UpdateStreamPacket (" << msg.begin_rect().x() | |
| 93 << ", " << msg.begin_rect().y() << ") [" | |
| 94 << msg.begin_rect().width() << " x " | |
| 95 << msg.begin_rect().height() << "]" | |
| 96 << std::endl; | |
| 97 } | |
| 98 | |
| 99 void HandleEndUpdateStreamMessage(HostMessage* host_msg) { | |
| 100 const EndUpdateStreamMessage& msg = host_msg->end_update_stream(); | |
| 101 std::cout << msg.GetTypeName() << std::endl; | |
| 102 } | |
| 103 | |
| 104 // JingleClient::Callback interface. | |
| 105 void OnStateChange(JingleClient* client, JingleClient::State state) { | |
| 106 if (state == JingleClient::CONNECTED) { | |
| 107 LOG(INFO) << "Connected as: " << client->GetFullJid(); | |
| 108 } else if (state == JingleClient::CLOSED) { | |
| 109 LOG(INFO) << "Connection closed."; | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 MessageLoop* main_loop_; | |
| 114 base::WaitableEvent* client_done_; | |
| 115 | |
| 116 DISALLOW_COPY_AND_ASSIGN(SimpleHostEventCallback); | |
| 117 }; | |
| 118 | |
| 119 int main(int argc, char** argv) { | |
| 120 base::AtExitManager exit_manager; | |
| 121 std::string host_jid; | |
| 122 std::string username; | |
| 123 std::string auth_token; | |
| 124 | |
| 125 if (!remoting::GetLoginInfo(argc, argv, &host_jid, &username, &auth_token)) { | |
| 126 std::cerr << "Cannot get valid login info." << std::endl; | |
| 127 return 1; | |
| 128 } | |
| 129 | |
| 130 // Start up a thread to run everything. | |
| 131 JingleThread network_thread; | |
| 132 network_thread.Start(); | |
| 133 | |
| 134 base::WaitableEvent client_done(false, false); | |
| 135 SimpleHostEventCallback handler(network_thread.message_loop(), &client_done); | |
| 136 JingleHostConnection connection(&network_thread); | |
| 137 connection.Connect(username, auth_token, host_jid, &handler); | |
| 138 | |
| 139 // Wait until the mainloop has been signaled to exit. | |
| 140 client_done.Wait(); | |
| 141 | |
| 142 connection.Disconnect(); | |
| 143 network_thread.message_loop()->PostTask(FROM_HERE, | |
| 144 new MessageLoop::QuitTask()); | |
| 145 network_thread.Stop(); | |
| 146 | |
| 147 return 0; | |
| 148 } | |
| OLD | NEW |