blob: f1d64ddb2a28e8198ce183c15444bc6caa5c99f2 [file] [log] [blame]
Jerome Poichet7c997852014-05-20 10:50:05 -07001// Copyright 2012 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "polo/pairing/serverpairingsession.h"
16
17#include <glog/logging.h>
18#include "polo/pairing/polochallengeresponse.h"
19#include "polo/util/poloutil.h"
20
21namespace polo {
22namespace pairing {
23
24ServerPairingSession::ServerPairingSession(wire::PoloWireAdapter *wire,
25 PairingContext *context,
26 PoloChallengeResponse* challenge,
27 const std::string& server_name)
28 : PairingSession(wire, context, challenge),
29 server_name_(server_name) {
30}
31
32ServerPairingSession::~ServerPairingSession() {
33}
34
35void ServerPairingSession::DoInitializationPhase() {
36 LOG(INFO) << "Waiting for PairingRequest...";
37 wire()->GetNextMessage();
38}
39
40void ServerPairingSession::DoConfigurationPhase() {
41 LOG(INFO) << "Waiting for Configuration...";
42 wire()->GetNextMessage();
43}
44
45void ServerPairingSession::OnPairingRequestMessage(
46 const message::PairingRequestMessage& message) {
47 set_service_name(message.service_name());
48
49 if (message.has_client_name()) {
50 set_peer_name(message.client_name());
51 }
52
53 message::PairingRequestAckMessage ack(service_name());
54 wire()->SendPairingRequestAckMessage(ack);
55
56 LOG(INFO) << "Waiting for Options...";
57 wire()->GetNextMessage();
58}
59
60void ServerPairingSession::OnOptionsMessage(
61 const message::OptionsMessage& message) {
62 // The client is responsible for negotiating a valid configuration, so just
63 // send the server options.
64 wire()->SendOptionsMessage(local_options());
65
66 DoConfigurationPhase();
67}
68
69void ServerPairingSession::OnConfigurationMessage(
70 const message::ConfigurationMessage& message) {
71 if (!SetConfiguration(message)) {
72 wire()->SendErrorMessage(kErrorBadConfiguration);
73 listener()->OnError(kErrorBadConfiguration);
74 return;
75 }
76
77 const encoding::EncodingOption& encoding = message.encoding();
78
79 if (GetLocalRole() == message::OptionsMessage::kDisplayDevice) {
80 if (!local_options().SupportsOutputEncoding(encoding)) {
81 LOG(ERROR) << "Unsupported output encoding requested: "
82 << encoding.encoding_type();
83 wire()->SendErrorMessage(kErrorBadConfiguration);
84 listener()->OnError(kErrorBadConfiguration);
85 return;
86 }
87 } else {
88 if (!local_options().SupportsInputEncoding(encoding)) {
89 LOG(ERROR) << "Unsupported input encoding requested: "
90 << encoding.encoding_type();
91 wire()->SendErrorMessage(kErrorBadConfiguration);
92 listener()->OnError(kErrorBadConfiguration);
93 return;
94 }
95 }
96
97 message::ConfigurationAckMessage ack;
98 wire()->SendConfigurationAckMessage(ack);
99
100 DoPairingPhase();
101}
102
103void ServerPairingSession::OnConfigurationAckMessage(
104 const message::ConfigurationAckMessage& message) {
105 LOG(ERROR) << "Received unexpected ConfigurationAckMessage";
106 wire()->SendErrorMessage(kErrorProtocol);
107 listener()->OnError(kErrorProtocol);
108}
109
110void ServerPairingSession::OnPairingRequestAckMessage(
111 const message::PairingRequestAckMessage& message) {
112 LOG(ERROR) << "Received unexpected PairingRequestAckMessage";
113 wire()->SendErrorMessage(kErrorProtocol);
114 listener()->OnError(kErrorProtocol);
115}
116
117} // namespace pairing
118} // namespace polo