Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 2 | * Copyright 2016 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 16 | package org.onosproject.store.cluster.messaging.impl; |
Yuta HIGUCHI | 0a0b9e4 | 2015-02-11 16:57:12 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.base.MoreObjects; |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 19 | import org.onlab.util.ByteArraySizeHashPrinter; |
| 20 | import org.onosproject.store.cluster.messaging.Endpoint; |
Yuta HIGUCHI | 0a0b9e4 | 2015-02-11 16:57:12 -0800 | [diff] [blame] | 21 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 22 | /** |
| 23 | * Internal message representation with additional attributes |
| 24 | * for supporting, synchronous request/reply behavior. |
| 25 | */ |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 26 | public final class InternalMessage { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 27 | |
Madan Jampani | a9e70a6 | 2016-03-02 16:28:18 -0800 | [diff] [blame] | 28 | /** |
| 29 | * Message status. |
| 30 | */ |
| 31 | public enum Status { |
| 32 | /** |
| 33 | * All ok. |
| 34 | */ |
| 35 | OK, |
| 36 | |
| 37 | /** |
| 38 | * Response status signifying no registered handler. |
| 39 | */ |
| 40 | ERROR_NO_HANDLER, |
| 41 | |
| 42 | /** |
| 43 | * Response status signifying an exception handling the message. |
| 44 | */ |
Madan Jampani | b825aeb | 2016-04-01 15:18:25 -0700 | [diff] [blame^] | 45 | ERROR_HANDLER_EXCEPTION, |
| 46 | |
| 47 | /** |
| 48 | * Reponse status signifying invalid message structure. |
| 49 | */ |
| 50 | PROTOCOL_EXCEPTION |
Madan Jampani | a9e70a6 | 2016-03-02 16:28:18 -0800 | [diff] [blame] | 51 | |
| 52 | // NOTE: For backwards compatibility it important that new enum constants |
| 53 | // be appended. |
| 54 | // FIXME: We should remove this restriction in the future. |
| 55 | } |
| 56 | |
Madan Jampani | b825aeb | 2016-04-01 15:18:25 -0700 | [diff] [blame^] | 57 | private final int preamble; |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 58 | private final long id; |
| 59 | private final Endpoint sender; |
| 60 | private final String type; |
| 61 | private final byte[] payload; |
Madan Jampani | a9e70a6 | 2016-03-02 16:28:18 -0800 | [diff] [blame] | 62 | private final Status status; |
Yuta HIGUCHI | c65f512 | 2014-10-07 10:05:59 -0700 | [diff] [blame] | 63 | |
Madan Jampani | b825aeb | 2016-04-01 15:18:25 -0700 | [diff] [blame^] | 64 | public InternalMessage(int preamble, long id, Endpoint sender, String type, byte[] payload) { |
| 65 | this(preamble, id, sender, type, payload, Status.OK); |
Madan Jampani | a9e70a6 | 2016-03-02 16:28:18 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Madan Jampani | b825aeb | 2016-04-01 15:18:25 -0700 | [diff] [blame^] | 68 | public InternalMessage(int preamble, long id, Endpoint sender, String type, byte[] payload, Status status) { |
| 69 | this.preamble = preamble; |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 70 | this.id = id; |
| 71 | this.sender = sender; |
| 72 | this.type = type; |
| 73 | this.payload = payload; |
Madan Jampani | a9e70a6 | 2016-03-02 16:28:18 -0800 | [diff] [blame] | 74 | this.status = status; |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Madan Jampani | b825aeb | 2016-04-01 15:18:25 -0700 | [diff] [blame^] | 77 | public int preamble() { |
| 78 | return preamble; |
| 79 | } |
| 80 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 81 | public long id() { |
| 82 | return id; |
| 83 | } |
| 84 | |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame] | 85 | public String type() { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 86 | return type; |
| 87 | } |
| 88 | |
| 89 | public Endpoint sender() { |
| 90 | return sender; |
| 91 | } |
| 92 | |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 93 | public byte[] payload() { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 94 | return payload; |
| 95 | } |
| 96 | |
Madan Jampani | a9e70a6 | 2016-03-02 16:28:18 -0800 | [diff] [blame] | 97 | public Status status() { |
| 98 | return status; |
| 99 | } |
| 100 | |
Yuta HIGUCHI | 0a0b9e4 | 2015-02-11 16:57:12 -0800 | [diff] [blame] | 101 | @Override |
| 102 | public String toString() { |
| 103 | return MoreObjects.toStringHelper(this) |
| 104 | .add("id", id) |
| 105 | .add("type", type) |
| 106 | .add("sender", sender) |
Madan Jampani | a9e70a6 | 2016-03-02 16:28:18 -0800 | [diff] [blame] | 107 | .add("status", status) |
Yuta HIGUCHI | 0a0b9e4 | 2015-02-11 16:57:12 -0800 | [diff] [blame] | 108 | .add("payload", ByteArraySizeHashPrinter.of(payload)) |
| 109 | .toString(); |
| 110 | } |
Yuta HIGUCHI | 92626c0 | 2014-10-06 15:46:18 -0700 | [diff] [blame] | 111 | } |