summaryrefslogtreecommitdiffstats
path: root/remoting/client/simple_client.cc
blob: 85561bd6db0dff730897138a21f4324ba789549b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// A simple client implements a minimal Chromoting client and shows
// network traffic for debugging.

#include <iostream>
#include <list>

#include "base/at_exit.h"
#include "base/message_loop.h"
#include "base/stl_util-inl.h"

#include "base/waitable_event.h"
#include "media/base/data_buffer.h"
#include "remoting/base/protocol_decoder.h"
#include "remoting/client/client_util.h"
#include "remoting/client/jingle_host_connection.h"
#include "remoting/jingle_glue/jingle_client.h"
#include "remoting/jingle_glue/jingle_thread.h"

using remoting::BeginUpdateStreamMessage;
using remoting::EndUpdateStreamMessage;
using remoting::HostConnection;
using remoting::HostMessage;
using remoting::HostMessageList;
using remoting::InitClientMessage;
using remoting::JingleClient;
using remoting::JingleHostConnection;
using remoting::JingleThread;
using remoting::ProtocolDecoder;
using remoting::UpdateStreamPacketMessage;

class SimpleHostEventCallback : public HostConnection::HostEventCallback {
 public:
  explicit SimpleHostEventCallback(MessageLoop* loop,
                                   base::WaitableEvent* client_done)
      : main_loop_(loop), client_done_(client_done) {
  }

  virtual void HandleMessages(HostConnection* conn,
                              HostMessageList* messages) {
    HostMessageList list;
    messages->swap(list);
    for (size_t i = 0; i < list.size(); ++i) {
      HostMessage* msg = list[i];
      if (msg->has_init_client()) {
        HandleInitClientMessage(msg);
      } else if (msg->has_begin_update_stream()) {
        HandleBeginUpdateStreamMessage(msg);
      } else if (msg->has_update_stream_packet()) {
        HandleUpdateStreamPacketMessage(msg);
      } else if (msg->has_end_update_stream()) {
        HandleEndUpdateStreamMessage(msg);
      }
    }
    STLDeleteElements<HostMessageList>(&list);
  }

  virtual void OnConnectionOpened(HostConnection* conn) {
    std::cout << "Connection establised." << std::endl;
  }

  virtual void OnConnectionClosed(HostConnection* conn) {
    std::cout << "Connection closed." << std::endl;
    client_done_->Signal();
  }

  virtual void OnConnectionFailed(HostConnection* conn) {
    std::cout << "Conection failed." << std::endl;
    client_done_->Signal();
  }

 private:
  void HandleInitClientMessage(HostMessage* host_msg) {
    const InitClientMessage& msg = host_msg->init_client();
    std::cout << "InitClient (" << msg.width()
              << ", " << msg.height() << ")" << std::endl;
  }

  void HandleBeginUpdateStreamMessage(HostMessage* host_msg) {
    const BeginUpdateStreamMessage& msg = host_msg->begin_update_stream();
  }

  void HandleUpdateStreamPacketMessage(HostMessage* host_msg) {
    const UpdateStreamPacketMessage& msg = host_msg->update_stream_packet();
    std::cout << "UpdateStreamPacket (" << msg.header().x()
              << ", " << msg.header().y() << ") ["
              << msg.header().width() << " x " << msg.header().height() << "]"
              << std::endl;
  }

  void HandleEndUpdateStreamMessage(HostMessage* host_msg) {
    const EndUpdateStreamMessage& msg = host_msg->end_update_stream();
  }

  // JingleClient::Callback interface.
  void OnStateChange(JingleClient* client, JingleClient::State state) {
    if (state == JingleClient::CONNECTED) {
      LOG(INFO) << "Connected as: " << client->GetFullJid();
    } else if (state == JingleClient::CLOSED) {
      LOG(INFO) << "Connection closed.";
    }
  }

  MessageLoop* main_loop_;
  base::WaitableEvent* client_done_;

  DISALLOW_COPY_AND_ASSIGN(SimpleHostEventCallback);
};

int main(int argc, char** argv) {
  base::AtExitManager exit_manager;
  std::string host_jid;
  std::string username;
  std::string auth_token;

  if (!remoting::GetLoginInfo(argc, argv, &host_jid, &username, &auth_token)) {
    std::cerr << "Cannot get valid login info." << std::endl;
    return 1;
  }

  // Start up a thread to run everything.
  JingleThread network_thread;
  network_thread.Start();

  base::WaitableEvent client_done(false, false);
  SimpleHostEventCallback handler(network_thread.message_loop(), &client_done);
  scoped_refptr<JingleHostConnection> connection =
      new JingleHostConnection(&network_thread, &handler);
  connection->Connect(username, auth_token, host_jid);

  // Wait until the mainloop has been signaled to exit.
  client_done.Wait();

  connection->Disconnect();
  network_thread.message_loop()->PostTask(FROM_HERE,
                                          new MessageLoop::QuitTask());
  network_thread.Stop();

  return 0;
}