blob: 3aa30dcb20632b00dc8c603b65fd62b822d6d381 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
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 */
tom1d416c52014-09-29 20:55:24 -070016package org.onlab.onos.store.cluster.messaging;
17
Madan Jampani8a895092014-10-17 16:55:50 -070018import java.io.IOException;
19
Madan Jampani890bc352014-10-01 22:35:29 -070020import org.onlab.onos.cluster.NodeId;
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080021import org.onlab.util.ByteArraySizeHashPrinter;
22
23import com.google.common.base.MoreObjects;
tom1d416c52014-09-29 20:55:24 -070024
Yuta HIGUCHI971addc2014-10-07 23:23:17 -070025// TODO: Should payload type be ByteBuffer?
tom1d416c52014-09-29 20:55:24 -070026/**
27 * Base message for cluster-wide communications.
28 */
Madan Jampani890bc352014-10-01 22:35:29 -070029public class ClusterMessage {
tom1d416c52014-09-29 20:55:24 -070030
Madan Jampani890bc352014-10-01 22:35:29 -070031 private final NodeId sender;
tom1d416c52014-09-29 20:55:24 -070032 private final MessageSubject subject;
Madan Jampani53e44e62014-10-07 12:39:51 -070033 private final byte[] payload;
tom1d416c52014-09-29 20:55:24 -070034
35 /**
36 * Creates a cluster message.
37 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080038 * @param sender message sender
tom1d416c52014-09-29 20:55:24 -070039 * @param subject message subject
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080040 * @param payload message payload
tom1d416c52014-09-29 20:55:24 -070041 */
Madan Jampani53e44e62014-10-07 12:39:51 -070042 public ClusterMessage(NodeId sender, MessageSubject subject, byte[] payload) {
Madan Jampani890bc352014-10-01 22:35:29 -070043 this.sender = sender;
tom1d416c52014-09-29 20:55:24 -070044 this.subject = subject;
Madan Jampani890bc352014-10-01 22:35:29 -070045 this.payload = payload;
tom1d416c52014-09-29 20:55:24 -070046 }
47
48 /**
Madan Jampani890bc352014-10-01 22:35:29 -070049 * Returns the id of the controller sending this message.
50 *
51 * @return message sender id.
52 */
53 public NodeId sender() {
54 return sender;
55 }
56
57 /**
tom1d416c52014-09-29 20:55:24 -070058 * Returns the message subject indicator.
59 *
60 * @return message subject
61 */
62 public MessageSubject subject() {
63 return subject;
64 }
65
Madan Jampani890bc352014-10-01 22:35:29 -070066 /**
67 * Returns the message payload.
68 *
69 * @return message payload.
70 */
Madan Jampani53e44e62014-10-07 12:39:51 -070071 public byte[] payload() {
Madan Jampani890bc352014-10-01 22:35:29 -070072 return payload;
tom1d416c52014-09-29 20:55:24 -070073 }
Madan Jampani8a895092014-10-17 16:55:50 -070074
75 /**
76 * Sends a response to the sender.
77 *
78 * @param data payload response.
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080079 * @throws IOException when I/O exception of some sort has occurred
Madan Jampani8a895092014-10-17 16:55:50 -070080 */
81 public void respond(byte[] data) throws IOException {
82 throw new IllegalStateException("One can only repond to message recived from others.");
83 }
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080084
85 @Override
86 public String toString() {
87 return MoreObjects.toStringHelper(getClass())
88 .add("sender", sender)
89 .add("subject", subject)
90 .add("payload", ByteArraySizeHashPrinter.of(payload))
91 .toString();
92 }
tom1d416c52014-09-29 20:55:24 -070093}