Home | History | Annotate | Download | only in pairing
      1 // 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 
     21 namespace polo {
     22 namespace pairing {
     23 
     24 ServerPairingSession::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 
     32 ServerPairingSession::~ServerPairingSession() {
     33 }
     34 
     35 void ServerPairingSession::DoInitializationPhase() {
     36   LOG(INFO) << "Waiting for PairingRequest...";
     37   wire()->GetNextMessage();
     38 }
     39 
     40 void ServerPairingSession::DoConfigurationPhase() {
     41   LOG(INFO) << "Waiting for Configuration...";
     42   wire()->GetNextMessage();
     43 }
     44 
     45 void 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 
     60 void 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 
     69 void 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 
    103 void ServerPairingSession::OnConfigurationAckMessage(
    104     const message::ConfigurationAckMessage& message) {
    105   LOG(ERROR) << "Received unexpected ConfigurationAckMessage";
    106   wire()->SendErrorMessage(kErrorProtocol);
    107   listener()->OnError(kErrorProtocol);
    108 }
    109 
    110 void 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
    119