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#ifndef POLO_WIRE_POLOWIREADAPTER_H_
16#define POLO_WIRE_POLOWIREADAPTER_H_
17
18#include "polo/wire/polowireinterface.h"
19
20#include "polo/pairing/message/messagelistener.h"
21
22namespace polo {
23namespace wire {
24
25// Abstract class for protocol adapters that send and receive Polo messages. The
26// adapter is responsible for serializing and deserializing messages sent and
27// received from the supplied PoloWireInterface.
28class PoloWireAdapter : public PoloWireListener {
29 public:
30  // Creates a new adapter on the given interface. The interface should only
31  // exist in the context of this adapter and will be freed when this adapter is
32  // freed.
33  // @param interface the interface used to send and receive data
34  explicit PoloWireAdapter(PoloWireInterface* interface);
35  virtual ~PoloWireAdapter() {}
36
37  // Sets the listener that will receive incoming Polo messages. A listener
38  // must be set before using this adapter.
39  void set_listener(pairing::message::MessageListener* listener);
40
41  // Gets the next message from the interface asynchronously. The listener
42  // will be invoked when a message has been received. An error will occur if
43  // this method is invoked again before a message or error is received by
44  // the listener.
45  virtual void GetNextMessage() = 0;
46
47  // Sends a configuration message to the peer.
48  virtual void SendConfigurationMessage(
49      const pairing::message::ConfigurationMessage& message) = 0;
50
51  // Sends a configuration acknowledgment to the peer.
52  virtual void SendConfigurationAckMessage(
53      const pairing::message::ConfigurationAckMessage& message) = 0;
54
55  // Sends an options message to the peer.
56  virtual void SendOptionsMessage(
57      const pairing::message::OptionsMessage& message) = 0;
58
59  // Sends a pairing request message to the peer.
60  virtual void SendPairingRequestMessage(
61      const pairing::message::PairingRequestMessage& message) = 0;
62
63  // Sends a pairing request acknowledgment to the peer.
64  virtual void SendPairingRequestAckMessage(
65      const pairing::message::PairingRequestAckMessage& message) = 0;
66
67  // Sends a secret message to the peer.
68  virtual void SendSecretMessage(
69      const pairing::message::SecretMessage& message) = 0;
70
71  // Sends a secret acknowledgment to the peer.
72  virtual void SendSecretAckMessage(
73      const pairing::message::SecretAckMessage& message) = 0;
74
75  // Sends an error message to the peer.
76  virtual void SendErrorMessage(pairing::PoloError error) = 0;
77
78 protected:
79  // Gets the Polo wire interface used to send and receive data.
80  PoloWireInterface* interface() { return interface_; }
81
82  // Get the listener that will be notified of received Polo messages.
83  pairing::message::MessageListener* listener() { return listener_; }
84
85 private:
86  PoloWireInterface* interface_;
87  pairing::message::MessageListener* listener_;
88
89  DISALLOW_COPY_AND_ASSIGN(PoloWireAdapter);
90};
91
92}  // namespace wire
93}  // namespace polo
94
95#endif  // POLO_WIRE_POLOWIREADAPTER_H_
96