Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | */ |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 16 | package org.onlab.netty; |
| 17 | |
| 18 | import java.io.IOException; |
| 19 | |
Yuta HIGUCHI | 0a0b9e4 | 2015-02-11 16:57:12 -0800 | [diff] [blame] | 20 | import org.onlab.util.ByteArraySizeHashPrinter; |
| 21 | |
| 22 | import com.google.common.base.MoreObjects; |
| 23 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Internal message representation with additional attributes |
| 26 | * for supporting, synchronous request/reply behavior. |
| 27 | */ |
| 28 | public final class InternalMessage implements Message { |
| 29 | |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame^] | 30 | public static final String REPLY_MESSAGE_TYPE = "NETTY_MESSAGING_REQUEST_REPLY"; |
Yuta HIGUCHI | c65f512 | 2014-10-07 10:05:59 -0700 | [diff] [blame] | 31 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 32 | private long id; |
| 33 | private Endpoint sender; |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame^] | 34 | private String type; |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 35 | private byte[] payload; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 36 | private transient NettyMessagingService messagingService; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 37 | |
| 38 | // Must be created using the Builder. |
| 39 | private InternalMessage() {} |
| 40 | |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame^] | 41 | InternalMessage(long id, Endpoint sender, String type, byte[] payload) { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 42 | this.id = id; |
| 43 | this.sender = sender; |
| 44 | this.type = type; |
| 45 | this.payload = payload; |
| 46 | } |
| 47 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 48 | public long id() { |
| 49 | return id; |
| 50 | } |
| 51 | |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame^] | 52 | public String type() { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 53 | return type; |
| 54 | } |
| 55 | |
| 56 | public Endpoint sender() { |
| 57 | return sender; |
| 58 | } |
| 59 | |
| 60 | @Override |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 61 | public byte[] payload() { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 62 | return payload; |
| 63 | } |
| 64 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 65 | protected void setMessagingService(NettyMessagingService messagingService) { |
| 66 | this.messagingService = messagingService; |
| 67 | } |
| 68 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 69 | @Override |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 70 | public void respond(byte[] data) throws IOException { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 71 | Builder builder = new Builder(messagingService); |
| 72 | InternalMessage message = builder.withId(this.id) |
Madan Jampani | 8710093 | 2014-10-21 16:46:12 -0700 | [diff] [blame] | 73 | .withSender(messagingService.localEp()) |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 74 | .withPayload(data) |
| 75 | .withType(REPLY_MESSAGE_TYPE) |
| 76 | .build(); |
| 77 | messagingService.sendAsync(sender, message); |
| 78 | } |
| 79 | |
Yuta HIGUCHI | 0a0b9e4 | 2015-02-11 16:57:12 -0800 | [diff] [blame] | 80 | @Override |
| 81 | public String toString() { |
| 82 | return MoreObjects.toStringHelper(this) |
| 83 | .add("id", id) |
| 84 | .add("type", type) |
| 85 | .add("sender", sender) |
| 86 | .add("payload", ByteArraySizeHashPrinter.of(payload)) |
| 87 | .toString(); |
| 88 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Builder for InternalMessages. |
| 92 | */ |
Yuta HIGUCHI | 24fb299 | 2014-10-06 22:51:45 -0700 | [diff] [blame] | 93 | public static final class Builder { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 94 | private InternalMessage message; |
| 95 | |
| 96 | public Builder(NettyMessagingService messagingService) { |
| 97 | message = new InternalMessage(); |
| 98 | message.messagingService = messagingService; |
| 99 | } |
| 100 | |
| 101 | public Builder withId(long id) { |
| 102 | message.id = id; |
| 103 | return this; |
| 104 | } |
| 105 | |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame^] | 106 | public Builder withType(String type) { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 107 | message.type = type; |
| 108 | return this; |
| 109 | } |
| 110 | |
| 111 | public Builder withSender(Endpoint sender) { |
| 112 | message.sender = sender; |
| 113 | return this; |
| 114 | } |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 115 | public Builder withPayload(byte[] payload) { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 116 | message.payload = payload; |
| 117 | return this; |
| 118 | } |
| 119 | |
| 120 | public InternalMessage build() { |
| 121 | return message; |
| 122 | } |
| 123 | } |
Yuta HIGUCHI | 92626c0 | 2014-10-06 15:46:18 -0700 | [diff] [blame] | 124 | } |