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