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_PROTOBUF_PROTOBUFWIREADAPTER_H_
16#define POLO_WIRE_PROTOBUF_PROTOBUFWIREADAPTER_H_
17
18#include <string>
19#include <vector>
20
21#include "polo/wire/polowireadapter.h"
22#include "polo/wire/protobuf/polo.pb.h"
23
24namespace polo {
25namespace wire {
26namespace protobuf {
27
28// Polo wire adapter that transmits Polo messages using protocol buffers.
29class ProtobufWireAdapter : public PoloWireAdapter {
30 public:
31  // Creates a new protocol buffer adapter on the given interface
32  // @param interface the wire interface used to send and receive data
33  explicit ProtobufWireAdapter(PoloWireInterface* interface);
34  virtual ~ProtobufWireAdapter() {}
35
36  /** @override */
37  virtual void GetNextMessage();
38
39  /** @override */
40  virtual void SendConfigurationMessage(
41      const pairing::message::ConfigurationMessage& message);
42
43  /** @override */
44  virtual void SendConfigurationAckMessage(
45      const pairing::message::ConfigurationAckMessage& message);
46
47  /** @override */
48  virtual void SendOptionsMessage(
49      const pairing::message::OptionsMessage& message);
50
51  /** @override */
52  virtual void SendPairingRequestMessage(
53      const pairing::message::PairingRequestMessage& message);
54
55  /** @override */
56  virtual void SendPairingRequestAckMessage(
57      const pairing::message::PairingRequestAckMessage& message);
58
59  /** @override */
60  virtual void SendSecretMessage(
61      const pairing::message::SecretMessage& message);
62
63  /** @override */
64  virtual void SendSecretAckMessage(
65      const pairing::message::SecretAckMessage& message);
66
67  /** @override */
68  virtual void SendErrorMessage(pairing::PoloError error);
69
70  /** @override */
71  virtual void OnBytesReceived(const std::vector<uint8_t>& data);
72
73  /** @override */
74  virtual void OnError();
75
76 private:
77  // The current read state.
78  enum ReadState {
79    // There is no read operation in progress.
80    kNone,
81
82    // Waiting to read the message preamble which is 4 bytes representing
83    // the size of the next message.
84    kPreamble,
85
86    // Waiting to read the message.
87    kMessage,
88  };
89
90  // Sends a message with the given type and payload. The payload should be
91  // the serialized string representation of a protobuf message of the given
92  // type.
93  void SendMessagePayload(OuterMessage_MessageType type,
94                          const std::string& payload);
95
96  // Sends the given outer message.
97  void SendOuterMessage(const OuterMessage& message);
98
99  // Parses a received protobuf message.
100  void ParseMessage(const std::vector<uint8_t>& data);
101
102  // Parses a configuration message from a serialized protobuf string.
103  void ParseConfigurationMessage(const std::string& payload);
104
105  // Parses a configuration ack message from a serialized protobuf string.
106  void ParseConfigurationAckMessage(const std::string& payload);
107
108  // Parses an options messages from a serialized protobuf string.
109  void ParseOptionsMessage(const std::string& payload);
110
111  // Parses a pairing request message from a serialized protobuf string.
112  void ParsePairingRequestMessage(const std::string& payload);
113
114  // Parses a pairing request ack message from a serialized protobuf string.
115  void ParsePairingRequestAckMessage(const std::string& payload);
116
117  // Parses a secret message from a serialized protobuf string.
118  void ParseSecretMessage(const std::string& payload);
119
120  // Parses a secret ack message from a serialized protobuf string.
121  void ParseSecretAckMessage(const std::string& payload);
122
123  // Converts an encoding type from the internal representation to the protobuf
124  // representation.
125  Options_Encoding_EncodingType EncodingTypeToProto(
126      encoding::EncodingOption::EncodingType type);
127
128  // Converts an encoding type from the protobuf representation to the internal
129  // representation.
130  encoding::EncodingOption::EncodingType EncodingTypeFromProto(
131      Options_Encoding_EncodingType type);
132
133  // Converts a role type from the internal representation to the protobuf
134  // representation.
135  Options_RoleType RoleToProto(
136      pairing::message::OptionsMessage::ProtocolRole role);
137
138  // Converts a role type from the protobuf representation to the internal
139  // representation.
140  pairing::message::OptionsMessage::ProtocolRole RoleFromProto(
141      Options_RoleType role);
142
143  ReadState read_state_;
144
145  DISALLOW_COPY_AND_ASSIGN(ProtobufWireAdapter);
146};
147
148}  // namespace protobuf
149}  // namespace wire
150}  // namespace polo
151
152#endif  // POLO_WIRE_PROTOBUF_PROTOBUFWIREADAPTER_H_
153