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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
// 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.
#include "build/build_config.h"
#if !defined(OS_WIN)
extern "C" {
#include <unistd.h>
}
#endif // !defined(OS_WIN)
#include <iostream>
#include <list>
#include "base/at_exit.h"
#include "media/base/data_buffer.h"
#include "remoting/base/constants.h"
#include "remoting/jingle_glue/jingle_channel.h"
#include "remoting/jingle_glue/jingle_client.h"
#include "remoting/jingle_glue/jingle_thread.h"
using remoting::JingleClient;
using remoting::JingleChannel;
using remoting::kChromotingTokenServiceName;
void SetConsoleEcho(bool on) {
#if defined(OS_WIN)
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
if ((hIn == INVALID_HANDLE_VALUE) || (hIn == NULL))
return;
DWORD mode;
if (!GetConsoleMode(hIn, &mode))
return;
if (on) {
mode = mode | ENABLE_ECHO_INPUT;
} else {
mode = mode & ~ENABLE_ECHO_INPUT;
}
SetConsoleMode(hIn, mode);
#else // defined(OS_WIN)
if (on)
system("stty echo");
else
system("stty -echo");
#endif // !defined(OS_WIN)
}
class JingleTestClient : public JingleChannel::Callback,
public JingleClient::Callback {
public:
virtual ~JingleTestClient() {}
void Run(const std::string& username, const std::string& auth_token,
const std::string& host_jid) {
// TODO(hclam): Fix the threading problem.
remoting::JingleThread jingle_thread;
jingle_thread.Start();
client_ = new JingleClient(&jingle_thread);
client_->Init(username, auth_token, kChromotingTokenServiceName, this);
if (host_jid != "") {
scoped_refptr<JingleChannel> channel = client_->Connect(host_jid, this);
channels_.push_back(channel);
}
while (true) {
std::string line;
std::getline(std::cin, line);
{
AutoLock auto_lock(channels_lock_);
// Broadcast message to all clients.
for (ChannelsList::iterator it = channels_.begin();
it != channels_.end(); ++it) {
uint8* buf = new uint8[line.length()];
memcpy(buf, line.c_str(), line.length());
(*it)->Write(new media::DataBuffer(buf, line.length()));
}
}
if (line == "exit")
break;
}
while (!channels_.empty()) {
channels_.front()->Close();
channels_.pop_front();
}
client_->Close();
jingle_thread.Stop();
}
// JingleChannel::Callback interface.
void OnStateChange(JingleChannel* channel, JingleChannel::State state) {
LOG(INFO) << "State of " << channel->jid() << " changed to " << state;
}
void OnPacketReceived(JingleChannel* channel,
scoped_refptr<media::DataBuffer> buffer) {
std::string str(reinterpret_cast<const char*>(buffer->GetData()),
buffer->GetDataSize());
std::cout << "(" << channel->jid() << "): " << str << std::endl;
}
// JingleClient::Callback interface.
void OnStateChange(JingleClient* client, JingleClient::State state) {
if (state == JingleClient::CONNECTED) {
std::cerr << "Connected as " << client->GetFullJid() << std::endl;
} else if (state == JingleClient::CLOSED) {
std::cerr << "Connection closed" << std::endl;
}
}
bool OnAcceptConnection(JingleClient* client, const std::string& jid,
JingleChannel::Callback** callback) {
std::cerr << "Accepting new connection from " << jid << std::endl;
*callback = this;
return true;
}
void OnNewConnection(JingleClient* client,
scoped_refptr<JingleChannel> channel) {
std::cerr << "Connected to " << channel->jid() << std::endl;
AutoLock auto_lock(channels_lock_);
channels_.push_back(channel);
}
private:
typedef std::list<scoped_refptr<JingleChannel> > ChannelsList;
scoped_refptr<JingleClient> client_;
ChannelsList channels_;
Lock channels_lock_;
};
int main(int argc, char** argv) {
if (argc > 2)
std::cerr << "Usage: " << argv[0] << " [<host_jid>]" << std::endl;
base::AtExitManager exit_manager;
std::string host_jid = argc == 2 ? argv[1] : "";
std::string username;
std::cout << "JID: ";
std::cin >> username;
std::string auth_token;
std::cout << "Auth token: ";
std::cin >> auth_token;
JingleTestClient client;
client.Run(username, auth_token, host_jid);
return 0;
}
|