OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "blimp/net/client_connection_manager.h" | 5 #include "blimp/net/client_connection_manager.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "blimp/common/create_blimp_message.h" | 8 #include "blimp/common/create_blimp_message.h" |
9 #include "blimp/common/proto/blimp_message.pb.h" | 9 #include "blimp/common/proto/blimp_message.pb.h" |
10 #include "blimp/common/protocol_version.h" | 10 #include "blimp/common/protocol_version.h" |
11 #include "blimp/net/blimp_connection.h" | 11 #include "blimp/net/blimp_connection.h" |
12 #include "blimp/net/blimp_message_processor.h" | 12 #include "blimp/net/blimp_message_processor.h" |
13 #include "blimp/net/blimp_transport.h" | 13 #include "blimp/net/blimp_transport.h" |
| 14 #include "blimp/net/browser_connection_handler.h" |
14 #include "blimp/net/connection_handler.h" | 15 #include "blimp/net/connection_handler.h" |
15 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
16 | 17 |
17 namespace blimp { | 18 namespace blimp { |
18 | 19 |
19 ClientConnectionManager::ClientConnectionManager( | 20 ClientConnectionManager::ClientConnectionManager( |
20 ConnectionHandler* connection_handler) | 21 ConnectionHandler* connection_handler) |
21 : connection_handler_(connection_handler) { | 22 : connection_handler_(connection_handler), weak_factory_(this) { |
22 DCHECK(connection_handler_); | 23 DCHECK(connection_handler_); |
23 } | 24 } |
24 | 25 |
25 ClientConnectionManager::~ClientConnectionManager() {} | 26 ClientConnectionManager::~ClientConnectionManager() {} |
26 | 27 |
27 void ClientConnectionManager::AddTransport( | 28 void ClientConnectionManager::AddTransport( |
28 scoped_ptr<BlimpTransport> transport) { | 29 scoped_ptr<BlimpTransport> transport) { |
29 DCHECK(transport); | 30 DCHECK(transport); |
30 transports_.push_back(std::move(transport)); | 31 transports_.push_back(std::move(transport)); |
31 } | 32 } |
32 | 33 |
33 void ClientConnectionManager::Connect() { | 34 void ClientConnectionManager::Connect() { |
34 // A |transport| added first is used first. When it fails to connect, | 35 // A |transport| added first is used first. When it fails to connect, |
35 // the next transport is used. | 36 // the next transport is used. |
36 DCHECK(!transports_.empty()); | 37 DCHECK(!transports_.empty()); |
37 Connect(0); | 38 Connect(0); |
38 } | 39 } |
39 | 40 |
40 void ClientConnectionManager::Connect(int transport_index) { | 41 void ClientConnectionManager::Connect(int transport_index) { |
| 42 DVLOG(1) << "ClientConnectionManager::Connect(" << transport_index << ")"; |
41 if (static_cast<size_t>(transport_index) < transports_.size()) { | 43 if (static_cast<size_t>(transport_index) < transports_.size()) { |
42 transports_[transport_index]->Connect( | 44 transports_[transport_index]->Connect( |
43 base::Bind(&ClientConnectionManager::OnConnectResult, | 45 base::Bind(&ClientConnectionManager::OnConnectResult, |
44 base::Unretained(this), transport_index)); | 46 base::Unretained(this), transport_index)); |
45 } else { | 47 } else { |
46 // TODO(haibinlu): add an error reporting path out for this. | 48 // TODO(haibinlu): add an error reporting path out for this. |
47 LOG(WARNING) << "All transports failed to connect"; | 49 LOG(WARNING) << "All transports failed to connect"; |
48 } | 50 } |
49 } | 51 } |
50 | 52 |
51 void ClientConnectionManager::OnConnectResult(int transport_index, int result) { | 53 void ClientConnectionManager::OnConnectResult(int transport_index, int result) { |
52 DCHECK_NE(result, net::ERR_IO_PENDING); | 54 DCHECK_NE(result, net::ERR_IO_PENDING); |
53 const auto& transport = transports_[transport_index]; | 55 const auto& transport = transports_[transport_index]; |
54 if (result == net::OK) { | 56 if (result == net::OK) { |
55 scoped_ptr<BlimpConnection> connection = transport->TakeConnection(); | 57 scoped_ptr<BlimpConnection> connection = transport->TakeConnection(); |
56 SendAuthenticationMessage(connection.get()); | 58 connection->AddConnectionErrorObserver(this); |
57 connection_handler_->HandleConnection(std::move(connection)); | 59 SendAuthenticationMessage(std::move(connection)); |
58 } else { | 60 } else { |
59 DVLOG(1) << "Transport " << transport->GetName() | 61 DVLOG(1) << "Transport " << transport->GetName() |
60 << " failed to connect:" << net::ErrorToString(result); | 62 << " failed to connect:" << net::ErrorToString(result); |
61 Connect(transport_index + 1); | 63 Connect(transport_index + 1); |
62 } | 64 } |
63 } | 65 } |
64 | 66 |
65 void ClientConnectionManager::SendAuthenticationMessage( | 67 void ClientConnectionManager::SendAuthenticationMessage( |
66 BlimpConnection* connection) { | 68 scoped_ptr<BlimpConnection> connection) { |
67 // TODO(haibinlu): get client token. | 69 DVLOG(1) << "Sending authentication message."; |
68 const char* client_token = ""; | |
69 connection->GetOutgoingMessageProcessor()->ProcessMessage( | 70 connection->GetOutgoingMessageProcessor()->ProcessMessage( |
70 CreateStartConnectionMessage(client_token, kProtocolVersion), | 71 CreateStartConnectionMessage(client_token_, kProtocolVersion), |
71 net::CompletionCallback()); | 72 base::Bind(&ClientConnectionManager::OnAuthenticationMessageSent, |
| 73 weak_factory_.GetWeakPtr(), |
| 74 base::Passed(std::move(connection)))); |
| 75 } |
| 76 |
| 77 void ClientConnectionManager::OnAuthenticationMessageSent( |
| 78 scoped_ptr<BlimpConnection> connection, |
| 79 int result) { |
| 80 DVLOG(1) << "AuthenticationMessageSent, result=" << result; |
| 81 if (result != net::OK) { |
| 82 // If a write error occurred, just throw away |connection|. |
| 83 // We don't need to propagate the error code here because the connection |
| 84 // will already have done so via the ErrorObserver object. |
| 85 return; |
| 86 } |
| 87 connection_handler_->HandleConnection(std::move(connection)); |
| 88 } |
| 89 |
| 90 void ClientConnectionManager::OnConnectionError(int error) { |
| 91 // TODO(kmarshall): Replace this with actual reconnection logic. |
| 92 VLOG(0) << "Connection dropped, error=" << error; |
| 93 Connect(); |
72 } | 94 } |
73 | 95 |
74 } // namespace blimp | 96 } // namespace blimp |
OLD | NEW |