1/*
2 * Copyright (C) 2009 Google Inc.  All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.polo.pairing.message;
18
19import com.google.polo.wire.PoloWireInterface;
20
21/**
22 * Base class for internal representation of Polo protocol messages.
23 *
24 * <p>Implementations of {@link PoloWireInterface} will translate to and from
25 * the wire version of protocol messages to subclasses of PoloMessage.
26 */
27public class PoloMessage {
28
29  /**
30   * List of all available Polo protocol message types, in order of protocol
31   * phases.  The values are synchronized with the constants defined in
32   * polo.proto.
33   */
34  public static enum PoloMessageType {
35    UNKNOWN(0),
36    PAIRING_REQUEST(10),
37    PAIRING_REQUEST_ACK(11),
38    OPTIONS(20),
39    CONFIGURATION(30),
40    CONFIGURATION_ACK(31),
41    SECRET(40),
42    SECRET_ACK(41);
43
44    private final int mIntVal;
45
46    private PoloMessageType(int intVal) {
47      mIntVal = intVal;
48    }
49
50    public static PoloMessageType fromIntVal(int intVal) {
51      for (PoloMessageType messageType : PoloMessageType.values()) {
52        if (messageType.getAsInt() == intVal) {
53          return messageType;
54        }
55      }
56      return null;
57    }
58
59    public int getAsInt() {
60      return mIntVal;
61    }
62  }
63
64  /**
65   * The Polo protocol message type of this instance.
66   */
67  private final PoloMessageType mType;
68
69  public PoloMessage(PoloMessageType type) {
70    mType = type;
71  }
72
73  public PoloMessageType getType() {
74    return mType;
75  }
76
77  @Override
78  public String toString() {
79    return "[" + mType.toString() + "]";
80  }
81
82}
83