blob: c820704d0b67235f25d713441092da632f611e9e [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;
tom1d416c52014-09-29 20:55:24 -070021
Yuta HIGUCHI971addc2014-10-07 23:23:17 -070022// TODO: Should payload type be ByteBuffer?
tom1d416c52014-09-29 20:55:24 -070023/**
24 * Base message for cluster-wide communications.
25 */
Madan Jampani890bc352014-10-01 22:35:29 -070026public class ClusterMessage {
tom1d416c52014-09-29 20:55:24 -070027
Madan Jampani890bc352014-10-01 22:35:29 -070028 private final NodeId sender;
tom1d416c52014-09-29 20:55:24 -070029 private final MessageSubject subject;
Madan Jampani53e44e62014-10-07 12:39:51 -070030 private final byte[] payload;
tom1d416c52014-09-29 20:55:24 -070031
32 /**
33 * Creates a cluster message.
34 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080035 * @param sender message sender
tom1d416c52014-09-29 20:55:24 -070036 * @param subject message subject
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080037 * @param payload message payload
tom1d416c52014-09-29 20:55:24 -070038 */
Madan Jampani53e44e62014-10-07 12:39:51 -070039 public ClusterMessage(NodeId sender, MessageSubject subject, byte[] payload) {
Madan Jampani890bc352014-10-01 22:35:29 -070040 this.sender = sender;
tom1d416c52014-09-29 20:55:24 -070041 this.subject = subject;
Madan Jampani890bc352014-10-01 22:35:29 -070042 this.payload = payload;
tom1d416c52014-09-29 20:55:24 -070043 }
44
45 /**
Madan Jampani890bc352014-10-01 22:35:29 -070046 * Returns the id of the controller sending this message.
47 *
48 * @return message sender id.
49 */
50 public NodeId sender() {
51 return sender;
52 }
53
54 /**
tom1d416c52014-09-29 20:55:24 -070055 * Returns the message subject indicator.
56 *
57 * @return message subject
58 */
59 public MessageSubject subject() {
60 return subject;
61 }
62
Madan Jampani890bc352014-10-01 22:35:29 -070063 /**
64 * Returns the message payload.
65 *
66 * @return message payload.
67 */
Madan Jampani53e44e62014-10-07 12:39:51 -070068 public byte[] payload() {
Madan Jampani890bc352014-10-01 22:35:29 -070069 return payload;
tom1d416c52014-09-29 20:55:24 -070070 }
Madan Jampani8a895092014-10-17 16:55:50 -070071
72 /**
73 * Sends a response to the sender.
74 *
75 * @param data payload response.
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080076 * @throws IOException when I/O exception of some sort has occurred
Madan Jampani8a895092014-10-17 16:55:50 -070077 */
78 public void respond(byte[] data) throws IOException {
79 throw new IllegalStateException("One can only repond to message recived from others.");
80 }
tom1d416c52014-09-29 20:55:24 -070081}